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(); }
public static Boolean not(Boolean x) { return Db.registerToken( ClassUtils.newObject(Boolean.class), new Function("", x) { public <T> void appendSQL(SQLStatement stat, Query<T> query) { stat.appendSQL("NOT "); query.appendSQL(stat, x[0]); } }); }
public static Boolean isNotNull(Object x) { return Db.registerToken( ClassUtils.newObject(Boolean.class), new Function("", x) { public <T> void appendSQL(SQLStatement stat, Query<T> query) { query.appendSQL(stat, x[0]); stat.appendSQL(" IS NOT NULL"); } }); }
public static Boolean like(String x, String pattern) { Boolean o = ClassUtils.newObject(Boolean.class); return Db.registerToken( o, new Function("LIKE", x, pattern) { public <T> void appendSQL(SQLStatement stat, Query<T> query) { stat.appendSQL("("); query.appendSQL(stat, x[0]); stat.appendSQL(" LIKE "); query.appendSQL(stat, x[1]); stat.appendSQL(")"); } }); }
public static Boolean or(Boolean... x) { return Db.registerToken( ClassUtils.newObject(Boolean.class), new Function("", (Object[]) x) { public <T> void appendSQL(SQLStatement stat, Query<T> query) { int i = 0; for (Object o : x) { if (i++ > 0) { stat.appendSQL(" OR "); } query.appendSQL(stat, o); } } }); }
private <X> List<X> select(Class<X> clazz, X x, boolean distinct) { List<X> result = New.arrayList(); TableDefinition<X> def = db.define(clazz); SQLStatement stat = getSelectStatement(distinct); def.appendSelectList(stat, this, x); appendFromWhere(stat); ResultSet rs = stat.executeQuery(); Statement s = null; try { s = rs.getStatement(); while (rs.next()) { X row = ClassUtils.newObject(clazz); def.readRow(row, rs); result.add(row); } } catch (SQLException e) { throw new RuntimeException(e); } finally { JdbcUtils.closeSilently(rs); JdbcUtils.closeSilently(s); } return result; }
public static Long count(Object x) { return Db.registerToken(ClassUtils.newObject(Long.class), new Function("COUNT", x)); }
@SuppressWarnings("unchecked") public static <T extends Number> T sum(T x) { return (T) Db.registerToken(ClassUtils.newObject(x.getClass()), new Function("SUM", x)); }
public static Integer length(Object x) { return Db.registerToken(ClassUtils.newObject(Integer.class), new Function("LENGTH", x)); }
@SuppressWarnings("unchecked") public static <X> X max(X x) { Class<X> clazz = (Class<X>) x.getClass(); X o = ClassUtils.newObject(clazz); return Db.registerToken(o, new Function("MAX", x)); }
void initWithNewObject(Object obj) { Object o = ClassUtils.newObject(field.getType()); setValue(obj, o); }