Package com.tccc.kos.commons.db
Class DatabaseFactory
java.lang.Object
com.tccc.kos.commons.db.DatabaseFactory
System component used to create a DataSource or Jdbi instance for a SQLite database.
 This will handle creation of the database file and migration of the schema using the
 supplied 
DatabaseMigrator. The result can be either a DataSoruce or Jdbi
 instance to the resulting database.- Since:
- 1.0
- Version:
- 2022-10-21
- See Also:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptiongetDataSource(DatabaseMigrator migrator, String name, File dir) Creates aDataSourcefor the specified database file in the specified directory.Return the default directory.getJdbi(DatabaseMigrator migrator, String name, File dir) Wrapper forgetDataSourcethat returnsJdbiinstance instead.migrate(DatabaseMigrator migrator, String name, File dir) This is a convenience method that simply callsgetDataSource()and then returns the migrator.voidsetDefaultDir(File dir) Set the default directory
- 
Constructor Details- 
DatabaseFactorypublic DatabaseFactory()
 
- 
- 
Method Details- 
setDefaultDirSet the default directory
- 
getDefaultDirReturn the default directory.
- 
migrateThis is a convenience method that simply callsgetDataSource()and then returns the migrator. When a database only has a single DAO, then this is typically used by passing in a new instance of the DAO, migrating it, and then returning the DAO instance so it can be added to the context, all in one line of code.- Parameters:
- migrator- used to migrate the database
- name- the database's name
- dir- the directory the database is located
- Returns:
- the migrator that was passed in
 
- 
getDataSourceCreates aDataSourcefor the specified database file in the specified directory. The supplied migrator is used to migrate the database to the latest schema before the data source is returned. If migration needs to occur, it is performed with a temporary data source until everything is ready, and thensetDataSource()is called on the migrator to indicate that things are stable and ready for use.When this method is called, the database is in one of the following states: - It exists and its schema version is correct/current (most of the time)
- It exists but its schema version is older (needs updating)
- It exists but its schema version is newer (needs rolling-back)
- It does not exist
 - Parameters:
- migrator- used to migrate the database
- name- the database's name
- dir- the directory the database is located
- Returns:
- the resulting datasource
 
- 
getJdbiWrapper forgetDataSourcethat returnsJdbiinstance instead. This will be the sameJdbiinstance used to callsetJdbion the migrator when migration is complete.- Parameters:
- migrator- used to migrate the database
- name- the database's name
- dir- the directory the database is located
- Returns:
- the resulting jdbi instance
 
 
-