/** * Join another table. * * @param alias an alias for the table to join * @return the joined query */ @SuppressWarnings("unchecked") public <U> QueryJoin innerJoin(U alias) { TableDefinition<T> def = (TableDefinition<T>) db.define(alias.getClass()); SelectTable<T> join = new SelectTable(db, this, alias, false); def.initSelectObject(join, alias, aliasMap); joins.add(join); return new QueryJoin(this, join); }
@SuppressWarnings("unchecked") static <T> Query<T> from(Db db, T alias) { Query<T> query = new Query<T>(db); TableDefinition<T> def = (TableDefinition<T>) db.define(alias.getClass()); query.from = new SelectTable<T>(db, query, alias, false); def.initSelectObject(query.from, alias, query.aliasMap); return query; }
<T> TableDefinition<T> define(Class<T> clazz) { TableDefinition<T> def = getTableDefinition(clazz); if (def == null) { def = new TableDefinition<T>(clazz); def.mapFields(); classMap.put(clazz, def); if (Table.class.isAssignableFrom(clazz)) { T t = instance(clazz); Table table = (Table) t; Define.define(def, table); } } return def; }
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; }
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; }