Java Code Examples for com.j256.ormlite.support.ConnectionSource#getDatabaseType()

The following examples show how to use com.j256.ormlite.support.ConnectionSource#getDatabaseType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApsTableUtils.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static <T, ID> int doCreateTable(ConnectionSource connectionSource, TableInfo<T, ID> tableInfo,
		boolean ifNotExists) throws SQLException {
	DatabaseType databaseType = connectionSource.getDatabaseType();
	logger.debug("creating table '{}'", tableInfo.getTableName());
	List<String> statements = new ArrayList<String>();
	List<String> queriesAfter = new ArrayList<String>();
	addCreateTableStatements(databaseType, tableInfo, statements, queriesAfter, ifNotExists);
	DatabaseConnection connection = connectionSource.getReadWriteConnection();
	try {
		int stmtC =
				doStatements(connection, "create", statements, false, databaseType.isCreateTableReturnsNegative(),
						databaseType.isCreateTableReturnsZero());
		stmtC += doCreateTestQueries(connection, databaseType, queriesAfter);
		return stmtC;
	} finally {
		connectionSource.releaseConnection(connection);
	}
}
 
Example 2
Source File: DatabaseTableConfigUtil.java    From ormlite-android with ISC License 6 votes vote down vote up
/**
 * Build our list table config from a class using some annotation fu around.
 */
public static <T> DatabaseTableConfig<T> fromClass(ConnectionSource connectionSource, Class<T> clazz)
		throws SQLException {
	DatabaseType databaseType = connectionSource.getDatabaseType();
	String tableName = DatabaseTableConfig.extractTableName(databaseType, clazz);
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	for (Class<?> classWalk = clazz; classWalk != null; classWalk = classWalk.getSuperclass()) {
		for (Field field : classWalk.getDeclaredFields()) {
			DatabaseFieldConfig config = configFromField(databaseType, tableName, field);
			if (config != null && config.isPersisted()) {
				fieldConfigs.add(config);
			}
		}
	}
	if (fieldConfigs.size() == 0) {
		return null;
	} else {
		return new DatabaseTableConfig<T>(clazz, tableName, fieldConfigs);
	}
}
 
Example 3
Source File: TableUtils.java    From ormlite-core with ISC License 6 votes vote down vote up
private static <T> int clearTable(ConnectionSource connectionSource, String schemaName, String tableName) throws SQLException {
	DatabaseType databaseType = connectionSource.getDatabaseType();
	StringBuilder sb = new StringBuilder(48);
	if (databaseType.isTruncateSupported()) {
		sb.append("TRUNCATE TABLE ");
	} else {
		sb.append("DELETE FROM ");
	}
	if (schemaName != null && schemaName.length() > 0){
		databaseType.appendEscapedEntityName(sb, schemaName);
		sb.append('.');
	}
	databaseType.appendEscapedEntityName(sb, tableName);
	String statement = sb.toString();
	logger.info("clearing table '{}' with '{}", tableName, statement);
	CompiledStatement compiledStmt = null;
	DatabaseConnection connection = connectionSource.getReadWriteConnection(tableName);
	try {
		compiledStmt = connection.compileStatement(statement, StatementType.EXECUTE, noFieldTypes,
				DatabaseConnection.DEFAULT_RESULT_FLAGS, false);
		return compiledStmt.runExecute();
	} finally {
		IOUtils.closeThrowSqlException(compiledStmt, "compiled statement");
		connectionSource.releaseConnection(connection);
	}
}
 
Example 4
Source File: TableUtils.java    From ormlite-core with ISC License 6 votes vote down vote up
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);
	}
}
 
Example 5
Source File: SchemaUtils.java    From ormlite-core with ISC License 6 votes vote down vote up
private static <T, ID> int doCreateSchema(ConnectionSource connectionSource, String schemaName,
                                          boolean ifNotExists) throws SQLException {
    DatabaseType databaseType = connectionSource.getDatabaseType();
    List<String> statements = new ArrayList<String>();
    List<String> queriesAfter = new ArrayList<String>();
    addCreateSchemaStatements(databaseType, schemaName, statements, queriesAfter, ifNotExists, true);
    DatabaseConnection connection = connectionSource.getReadWriteConnection(schemaName);
    try {
        int stmtC = doStatements(connection, "create", statements, false,
                databaseType.isCreateSchemaReturnsNegative(), databaseType.isCreateSchemaReturnsZero());
        stmtC += doCreateTestQueries(connection, databaseType, queriesAfter);
        return stmtC;
    } finally {
        connectionSource.releaseConnection(connection);
    }
}
 
Example 6
Source File: FieldConfigMain.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: TableUtils.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Return an list of SQL statements that need to be run to create a table. To do the work of creating, you should
 * call {@link #createTable}.
 * 
 * @param connectionSource
 *            Our connect source which is used to get the database type, not to apply the creates.
 * @param tableConfig
 *            Hand or spring wired table configuration. If null then the class must have {@link DatabaseField}
 *            annotations.
 * @return The list of table create statements.
 */
public static <T, ID> List<String> getCreateTableStatements(ConnectionSource connectionSource,
		DatabaseTableConfig<T> tableConfig) throws SQLException {
	Dao<T, ID> dao = DaoManager.createDao(connectionSource, tableConfig);
	DatabaseType databaseType = connectionSource.getDatabaseType();
	if (dao instanceof BaseDaoImpl<?, ?>) {
		return addCreateTableStatements(databaseType, ((BaseDaoImpl<?, ?>) dao).getTableInfo(), false, false);
	} else {
		tableConfig.extractFieldTypes(databaseType);
		TableInfo<T, ID> tableInfo = new TableInfo<T, ID>(databaseType, tableConfig);
		return addCreateTableStatements(databaseType, tableInfo, false, false);
	}
}
 
Example 8
Source File: TableUtils.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Issue the database statements to drop the table associated with a dao.
 * 
 * @param dao
 *            Associated dao.
 * @return The number of statements executed to do so.
 */
public static <T, ID> int dropTable(Dao<T, ID> dao, boolean ignoreErrors) throws SQLException {
	ConnectionSource connectionSource = dao.getConnectionSource();
	Class<T> dataClass = dao.getDataClass();
	DatabaseType databaseType = connectionSource.getDatabaseType();
	if (dao instanceof BaseDaoImpl<?, ?>) {
		return doDropTable(databaseType, connectionSource, ((BaseDaoImpl<?, ?>) dao).getTableInfo(), ignoreErrors);
	} else {
		TableInfo<T, ID> tableInfo = new TableInfo<T, ID>(databaseType, dataClass);
		return doDropTable(databaseType, connectionSource, tableInfo, ignoreErrors);
	}
}
 
Example 9
Source File: TableUtils.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * 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);
	String schemaName = DatabaseTableConfig.extractSchemaName(dataClass);
	if (databaseType.isEntityNamesMustBeUpCase()) {
		tableName = databaseType.upCaseEntityName(tableName);
	}
	return clearTable(connectionSource, schemaName, tableName);
}
 
Example 10
Source File: TableUtils.java    From ormlite-core with ISC License 5 votes vote down vote up
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);
	}
}
 
Example 11
Source File: TableInfo.java    From ormlite-core with ISC License 4 votes vote down vote up
/**
 * @deprecated Use {@link #TableInfo(DatabaseType, Class)}
 */
@Deprecated
public TableInfo(ConnectionSource connectionSource, BaseDaoImpl<T, ID> baseDaoImpl, Class<T> dataClass)
		throws SQLException {
	this(connectionSource.getDatabaseType(), dataClass);
}
 
Example 12
Source File: TableUtils.java    From ormlite-core with ISC License 3 votes vote down vote up
/**
 * Issue the database statements to drop the table associated with a table configuration.
 * 
 * <p>
 * <b>WARNING:</b> This is [obviously] very destructive and is unrecoverable.
 * </p>
 * 
 * @param connectionSource
 *            Associated connection source.
 * @param tableConfig
 *            Hand or spring wired table configuration. If null then the class must have {@link DatabaseField}
 *            annotations.
 * @param ignoreErrors
 *            If set to true then try each statement regardless of {@link SQLException} thrown previously.
 * @return The number of statements executed to do so.
 */
public static <T, ID> int dropTable(ConnectionSource connectionSource, DatabaseTableConfig<T> tableConfig,
		boolean ignoreErrors) throws SQLException {
	DatabaseType databaseType = connectionSource.getDatabaseType();
	Dao<T, ID> dao = DaoManager.createDao(connectionSource, tableConfig);
	if (dao instanceof BaseDaoImpl<?, ?>) {
		return doDropTable(databaseType, connectionSource, ((BaseDaoImpl<?, ?>) dao).getTableInfo(), ignoreErrors);
	} else {
		tableConfig.extractFieldTypes(databaseType);
		TableInfo<T, ID> tableInfo = new TableInfo<T, ID>(databaseType, tableConfig);
		return doDropTable(databaseType, connectionSource, tableInfo, ignoreErrors);
	}
}
 
Example 13
Source File: SchemaUtils.java    From ormlite-core with ISC License 2 votes vote down vote up
/**
 * Issue the database statements to drop the schema associated with a schema configuration.
 *
 * <p>
 * <b>WARNING:</b> This is [obviously] very destructive and is unrecoverable.
 * </p>
 *
 * @param connectionSource Associated connection source.
 * @param schemaName     schema name
 * @param ignoreErrors     If set to true then try each statement regardless of {@link SQLException} thrown previously.
 * @return The number of statements executed to do so.
 */
public static <T, ID> int dropSchema(ConnectionSource connectionSource, String schemaName,
                                     boolean ignoreErrors) throws SQLException {
    DatabaseType databaseType = connectionSource.getDatabaseType();
    return doDropSchema(databaseType, connectionSource, schemaName, ignoreErrors);
}