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"); } }
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"); } }
@Override protected void appendStatementStart(StringBuilder sb, List<ArgumentHolder> argList) { sb.append("DELETE FROM "); databaseType.appendEscapedEntityName(sb, tableInfo.getTableName()); sb.append(' '); }
protected static <T, ID> String buildStatement(DatabaseType databaseType, TableInfo<T, ID> tableInfo, FieldType idFieldType) { // build the select statement by hand StringBuilder sb = new StringBuilder(64); appendTableName(databaseType, sb, "SELECT * FROM ", tableInfo.getTableName()); appendWhereFieldEq(databaseType, idFieldType, sb, null); return sb.toString(); }
@Override public boolean isTableExists() throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return connection.isTableExists(tableInfo.getTableName()); } finally { connectionSource.releaseConnection(connection); } }
@Override public boolean idExists(ID id) throws SQLException { DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.ifExists(connection, id); } finally { connectionSource.releaseConnection(connection); } }
/** * Call batch tasks inside of a connection which may, or may not, have been "saved". */ public <CT> CT callBatchTasks(ConnectionSource connectionSource, Callable<CT> callable) throws SQLException { if (connectionSource.isSingleConnection(tableInfo.getTableName())) { synchronized (this) { return doCallBatchTasks(connectionSource, callable); } } else { return doCallBatchTasks(connectionSource, callable); } }
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 }); }
@Override public T queryForId(ID id) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForId(connection, id, objectCache); } finally { connectionSource.releaseConnection(connection); } }
@Override public T queryForFirst(PreparedQuery<T> preparedQuery) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForFirst(connection, preparedQuery, objectCache); } finally { connectionSource.releaseConnection(connection); } }
@Override public long countOf() throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForCountStar(connection); } finally { connectionSource.releaseConnection(connection); } }
@Override public int executeRaw(String statement, String... arguments) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadWriteConnection(tableInfo.getTableName()); try { return statementExecutor.executeRaw(connection, statement, arguments); } catch (SQLException e) { throw SqlExceptionUtil.create("Could not run raw execute statement " + statement, e); } finally { connectionSource.releaseConnection(connection); } }
@Override public int executeRawNoArgs(String statement) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadWriteConnection(tableInfo.getTableName()); try { return statementExecutor.executeRawNoArgs(connection, statement); } catch (SQLException e) { throw SqlExceptionUtil.create("Could not run raw execute statement " + statement, e); } finally { connectionSource.releaseConnection(connection); } }
@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 int updateRaw(String statement, String... arguments) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadWriteConnection(tableInfo.getTableName()); try { return statementExecutor.updateRaw(connection, statement, arguments); } catch (SQLException e) { throw SqlExceptionUtil.create("Could not run raw update statement " + statement, e); } finally { connectionSource.releaseConnection(connection); } }
@Test public void testLimit() throws Exception { QueryBuilder<Foo, Integer> qb = new QueryBuilder<Foo, Integer>(databaseType, baseFooTableInfo, null); long limit = 103; qb.limit(limit); StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM "); databaseType.appendEscapedEntityName(sb, baseFooTableInfo.getTableName()); sb.append(" LIMIT ").append(limit).append(' '); assertEquals(sb.toString(), qb.prepareStatementString()); }
@Test public void testSelectAll() throws Exception { QueryBuilder<Foo, Integer> qb = new QueryBuilder<Foo, Integer>(databaseType, baseFooTableInfo, null); StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM "); databaseType.appendEscapedEntityName(sb, baseFooTableInfo.getTableName()); sb.append(' '); assertEquals(sb.toString(), qb.prepareStatementString()); }
@Test public void testDistinct() throws Exception { QueryBuilder<Foo, Integer> qb = new QueryBuilder<Foo, Integer>(databaseType, baseFooTableInfo, null); qb.distinct(); StringBuilder sb = new StringBuilder(); sb.append("SELECT DISTINCT * FROM "); databaseType.appendEscapedEntityName(sb, baseFooTableInfo.getTableName()); sb.append(' '); assertEquals(sb.toString(), qb.prepareStatementString()); }
@Test public void testPrepareStatement() throws Exception { Dao<Foo, Integer> dao = createDao(Foo.class, false); QueryBuilder<Foo, Integer> qb = new QueryBuilder<Foo, Integer>(databaseType, baseFooTableInfo, dao); PreparedQuery<Foo> stmt = qb.prepare(); stmt.getStatement(); StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM "); databaseType.appendEscapedEntityName(sb, baseFooTableInfo.getTableName()); sb.append(' '); assertEquals(sb.toString(), qb.prepareStatementString()); }
@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()); }