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

The following examples show how to use com.j256.ormlite.db.DatabaseType#appendEscapedEntityName() . 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: MappedDeleteCollection.java    From ormlite-core with ISC License 6 votes vote down vote up
private static void appendWhereIds(DatabaseType databaseType, FieldType idField, StringBuilder sb, int numDatas,
		FieldType[] fieldTypes) {
	sb.append("WHERE ");
	databaseType.appendEscapedEntityName(sb, idField.getColumnName());
	sb.append(" IN (");
	boolean first = true;
	for (int i = 0; i < numDatas; i++) {
		if (first) {
			first = false;
		} else {
			sb.append(',');
		}
		sb.append('?');
		if (fieldTypes != null) {
			fieldTypes[i] = idField;
		}
	}
	sb.append(") ");
}
 
Example 2
Source File: Not.java    From ormlite-core with ISC License 6 votes vote down vote up
@Override
public void appendSql(DatabaseType databaseType, String tableName, StringBuilder sb,
		List<ArgumentHolder> selectArgList) throws SQLException {
	if (comparison == null && exists == null) {
		throw new IllegalStateException("Clause has not been set in NOT operation");
	}
	// this generates: (NOT 'x' = 123 )
	if (comparison == null) {
		sb.append("(NOT ");
		exists.appendSql(databaseType, tableName, sb, selectArgList);
	} else {
		sb.append("(NOT ");
		if (tableName != null) {
			databaseType.appendEscapedEntityName(sb, tableName);
			sb.append('.');
		}
		databaseType.appendEscapedEntityName(sb, comparison.getColumnName());
		sb.append(' ');
		comparison.appendOperation(sb);
		comparison.appendValue(databaseType, sb, selectArgList);
	}
	sb.append(") ");
}
 
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
/**
 * Generate and return the list of statements to drop a database table.
 */
private static <T, ID> void addDropTableStatements(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
		List<String> statements, boolean logDetails) {
	List<String> statementsBefore = new ArrayList<String>();
	List<String> statementsAfter = new ArrayList<String>();
	for (FieldType fieldType : tableInfo.getFieldTypes()) {
		databaseType.dropColumnArg(fieldType, statementsBefore, statementsAfter);
	}
	StringBuilder sb = new StringBuilder(64);
	if (logDetails) {
		logger.info("dropping table '{}'", tableInfo.getTableName());
	}
	sb.append("DROP TABLE ");
	if (tableInfo.getSchemaName() != null && tableInfo.getSchemaName().length() > 0){
		databaseType.appendEscapedEntityName(sb, tableInfo.getSchemaName());
		sb.append('.');
	}
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(' ');
	statements.addAll(statementsBefore);
	statements.add(sb.toString());
	statements.addAll(statementsAfter);
}
 
Example 5
Source File: SchemaUtils.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Generate and return the list of statements to create a database schema and any associated features.
 */
private static <T, ID> void addCreateSchemaStatements(DatabaseType databaseType, String schemaName,
                                                      List<String> statements, List<String> queriesAfter, boolean ifNotExists, boolean logDetails)
        throws SQLException {
    StringBuilder sb = new StringBuilder(256);
    if (logDetails) {
        logger.info("creating schema '{}'", schemaName);
    }
    sb.append("CREATE SCHEMA ");
    if (ifNotExists && databaseType.isCreateIfNotExistsSupported()) {
        sb.append("IF NOT EXISTS ");
    }
    databaseType.appendEscapedEntityName(sb, schemaName);
    databaseType.appendCreateSchemaSuffix(sb);
    statements.add(sb.toString());
}
 
Example 6
Source File: BaseMappedStatement.java    From ormlite-core with ISC License 5 votes vote down vote up
static void appendTableName(DatabaseType databaseType, StringBuilder sb, String prefix, String tableName) {
	if (prefix != null) {
		sb.append(prefix);
	}
	databaseType.appendEscapedEntityName(sb, tableName);
	sb.append(' ');
}
 
Example 7
Source File: BaseMappedStatement.java    From ormlite-core with ISC License 5 votes vote down vote up
static void appendTableName(DatabaseType databaseType, StringBuilder sb, String prefix, TableInfo<?, ?> tableInfo) {
	if (prefix != null) {
		sb.append(prefix);
	}
	if (tableInfo.getSchemaName() != null && tableInfo.getSchemaName().length() > 0){
		databaseType.appendEscapedEntityName(sb, tableInfo.getSchemaName());
		sb.append('.');
	}
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(' ');
}
 
Example 8
Source File: BaseMappedStatement.java    From ormlite-core with ISC License 5 votes vote down vote up
static void appendFieldColumnName(DatabaseType databaseType, StringBuilder sb, FieldType fieldType,
		List<FieldType> fieldTypeList) {
	databaseType.appendEscapedEntityName(sb, fieldType.getColumnName());
	if (fieldTypeList != null) {
		fieldTypeList.add(fieldType);
	}
	sb.append(' ');
}
 
Example 9
Source File: BaseComparison.java    From ormlite-core with ISC License 5 votes vote down vote up
@Override
public void appendSql(DatabaseType databaseType, String tableName, StringBuilder sb, List<ArgumentHolder> argList)
		throws SQLException {
	if (tableName != null) {
		databaseType.appendEscapedEntityName(sb, tableName);
		sb.append('.');
	}
	databaseType.appendEscapedEntityName(sb, columnName);
	sb.append(' ');
	appendOperation(sb);
	// this needs to call appendValue (not appendArgOrValue) because it may be overridden
	appendValue(databaseType, sb, argList);
}
 
Example 10
Source File: SchemaUtils.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Generate and return the list of statements to drop a database schema.
 */
private static <T, ID> void addDropSchemaStatements(DatabaseType databaseType, String schemaName,
                                                    List<String> statements, boolean logDetails) {
    StringBuilder sb = new StringBuilder(64);
    if (logDetails) {
        logger.info("dropping schema '{}'", schemaName);
    }
    sb.append("DROP SCHEMA ");
    databaseType.appendEscapedEntityName(sb, schemaName);
    sb.append(' ');
    statements.add(sb.toString());
}
 
Example 11
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 12
Source File: ApsTableUtils.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static <T, ID> void addCreateIndexStatements(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
		List<String> statements, boolean ifNotExists, boolean unique) {
	// run through and look for index annotations
	Map<String, List<String>> indexMap = new HashMap<String, List<String>>();
	for (FieldType fieldType : tableInfo.getFieldTypes()) {
		String indexName;
		if (unique) {
			indexName = fieldType.getUniqueIndexName();
		} else {
			indexName = fieldType.getIndexName();
		}
		if (indexName == null) {
			continue;
		}
		indexName = checkOracleIndexKeyName(databaseType, indexName, indexMap);
		List<String> columnList = indexMap.get(indexName);
		if (columnList == null) {
			columnList = new ArrayList<String>();
			indexMap.put(indexName, columnList);
		}
		columnList.add(fieldType.getColumnName());
	}
	StringBuilder sb = new StringBuilder(128);
	for (Map.Entry<String, List<String>> indexEntry : indexMap.entrySet()) {
		logger.debug("creating index '{}' for table '{}", indexEntry.getKey(), tableInfo.getTableName());
		sb.append("CREATE ");
		if (unique) {
			sb.append("UNIQUE ");
		}
		sb.append("INDEX ");
		if (ifNotExists && databaseType.isCreateIndexIfNotExistsSupported()) {
			sb.append("IF NOT EXISTS ");
		}
		String indexKey = indexEntry.getKey();
		databaseType.appendEscapedEntityName(sb, indexKey);
		sb.append(" ON ");
		databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
		sb.append(" ( ");
		boolean first = true;
		for (String columnName : indexEntry.getValue()) {
			if (first) {
				first = false;
			} else {
				sb.append(", ");
			}
			databaseType.appendEscapedEntityName(sb, columnName);
		}
		sb.append(" )");
		statements.add(sb.toString());
		sb.setLength(0);
	}
}
 
Example 13
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);
}
 
Example 14
Source File: TableUtils.java    From ormlite-core with ISC License 4 votes vote down vote up
private static <T, ID> void addCreateIndexStatements(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
		List<String> statements, boolean ifNotExists, boolean unique, boolean logDetails) {
	// run through and look for index annotations
	Map<String, List<String>> indexMap = new HashMap<String, List<String>>();
	for (FieldType fieldType : tableInfo.getFieldTypes()) {
		String indexName;
		if (unique) {
			indexName = fieldType.getUniqueIndexName();
		} else {
			indexName = fieldType.getIndexName();
		}
		if (indexName == null) {
			continue;
		}

		List<String> columnList = indexMap.get(indexName);
		if (columnList == null) {
			columnList = new ArrayList<String>();
			indexMap.put(indexName, columnList);
		}
		columnList.add(fieldType.getColumnName());
	}

	StringBuilder sb = new StringBuilder(128);
	for (Map.Entry<String, List<String>> indexEntry : indexMap.entrySet()) {
		if (logDetails) {
			logger.info("creating index '{}' for table '{}", indexEntry.getKey(), tableInfo.getTableName());
		}
		sb.append("CREATE ");
		if (unique) {
			sb.append("UNIQUE ");
		}
		sb.append("INDEX ");
		if (ifNotExists && databaseType.isCreateIndexIfNotExistsSupported()) {
			sb.append("IF NOT EXISTS ");
		}
		databaseType.appendEscapedEntityName(sb, indexEntry.getKey());
		sb.append(" ON ");
		databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
		sb.append(" ( ");
		boolean first = true;
		for (String columnName : indexEntry.getValue()) {
			if (first) {
				first = false;
			} else {
				sb.append(", ");
			}
			databaseType.appendEscapedEntityName(sb, columnName);
		}
		sb.append(" )");
		statements.add(sb.toString());
		sb.setLength(0);
	}
}
 
Example 15
Source File: TestUtils.java    From ormlite-core with ISC License 4 votes vote down vote up
public static String appendEscapedEntityName(DatabaseType databaseType, String word) {
	StringBuilder sb = new StringBuilder();
	databaseType.appendEscapedEntityName(sb, word);
	return sb.toString();
}