Java Code Examples for com.j256.ormlite.db.DatabaseType#appendCreateTableSuffix()

The following examples show how to use com.j256.ormlite.db.DatabaseType#appendCreateTableSuffix() . 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 4 votes vote down vote up
/**
 * Generate and return the list of statements to create a database table and any associated features.
 */
private static <T, ID> void addCreateTableStatements(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
		List<String> statements, List<String> queriesAfter, boolean ifNotExists) throws SQLException {
	StringBuilder sb = new StringBuilder(256);
	sb.append("CREATE TABLE ");
	if (ifNotExists && databaseType.isCreateIfNotExistsSupported()) {
		sb.append("IF NOT EXISTS ");
	}
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" (");
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	List<String> statementsAfter = new ArrayList<String>();
	// our statement will be set here later
	boolean first = true;
	for (FieldType fieldType : tableInfo.getFieldTypes()) {
		// skip foreign collections
		if (fieldType.isForeignCollection()) {
			continue;
		} else if (first) {
			first = false;
		} else {
			sb.append(", ");
		}
		String columnDefinition = fieldType.getColumnDefinition();
		if (columnDefinition == null) {
			// we have to call back to the database type for the specific create syntax
			databaseType.appendColumnArg(tableInfo.getTableName(), sb, fieldType, additionalArgs, statementsBefore,
					statementsAfter, queriesAfter);
		} else {
			// hand defined field
			databaseType.appendEscapedEntityName(sb, fieldType.getColumnName());
			sb.append(' ').append(columnDefinition).append(' ');
		}
	}
	// add any sql that sets any primary key fields
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, statementsAfter,
			queriesAfter);
	// add any sql that sets any unique fields
	databaseType.addUniqueComboSql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, statementsAfter,
			queriesAfter);
	for (String arg : additionalArgs) {
		// we will have spat out one argument already so we don't have to do the first dance
		sb.append(", ").append(arg);
	}
	sb.append(") ");
	databaseType.appendCreateTableSuffix(sb);
	statements.addAll(statementsBefore);
	statements.add(sb.toString());
	statements.addAll(statementsAfter);
	addCreateIndexStatements(databaseType, tableInfo, statements, ifNotExists, false);
	addCreateIndexStatements(databaseType, tableInfo, statements, ifNotExists, true);
}
 
Example 2
Source File: TableUtils.java    From ormlite-core with ISC License 4 votes vote down vote up
/**
 * Generate and return the list of statements to create a database table and any associated features.
 */
private static <T, ID> void addCreateTableStatements(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
		List<String> statements, List<String> queriesAfter, boolean ifNotExists, boolean logDetails)
		throws SQLException {
	StringBuilder sb = new StringBuilder(256);
	if (logDetails) {
		logger.info("creating table '{}'", tableInfo.getTableName());
	}
	sb.append("CREATE TABLE ");
	if (ifNotExists && databaseType.isCreateIfNotExistsSupported()) {
		sb.append("IF NOT EXISTS ");
	}
	if (tableInfo.getSchemaName() != null && tableInfo.getSchemaName().length() > 0){
		databaseType.appendEscapedEntityName(sb, tableInfo.getSchemaName());
		sb.append('.');
	}
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" (");
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	List<String> statementsAfter = new ArrayList<String>();
	// our statement will be set here later
	boolean first = true;
	for (FieldType fieldType : tableInfo.getFieldTypes()) {
		// skip foreign collections
		if (fieldType.isForeignCollection()) {
			continue;
		} else if (first) {
			first = false;
		} else {
			sb.append(", ");
		}
		String columnDefinition = fieldType.getColumnDefinition();
		if (columnDefinition == null) {
			// we have to call back to the database type for the specific create syntax
			databaseType.appendColumnArg(tableInfo.getTableName(), sb, fieldType, additionalArgs, statementsBefore,
					statementsAfter, queriesAfter);
		} else {
			// hand defined field
			databaseType.appendEscapedEntityName(sb, fieldType.getColumnName());
			sb.append(' ').append(columnDefinition).append(' ');
		}
	}
	// add any sql that sets any primary key fields
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, statementsAfter,
			queriesAfter);
	// add any sql that sets any unique fields
	databaseType.addUniqueComboSql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, statementsAfter,
			queriesAfter);
	for (String arg : additionalArgs) {
		// we will have spat out one argument already so we don't have to do the first dance
		sb.append(", ").append(arg);
	}
	sb.append(") ");
	databaseType.appendCreateTableSuffix(sb);
	statements.addAll(statementsBefore);
	statements.add(sb.toString());
	statements.addAll(statementsAfter);
	addCreateIndexStatements(databaseType, tableInfo, statements, ifNotExists, false, logDetails);
	addCreateIndexStatements(databaseType, tableInfo, statements, ifNotExists, true, logDetails);
}