com.j256.ormlite.table.DatabaseTableConfig Java Examples

The following examples show how to use com.j256.ormlite.table.DatabaseTableConfig. 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: BaseCoreTest.java    From ormlite-core with ISC License 6 votes vote down vote up
private <T, ID> Dao<T, ID> configDao(BaseDaoImpl<T, ID> dao, boolean createTable) throws SQLException {
	if (connectionSource == null) {
		throw new SQLException("Connection source is null");
	}
	if (createTable) {
		DatabaseTableConfig<T> tableConfig = dao.getTableConfig();
		if (tableConfig == null) {
			tableConfig = DatabaseTableConfig.fromClass(databaseType, dao.getDataClass());
		}
		if (tableConfig.getSchemaName() != null && tableConfig.getSchemaName().length() > 0){
			createSchema(tableConfig);
		}
		createTable(tableConfig, true);
	}
	return dao;
}
 
Example #2
Source File: ModuleDatabase.java    From AndroidStarter with Apache License 2.0 6 votes vote down vote up
@Provides
@Singleton
public DAORepo provideDAORepo(@NonNull final DatabaseHelperAndroidStarter poDatabaseHelperAndroidStarter) {
    try {
        final ConnectionSource loConnectionSource = poDatabaseHelperAndroidStarter.getConnectionSource();
        final DatabaseTableConfig<RepoEntity> loTableConfig = DatabaseTableConfigUtil.fromClass(loConnectionSource, RepoEntity.class);
        if (loTableConfig != null) {
            return new DAORepo(loConnectionSource, loTableConfig);
        } else {
            return new DAORepo(loConnectionSource);
        }
    } catch (final SQLException loException) {
        if (BuildConfig.DEBUG && DEBUG) {
            Logger.t(TAG).e(loException, "");
        }
    }
    return null;
}
 
Example #3
Source File: OrmLiteProvider.java    From android-atleap with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected String getIdFieldName(SQLiteMatcherEntry entry) {
    OrmLiteMatcherEntry ormliteEntry = (OrmLiteMatcherEntry)entry;
    if (ormliteEntry.getClazz() == null) {
        throw new IllegalStateException("In order to use ITEM type you should fill in class");
    }

    try {
        DatabaseTableConfig config = getDatabaseTableConfig(ormliteEntry.getClazz());
        FieldType[] fieldTypes = config.getFieldTypes(mDatabaseHelper.getConnectionSource().getDatabaseType());
        FieldType idFieldType = null;
        for (FieldType fieldType : fieldTypes) {
            if(fieldType.isId() || fieldType.isGeneratedId() || fieldType.isGeneratedIdSequence()) {
                idFieldType = fieldType;
            }
        }
        if (idFieldType == null) {
            throw new IllegalStateException("Cannot find id field");
        }
        return idFieldType.getColumnName();
    } catch (SQLException ex) {
        Log.e(TAG, "Cannot get id field", ex);
        throw new IllegalStateException("Cannot find id field");
    }
}
 
Example #4
Source File: DaoManager.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Helper method to lookup a DAO if it has already been associated with the table-config. Otherwise this returns
 * null.
 */
public synchronized static <D extends Dao<T, ?>, T> D lookupDao(ConnectionSource connectionSource,
		DatabaseTableConfig<T> tableConfig) {
	if (connectionSource == null) {
		throw new IllegalArgumentException("connectionSource argument cannot be null");
	}
	TableConfigConnectionSource key = new TableConfigConnectionSource(connectionSource, tableConfig);
	Dao<?, ?> dao = lookupDao(key);
	if (dao == null) {
		return null;
	} else {
		@SuppressWarnings("unchecked")
		D castDao = (D) dao;
		return castDao;
	}
}
 
Example #5
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 #6
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 #7
Source File: DaoManager.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Creates the DAO if we have config information cached and caches the DAO.
 */
private static <D, T> D createDaoFromConfig(ConnectionSource connectionSource, Class<T> clazz) throws SQLException {
	// no loaded configs
	if (configMap == null) {
		return null;
	}

	@SuppressWarnings("unchecked")
	DatabaseTableConfig<T> config = (DatabaseTableConfig<T>) configMap.get(clazz);
	// if we don't config information cached return null
	if (config == null) {
		return null;
	}

	// else create a DAO using configuration
	Dao<T, ?> configedDao = doCreateDao(connectionSource, config);
	@SuppressWarnings("unchecked")
	D castDao = (D) configedDao;
	return castDao;
}
 
Example #8
Source File: DaoManagerTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testSelfReferenceWithLoadedConfig() throws Exception {
	DaoManager.clearCache();
	/*
	 * If a class was loaded as a config (this was found under Android) then, when it went recursive it would build
	 * itself and set its foreign field to be a primitive. Then when it re-configured itself it would scream because
	 * the primitive was marked as foreign.
	 * 
	 * The answer was to do a better job of pre-caching the DAOs in the DaoManager.
	 */
	DatabaseTableConfig<SelfReference> config = DatabaseTableConfig.fromClass(databaseType, SelfReference.class);
	@SuppressWarnings("unchecked")
	List<DatabaseTableConfig<?>> configs = new ArrayList<DatabaseTableConfig<?>>(Arrays.asList(config));
	DaoManager.addCachedDatabaseConfigs(configs);
	// this used to throw an exception
	DaoManager.createDao(connectionSource, SelfReference.class);
}
 
Example #9
Source File: DaoManagerTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testMoreComplexClassLoopWithLoadedConfig() throws Exception {
	DaoManager.clearCache();
	DatabaseTableConfig<MoreComplexLoopOne> config1 =
			DatabaseTableConfig.fromClass(databaseType, MoreComplexLoopOne.class);
	DatabaseTableConfig<MoreComplexLoopTwo> config2 =
			DatabaseTableConfig.fromClass(databaseType, MoreComplexLoopTwo.class);
	DatabaseTableConfig<MoreComplexLoopThree> config3 =
			DatabaseTableConfig.fromClass(databaseType, MoreComplexLoopThree.class);
	DatabaseTableConfig<MoreComplexLoopFour> config4 =
			DatabaseTableConfig.fromClass(databaseType, MoreComplexLoopFour.class);
	@SuppressWarnings("unchecked")
	List<DatabaseTableConfig<?>> configs =
			new ArrayList<DatabaseTableConfig<?>>(Arrays.asList(config1, config2, config3, config4));
	DaoManager.addCachedDatabaseConfigs(configs);
	assertNotNull(DaoManager.createDao(connectionSource, MoreComplexLoopOne.class));
	assertNotNull(DaoManager.createDao(connectionSource, MoreComplexLoopTwo.class));
	assertNotNull(DaoManager.createDao(connectionSource, MoreComplexLoopThree.class));
	assertNotNull(DaoManager.createDao(connectionSource, MoreComplexLoopFour.class));
}
 
Example #10
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 #11
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 #12
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 #13
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 #14
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 #15
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testClassLoopWithLoadedConfig() throws Exception {
	DaoManager.clearCache();
	DatabaseTableConfig<LoopOne> config1 = DatabaseTableConfig.fromClass(databaseType, LoopOne.class);
	DatabaseTableConfig<LoopTwo> config2 = DatabaseTableConfig.fromClass(databaseType, LoopTwo.class);
	@SuppressWarnings("unchecked")
	List<DatabaseTableConfig<?>> configs = new ArrayList<DatabaseTableConfig<?>>(Arrays.asList(config1, config2));
	DaoManager.addCachedDatabaseConfigs(configs);
	assertNotNull(DaoManager.createDao(connectionSource, LoopOne.class));
	assertNotNull(DaoManager.createDao(connectionSource, LoopTwo.class));
}
 
Example #16
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
private <T> void testTable(DatabaseTableConfig<T> config) throws Exception {
	Dao<T, Void> dao1 = DaoManager.createDao(connectionSource, config);
	Dao<T, Void> dao2 = DaoManager.createDao(connectionSource, config);
	Dao<T, Void> dao3 = DaoManager.lookupDao(connectionSource, config);
	assertSame(dao1, dao2);
	assertSame(dao1, dao3);
	DaoManager.clearCache();
	Dao<T, ?> dao4 = DaoManager.createDao(connectionSource, config);
	assertNotSame(dao4, dao2);
}
 
Example #17
Source File: BaseCoreTest.java    From ormlite-core with ISC License 5 votes vote down vote up
protected <T, ID> Dao<T, ID> createDao(DatabaseTableConfig<T> tableConfig, boolean createTable)
		throws SQLException {
	if (connectionSource == null) {
		throw new SQLException("Connection source is null");
	}
	@SuppressWarnings("unchecked")
	BaseDaoImpl<T, ID> dao = (BaseDaoImpl<T, ID>) DaoManager.createDao(connectionSource, tableConfig);
	return configDao(dao, createTable);
}
 
Example #18
Source File: BaseCoreTest.java    From ormlite-core with ISC License 5 votes vote down vote up
protected <T> void createTable(DatabaseTableConfig<T> tableConfig, boolean dropAtEnd) throws SQLException {
	try {
		// first we drop it in case it existed before
		dropTable(tableConfig, true);
	} catch (SQLException ignored) {
		// ignore any errors about missing tables
	}
	TableUtils.createTable(connectionSource, tableConfig);
}
 
Example #19
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 #20
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testSetters() throws Exception {
	DatabaseTableConfig<Foo> config = DatabaseTableConfig.fromClass(databaseType, Foo.class);
	BaseDaoImpl<Foo, Integer> dao = new BaseDaoImpl<Foo, Integer>(Foo.class) {
	};
	dao.setTableConfig(config);
	dao.setConnectionSource(connectionSource);
	assertSame(config, dao.getTableConfig());
}
 
Example #21
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testTableConfig() throws Exception {
	DatabaseTableConfig<Foo> config = DatabaseTableConfig.fromClass(databaseType, Foo.class);
	BaseDaoImpl<Foo, Integer> dao = new BaseDaoImpl<Foo, Integer>(connectionSource, config) {
	};
	assertSame(config, dao.getTableConfig());
}
 
Example #22
Source File: RuntimeExceptionDaoTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCreateDaoTableConfig() throws Exception {
	createDao(Foo.class, true);
	RuntimeExceptionDao<Foo, String> dao =
			RuntimeExceptionDao.createDao(connectionSource, DatabaseTableConfig.fromClass(databaseType, Foo.class));
	assertEquals(0, dao.countOf());
}
 
Example #23
Source File: BaseDatabaseType.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * @throws SQLException
 *             for sub classes.
 */
@Override
public <T> DatabaseTableConfig<T> extractDatabaseTableConfig(ConnectionSource connectionSource, Class<T> clazz)
		throws SQLException {
	// default is no default extractor
	return null;
}
 
Example #24
Source File: DatabaseFieldConfig.java    From ormlite-core with ISC License 5 votes vote down vote up
public DatabaseFieldConfig(String fieldName, String columnName, DataType dataType, String defaultValue, int width,
		boolean canBeNull, boolean id, boolean generatedId, String generatedIdSequence, boolean foreign,
		DatabaseTableConfig<?> foreignTableConfig, boolean useGetSet, Enum<?> unknownEnumValue, boolean throwIfNull,
		String format, boolean unique, String indexName, String uniqueIndexName, boolean autoRefresh,
		int maxForeignAutoRefreshLevel, int maxForeignCollectionLevel) {
	this.fieldName = fieldName;
	this.columnName = columnName;
	this.dataType = dataType;
	this.defaultValue = defaultValue;
	this.width = width;
	this.canBeNull = canBeNull;
	this.id = id;
	this.generatedId = generatedId;
	this.generatedIdSequence = generatedIdSequence;
	this.foreign = foreign;
	this.foreignTableConfig = foreignTableConfig;
	this.useGetSet = useGetSet;
	this.unknownEnumValue = unknownEnumValue;
	this.throwIfNull = throwIfNull;
	this.format = format;
	this.unique = unique;
	this.indexName = indexName;
	this.uniqueIndexName = uniqueIndexName;
	this.foreignAutoRefresh = autoRefresh;
	this.maxForeignAutoRefreshLevel = maxForeignAutoRefreshLevel;
	this.foreignCollectionMaxEagerLevel = maxForeignCollectionLevel;
}
 
Example #25
Source File: DaoManager.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * This adds database table configurations to the internal cache which can be used to speed up DAO construction.
 * This is especially true of Android and other mobile platforms.
 */
public static synchronized void addCachedDatabaseConfigs(Collection<DatabaseTableConfig<?>> configs) {
	Map<Class<?>, DatabaseTableConfig<?>> newMap;
	if (configMap == null) {
		newMap = new HashMap<Class<?>, DatabaseTableConfig<?>>();
	} else {
		newMap = new HashMap<Class<?>, DatabaseTableConfig<?>>(configMap);
	}
	for (DatabaseTableConfig<?> config : configs) {
		newMap.put(config.getDataClass(), config);
		logger.info("Loaded configuration for {}", config.getDataClass());
	}
	configMap = newMap;
}
 
Example #26
Source File: DaoManager.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Same as {@link #registerDao(ConnectionSource, Dao)} but this allows you to register it just with its
 * {@link DatabaseTableConfig}. This allows multiple versions of the DAO to be configured if necessary.
 */
public static synchronized void registerDaoWithTableConfig(ConnectionSource connectionSource, Dao<?, ?> dao) {
	if (connectionSource == null) {
		throw new IllegalArgumentException("connectionSource argument cannot be null");
	}
	if (dao instanceof BaseDaoImpl) {
		DatabaseTableConfig<?> tableConfig = ((BaseDaoImpl<?, ?>) dao).getTableConfig();
		if (tableConfig != null) {
			addDaoToTableMap(new TableConfigConnectionSource(connectionSource, tableConfig), dao);
			return;
		}
	}
	addDaoToClassMap(new ClassConnectionSource(connectionSource, dao.getDataClass()), dao);
}
 
Example #27
Source File: DaoManager.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Helper method to create a DAO object without having to define a class. This checks to see if the DAO has already
 * been created. If not then it is a call through to
 * {@link BaseDaoImpl#createDao(ConnectionSource, DatabaseTableConfig)}.
 */
public synchronized static <D extends Dao<T, ?>, T> D createDao(ConnectionSource connectionSource,
		DatabaseTableConfig<T> tableConfig) throws SQLException {
	if (connectionSource == null) {
		throw new IllegalArgumentException("connectionSource argument cannot be null");
	}
	return doCreateDao(connectionSource, tableConfig);
}
 
Example #28
Source File: RuntimeExceptionDao.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Call through to {@link DaoManager#createDao(ConnectionSource, DatabaseTableConfig)} with the returned DAO wrapped
 * in a RuntimeExceptionDao.
 */
public static <T, ID> RuntimeExceptionDao<T, ID> createDao(ConnectionSource connectionSource,
		DatabaseTableConfig<T> tableConfig) throws SQLException {
	@SuppressWarnings("unchecked")
	Dao<T, ID> castDao = (Dao<T, ID>) DaoManager.createDao(connectionSource, tableConfig);
	return new RuntimeExceptionDao<T, ID>(castDao);
}
 
Example #29
Source File: OrmLiteProvider.java    From android-atleap with Apache License 2.0 5 votes vote down vote up
protected DatabaseTableConfig getDatabaseTableConfig(Class clazz) {
    DatabaseTableConfig config = mTableConfigs.get(clazz);
    if (config == null) {
        try {
            config = DatabaseTableConfigUtil.fromClass(mDatabaseHelper.getConnectionSource(), clazz);
            config.extractFieldTypes(mDatabaseHelper.getConnectionSource());
        } catch (SQLException ex) {
            Log.e(TAG, "Cannot get table config", ex);
        }
        if (config != null) {
            mTableConfigs.put(clazz, config);
        }
    }
    return config;
}
 
Example #30
Source File: BaseDaoImpl.java    From ormlite-core with ISC License 5 votes vote down vote up
private BaseDaoImpl(ConnectionSource connectionSource, Class<T> dataClass, DatabaseTableConfig<T> tableConfig)
		throws SQLException {
	this.dataClass = dataClass;
	this.tableConfig = tableConfig;
	this.constructor = findNoArgConstructor(dataClass);
	if (connectionSource != null) {
		this.connectionSource = connectionSource;
		initialize();
	}
}