Java Code Examples for com.j256.ormlite.table.TableInfo#getFieldTypes()

The following examples show how to use com.j256.ormlite.table.TableInfo#getFieldTypes() . 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: OracleDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdSequenceAutoName() throws Exception {
	TableInfo<GeneratedIdSequenceAutoName, Integer> tableInfo =
			new TableInfo<GeneratedIdSequenceAutoName, Integer>(databaseType, GeneratedIdSequenceAutoName.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	FieldType idField = tableInfo.getFieldTypes()[0];
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, idField, additionalArgs, statementsBefore, null, null);
	assertEquals(1, statementsBefore.size());
	String seqName = databaseType
			.generateIdSequenceName(GeneratedIdSequenceAutoName.class.getSimpleName().toLowerCase(), idField);
	assertTrue(statementsBefore.get(0).contains(seqName));
	assertEquals(0, additionalArgs.size());
}
 
Example 2
Source File: HsqldbDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdSequenceAutoName() throws Exception {
	TableInfo<GeneratedIdSequenceAutoName, Integer> tableInfo =
			new TableInfo<GeneratedIdSequenceAutoName, Integer>(databaseType, GeneratedIdSequenceAutoName.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	FieldType idField = tableInfo.getFieldTypes()[0];
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, idField, additionalArgs, statementsBefore, null, null);
	databaseType.addPrimaryKeySql(new FieldType[] { idField }, additionalArgs, statementsBefore, null, null);
	String seqName = databaseType
			.generateIdSequenceName(GeneratedIdSequenceAutoName.class.getSimpleName().toLowerCase(), idField);
	assertTrue(sb + " should contain gen-id-seq-name stuff",
			sb.toString().contains(" GENERATED BY DEFAULT AS IDENTITY "));
	// sequence, sequence table, insert
	assertEquals(1, statementsBefore.size());
	assertTrue(statementsBefore.get(0).contains(seqName.toUpperCase()));
	assertEquals(1, additionalArgs.size());
	assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
}
 
Example 3
Source File: MappedPreparedQueryTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testLimit() throws Exception {
	Dao<LocalFoo, Integer> fooDao = createDao(LocalFoo.class, true);
	List<LocalFoo> foos = new ArrayList<LocalFoo>();
	LocalFoo foo = new LocalFoo();
	// create foo #1
	fooDao.create(foo);
	foos.add(foo);
	foo = new LocalFoo();
	// create foo #2
	fooDao.create(foo);
	foos.add(foo);

	TableInfo<LocalFoo, Integer> tableInfo = new TableInfo<LocalFoo, Integer>(databaseType, LocalFoo.class);
	MappedPreparedStmt<LocalFoo, Integer> preparedQuery = new MappedPreparedStmt<LocalFoo, Integer>(fooDao,
			tableInfo, "select * from " + TABLE_NAME, new FieldType[0], tableInfo.getFieldTypes(),
			new ArgumentHolder[0], 1L, StatementType.SELECT, false);

	checkResults(foos, preparedQuery, 1);
	preparedQuery = new MappedPreparedStmt<LocalFoo, Integer>(fooDao, tableInfo, "select * from " + TABLE_NAME,
			new FieldType[0], tableInfo.getFieldTypes(), new ArgumentHolder[0], null, StatementType.SELECT, false);
	checkResults(foos, preparedQuery, 2);
}
 
Example 4
Source File: PostgresDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdSequenceAutoName() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<GeneratedIdSequenceAutoName, Integer> tableInfo =
			new TableInfo<GeneratedIdSequenceAutoName, Integer>(databaseType, GeneratedIdSequenceAutoName.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	FieldType idField = tableInfo.getFieldTypes()[0];
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	List<String> queriesAfter = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, idField, additionalArgs, statementsBefore, null, queriesAfter);
	databaseType.addPrimaryKeySql(new FieldType[] { idField }, additionalArgs, statementsBefore, null,
			queriesAfter);
	String seqName = databaseType
			.generateIdSequenceName(GeneratedIdSequenceAutoName.class.getSimpleName().toLowerCase(), idField);
	assertTrue(sb.toString().contains(" DEFAULT NEXTVAL('\"" + seqName + "\"')"));
	assertEquals(1, statementsBefore.size());
	assertTrue(statementsBefore.get(0).contains(seqName));
	assertEquals(1, additionalArgs.size());
	assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
	assertEquals(0, queriesAfter.size());
}
 
Example 5
Source File: Db2DatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testByte() throws Exception {
	TableInfo<AllTypes, Void> tableInfo = new TableInfo<AllTypes, Void>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType byteField = tableInfo.getFieldTypes()[3];
	assertEquals("byteField", byteField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, byteField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("SMALLINT"));
}
 
Example 6
Source File: DerbyEmbeddedDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testByte() throws Exception {
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType byteField = tableInfo.getFieldTypes()[3];
	assertEquals("byteField", byteField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, byteField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("SMALLINT"));
}
 
Example 7
Source File: DerbyEmbeddedDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testBoolean() throws Exception {
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType booleanField = tableInfo.getFieldTypes()[1];
	assertEquals("booleanField", booleanField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, booleanField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString() + " not in right format", sb.toString().contains("SMALLINT"));
}
 
Example 8
Source File: MysqlDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testBoolean() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType booleanField = tableInfo.getFieldTypes()[1];
	assertEquals("booleanField", booleanField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, booleanField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("TINYINT(1)"));
}
 
Example 9
Source File: SqlServerDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testDate() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType byteField = tableInfo.getFieldTypes()[2];
	assertEquals("dateField", byteField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, byteField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("DATETIME"));
}
 
Example 10
Source File: SqlServerDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testByte() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType byteField = tableInfo.getFieldTypes()[3];
	assertEquals("byteField", byteField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, byteField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("SMALLINT"));
}
 
Example 11
Source File: SqlServerDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testBoolean() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType booleanField = tableInfo.getFieldTypes()[1];
	assertEquals("booleanField", booleanField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, booleanField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("BIT"));
}
 
Example 12
Source File: MappedQueryForFieldEq.java    From ormlite-core with ISC License 5 votes vote down vote up
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");
}
 
Example 13
Source File: Db2DatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testBoolean() throws Exception {
	TableInfo<AllTypes, Void> tableInfo = new TableInfo<AllTypes, Void>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType booleanField = tableInfo.getFieldTypes()[1];
	assertEquals("booleanField", booleanField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, booleanField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("SMALLINT"));
}
 
Example 14
Source File: MappedRefresh.java    From ormlite-core with ISC License 5 votes vote down vote up
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());
}
 
Example 15
Source File: MappedPreparedQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testMapRow() throws Exception {
	Dao<LocalFoo, Integer> fooDao = createDao(LocalFoo.class, true);
	LocalFoo foo1 = new LocalFoo();
	fooDao.create(foo1);

	TableInfo<LocalFoo, Integer> tableInfo = new TableInfo<LocalFoo, Integer>(databaseType, LocalFoo.class);
	MappedPreparedStmt<LocalFoo, Integer> rowMapper =
			new MappedPreparedStmt<LocalFoo, Integer>(fooDao, tableInfo, null, new FieldType[0],
					tableInfo.getFieldTypes(), new ArgumentHolder[0], null, StatementType.SELECT, false);

	DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME);
	CompiledStatement stmt = null;
	try {
		stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, new FieldType[0],
				DatabaseConnection.DEFAULT_RESULT_FLAGS, true);

		DatabaseResults results = stmt.runQuery(null);
		while (results.next()) {
			LocalFoo foo2 = rowMapper.mapRow(results);
			assertEquals(foo1.id, foo2.id);
		}
	} finally {
		if (stmt != null) {
			stmt.close();
		}
		connectionSource.releaseConnection(conn);
	}
}
 
Example 16
Source File: OracleDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testLong() throws Exception {
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType booleanField = tableInfo.getFieldTypes()[6];
	assertEquals("longField", booleanField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, booleanField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("NUMERIC"));
}
 
Example 17
Source File: OracleDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testByte() throws Exception {
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType byteField = tableInfo.getFieldTypes()[3];
	assertEquals("byteField", byteField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, byteField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("SMALLINT"));
}
 
Example 18
Source File: MariaDbDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testBoolean() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<AllTypes, Integer> tableInfo = new TableInfo<AllTypes, Integer>(databaseType, AllTypes.class);
	assertEquals(9, tableInfo.getFieldTypes().length);
	FieldType booleanField = tableInfo.getFieldTypes()[1];
	assertEquals("booleanField", booleanField.getColumnName());
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, booleanField, additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains("TINYINT(1)"));
}
 
Example 19
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 20
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);
	}
}