com.j256.ormlite.db.DatabaseType Java Examples
The following examples show how to use
com.j256.ormlite.db.DatabaseType.
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: TableUtils.java From ormlite-core with ISC License | 6 votes |
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 #2
Source File: DatabaseFieldConfig.java From ormlite-core with ISC License | 6 votes |
/** * Find and return the appropriate getter method for field. * * @return Get method or null (or throws IllegalArgumentException) if none found. */ public static Method findGetMethod(Field field, DatabaseType databaseType, boolean throwExceptions) throws IllegalArgumentException { Method fieldGetMethod = findMethodFromNames(field, true, throwExceptions, methodFromField(field, "get", databaseType, true), methodFromField(field, "get", databaseType, false), methodFromField(field, "is", databaseType, true), methodFromField(field, "is", databaseType, false)); if (fieldGetMethod == null) { return null; } if (fieldGetMethod.getReturnType() != field.getType()) { if (throwExceptions) { throw new IllegalArgumentException("Return type of get method " + fieldGetMethod.getName() + " does not return " + field.getType()); } else { return null; } } return fieldGetMethod; }
Example #3
Source File: DatabaseFieldConfig.java From ormlite-core with ISC License | 6 votes |
/** * Find and return the appropriate setter method for field. * * @return Set method or null (or throws IllegalArgumentException) if none found. */ public static Method findSetMethod(Field field, DatabaseType databaseType, boolean throwExceptions) throws IllegalArgumentException { Method fieldSetMethod = findMethodFromNames(field, false, throwExceptions, methodFromField(field, "set", databaseType, true), methodFromField(field, "set", databaseType, false)); if (fieldSetMethod == null) { return null; } if (fieldSetMethod.getReturnType() != void.class) { if (throwExceptions) { throw new IllegalArgumentException("Return type of set method " + fieldSetMethod.getName() + " returns " + fieldSetMethod.getReturnType() + " instead of void"); } else { return null; } } return fieldSetMethod; }
Example #4
Source File: MappedCreate.java From ormlite-core with ISC License | 6 votes |
private static boolean isFieldCreatable(DatabaseType databaseType, FieldType fieldType) { // we don't insert anything if it is a collection if (fieldType.isForeignCollection()) { // skip foreign collections return false; } else if (fieldType.isReadOnly()) { // ignore read-only fields return false; } else if (databaseType.isIdSequenceNeeded() && databaseType.isSelectSequenceBeforeInsert()) { // we need to query for the next value from the sequence and the idField is inserted afterwards return true; } else if (fieldType.isGeneratedId() && !fieldType.isSelfGeneratedId() && !fieldType.isAllowGeneratedIdInsert()) { // skip generated-id fields because they will be auto-inserted return false; } else { return true; } }
Example #5
Source File: DatabaseTableConfigUtil.java From ormlite-android with ISC License | 6 votes |
/** * 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 #6
Source File: DatabaseTableConfig.java From ormlite-core with ISC License | 6 votes |
/** * Extract and return the table name for a class. */ public static <T> String extractTableName(DatabaseType databaseType, Class<T> clazz) { DatabaseTable databaseTable = clazz.getAnnotation(DatabaseTable.class); String name = null; if (databaseTable != null && databaseTable.tableName() != null && databaseTable.tableName().length() > 0) { name = databaseTable.tableName(); } if (name == null && javaxPersistenceConfigurer != null) { name = javaxPersistenceConfigurer.getEntityName(clazz); } if (name == null) { // if the name isn't specified, it is the class name lowercased if (databaseType == null) { // database-type is optional so if it is not specified we just use english name = clazz.getSimpleName().toLowerCase(Locale.ENGLISH); } else { name = databaseType.downCaseString(clazz.getSimpleName(), true); } } return name; }
Example #7
Source File: MappedCreateTest.java From ormlite-core with ISC License | 6 votes |
@Test public void testGeneratedIdSequenceLong() throws Exception { DatabaseType databaseType = new NeedsSequenceDatabaseType(); connectionSource.setDatabaseType(databaseType); Dao<GeneratedIdLong, Long> dao = createDao(GeneratedIdLong.class, false); StatementExecutor<GeneratedIdLong, Long> se = new StatementExecutor<GeneratedIdLong, Long>(databaseType, new TableInfo<GeneratedIdLong, Long>(databaseType, GeneratedIdLong.class), dao); DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L); expect(databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), (GeneratedKeyHolder) isNull())).andReturn(1); replay(databaseConnection); GeneratedIdLong genIdSeq = new GeneratedIdLong(); se.create(databaseConnection, genIdSeq, null); verify(databaseConnection); }
Example #8
Source File: In.java From ormlite-core with ISC License | 6 votes |
@Override public void appendValue(DatabaseType databaseType, StringBuilder sb, List<ArgumentHolder> columnArgList) throws SQLException { sb.append('('); boolean first = true; for (Object value : objects) { if (value == null) { throw new IllegalArgumentException("one of the IN values for '" + columnName + "' is null"); } if (first) { first = false; } else { sb.append(','); } // for each of our arguments, add it to the output super.appendArgOrValue(databaseType, fieldType, sb, columnArgList, value); } sb.append(") "); }
Example #9
Source File: DatabaseTableConfig.java From ormlite-core with ISC License | 6 votes |
private static <T> FieldType[] extractFieldTypes(DatabaseType databaseType, Class<T> clazz, String tableName) throws SQLException { List<FieldType> fieldTypes = new ArrayList<FieldType>(); for (Class<?> classWalk = clazz; classWalk != null; classWalk = classWalk.getSuperclass()) { for (Field field : classWalk.getDeclaredFields()) { FieldType fieldType = FieldType.createFieldType(databaseType, tableName, field, clazz); if (fieldType != null) { fieldTypes.add(fieldType); } } } if (fieldTypes.isEmpty()) { throw new IllegalArgumentException( "No fields have a " + DatabaseField.class.getSimpleName() + " annotation in " + clazz); } return fieldTypes.toArray(new FieldType[fieldTypes.size()]); }
Example #10
Source File: ManyClause.java From ormlite-core with ISC License | 6 votes |
@Override public void appendSql(DatabaseType databaseType, String tableName, StringBuilder sb, List<ArgumentHolder> selectArgList) throws SQLException { sb.append('('); first.appendSql(databaseType, tableName, sb, selectArgList); if (second != null) { sb.append(operation); sb.append(' '); second.appendSql(databaseType, tableName, sb, selectArgList); } if (others != null) { for (int i = startOthersAt; i < others.length; i++) { sb.append(operation); sb.append(' '); others[i].appendSql(databaseType, tableName, sb, selectArgList); } } sb.append(") "); }
Example #11
Source File: SchemaUtils.java From ormlite-core with ISC License | 6 votes |
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 #12
Source File: TableUtils.java From ormlite-core with ISC License | 6 votes |
/** * 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 #13
Source File: MappedCreateTest.java From ormlite-core with ISC License | 6 votes |
@Test public void testGeneratedIdSequence() throws Exception { DatabaseType databaseType = new NeedsSequenceDatabaseType(); connectionSource.setDatabaseType(databaseType); TableInfo<GeneratedId, Integer> tableInfo = new TableInfo<GeneratedId, Integer>(databaseType, GeneratedId.class); Dao<GeneratedId, Integer> dao = createDao(GeneratedId.class, false); StatementExecutor<GeneratedId, Integer> se = new StatementExecutor<GeneratedId, Integer>(databaseType, tableInfo, dao); DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L); expect(databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), (GeneratedKeyHolder) isNull())).andReturn(1); replay(databaseConnection); GeneratedId genIdSeq = new GeneratedId(); se.create(databaseConnection, genIdSeq, null); verify(databaseConnection); }
Example #14
Source File: TableUtils.java From ormlite-core with ISC License | 6 votes |
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 #15
Source File: MappedDeleteCollection.java From ormlite-core with ISC License | 6 votes |
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 #16
Source File: Not.java From ormlite-core with ISC License | 6 votes |
@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 #17
Source File: ApsTableUtils.java From entando-core with GNU Lesser General Public License v3.0 | 6 votes |
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 #18
Source File: WrappedConnectionSource.java From ormlite-core with ISC License | 5 votes |
public void setDatabaseType(DatabaseType databaseType) { Method method; try { method = cs.getClass().getMethod("setDatabaseType", new Class[] { DatabaseType.class }); method.invoke(cs, databaseType); } catch (Exception e) { throw new RuntimeException("Could not set database type", e); } }
Example #19
Source File: TableUtils.java From ormlite-core with ISC License | 5 votes |
private static int doCreateTestQueries(DatabaseConnection connection, DatabaseType databaseType, List<String> queriesAfter) throws SQLException { int stmtC = 0; // now execute any test queries which test the newly created table for (String query : queriesAfter) { CompiledStatement compiledStmt = null; try { compiledStmt = connection.compileStatement(query, StatementType.SELECT, noFieldTypes, DatabaseConnection.DEFAULT_RESULT_FLAGS, false); // we don't care about an object cache here DatabaseResults results = compiledStmt.runQuery(null); int rowC = 0; // count the results for (boolean isThereMore = results.first(); isThereMore; isThereMore = results.next()) { rowC++; } logger.info("executing create table after-query got {} results: {}", rowC, query); } catch (SQLException e) { // we do this to make sure that the statement is in the exception throw SqlExceptionUtil.create("executing create table after-query failed: " + query, e); } finally { // result set is closed by the statement being closed IOUtils.closeThrowSqlException(compiledStmt, "compiled statement"); } stmtC++; } return stmtC; }
Example #20
Source File: TableUtils.java From ormlite-core with ISC License | 5 votes |
private static <T, ID> List<String> addCreateTableStatements(DatabaseType databaseType, TableInfo<T, ID> tableInfo, boolean ifNotExists, boolean logDetails) throws SQLException { List<String> statements = new ArrayList<String>(); List<String> queriesAfter = new ArrayList<String>(); addCreateTableStatements(databaseType, tableInfo, statements, queriesAfter, ifNotExists, logDetails); return statements; }
Example #21
Source File: DatabaseTableConfig.java From ormlite-core with ISC License | 5 votes |
private DatabaseTableConfig(DatabaseType databaseType, Class<T> dataClass, String schemaName, String tableName, FieldType[] fieldTypes) { // NOTE: databaseType may be null this.databaseType = databaseType; this.dataClass = dataClass; this.schemaName = schemaName; this.tableName = tableName; this.fieldTypes = fieldTypes; }
Example #22
Source File: TableUtils.java From ormlite-core with ISC License | 5 votes |
/** * 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 #23
Source File: DatabaseTableConfig.java From ormlite-core with ISC License | 5 votes |
/** * 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(DatabaseType databaseType, Class<T> clazz) throws SQLException { String tableName = extractTableName(databaseType, clazz); String schemaName = extractSchemaName(clazz); if (databaseType.isEntityNamesMustBeUpCase()) { tableName = databaseType.upCaseEntityName(tableName); schemaName = databaseType.upCaseEntityName(schemaName); } return new DatabaseTableConfig<T>(databaseType, clazz, schemaName, tableName, extractFieldTypes(databaseType, clazz, tableName)); }
Example #24
Source File: BaseMappedStatement.java From ormlite-core with ISC License | 5 votes |
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 #25
Source File: DatabaseTableConfig.java From ormlite-core with ISC License | 5 votes |
/** * Return the field types associated with this configuration. */ public FieldType[] getFieldTypes(DatabaseType databaseType) throws SQLException { if (fieldTypes == null) { throw new SQLException("Field types have not been extracted in table config"); } return fieldTypes; }
Example #26
Source File: TableUtils.java From ormlite-core with ISC License | 5 votes |
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 #27
Source File: FieldType.java From ormlite-core with ISC License | 5 votes |
/** * Return An instantiated {@link FieldType} or null if the field does not have a {@link DatabaseField} annotation. */ public static FieldType createFieldType(DatabaseType databaseType, String tableName, Field field, Class<?> parentClass) throws SQLException { DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, tableName, field); if (fieldConfig == null) { return null; } else { return new FieldType(databaseType, tableName, field, fieldConfig, parentClass); } }
Example #28
Source File: BaseComparison.java From ormlite-core with ISC License | 5 votes |
@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 #29
Source File: MappedDelete.java From ormlite-core with ISC License | 5 votes |
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); appendWhereFieldEq(databaseType, idField, sb, null); return new MappedDelete<T, ID>(dao, tableInfo, sb.toString(), new FieldType[] { idField }); }
Example #30
Source File: Exists.java From ormlite-core with ISC License | 5 votes |
@Override public void appendSql(DatabaseType databaseType, String tableName, StringBuilder sb, List<ArgumentHolder> argList) throws SQLException { sb.append("EXISTS ("); subQueryBuilder.appendStatementString(sb, argList); sb.append(") "); }