com.j256.ormlite.field.DatabaseFieldConfig Java Examples

The following examples show how to use com.j256.ormlite.field.DatabaseFieldConfig. 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: JdbcBaseDaoImplTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testFieldConfig() throws Exception {
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	fieldConfigs.add(new DatabaseFieldConfig("id", "id2", DataType.UNKNOWN, null, 0, false, false, true, null,
			false, null, false, null, false, null, false, null, null, false, -1, 0));
	fieldConfigs.add(new DatabaseFieldConfig("stuff", "stuffy", DataType.UNKNOWN, null, 0, false, false, false,
			null, false, null, false, null, false, null, false, null, null, false, -1, 0));
	DatabaseTableConfig<NoAnno> tableConfig = new DatabaseTableConfig<NoAnno>(NoAnno.class, "noanno", fieldConfigs);
	Dao<NoAnno, Integer> noAnnotaionDao = createDao(tableConfig, true);
	NoAnno noa = new NoAnno();
	String stuff = "qpoqwpjoqwp12";
	noa.stuff = stuff;
	assertEquals(1, noAnnotaionDao.create(noa));
	NoAnno noa2 = noAnnotaionDao.queryForId(noa.id);
	assertEquals(noa.id, noa2.id);
	assertEquals(stuff, noa2.stuff);
}
 
Example #2
Source File: DatabaseTableConfigLoaderTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testQuickEndOfConfig() throws Exception {
	StringBuilder value = new StringBuilder();
	value.append(TABLE_START);
	value.append("dataClass=").append(Foo.class.getName()).append(LINE_SEP);
	value.append("# --table-fields-start--").append(LINE_SEP);
	value.append("# --field-start--").append(LINE_SEP);
	value.append("fieldName=xxx").append(LINE_SEP);
	value.append("# --field-end--").append(LINE_SEP);
	value.append("# --field-start--").append(LINE_SEP);
	List<DatabaseTableConfig<?>> tables =
			DatabaseTableConfigLoader.loadDatabaseConfigFromReader(new BufferedReader(new StringReader(
					value.toString())));
	assertEquals(1, tables.size());
	DatabaseTableConfig<?> config = tables.get(0);
	List<DatabaseFieldConfig> fields = config.getFieldConfigs();
	assertEquals(1, fields.size());
}
 
Example #3
Source File: DatabaseTableConfigTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testSetTableNameCase() {
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	DatabaseFieldConfig fieldId = new DatabaseFieldConfig("id");
	fieldId.setId(true);
	fieldConfigs.add(fieldId);
	fieldConfigs.add(new DatabaseFieldConfig("stuff"));

	DatabaseTableConfig<SubWithoutAnno> tableConfig = new DatabaseTableConfig<SubWithoutAnno>();
	tableConfig.setDataClass(SubWithoutAnno.class);
	String tableName = "mixEDcaSE";
	tableConfig.setTableName(tableName);
	tableConfig.setFieldConfigs(fieldConfigs);
	tableConfig.initialize();

	assertEquals(tableName, tableConfig.getTableName());
}
 
Example #4
Source File: DatabaseTableConfigTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testSetFieldConfigs() throws SQLException {
	DatabaseTableConfig<DatabaseTableAnno> dbTableConf = new DatabaseTableConfig<DatabaseTableAnno>();
	dbTableConf.setDataClass(DatabaseTableAnno.class);
	dbTableConf.setTableName(TABLE_NAME);
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	fieldConfigs.add(new DatabaseFieldConfig("stuff", null, DataType.UNKNOWN, "", 0, true, false, false, null,
			false, null, false, null, false, null, false, null, null, false,
			DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0));
	dbTableConf.setFieldConfigs(fieldConfigs);
	dbTableConf.initialize();
	assertEquals(DatabaseTableAnno.class, dbTableConf.getDataClass());
	assertEquals(TABLE_NAME, dbTableConf.getTableName());
	dbTableConf.extractFieldTypes(databaseType);
	FieldType[] fieldTypes = dbTableConf.getFieldTypes(databaseType);
	assertEquals(1, fieldTypes.length);
	assertEquals("stuff", fieldTypes[0].getColumnName());
}
 
Example #5
Source File: DatabaseTableConfigLoader.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Read all of the fields information from the configuration file.
 */
private static <T> void readFields(BufferedReader reader, DatabaseTableConfig<T> config) throws SQLException {
	List<DatabaseFieldConfig> fields = new ArrayList<DatabaseFieldConfig>();
	while (true) {
		String line;
		try {
			line = reader.readLine();
		} catch (IOException e) {
			throw SqlExceptionUtil.create("Could not read next field from config file", e);
		}
		if (line == null || line.equals(CONFIG_FILE_FIELDS_END)) {
			break;
		}
		DatabaseFieldConfig fieldConfig = DatabaseFieldConfigLoader.fromReader(reader);
		if (fieldConfig == null) {
			break;
		}
		fields.add(fieldConfig);
	}
	config.setFieldConfigs(fields);
}
 
Example #6
Source File: DatabaseTableConfigUtil.java    From ormlite-android with ISC License 6 votes vote down vote up
/**
 * Instead of calling the annotation methods directly, we peer inside the proxy and investigate the array of
 * AnnotationMember objects stored by the AnnotationFactory.
 */
private static DatabaseFieldConfig buildConfig(DatabaseField databaseField, String tableName, Field field)
		throws Exception {
	InvocationHandler proxy = Proxy.getInvocationHandler(databaseField);
	if (proxy.getClass() != annotationFactoryClazz) {
		return null;
	}
	// this should be an array of AnnotationMember objects
	Object elementsObject = elementsField.get(proxy);
	if (elementsObject == null) {
		return null;
	}
	DatabaseFieldConfig config = new DatabaseFieldConfig(field.getName());
	Object[] objs = (Object[]) elementsObject;
	for (int i = 0; i < configFieldNums.length; i++) {
		Object value = valueField.get(objs[i]);
		if (value != null) {
			assignConfigField(configFieldNums[i], config, field, value);
		}
	}
	return config;
}
 
Example #7
Source File: DatabaseTableConfigUtil.java    From ormlite-android with ISC License 6 votes vote down vote up
/**
 * 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 #8
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDaoClassDifferentDao() throws Exception {
	testClass(Baz.class);
	DatabaseTableConfig<Baz> tableConfig = new DatabaseTableConfig<Baz>(databaseType, Baz.class,
			Arrays.asList(new DatabaseFieldConfig("foo", null, DataType.UNKNOWN, null, 0, false, false, false, null,
					false, null, false, null, false, null, false, null, null, false,
					DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0)));
	testTable(tableConfig);
}
 
Example #9
Source File: OrmLiteConfigUtil.java    From ormlite-android-gradle-plugin with Apache License 2.0 5 votes vote down vote up
private static void writeConfigForTable(BufferedWriter writer, Class<?> clazz) throws SQLException, IOException {
    String tableName = DatabaseTableConfig.extractTableName(clazz);
    List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
    // walk up the classes finding the fields
    try {
        for (Class<?> working = clazz; working != null; working = working.getSuperclass()) {
            for (Field field : working.getDeclaredFields()) {
                DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, tableName, field);
                if (fieldConfig != null) {
                    fieldConfigs.add(fieldConfig);
                }
            }
        }
    } catch (Error e) {
        System.err.println("Skipping " + clazz + " because we got an error finding its definition: "
                               + e.getMessage());
        return;
    }
    if (fieldConfigs.isEmpty()) {
        System.out.println("Skipping " + clazz + " because no annotated fields found");
        return;
    }
    @SuppressWarnings({"rawtypes", "unchecked"})
    DatabaseTableConfig<?> tableConfig = new DatabaseTableConfig(clazz, tableName, fieldConfigs);
    DatabaseTableConfigLoader.write(writer, tableConfig);
    writer.append("#################################");
    writer.newLine();
    System.out.println("Wrote config for " + clazz);
}
 
Example #10
Source File: DatabaseTableConfigLoaderTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testConfigEntriesFromStream() throws Exception {
	StringBuilder value = new StringBuilder();
	value.append(TABLE_START);
	value.append("# random comment").append(LINE_SEP);
	// blank line
	value.append(LINE_SEP);
	value.append("dataClass=").append(Foo.class.getName()).append(LINE_SEP);
	String tableName = "fprwojfgopwejfw";
	value.append("tableName=").append(tableName).append(LINE_SEP);
	value.append("# --table-fields-start--").append(LINE_SEP);
	value.append("# --field-start--").append(LINE_SEP);
	String fieldName = "weopjfwefjw";
	value.append("fieldName=").append(fieldName).append(LINE_SEP);
	value.append("canBeNull=true").append(LINE_SEP);
	value.append("generatedId=true").append(LINE_SEP);
	value.append("# --field-end--").append(LINE_SEP);
	value.append("# --table-fields-end--").append(LINE_SEP);
	value.append(TABLE_END);
	List<DatabaseTableConfig<?>> tables =
			DatabaseTableConfigLoader.loadDatabaseConfigFromReader(new BufferedReader(new StringReader(
					value.toString())));
	assertEquals(1, tables.size());
	assertEquals(tableName, tables.get(0).getTableName());
	DatabaseTableConfig<?> config = tables.get(0);
	List<DatabaseFieldConfig> fields = config.getFieldConfigs();
	assertEquals(1, fields.size());
	assertEquals(fieldName, fields.get(0).getFieldName());
}
 
Example #11
Source File: DatabaseTableConfigLoaderTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testConfigFile() throws Exception {
	DatabaseTableConfig<NoFields> config = new DatabaseTableConfig<NoFields>();
	StringBuilder body = new StringBuilder();
	StringWriter writer = new StringWriter();
	BufferedWriter buffer = new BufferedWriter(writer);

	Class<NoFields> clazz = NoFields.class;
	config.setDataClass(clazz);
	body.append("dataClass=").append(clazz.getName()).append(LINE_SEP);
	checkConfigOutput(config, body, writer, buffer, false);

	String tableName = "pojgefwpjoefwpjo";
	config.setTableName(tableName);
	body.append("tableName=").append(tableName).append(LINE_SEP);
	checkConfigOutput(config, body, writer, buffer, false);

	DatabaseFieldConfig field1 = new DatabaseFieldConfig();
	String columnName = "efjpowefpjoefw";
	field1.setColumnName(columnName);
	config.setFieldConfigs(Arrays.asList(field1));
	StringWriter fieldWriter = new StringWriter();
	BufferedWriter fieldBuffer = new BufferedWriter(fieldWriter);
	DatabaseFieldConfigLoader.write(fieldBuffer, field1, tableName);
	fieldBuffer.flush();
	body.append("# --table-fields-start--").append(LINE_SEP);
	body.append(fieldWriter.toString());
	checkConfigOutput(config, body, writer, buffer, true);
}
 
Example #12
Source File: TableInfoTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testZeroFieldConfigsSpecified() throws Exception {
	DatabaseTableConfig<NoTableNameAnnotation> tableConfig = new DatabaseTableConfig<NoTableNameAnnotation>(
			databaseType, NoTableNameAnnotation.class, new ArrayList<DatabaseFieldConfig>());
	tableConfig.extractFieldTypes(databaseType);
	new TableInfo<NoTableNameAnnotation, Void>(databaseType, tableConfig);
}
 
Example #13
Source File: DatabaseTableConfigTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testBaseClassHandlingWithoutAnno() throws Exception {
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	DatabaseFieldConfig fieldId = new DatabaseFieldConfig("id");
	fieldId.setId(true);
	fieldConfigs.add(fieldId);
	fieldConfigs.add(new DatabaseFieldConfig("stuff"));

	DatabaseTableConfig<SubWithoutAnno> dbTableConf =
			new DatabaseTableConfig<SubWithoutAnno>(databaseType, SubWithoutAnno.class, fieldConfigs);
	dbTableConf.extractFieldTypes(databaseType);

	FieldType[] fieldTypes = dbTableConf.getFieldTypes(databaseType);
	assertTrue(fieldTypes.length >= 2);
	boolean seeId = false;
	boolean seeStuff = false;
	for (FieldType fieldType : fieldTypes) {
		String fieldName = fieldType.getFieldName();
		if (fieldName.equals("id")) {
			seeId = true;
		} else if (fieldType.getFieldName().equals("stuff")) {
			seeStuff = true;
		}
	}
	assertTrue(seeId);
	assertTrue(seeStuff);
}
 
Example #14
Source File: DatabaseTableConfigTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testSetNoFields() throws SQLException {
	DatabaseTableConfig<DatabaseTableAnno> dbTableConf = new DatabaseTableConfig<DatabaseTableAnno>();
	dbTableConf.setDataClass(DatabaseTableAnno.class);
	dbTableConf.setTableName(TABLE_NAME);
	dbTableConf.setFieldConfigs(new ArrayList<DatabaseFieldConfig>());
	dbTableConf.initialize();
	dbTableConf.extractFieldTypes(databaseType);
}
 
Example #15
Source File: DatabaseTableConfigTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testSetFieldConfigsNoMatchingField() throws SQLException {
	DatabaseTableConfig<DatabaseTableAnno> dbTableConf = new DatabaseTableConfig<DatabaseTableAnno>();
	dbTableConf.setDataClass(DatabaseTableAnno.class);
	dbTableConf.setTableName(TABLE_NAME);
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	fieldConfigs.add(new DatabaseFieldConfig("notstuff", null, DataType.UNKNOWN, "", 0, true, false, false, null,
			false, null, false, null, false, null, false, null, null, false, 0, 0));
	dbTableConf.setFieldConfigs(fieldConfigs);
	dbTableConf.initialize();
	assertEquals(DatabaseTableAnno.class, dbTableConf.getDataClass());
	assertEquals(TABLE_NAME, dbTableConf.getTableName());
	dbTableConf.extractFieldTypes(databaseType);
}
 
Example #16
Source File: OrmLiteConfigUtil.java    From ormlite-android with ISC License 5 votes vote down vote up
private static void writeConfigForTable(BufferedWriter writer, Class<?> clazz, boolean sortClasses)
		throws SQLException, IOException {
	String tableName = DatabaseTableConfig.extractTableName(databaseType, clazz);
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	// walk up the classes finding the fields
	try {
		for (Class<?> working = clazz; working != null; working = working.getSuperclass()) {
			for (Field field : working.getDeclaredFields()) {
				DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, tableName, field);
				if (fieldConfig != null) {
					fieldConfigs.add(fieldConfig);
				}
			}
		}
	} catch (Error e) {
		System.err.println(
				"Skipping " + clazz + " because we got an error finding its definition: " + e.getMessage());
		return;
	}
	if (fieldConfigs.isEmpty()) {
		System.out.println("Skipping " + clazz + " because no annotated fields found");
		return;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	DatabaseTableConfig<?> tableConfig = new DatabaseTableConfig(clazz, tableName, fieldConfigs);
	DatabaseTableConfigLoader.write(writer, tableConfig);
	writer.append("#################################");
	writer.newLine();
	System.out.println("Wrote config for " + clazz);
}
 
Example #17
Source File: DatabaseTableConfigTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testFieldConfigConstructorDataType() {
	DataType dataType = DataType.BIG_DECIMAL;
	DatabaseFieldConfig config = new DatabaseFieldConfig("stuff", null, dataType, null, 0, true, false, false, null,
			false, null, false, null, false, null, false, null, null, false,
			DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0);
	assertEquals(dataType, config.getDataType());
}
 
Example #18
Source File: DatabaseTableConfigTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testFieldConfigConstructor() throws SQLException {
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	fieldConfigs.add(new DatabaseFieldConfig("stuff", null, DataType.UNKNOWN, "", 0, true, false, false, null,
			false, null, false, null, false, null, false, null, null, false,
			DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0));
	DatabaseTableConfig<DatabaseTableAnno> dbTableConf =
			new DatabaseTableConfig<DatabaseTableAnno>(databaseType, DatabaseTableAnno.class, fieldConfigs);
	assertEquals(DatabaseTableAnno.class, dbTableConf.getDataClass());
	assertEquals(TABLE_NAME, dbTableConf.getTableName());
	dbTableConf.extractFieldTypes(databaseType);
	FieldType[] fieldTypes = dbTableConf.getFieldTypes(databaseType);
	assertEquals(1, fieldTypes.length);
	assertEquals("stuff", fieldTypes[0].getColumnName());
}
 
Example #19
Source File: JavaxPersistenceTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testUpperCaseFieldNames() throws Exception {
	Field[] fields = Javax.class.getDeclaredFields();
	UpperCaseFieldDatabaseType ucDatabaseType = new UpperCaseFieldDatabaseType();
	for (Field field : fields) {
		DatabaseFieldConfig config = new JavaxPersistenceImpl().createFieldConfig(ucDatabaseType, field);
		if (field.getName().equals("id")) {
			assertTrue(config.isId());
			assertFalse(config.isGeneratedId());
			assertEquals("ID", config.getFieldName());
		}
	}
}
 
Example #20
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDaoClassGenericDaoWithId() throws Exception {
	testClass(GenericBaz.class);
	DatabaseTableConfig<GenericBaz> tableConfig =
			new DatabaseTableConfig<GenericBaz>(databaseType, GenericBaz.class,
					Arrays.asList(new DatabaseFieldConfig("foo", null, DataType.UNKNOWN, null, 0, false, false,
							false, null, false, null, false, null, false, null, false, null, null, false,
							DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0)));
	testTable(tableConfig);
}
 
Example #21
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDaoClassGenericDao() throws Exception {
	testClass(GenericBar.class);
	DatabaseTableConfig<GenericBar> tableConfig =
			new DatabaseTableConfig<GenericBar>(databaseType, GenericBar.class,
					Arrays.asList(new DatabaseFieldConfig("foo", null, DataType.UNKNOWN, null, 0, false, false,
							false, null, false, null, false, null, false, null, false, null, null, false,
							DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0)));
	testTable(tableConfig);
}
 
Example #22
Source File: DatabaseTableConfigUtil.java    From ormlite-android with ISC License 5 votes vote down vote up
/**
 * Extract our configuration information from the field by looking for a {@link DatabaseField} annotation.
 */
private static DatabaseFieldConfig configFromField(DatabaseType databaseType, String tableName, Field field)
		throws SQLException {

	if (configFieldNums == null) {
		return DatabaseFieldConfig.fromField(databaseType, tableName, field);
	}

	/*
	 * This, unfortunately, we can't get around. This creates a AnnotationFactory, an array of AnnotationMember
	 * fields, and possibly another array of AnnotationMember values. This creates a lot of GC'd objects.
	 */
	DatabaseField databaseField = field.getAnnotation(DatabaseField.class);

	DatabaseFieldConfig config = null;
	try {
		if (databaseField != null) {
			config = buildConfig(databaseField, tableName, field);
		}
	} catch (Exception e) {
		// ignored so we will configure normally below
	}

	if (config == null) {
		/*
		 * We configure this the old way because we might be using javax annotations, have a ForeignCollectionField,
		 * or may still be using the deprecated annotations. At this point we know that there isn't a @DatabaseField
		 * or we can't do our reflection hacks for some reason.
		 */
		return DatabaseFieldConfig.fromField(databaseType, tableName, field);
	} else {
		workedC++;
		return config;
	}
}
 
Example #23
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDaoClassBaseDaoImpl() throws Exception {
	testClass(Bar.class);
	DatabaseTableConfig<Bar> tableConfig = new DatabaseTableConfig<Bar>(databaseType, Bar.class,
			Arrays.asList(new DatabaseFieldConfig("foo", null, DataType.UNKNOWN, null, 0, false, false, false, null,
					false, null, false, null, false, null, false, null, null, false,
					DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0)));
	testTable(tableConfig);
}
 
Example #24
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testRegisterDaoTable() throws Exception {
	DatabaseTableConfig<Bar> tableConfig = new DatabaseTableConfig<Bar>(databaseType, Bar.class,
			Arrays.asList(new DatabaseFieldConfig("foo", null, DataType.UNKNOWN, null, 0, false, false, false, null,
					false, null, false, null, false, null, false, null, null, false,
					DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0)));
	Dao<Bar, Void> dao = DaoManager.lookupDao(connectionSource, tableConfig);
	assertNull(dao);
	Dao<? extends Bar, Object> daoImpl = BaseDaoImpl.createDao(connectionSource, tableConfig);
	DaoManager.registerDaoWithTableConfig(connectionSource, daoImpl);
	dao = DaoManager.lookupDao(connectionSource, tableConfig);
	assertSame(daoImpl, dao);
}
 
Example #25
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCreateClass() throws Exception {
	testClass(Foo.class);
	DatabaseTableConfig<Foo> tableConfig = new DatabaseTableConfig<Foo>(databaseType, Foo.class,
			Arrays.asList(new DatabaseFieldConfig("id", null, DataType.UNKNOWN, null, 0, false, false, false, null,
					false, null, false, null, false, null, false, null, null, false,
					DatabaseFieldConfig.NO_MAX_FOREIGN_AUTO_REFRESH_LEVEL_SPECIFIED, 0)));
	testTable(tableConfig);
}
 
Example #26
Source File: ForeignCollectionTest.java    From ormlite-core with ISC License 5 votes vote down vote up
private Dao<Account, Integer> createLazyOrderDao() throws SQLException, Exception {
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	for (Field field : Account.class.getDeclaredFields()) {
		DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, "account", field);
		if (fieldConfig != null) {
			if (fieldConfig.isForeignCollection()) {
				fieldConfig.setForeignCollectionEager(false);
			}
			fieldConfigs.add(fieldConfig);
		}
	}
	DatabaseTableConfig<Account> tableConfig = new DatabaseTableConfig<Account>(databaseType, Account.class, fieldConfigs);
	Dao<Account, Integer> accountDao = createDao(tableConfig, true);
	return accountDao;
}
 
Example #27
Source File: DatabaseTableConfigLoader.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Write the config to the writer.
 */
private static <T> void writeConfig(BufferedWriter writer, DatabaseTableConfig<T> config) throws IOException,
		SQLException {
	writer.append(CONFIG_FILE_START_MARKER);
	writer.newLine();
	if (config.getDataClass() != null) {
		writer.append(FIELD_NAME_DATA_CLASS).append('=').append(config.getDataClass().getName());
		writer.newLine();
	}
	if (config.getSchemaName() != null) {
		writer.append(FIELD_NAME_SCHEMA_NAME).append('=').append(config.getSchemaName());
		writer.newLine();
	}
	if (config.getTableName() != null) {
		writer.append(FIELD_NAME_TABLE_NAME).append('=').append(config.getTableName());
		writer.newLine();
	}
	writer.append(CONFIG_FILE_FIELDS_START);
	writer.newLine();
	if (config.getFieldConfigs() != null) {
		for (DatabaseFieldConfig field : config.getFieldConfigs()) {
			DatabaseFieldConfigLoader.write(writer, field, config.getTableName());
		}
	}
	writer.append(CONFIG_FILE_FIELDS_END);
	writer.newLine();
	writer.append(CONFIG_FILE_END_MARKER);
	writer.newLine();
}
 
Example #28
Source File: DatabaseTableConfig.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Setup a table config associated with the dataClass, table-name, and field configurations.
 */
public DatabaseTableConfig(Class<T> dataClass, String schemaName, String tableName, List<DatabaseFieldConfig> fieldConfigs) {
	this.dataClass = dataClass;
	this.schemaName = schemaName;
	this.tableName = tableName;
	this.fieldConfigs = fieldConfigs;
}
 
Example #29
Source File: DaoManagerTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test
public void testLookupTableDaoUnknown() {
	assertNull(DaoManager.lookupDao(connectionSource, new DatabaseTableConfig<DaoManagerTest>(databaseType,
			DaoManagerTest.class, new ArrayList<DatabaseFieldConfig>())));
}
 
Example #30
Source File: DatabaseTableConfig.java    From ormlite-core with ISC License 4 votes vote down vote up
public List<DatabaseFieldConfig> getFieldConfigs() {
	return fieldConfigs;
}