public QueryBuilder(DatabaseType databaseType, TableInfo<T, ID> tableInfo, Dao<T, ID> dao) { super(databaseType, tableInfo, dao, StatementType.SELECT); this.idField = tableInfo.getIdField(); this.selectIdColumn = (idField != null); }
public QueryBuilder(DatabaseType databaseType, TableInfo<T, ID> tableInfo, Dao<T, ID> dao) { super(databaseType, tableInfo, dao, StatementType.SELECT); this.idField = tableInfo.getIdField(); this.selectIdColumn = (idField != null); }
protected Where(TableInfo<T, ID> tableInfo, StatementBuilder<T, ID> statementBuilder, DatabaseType databaseType) { // limit the constructor scope this.tableInfo = tableInfo; this.statementBuilder = statementBuilder; this.idFieldType = tableInfo.getIdField(); if (idFieldType == null) { this.idColumnName = null; } else { this.idColumnName = idFieldType.getColumnName(); } this.databaseType = databaseType; }
protected BaseMappedStatement(TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes) { this.tableInfo = tableInfo; this.clazz = tableInfo.getDataClass(); this.idField = tableInfo.getIdField(); this.statement = statement; this.argFieldTypes = argFieldTypes; }
protected Where(TableInfo<T, ID> tableInfo, StatementBuilder<T, ID> statementBuilder, DatabaseType databaseType) { // limit the constructor scope this.tableInfo = tableInfo; this.statementBuilder = statementBuilder; this.idFieldType = tableInfo.getIdField(); if (idFieldType == null) { this.idColumnName = null; } else { this.idColumnName = idFieldType.getColumnName(); } this.databaseType = databaseType; }
@Override public ID extractId(T data) throws SQLException { checkForInitialized(); FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException("Class " + dataClass + " does not have an id field"); } @SuppressWarnings("unchecked") ID id = (ID) idField.extractJavaFieldValue(data); return id; }
@Override public ID extractId(T data) throws SQLException { checkForInitialized(); FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException("Class " + dataClass + " does not have an id field"); } @SuppressWarnings("unchecked") ID id = (ID) idField.extractJavaFieldValue(data); return id; }
public static <T, ID> MappedRefresh<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo) throws SQLException { FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException("Cannot refresh " + tableInfo.getDataClass() + " because it doesn't have an id field"); } String statement = buildStatement(databaseType, tableInfo, idField); return new MappedRefresh<T, ID>(tableInfo, statement, new FieldType[] { tableInfo.getIdField() }, tableInfo.getFieldTypes()); } }
protected BaseMappedStatement(Dao<T, ID> dao, TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes) { this.dao = dao; this.connectionSource = dao.getConnectionSource(); this.tableInfo = tableInfo; this.clazz = tableInfo.getDataClass(); this.idField = tableInfo.getIdField(); this.statement = statement; this.argFieldTypes = argFieldTypes; }
public static <T, ID> MappedDelete<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo) throws SQLException { FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException("Cannot delete from " + tableInfo.getDataClass() + " because it doesn't have an id field"); } StringBuilder sb = new StringBuilder(64); appendTableName(databaseType, sb, "DELETE FROM ", tableInfo.getTableName()); appendWhereFieldEq(databaseType, idField, sb, null); return new MappedDelete<T, ID>(tableInfo, sb.toString(), new FieldType[] { idField }); }
public static <T, ID> MappedRefresh<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo) throws SQLException { FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException( "Cannot refresh " + tableInfo.getDataClass() + " because it doesn't have an id field"); } DatabaseType databaseType = dao.getConnectionSource().getDatabaseType(); String statement = buildStatement(databaseType, tableInfo, idField); return new MappedRefresh<T, ID>(dao, tableInfo, statement, new FieldType[] { tableInfo.getIdField() }, tableInfo.getFieldTypes()); } }
/** * Delete all of the objects in the collection. This builds a {@link MappedDeleteCollection} on the fly because the * datas could be variable sized. */ public static <T, ID> int deleteObjects(Dao<T, ID> dao, TableInfo<T, ID> tableInfo, DatabaseConnection databaseConnection, Collection<T> datas, ObjectCache objectCache) throws SQLException { MappedDeleteCollection<T, ID> deleteCollection = MappedDeleteCollection.build(dao, tableInfo, datas.size()); Object[] fieldObjects = new Object[datas.size()]; FieldType idField = tableInfo.getIdField(); int objC = 0; for (T data : datas) { fieldObjects[objC] = idField.extractJavaFieldToSqlArgValue(data); objC++; } return updateRows(databaseConnection, tableInfo.getDataClass(), deleteCollection, fieldObjects, objectCache); }
public static <T, ID> MappedUpdateId<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo) throws SQLException { FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException("Cannot update-id in " + tableInfo.getDataClass() + " because it doesn't have an id field"); } StringBuilder sb = new StringBuilder(64); appendTableName(databaseType, sb, "UPDATE ", tableInfo.getTableName()); sb.append("SET "); appendFieldColumnName(databaseType, sb, idField, null); sb.append("= ? "); appendWhereFieldEq(databaseType, idField, sb, null); return new MappedUpdateId<T, ID>(tableInfo, sb.toString(), new FieldType[] { idField, idField }); }
/** * This is private because the execute is the only method that should be called here. */ private static <T, ID> MappedDeleteCollection<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo, int dataSize) throws SQLException { FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException("Cannot delete " + tableInfo.getDataClass() + " because it doesn't have an id field defined"); } StringBuilder sb = new StringBuilder(128); appendTableName(databaseType, sb, "DELETE FROM ", tableInfo.getTableName()); FieldType[] argFieldTypes = new FieldType[dataSize]; appendWhereIds(databaseType, idField, sb, dataSize, argFieldTypes); return new MappedDeleteCollection<T, ID>(tableInfo, sb.toString(), argFieldTypes); }
public static <T, ID> MappedQueryForFieldEq<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo, FieldType idFieldType) throws SQLException { if (idFieldType == null) { idFieldType = tableInfo.getIdField(); if (idFieldType == null) { throw new SQLException("Cannot query-for-id with " + tableInfo.getDataClass() + " because it doesn't have an id field"); } } String statement = buildStatement(databaseType, tableInfo, idFieldType); return new MappedQueryForFieldEq<T, ID>(tableInfo, statement, new FieldType[] { idFieldType }, tableInfo.getFieldTypes(), "query-for-id"); }
public static <T, ID> MappedDelete<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo) throws SQLException { FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException( "Cannot delete from " + tableInfo.getDataClass() + " because it doesn't have an id field"); } StringBuilder sb = new StringBuilder(64); DatabaseType databaseType = dao.getConnectionSource().getDatabaseType(); appendTableName(databaseType, sb, "DELETE FROM ", tableInfo.getTableName()); appendWhereFieldEq(databaseType, idField, sb, null); return new MappedDelete<T, ID>(dao, tableInfo, sb.toString(), new FieldType[] { idField }); }
public static <T, ID> MappedUpdateId<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo) throws SQLException { FieldType idField = tableInfo.getIdField(); if (idField == null) { throw new SQLException( "Cannot update-id in " + tableInfo.getDataClass() + " because it doesn't have an id field"); } StringBuilder sb = new StringBuilder(64); DatabaseType databaseType = dao.getConnectionSource().getDatabaseType(); appendTableName(databaseType, sb, "UPDATE ", tableInfo.getTableName()); sb.append("SET "); appendFieldColumnName(databaseType, sb, idField, null); sb.append("= ? "); appendWhereFieldEq(databaseType, idField, sb, null); return new MappedUpdateId<T, ID>(dao, tableInfo, sb.toString(), new FieldType[] { idField, idField }); }
public boolean ifExists(DatabaseConnection connection, ID id) throws SQLException { if (ifExistsQuery == null) { QueryBuilder<T, ID> qb = new QueryBuilder<T, ID>(databaseType, tableInfo, dao); qb.selectRaw("COUNT(*)"); /* * NOTE: bit of a hack here because the select arg is never used but it _can't_ be a constant because we set * field-name and field-type on it. */ qb.where().eq(tableInfo.getIdField().getColumnName(), new SelectArg()); ifExistsQuery = qb.prepareStatementString(); ifExistsFieldTypes = new FieldType[] { tableInfo.getIdField() }; } Object idSqlArg = tableInfo.getIdField().convertJavaFieldToSqlArgValue(id); long count = connection.queryForLong(ifExistsQuery, new Object[] { idSqlArg }, ifExistsFieldTypes); logger.debug("query of '{}' returned {}", ifExistsQuery, count); return (count != 0); }
public static <T, ID> MappedQueryForFieldEq<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo, FieldType idFieldType) throws SQLException { if (idFieldType == null) { idFieldType = tableInfo.getIdField(); if (idFieldType == null) { throw new SQLException("Cannot query-for-id with " + tableInfo.getDataClass() + " because it doesn't have an id field"); } } DatabaseType databaseType = dao.getConnectionSource().getDatabaseType(); String statement = buildStatement(databaseType, tableInfo, idFieldType); return new MappedQueryForFieldEq<T, ID>(dao, tableInfo, statement, new FieldType[] { idFieldType }, tableInfo.getFieldTypes(), "query-for-id"); }
@Test public void testBasic() throws SQLException { TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class); assertEquals(Foo.class, tableInfo.getDataClass()); assertEquals(TABLE_NAME, tableInfo.getTableName()); assertEquals(COLUMN_NAME, tableInfo.getIdField().getColumnName()); assertEquals(1, tableInfo.getFieldTypes().length); assertSame(tableInfo.getIdField(), tableInfo.getFieldTypes()[0]); assertEquals(COLUMN_NAME, tableInfo.getFieldTypeByColumnName(COLUMN_NAME).getColumnName()); }