void delete(Db db, Object obj) { if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) { throw new IllegalStateException( "No primary key columns defined " + "for table " + obj.getClass() + " - no update possible"); } SQLStatement stat = new SQLStatement(db); StatementBuilder buff = new StatementBuilder("DELETE FROM "); buff.append(db.getDialect().getTableName(schemaName, tableName)); buff.resetCount(); Object alias = ClassUtils.newObject(obj.getClass()); Query<Object> query = Query.from(db, alias); boolean firstCondition = true; for (FieldDefinition field : fields) { if (field.isPrimaryKey) { Object aliasValue = field.getValue(alias); Object value = field.getValue(obj); if (!firstCondition) { query.addConditionToken(ConditionAndOr.AND); } firstCondition = false; query.addConditionToken(new Condition<Object>(aliasValue, value, CompareType.EQUAL)); } } stat.setSQL(buff.toString()); query.appendWhere(stat); StatementLogger.delete(stat.getSQL()); stat.executeUpdate(); }
void initSelectObject(SelectTable<T> table, Object obj, Map<Object, SelectColumn<T>> map) { for (FieldDefinition def : fields) { def.initWithNewObject(obj); SelectColumn<T> column = new SelectColumn<T>(table, def); map.put(def.getValue(obj), column); } }
<Y, X> void copyAttributeValues(Query<Y> query, X to, X map) { for (FieldDefinition def : fields) { Object obj = def.getValue(map); SelectColumn<Y> col = query.getSelectColumn(obj); Object value = col.getCurrentValue(); def.setValue(to, value); } }
<Y, X> void appendSelectList(SQLStatement stat, Query<Y> query, X x) { for (int i = 0; i < fields.size(); i++) { if (i > 0) { stat.appendSQL(", "); } FieldDefinition def = fields.get(i); Object obj = def.getValue(x); query.appendSQL(stat, obj); } }
/** Optionally truncates strings to the maximum length */ private static Object getValue(Object obj, FieldDefinition field) { Object value = field.getValue(obj); if (field.trimString && field.maxLength > 0) { if (value instanceof String) { // clip strings String s = (String) value; if (s.length() > field.maxLength) { return s.substring(0, field.maxLength); } return s; } return value; } // standard behavior return value; }
void initObject(Object obj, Map<Object, FieldDefinition> map) { for (FieldDefinition def : fields) { def.initWithNewObject(obj); map.put(def.getValue(obj), def); } }