Java Code Examples for com.j256.ormlite.field.FieldType#createFieldType()

The following examples show how to use com.j256.ormlite.field.FieldType#createFieldType() . 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: HsqldbDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testBadGeneratedId() throws Exception {
	Field field = GeneratedId.class.getField("id");
	DatabaseType databaseType = createMock(DatabaseType.class);
	expect(databaseType.isIdSequenceNeeded()).andReturn(false);
	DataPersister dataPersister = createMock(DataPersister.class);
	expect(databaseType.getDataPersister(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
	expect(databaseType.getFieldConverter(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
	expect(databaseType.isEntityNamesMustBeUpCase()).andReturn(true);
	expect(databaseType.upCaseEntityName("id")).andReturn("ID");
	replay(databaseType);
	connectionSource.setDatabaseType(databaseType);
	try {
		FieldType fieldType = FieldType.createFieldType(databaseType, "foo", field, GeneratedId.class);
		verify(databaseType);
		StringBuilder sb = new StringBuilder();
		List<String> statementsBefore = new ArrayList<String>();
		databaseType.appendColumnArg(null, sb, fieldType, null, statementsBefore, null, null);
	} finally {
		connectionSource.setDatabaseType(databaseType);
	}
}
 
Example 2
Source File: SerializableTypeTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testSerializableNoValue() throws Exception {
	Class<LocalSerializable> clazz = LocalSerializable.class;
	Dao<LocalSerializable, Object> dao = createDao(clazz, true);
	LocalSerializable foo = new LocalSerializable();
	foo.serializable = null;
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME);
	CompiledStatement stmt = null;
	try {
		stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
				DatabaseConnection.DEFAULT_RESULT_FLAGS, true);
		DatabaseResults results = stmt.runQuery(null);
		assertTrue(results.next());
		FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME,
				clazz.getDeclaredField(SERIALIZABLE_COLUMN), clazz);
		assertNull(DataType.SERIALIZABLE.getDataPersister().resultToJava(fieldType, results,
				results.findColumn(SERIALIZABLE_COLUMN)));
	} finally {
		if (stmt != null) {
			stmt.close();
		}
		connectionSource.releaseConnection(conn);
	}
}
 
Example 3
Source File: BaseCoreStmtTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Override
@Before
public void before() throws Exception {
	super.before();

	Field field = Foo.class.getDeclaredField("stringField");
	assertEquals(String.class, field.getType());
	stringFieldType = FieldType.createFieldType(databaseType, "BaseFoo", field, Foo.class);
	stringFieldType.configDaoInformation(connectionSource, Foo.class);
	field = Foo.class.getDeclaredField("val");
	assertEquals(int.class, field.getType());
	numberFieldType = FieldType.createFieldType(databaseType, "BaseFoo", field, Foo.class);
	numberFieldType.configDaoInformation(connectionSource, Foo.class);
	field = Foreign.class.getDeclaredField("foo");
	assertEquals(Foo.class, field.getType());
	foreignFieldType = FieldType.createFieldType(databaseType, "BaseFoo", field, Foreign.class);
	foreignFieldType.configDaoInformation(connectionSource, Foreign.class);

	baseFooTableInfo = new TableInfo<Foo, Integer>(databaseType, Foo.class);
	baseSchemaFooTableInfo = new TableInfo<SchemaFoo, Integer>(databaseType, SchemaFoo.class);
}
 
Example 4
Source File: OracleDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testBadGeneratedId() throws Exception {
	Field field = GeneratedId.class.getField("id");
	DatabaseType mockDb = createMock(DatabaseType.class);
	expect(mockDb.isIdSequenceNeeded()).andReturn(false);
	DataPersister dataPersister = createMock(DataPersister.class);
	expect(mockDb.getDataPersister(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
	expect(mockDb.getFieldConverter(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
	expect(mockDb.isEntityNamesMustBeUpCase()).andReturn(false);
	replay(mockDb);
	FieldType fieldType = FieldType.createFieldType(mockDb, "foo", field, GeneratedId.class);
	verify(mockDb);
	StringBuilder sb = new StringBuilder();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, fieldType, null, statementsBefore, null, null);
}
 
Example 5
Source File: BaseMappedQueryTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testMappedQuery() throws Exception {
	Field field = Foo.class.getDeclaredField(Foo.ID_COLUMN_NAME);
	String tableName = "basefoo";
	Dao<Foo, Integer> dao = createDao(Foo.class, false);
	FieldType[] resultFieldTypes =
			new FieldType[] { FieldType.createFieldType(databaseType, tableName, field, Foo.class) };
	BaseMappedQuery<Foo, Integer> baseMappedQuery = new BaseMappedQuery<Foo, Integer>(dao, baseFooTableInfo,
			"select * from " + tableName, new FieldType[0], resultFieldTypes) {
	};
	DatabaseResults results = createMock(DatabaseResults.class);
	int colN = 1;
	expect(results.getObjectCacheForRetrieve()).andReturn(null);
	expect(results.getObjectCacheForStore()).andReturn(null);
	expect(results.findColumn(Foo.ID_COLUMN_NAME)).andReturn(colN);
	int id = 63365;
	expect(results.getInt(colN)).andReturn(id);
	replay(results);
	Foo baseFoo = baseMappedQuery.mapRow(results);
	assertNotNull(baseFoo);
	assertEquals(id, baseFoo.id);
	verify(results);
}
 
Example 6
Source File: WhereTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testToString() throws Exception {
	Where<Foo, String> where = new Where<Foo, String>(createTableInfo(), null, databaseType);
	assertTrue(where.toString().contains("empty where clause"));
	String value = "bar";
	FieldType numberFieldType = FieldType.createFieldType(databaseType, "foo",
			Foo.class.getDeclaredField(Foo.VAL_COLUMN_NAME), Foo.class);
	SimpleComparison eq =
			new SimpleComparison(Foo.VAL_COLUMN_NAME, numberFieldType, value, SimpleComparison.EQUAL_TO_OPERATION);
	where.eq(Foo.VAL_COLUMN_NAME, value);
	assertTrue(where.toString().contains(eq.toString()));
}
 
Example 7
Source File: BaseCoreDatabaseTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testBooleanConverterResultToJava() throws Exception {
	DatabaseResults results = createMock(DatabaseResults.class);
	boolean first = Boolean.TRUE;
	boolean second = Boolean.FALSE;
	expect(results.getByte(1)).andReturn((byte) 1);
	expect(results.getByte(2)).andReturn((byte) 0);
	replay(results);
	FieldType fieldType = FieldType.createFieldType(databaseType, "foo", ManyFields.class.getDeclaredField("bool"),
			ManyFields.class);
	assertEquals(first, booleanFieldConverter.resultToJava(fieldType, results, 1));
	assertEquals(second, booleanFieldConverter.resultToJava(fieldType, results, 2));
	verify(results);
}
 
Example 8
Source File: BaseMappedQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testMappedQueryCached() throws Exception {
	Field field = Foo.class.getDeclaredField(Foo.ID_COLUMN_NAME);
	String tableName = "basefoo";
	FieldType[] resultFieldTypes =
			new FieldType[] { FieldType.createFieldType(databaseType, tableName, field, Foo.class) };
	Dao<Foo, Integer> dao = createDao(Foo.class, false);
	BaseMappedQuery<Foo, Integer> baseMappedQuery = new BaseMappedQuery<Foo, Integer>(dao, baseFooTableInfo,
			"select * from " + tableName, new FieldType[0], resultFieldTypes) {
	};
	DatabaseResults results = createMock(DatabaseResults.class);
	int colN = 1;
	ObjectCache objectCache = createMock(ObjectCache.class);
	expect(results.getObjectCacheForRetrieve()).andReturn(objectCache);
	int id = 63365;
	expect(results.getInt(colN)).andReturn(id);
	expect(objectCache.get(Foo.class, id)).andReturn(null);
	objectCache.put(eq(Foo.class), eq(id), isA(Foo.class));
	expect(results.getObjectCacheForStore()).andReturn(objectCache);
	expect(results.findColumn(Foo.ID_COLUMN_NAME)).andReturn(colN);
	expect(results.getInt(colN)).andReturn(id);

	replay(results, objectCache);
	Foo baseFoo = baseMappedQuery.mapRow(results);
	assertNotNull(baseFoo);
	assertEquals(id, baseFoo.id);
	verify(results, objectCache);
}
 
Example 9
Source File: PostgresDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testDropSequence() throws Exception {
	if (connectionSource == null) {
		return;
	}
	Field field = GeneratedId.class.getField("id");
	FieldType fieldType = FieldType.createFieldType(databaseType, "foo", field, GeneratedId.class);
	List<String> statementsBefore = new ArrayList<String>();
	List<String> statementsAfter = new ArrayList<String>();
	databaseType.dropColumnArg(fieldType, statementsBefore, statementsAfter);
	assertEquals(0, statementsBefore.size());
	assertEquals(1, statementsAfter.size());
	assertTrue(statementsAfter.get(0).contains("DROP SEQUENCE "));
}
 
Example 10
Source File: BaseSqliteDatabaseTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testConfigureGeneratedIdNotInteger() throws Exception {
	Field field = Foo.class.getField("stringField");
	FieldType fieldType = FieldType.createFieldType(databaseType, "foo", field, Foo.class);
	OurSqliteDatabaseType dbType = new OurSqliteDatabaseType();
	StringBuilder sb = new StringBuilder();
	dbType.configureGeneratedId(null, sb, fieldType, new ArrayList<String>(), null, new ArrayList<String>(),
			new ArrayList<String>());
}
 
Example 11
Source File: BaseCoreDatabaseTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
private void testFooColumn(DatabaseType databaseType, String fieldName, String expected) throws Exception {
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> stmtsBefore = new ArrayList<String>();
	List<String> stmtsAfter = new ArrayList<String>();
	List<String> queriesAfter = new ArrayList<String>();
	FieldType fieldType = FieldType.createFieldType(databaseType, "foo",
			ManyFields.class.getDeclaredField(fieldName), ManyFields.class);
	databaseType.appendColumnArg(null, sb, fieldType, additionalArgs, stmtsBefore, stmtsAfter, queriesAfter);
	StringBuilder expectedSb = new StringBuilder();
	databaseType.appendEscapedEntityName(expectedSb, fieldName);
	expectedSb.append(' ').append(expected).append(' ');
	assertEquals(expectedSb.toString(), sb.toString());
}
 
Example 12
Source File: OracleDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testDropSequence() throws Exception {
	Field field = GeneratedId.class.getField("id");
	FieldType fieldType = FieldType.createFieldType(databaseType, "foo", field, GeneratedId.class);
	List<String> statementsBefore = new ArrayList<String>();
	List<String> statementsAfter = new ArrayList<String>();
	databaseType.dropColumnArg(fieldType, statementsBefore, statementsAfter);
	assertEquals(0, statementsBefore.size());
	assertEquals(1, statementsAfter.size());
	assertTrue(statementsAfter.get(0).contains("DROP SEQUENCE "));
}
 
Example 13
Source File: SqlDateTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test(expected = SQLException.class)
public void testSqlDateParseInvalid() throws Exception {
	FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME,
			LocalDate.class.getDeclaredField(DATE_COLUMN), LocalDate.class);
	dataType.getDataPersister().parseDefaultString(fieldType, "not valid date string");
}
 
Example 14
Source File: BaseTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
protected <T, ID> void testType(Dao<T, ID> dao, T foo, Class<T> clazz, Object javaVal, Object defaultSqlVal,
		Object sqlArg, String defaultValStr, DataType dataType, String columnName, boolean isValidGeneratedType,
		boolean isAppropriateId, boolean isEscapedValue, boolean isPrimitive, boolean isSelectArgRequired,
		boolean isStreamType, boolean isComparable, boolean isConvertableId) throws Exception {
	DataPersister dataPersister = dataType.getDataPersister();
	DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME);
	CompiledStatement stmt = null;
	if (sqlArg != null) {
		assertEquals(defaultSqlVal.getClass(), sqlArg.getClass());
	}
	try {
		stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
				DatabaseConnection.DEFAULT_RESULT_FLAGS, true);
		DatabaseResults results = stmt.runQuery(null);
		assertTrue(results.next());
		int colNum = results.findColumn(columnName);
		Field field = clazz.getDeclaredField(columnName);
		FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME, field, clazz);
		assertEquals(dataType.getDataPersister(), fieldType.getDataPersister());
		Class<?>[] classes = fieldType.getDataPersister().getAssociatedClasses();
		if (classes.length > 0) {
			assertTrue(classes[0].isAssignableFrom(fieldType.getType()));
		}
		assertTrue(fieldType.getDataPersister().isValidForField(field));
		if (javaVal instanceof byte[]) {
			assertTrue(Arrays.equals((byte[]) javaVal,
					(byte[]) dataPersister.resultToJava(fieldType, results, colNum)));
		} else {
			Map<String, Integer> colMap = new HashMap<String, Integer>();
			colMap.put(columnName, colNum);
			Object result = fieldType.resultToJava(results, colMap);
			assertEquals(javaVal, result);
		}
		if (dataType == DataType.SERIALIZABLE) {
			try {
				dataPersister.parseDefaultString(fieldType, "");
				fail("parseDefaultString should have thrown for " + dataType);
			} catch (SQLException e) {
				// expected
			}
		} else if (defaultValStr != null) {
			Object parsedDefault = dataPersister.parseDefaultString(fieldType, defaultValStr);
			assertEquals(defaultSqlVal.getClass(), parsedDefault.getClass());
			if (dataType == DataType.BYTE_ARRAY || dataType == DataType.STRING_BYTES) {
				assertTrue(Arrays.equals((byte[]) defaultSqlVal, (byte[]) parsedDefault));
			} else {
				assertEquals(defaultSqlVal, parsedDefault);
			}
		}
		if (sqlArg == null) {
			// noop
		} else if (sqlArg instanceof byte[]) {
			assertTrue(Arrays.equals((byte[]) sqlArg, (byte[]) dataPersister.javaToSqlArg(fieldType, javaVal)));
		} else {
			assertEquals(sqlArg, dataPersister.javaToSqlArg(fieldType, javaVal));
		}
		assertEquals(isValidGeneratedType, dataPersister.isValidGeneratedType());
		assertEquals(isAppropriateId, dataPersister.isAppropriateId());
		assertEquals(isEscapedValue, dataPersister.isEscapedValue());
		assertEquals(isEscapedValue, dataPersister.isEscapedDefaultValue());
		assertEquals(isPrimitive, dataPersister.isPrimitive());
		assertEquals(isSelectArgRequired, dataPersister.isArgumentHolderRequired());
		assertEquals(isStreamType, dataPersister.isStreamType());
		assertEquals(isComparable, dataPersister.isComparable());
		if (isConvertableId) {
			assertNotNull(dataPersister.convertIdNumber(10));
		} else {
			assertNull(dataPersister.convertIdNumber(10));
		}
		List<T> list = dao.queryForAll();
		assertEquals(1, list.size());
		assertTrue(dao.objectsEqual(foo, list.get(0)));
		// if we have a value then look for it, floats don't find any results because of rounding issues
		if (javaVal != null && dataPersister.isComparable() && dataType != DataType.FLOAT
				&& dataType != DataType.FLOAT_OBJ) {
			// test for inline arguments
			list = dao.queryForMatching(foo);
			assertEquals(1, list.size());
			assertTrue(dao.objectsEqual(foo, list.get(0)));
			// test for SelectArg arguments
			list = dao.queryForMatchingArgs(foo);
			assertEquals(1, list.size());
			assertTrue(dao.objectsEqual(foo, list.get(0)));
		}
		if (dataType == DataType.STRING_BYTES || dataType == DataType.BYTE_ARRAY
				|| dataType == DataType.SERIALIZABLE) {
			// no converting from string to value
		} else {
			// test string conversion
			String stringVal = results.getString(colNum);
			Object convertedJavaVal = fieldType.convertStringToJavaField(stringVal, 0);
			assertEquals(javaVal, convertedJavaVal);
		}
	} finally {
		if (stmt != null) {
			stmt.close();
		}
		connectionSource.releaseConnection(conn);
	}
}
 
Example 15
Source File: DateLongTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test(expected = SQLException.class)
public void testDateLongParseInvalid() throws Exception {
	FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME,
			LocalDateLong.class.getDeclaredField(DATE_COLUMN), LocalDateLong.class);
	DataType.DATE_LONG.getDataPersister().parseDefaultString(fieldType, "not valid long number");
}
 
Example 16
Source File: DateIntegerTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test(expected = SQLException.class)
public void testDateIntegerParseInvalid() throws Exception {
	FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME,
			LocalDateInteger.class.getDeclaredField(DATE_COLUMN), LocalDateInteger.class);
	DataType.DATE_INTEGER.getDataPersister().parseDefaultString(fieldType, "not valid int number");
}
 
Example 17
Source File: DateTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test(expected = SQLException.class)
public void testDateParseInvalid() throws Exception {
	FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME,
			LocalDate.class.getDeclaredField(DATE_COLUMN), LocalDate.class);
	DataType.DATE.getDataPersister().parseDefaultString(fieldType, "not valid date string");
}
 
Example 18
Source File: DateTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testInvalidDateField() throws Exception {
	FieldType.createFieldType(databaseType, TABLE_NAME, InvalidDate.class.getDeclaredField("notDate"),
			LocalDate.class);
}
 
Example 19
Source File: DateStringTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test(expected = SQLException.class)
public void testDateStringParseInvalid() throws Exception {
	FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME,
			LocalDateString.class.getDeclaredField(DATE_COLUMN), LocalDateString.class);
	DataType.DATE_STRING.getDataPersister().parseDefaultString(fieldType, "not valid date string");
}
 
Example 20
Source File: TimeStampTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test(expected = SQLException.class)
public void testTimeStampParseInvalid() throws Exception {
	FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME,
			LocalTimeStamp.class.getDeclaredField(TIME_STAMP_COLUMN), LocalTimeStamp.class);
	dataType.getDataPersister().parseDefaultString(fieldType, "not valid date string");
}