/** * @deprecated Use {@link #TableInfo(DatabaseType, Class)} */ @Deprecated public TableInfo(ConnectionSource connectionSource, BaseDaoImpl<T, ID> baseDaoImpl, Class<T> dataClass) throws SQLException { this(connectionSource.getDatabaseType(), dataClass); }
/** * @deprecated Use {@link #getCreateTableStatements(DatabaseType, Class)}. */ @Deprecated public static <T, ID> List<String> getCreateTableStatements(ConnectionSource connectionSource, Class<T> dataClass) throws SQLException { return getCreateTableStatements(connectionSource.getDatabaseType(), dataClass); }
/** * @deprecated Use {@link #createFieldType(DatabaseType, String, Field, Class)}. */ @Deprecated public static FieldType createFieldType(ConnectionSource connectionSource, String tableName, Field field, Class<?> parentClass) throws SQLException { return createFieldType(connectionSource.getDatabaseType(), tableName, field, parentClass); }
/** * @deprecated Use {@link #fromClass(DatabaseType, Class)} */ @Deprecated public static <T> DatabaseTableConfig<T> fromClass(ConnectionSource connectionSource, Class<T> clazz) throws SQLException { return fromClass(connectionSource.getDatabaseType(), clazz); }
@Override public DatabaseType getDatabaseType() { return connectionSource.getDatabaseType(); }
private static <T, ID> List<String> addCreateTableStatements(ConnectionSource connectionSource, TableInfo<T, ID> tableInfo, boolean ifNotExists) throws SQLException { List<String> statements = new ArrayList<String>(); List<String> queriesAfter = new ArrayList<String>(); addCreateTableStatements(connectionSource.getDatabaseType(), tableInfo, statements, queriesAfter, ifNotExists); return statements; }
@Override public DatabaseType getDatabaseType() { return cs.getDatabaseType(); }
/** * Creates a holder of information about a table/class. * * @param connectionSource * Source of our database connections. * @param baseDaoImpl * Associated BaseDaoImpl. * @param dataClass * Class that we are holding information about. */ public TableInfo(ConnectionSource connectionSource, BaseDaoImpl<T, ID> baseDaoImpl, Class<T> dataClass) throws SQLException { this(connectionSource.getDatabaseType(), baseDaoImpl, DatabaseTableConfig.fromClass(connectionSource, dataClass)); }
/** * Return An instantiated {@link FieldType} or null if the field does not have a {@link DatabaseField} annotation. */ public static FieldType createFieldType(ConnectionSource connectionSource, String tableName, Field field, Class<?> parentClass) throws SQLException { DatabaseType databaseType = connectionSource.getDatabaseType(); DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, tableName, field); if (fieldConfig == null) { return null; } else { return new FieldType(connectionSource, tableName, field, fieldConfig, parentClass); } }
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 }); }
/** * Clear all data out of the table. For certain database types and with large sized tables, which may take a long * time. In some configurations, it may be faster to drop and re-create the table. * * <p> * <b>WARNING:</b> This is [obviously] very destructive and is unrecoverable. * </p> */ public static <T> int clearTable(ConnectionSource connectionSource, Class<T> dataClass) throws SQLException { DatabaseType databaseType = connectionSource.getDatabaseType(); String tableName = DatabaseTableConfig.extractTableName(databaseType, dataClass); if (databaseType.isEntityNamesMustBeUpCase()) { tableName = databaseType.upCaseEntityName(tableName); } return clearTable(connectionSource, tableName); }
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(expected = SQLException.class) public void testNoIdBuildDelete() throws Exception { DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); ConnectionSource connectionSource = createMock(ConnectionSource.class); expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes(); replay(connectionSource); Dao<NoId, Void> dao = createDao(NoId.class, false); MappedDeleteCollection.deleteObjects(dao, new TableInfo<NoId, Void>(databaseType, NoId.class), databaseConnection, new ArrayList<NoId>(), null); }
protected FieldType getIdField(Class clazz) throws SQLException { DatabaseTableConfig config = getDatabaseTableConfig(clazz); FieldType[] fieldTypes = config.getFieldTypes(databaseHelper.getConnectionSource().getDatabaseType()); FieldType idFieldType = null; for (FieldType fieldType : fieldTypes) { if (fieldType.isId() || fieldType.isGeneratedId() || fieldType.isGeneratedIdSequence()) { idFieldType = fieldType; } } if (idFieldType == null) { throw new IllegalStateException("Cannot find id field"); } return idFieldType; }
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()); } }
private static <T, ID> int doCreateTable(ConnectionSource connectionSource, TableInfo<T, ID> tableInfo, boolean ifNotExists) throws SQLException { DatabaseType databaseType = connectionSource.getDatabaseType(); List<String> statements = new ArrayList<String>(); List<String> queriesAfter = new ArrayList<String>(); addCreateTableStatements(databaseType, tableInfo, statements, queriesAfter, ifNotExists, true); DatabaseConnection connection = connectionSource.getReadWriteConnection(tableInfo.getTableName()); try { int stmtC = doStatements(connection, "create", statements, false, databaseType.isCreateTableReturnsNegative(), databaseType.isCreateTableReturnsZero()); stmtC += doCreateTestQueries(connection, databaseType, queriesAfter); return stmtC; } finally { connectionSource.releaseConnection(connection); } }
/** * Extract the DatabaseTableConfig for a particular class by looking for class and field annotations. This is used * by internal classes to configure a class. */ public static <T> DatabaseTableConfig<T> fromClass(ConnectionSource connectionSource, Class<T> clazz) throws SQLException { DatabaseType databaseType = connectionSource.getDatabaseType(); String tableName = extractTableName(databaseType, clazz); if (databaseType.isEntityNamesMustBeUpCase()) { tableName = databaseType.upCaseEntityName(tableName); } return new DatabaseTableConfig<T>(databaseType, clazz, tableName, extractFieldTypes(connectionSource, clazz, tableName)); }
private static <T, ID> int doCreateTable(Dao<T, ID> dao, boolean ifNotExists) throws SQLException { ConnectionSource connectionSource = dao.getConnectionSource(); DatabaseType databaseType = connectionSource.getDatabaseType(); if (dao instanceof BaseDaoImpl<?, ?>) { return doCreateTable(connectionSource, ((BaseDaoImpl<?, ?>) dao).getTableInfo(), ifNotExists); } else { TableInfo<T, ID> tableInfo = new TableInfo<T, ID>(databaseType, dao.getDataClass()); return doCreateTable(connectionSource, tableInfo, ifNotExists); } }
/** * Setup our database and DAOs */ private void setupDatabase(ConnectionSource connectionSource) throws Exception { DatabaseType databaseType = connectionSource.getDatabaseType(); DatabaseTableConfig<Account> accountTableConfig = buildAccountTableConfig(databaseType); accountDao = DaoManager.createDao(connectionSource, accountTableConfig); DatabaseTableConfig<Delivery> deliveryTableConfig = buildDeliveryTableConfig(databaseType, accountTableConfig); deliveryDao = DaoManager.createDao(connectionSource, deliveryTableConfig); // if you need to create the table TableUtils.createTable(connectionSource, accountTableConfig); TableUtils.createTable(connectionSource, deliveryTableConfig); }