Java Code Examples for com.j256.ormlite.dao.DaoManager#createDao()

The following examples show how to use com.j256.ormlite.dao.DaoManager#createDao() . 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: ManyToManyMain.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
/**
 * Setup our database and DAOs
 */
private void setupDatabase(ConnectionSource connectionSource) throws Exception {

	/**
	 * Create our DAOs. One for each class and associated table.
	 */
	userDao = DaoManager.createDao(connectionSource, User.class);
	postDao = DaoManager.createDao(connectionSource, Post.class);
	userPostDao = DaoManager.createDao(connectionSource, UserPost.class);

	/**
	 * Create the tables for our example. This would not be necessary if the tables already existed.
	 */
	TableUtils.createTable(connectionSource, User.class);
	TableUtils.createTable(connectionSource, Post.class);
	TableUtils.createTable(connectionSource, UserPost.class);
}
 
Example 2
Source File: ORMLiteIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@BeforeClass
public static void setup() throws SQLException {
    connectionSource = new JdbcPooledConnectionSource("jdbc:h2:mem:myDb");
    TableUtils.createTableIfNotExists(connectionSource, Library.class);
    TableUtils.createTableIfNotExists(connectionSource, Address.class);
    TableUtils.createTableIfNotExists(connectionSource, Book.class);

    libraryDao = DaoManager.createDao(connectionSource, Library.class);

    bookDao = DaoManager.createDao(connectionSource, Book.class);
}
 
Example 3
Source File: TableFactory.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void createTable(Class tableClass, ConnectionSource connectionSource) throws Throwable {
	int result = 0;
	String logTableName = this.getDatabaseName() + "/" + getTableName(tableClass);
	try {
		result = ApsTableUtils.createTable(connectionSource, tableClass);
		if (result > 0) {
			_logger.info("Created table - {}", logTableName);
			Object tableModel = tableClass.newInstance();
			if (tableModel instanceof ExtendedColumnDefinition) {
				String[] extensions = ((ExtendedColumnDefinition) tableModel).extensions(this.getType());
				if (null != extensions && extensions.length > 0) {
					Dao dao = DaoManager.createDao(connectionSource, tableClass);
					for (int i = 0; i < extensions.length; i++) {
						String query = extensions[i];
						dao.executeRaw(query);
					}
				}
			}
		} else {
			throw new RuntimeException("Error creating table from class " + logTableName);
		}
	} catch (Throwable t) {
		_logger.error("Error creating table {}", logTableName, t);
		if (result > 0) {
			TableUtils.dropTable(connectionSource, tableClass, true);
		}
		throw new ApsSystemException("Error creating table " + logTableName, t);
	}
}
 
Example 4
Source File: DatabaseManager.java    From photoviewer with Apache License 2.0 5 votes vote down vote up
private void createPhotoEntityDao() {
    try {
        mPhotosDao = DaoManager.createDao(mConnectionSource, PhotoEntity.class);
        mPhotosDao.setObjectCache(true);
    } catch (SQLException e) {
        Log.wtf(LOG_TAG_DB, "Creation of Dao<" + PhotoEntity.class + "> failed", e);
    }
}
 
Example 5
Source File: ORMLiteIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenCustomDao_whenSave_thenOk() throws SQLException, IOException {
    Library library = new Library();
    library.setName("My Library");

    LibraryDao customLibraryDao = DaoManager.createDao(connectionSource, Library.class);
    customLibraryDao.create(library);
    assertEquals(1, customLibraryDao.findByName("My Library")
        .size());
}
 
Example 6
Source File: SpatialReferenceSystemDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Geometry Columns DAO
 * 
 * @return geometry columns dao
 * @throws SQLException
 *             upon creation failure
 */
private GeometryColumnsDao getGeometryColumnsDao() throws SQLException {
	if (geometryColumnsDao == null) {
		geometryColumnsDao = DaoManager.createDao(connectionSource,
				GeometryColumns.class);
	}
	return geometryColumnsDao;
}
 
Example 7
Source File: SpatialReferenceSystemDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Contents DAO
 * 
 * @return contents dao
 * @throws SQLException
 *             upon creation failure
 */
private ContentsDao getContentsDao() throws SQLException {
	if (contentsDao == null) {
		contentsDao = DaoManager
				.createDao(connectionSource, Contents.class);
	}
	return contentsDao;
}
 
Example 8
Source File: ContentsDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Tile Matrix DAO
 * 
 * @return tile matrix dao
 * @throws SQLException
 *             upon dao creation failure
 */
private TileMatrixDao getTileMatrixDao() throws SQLException {
	if (tileMatrixDao == null) {
		tileMatrixDao = DaoManager.createDao(connectionSource,
				TileMatrix.class);
	}
	return tileMatrixDao;
}
 
Example 9
Source File: ContentsDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Tile Matrix Set DAO
 * 
 * @return tile matrix set dao
 * @throws SQLException
 *             upon dao creation failure
 */
private TileMatrixSetDao getTileMatrixSetDao() throws SQLException {
	if (tileMatrixSetDao == null) {
		tileMatrixSetDao = DaoManager.createDao(connectionSource,
				TileMatrixSet.class);
	}
	return tileMatrixSetDao;
}
 
Example 10
Source File: ContentsDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Geometry Columns DAO
 * 
 * @return geometry columns dao
 * @throws SQLException
 *             upon dao creation failure
 */
private GeometryColumnsDao getGeometryColumnsDao() throws SQLException {
	if (geometryColumnsDao == null) {
		geometryColumnsDao = DaoManager.createDao(connectionSource,
				GeometryColumns.class);
	}
	return geometryColumnsDao;
}
 
Example 11
Source File: DataColumnConstraintsDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Data Columns DAO
 * 
 * @return data columns dao
 * @throws SQLException
 */
private DataColumnsDao getDataColumnsDao() throws SQLException {
	if (dataColumnsDao == null) {
		dataColumnsDao = DaoManager.createDao(connectionSource,
				DataColumns.class);
	}
	return dataColumnsDao;
}
 
Example 12
Source File: BaseCoreTest.java    From ormlite-core with ISC License 5 votes vote down vote up
protected <T, ID> Dao<T, ID> createDao(Class<T> clazz, 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, clazz);
	return configDao(dao, createTable);
}
 
Example 13
Source File: MetadataDao.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get or create a Metadata Reference DAO
 * 
 * @return metadata reference dao
 * @throws SQLException
 */
private MetadataReferenceDao getMetadataReferenceDao() throws SQLException {
	if (metadataReferenceDao == null) {
		metadataReferenceDao = DaoManager.createDao(connectionSource,
				MetadataReference.class);
	}
	return metadataReferenceDao;
}
 
Example 14
Source File: SimpleMain.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
/**
 * Setup our database and DAOs
 */
private void setupDatabase(ConnectionSource connectionSource) throws Exception {

	accountDao = DaoManager.createDao(connectionSource, Account.class);

	// if you need to create the table
	TableUtils.createTable(connectionSource, Account.class);
}
 
Example 15
Source File: ForeignCollectionMain.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
/**
 * Setup our database and DAOs
 */
private void setupDatabase(ConnectionSource connectionSource) throws Exception {

	accountDao = DaoManager.createDao(connectionSource, Account.class);
	orderDao = DaoManager.createDao(connectionSource, Order.class);

	// if you need to create the table
	TableUtils.createTable(connectionSource, Account.class);
	TableUtils.createTable(connectionSource, Order.class);
}
 
Example 16
Source File: TableUtils.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Return an list of SQL statements that need to be run to create a table. To do the work of creating, you should
 * call {@link #createTable}.
 * 
 * @param connectionSource
 *            Our connect source which is used to get the database type, not to apply the creates.
 * @param tableConfig
 *            Hand or spring wired table configuration. If null then the class must have {@link DatabaseField}
 *            annotations.
 * @return The list of table create statements.
 */
public static <T, ID> List<String> getCreateTableStatements(ConnectionSource connectionSource,
		DatabaseTableConfig<T> tableConfig) throws SQLException {
	Dao<T, ID> dao = DaoManager.createDao(connectionSource, tableConfig);
	DatabaseType databaseType = connectionSource.getDatabaseType();
	if (dao instanceof BaseDaoImpl<?, ?>) {
		return addCreateTableStatements(databaseType, ((BaseDaoImpl<?, ?>) dao).getTableInfo(), false, false);
	} else {
		tableConfig.extractFieldTypes(databaseType);
		TableInfo<T, ID> tableInfo = new TableInfo<T, ID>(databaseType, tableConfig);
		return addCreateTableStatements(databaseType, tableInfo, false, false);
	}
}
 
Example 17
Source File: FoodService.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public Dao<Food, Long> getDao() {
    try {
        return DaoManager.createDao(this.getConnectionSource(), Food.class);
    } catch (SQLException e) {
        log.error("Cannot create Dao for Food.class");
    }

    return null;
}
 
Example 18
Source File: FeatureInfoBuilder.java    From geopackage-android-map with MIT License 5 votes vote down vote up
/**
 * Project the geometry into the provided projection
 *
 * @param geometryData geometry data
 * @param projection   projection
 */
public void projectGeometry(GeoPackageGeometryData geometryData, Projection projection) {

    if (geometryData.getGeometry() != null) {

        try {
            SpatialReferenceSystemDao srsDao = DaoManager.createDao(featureDao.getDb().getConnectionSource(), SpatialReferenceSystem.class);
            int srsId = geometryData.getSrsId();
            SpatialReferenceSystem srs = srsDao.queryForId((long) srsId);

            if (!projection.equals(srs.getOrganization(), srs.getOrganizationCoordsysId())) {

                Projection geomProjection = srs.getProjection();
                ProjectionTransform transform = geomProjection.getTransformation(projection);

                Geometry projectedGeometry = transform.transform(geometryData.getGeometry());
                geometryData.setGeometry(projectedGeometry);
                SpatialReferenceSystem projectionSrs = srsDao.getOrCreateCode(projection.getAuthority(), Long.parseLong(projection.getCode()));
                geometryData.setSrsId((int) projectionSrs.getSrsId());
            }
        } catch (SQLException e) {
            throw new GeoPackageException("Failed to project geometry to projection with Authority: "
                    + projection.getAuthority() + ", Code: " + projection.getCode(), e);
        }
    }

}
 
Example 19
Source File: SchemaUtils.java    From ormlite-core with ISC License 4 votes vote down vote up
/**
 * Create a schema if it does not already exist. This is not supported by all databases.
 */
public static <T> int createSchemaIfNotExists(ConnectionSource connectionSource, Class<T> dataClass)
        throws SQLException {
    Dao<T, ?> dao = DaoManager.createDao(connectionSource, dataClass);
    return doCreateSchema(dao.getConnectionSource(), dao.getTableInfo().getSchemaName(), true);
}
 
Example 20
Source File: DaoFactory.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
/**
 * Create and return a Dao based on the arguments.
 */
public static <T, ID> Dao<T, ID> createDao(ConnectionSource connectionSource, DatabaseTableConfig<T> tableConfig)
		throws SQLException {
	return DaoManager.createDao(connectionSource, tableConfig);
}