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()); } }
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 }); }
/** * Match up our joined fields so we can throw a nice exception immediately if you can't join with this type. */ private void matchJoinedFieldsByName(JoinInfo joinInfo, String localColumnName, String joinedColumnName, QueryBuilder<?, ?> joinedQueryBuilder) throws SQLException { joinInfo.localField = tableInfo.getFieldTypeByColumnName(localColumnName); if (joinInfo.localField == null) { throw new SQLException("Could not find field in " + tableInfo.getDataClass() + " that has column-name '" + localColumnName + "'"); } joinInfo.remoteField = joinedQueryBuilder.tableInfo.getFieldTypeByColumnName(joinedColumnName); if (joinInfo.remoteField == null) { throw new SQLException("Could not find field in " + joinedQueryBuilder.tableInfo.getDataClass() + " that has column-name '" + joinedColumnName + "'"); } }
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; }
@Test(expected = SQLException.class) public void testGeneratedIdSequenceNotSupported() throws Exception { TableInfo<GeneratedIdSequence, Integer> tableInfo = new TableInfo<GeneratedIdSequence, Integer>(connectionSource, null, GeneratedIdSequence.class); assertEquals(2, tableInfo.getFieldTypes().length); StringBuilder sb = new StringBuilder(); ArrayList<String> additionalArgs = new ArrayList<String>(); ArrayList<String> statementsBefore = new ArrayList<String>(); databaseType.appendColumnArg(null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore, null, null); }
@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()); }
@Test(expected = SQLException.class) public void testUpdateForeignCollection() throws Exception { UpdateBuilder<OurForeignCollection, Integer> stmtb = new UpdateBuilder<OurForeignCollection, Integer>(databaseType, new TableInfo<OurForeignCollection, Integer>(databaseType, OurForeignCollection.class), null); stmtb.updateColumnValue(OurForeignCollection.FOOS_FIELD_NAME, null); }
public StatementBuilder(DatabaseType databaseType, TableInfo<T, ID> tableInfo, Dao<T, ID> dao, StatementType type) { this.databaseType = databaseType; this.tableInfo = tableInfo; this.tableName = tableInfo.getTableName(); this.dao = dao; this.type = type; if (!type.isOkForStatementBuilder()) { throw new IllegalStateException("Building a statement from a " + type + " statement is not allowed"); } }
@Test public void testNoTableNameInAnnotation() throws Exception { TableInfo<NoTableNameAnnotation, Void> tableInfo = new TableInfo<NoTableNameAnnotation, Void>(databaseType, NoTableNameAnnotation.class); assertEquals(NoTableNameAnnotation.class.getSimpleName().toLowerCase(), tableInfo.getTableName()); }
@Override public FieldType findForeignFieldType(Class<?> clazz) { checkForInitialized(); for (FieldType fieldType : tableInfo.getFieldTypes()) { if (fieldType.getType() == clazz) { return fieldType; } } return null; }
resultFieldTypes = tableInfo.getFieldTypes(); return; continue; FieldType fieldType = tableInfo.getFieldTypeByColumnName(select.getColumnName());
@Test public void testUnknownForeignField() throws Exception { TableInfo<Foreign, Void> tableInfo = new TableInfo<Foreign, Void>(databaseType, Foreign.class); try { tableInfo.getFieldTypeByColumnName("foo"); fail("expected exception"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("'" + Foreign.FOREIGN_FIELD_NAME + "'")); assertTrue(e.getMessage().contains("'foo'")); } }
@Test public void testObjectToString() throws Exception { String id = "f11232oo"; Foo foo = new Foo(); foo.id = id; assertEquals(id, foo.id); TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class); assertTrue(tableInfo.objectToString(foo).contains(id)); }
/** * Create and return an {@link SelectIterator} for the class using a prepared statement. */ public SelectIterator<T, ID> buildIterator(BaseDaoImpl<T, ID> classDao, ConnectionSource connectionSource, PreparedStmt<T> preparedStmt, ObjectCache objectCache, int resultFlags) throws SQLException { DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); CompiledStatement compiledStatement = null; try { compiledStatement = preparedStmt.compile(connection, StatementType.SELECT, resultFlags); SelectIterator<T, ID> iterator = new SelectIterator<T, ID>(tableInfo.getDataClass(), classDao, preparedStmt, connectionSource, connection, compiledStatement, preparedStmt.getStatement(), objectCache); connection = null; compiledStatement = null; return iterator; } finally { IOUtils.closeThrowSqlException(compiledStatement, "compiled statement"); if (connection != null) { connectionSource.releaseConnection(connection); } } }
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); }
foreignTableInfo = castDao.getTableInfo(); foreignIdField = foreignTableInfo.getIdField(); if (foreignIdField == null) { throw new IllegalArgumentException("Foreign field " + fieldClass + " does not have id field"); foreignRefField = foreignIdField; } else { foreignRefField = foreignTableInfo.getFieldTypeByColumnName(foreignColumnName); if (foreignRefField == null) { throw new IllegalArgumentException( foreignIdField = foreignTableInfo.getIdField(); if (foreignIdField == null) { throw new IllegalArgumentException("Foreign field " + fieldClass + " does not have id field");
/** * Verify the columnName is valid and return its FieldType. * * @throws IllegalArgumentException * if the column name is not valid. */ protected FieldType verifyColumnName(String columnName) { return tableInfo.getFieldTypeByColumnName(columnName); }
@Override public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { // create our object T rowObj = tableInfo.createObject(); for (int i = 0; i < columnNames.length; i++) { // sanity check, prolly will never happen but let's be careful out there if (i >= resultColumns.length) { continue; } // run through and convert each field FieldType fieldType = tableInfo.getFieldTypeByColumnName(columnNames[i]); Object fieldObj = fieldType.convertStringToJavaField(resultColumns[i], i); // assign it to the row object fieldType.assignField(rowObj, fieldObj, false, null); } return rowObj; } }
@Override public String objectToString(T data) { checkForInitialized(); return tableInfo.objectToString(data); }
private MappedCreate(Dao<T, ID> dao, TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes, String queryNextSequenceStmt, int versionFieldTypeIndex) { super(dao, tableInfo, statement, argFieldTypes); this.dataClassName = tableInfo.getDataClass().getSimpleName(); this.queryNextSequenceStmt = queryNextSequenceStmt; this.versionFieldTypeIndex = versionFieldTypeIndex; }