public <A> QueryWhere<T> where(Filter filter) { HashMap<String, Object> fieldMap = New.hashMap(); for (Field f : filter.getClass().getDeclaredFields()) { f.setAccessible(true); try { Object obj = f.get(filter); if (obj == from.getAlias()) { List<TableDefinition.FieldDefinition> fields = from.getAliasDefinition().getFields(); String name = f.getName(); for (TableDefinition.FieldDefinition field : fields) { String n = name + "." + field.field.getName(); Object o = field.field.get(obj); fieldMap.put(n, o); } } fieldMap.put(f.getName(), f.get(filter)); } catch (Exception e) { throw new RuntimeException(e); } } Token filterCode = new ClassReader().decompile(filter, fieldMap, "where"); // String filterQuery = filterCode.toString(); conditions.add(filterCode); return new QueryWhere<T>(this); }
public int delete() { SQLStatement stat = new SQLStatement(db); stat.appendSQL("DELETE FROM "); from.appendSQL(stat); appendWhere(stat); StatementLogger.delete(stat.getSQL()); return stat.executeUpdate(); }
@SuppressWarnings("unchecked") void appendFromWhere(SQLStatement stat) { stat.appendSQL(" FROM "); from.appendSQL(stat); for (SelectTable join : joins) { join.appendSQLAsJoin(stat, this); } appendWhere(stat); if (groupByExpressions != null) { stat.appendSQL(" GROUP BY "); int i = 0; for (Object obj : groupByExpressions) { if (i++ > 0) { stat.appendSQL(", "); } appendSQL(stat, obj); stat.appendSQL(" "); } } if (!orderByList.isEmpty()) { stat.appendSQL(" ORDER BY "); int i = 0; for (OrderExpression<T> o : orderByList) { if (i++ > 0) { stat.appendSQL(", "); } o.appendSQL(stat); stat.appendSQL(" "); } } if (limit > 0) { db.getDialect().appendLimit(stat, limit); } if (offset > 0) { db.getDialect().appendOffset(stat, offset); } StatementLogger.select(stat.getSQL()); }
private List<T> select(boolean distinct) { List<T> result = New.arrayList(); TableDefinition<T> def = from.getAliasDefinition(); SQLStatement stat = getSelectStatement(distinct); def.appendSelectList(stat); appendFromWhere(stat); ResultSet rs = stat.executeQuery(); Statement s = null; try { s = rs.getStatement(); while (rs.next()) { T item = from.newObject(); from.getAliasDefinition().readRow(item, rs); result.add(item); } } catch (SQLException e) { throw new RuntimeException(e); } finally { JdbcUtils.closeSilently(rs); JdbcUtils.closeSilently(s); } return result; }
public int update() { if (updateColumnDeclarations.size() == 0) { throw new RuntimeException("Missing set or increment call."); } SQLStatement stat = new SQLStatement(db); stat.appendSQL("UPDATE "); from.appendSQL(stat); stat.appendSQL(" SET "); int i = 0; for (UpdateColumn declaration : updateColumnDeclarations) { if (i++ > 0) { stat.appendSQL(", "); } declaration.appendSQL(stat); } appendWhere(stat); StatementLogger.update(stat.getSQL()); return stat.executeUpdate(); }