Package jslx.dbutilities.dbutil
Class DatabaseFactory
- java.lang.Object
-
- jslx.dbutilities.dbutil.DatabaseFactory
-
public class DatabaseFactory extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description DatabaseFactory()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
copyEmbeddedDerbyDatabase(java.nio.file.Path sourceDB, java.lang.String dupName, java.nio.file.Path directory)
Duplicates the database into a new database with the supplied name and directory.static void
copyEmbeddedDerbyDatabase(javax.sql.DataSource ds, java.nio.file.Path sourceDB, java.lang.String dupName, java.nio.file.Path directory)
Uses an active database connection and derby system commands to freeze the database, uses system OS commands to copy the database, and then unfreezes the database.static javax.sql.DataSource
createClientDerbyDataSourceWithLocalHost(java.lang.String dbName, java.lang.String user, java.lang.String pWord, boolean create)
static org.sqlite.SQLiteConfig
createDefaultSQLiteConfiguration()
Creates a recommended non-read only configuration that has been tested for performance.static org.sqlite.SQLiteConfig
createDefaultSQLiteConfiguration(boolean readOnly)
Creates a recommended configuration that has been tested for performance.static DatabaseIfc
createEmbeddedDerbyDatabase(java.lang.String dbName)
If the database already exists it is deleted.static DatabaseIfc
createEmbeddedDerbyDatabase(java.lang.String dbName, java.nio.file.Path dbDir)
If the database already exists it is deletedstatic DatabaseIfc
createEmbeddedDerbyDatabase(java.lang.String dbLabel, javax.sql.DataSource dataSource)
static javax.sql.DataSource
createEmbeddedDerbyDataSource(java.lang.String dbName)
Assumes that the database existsstatic javax.sql.DataSource
createEmbeddedDerbyDataSource(java.lang.String dbName, boolean create)
static javax.sql.DataSource
createEmbeddedDerbyDataSource(java.lang.String dbName, java.lang.String user, java.lang.String pWord, boolean create)
static javax.sql.DataSource
createEmbeddedDerbyDataSource(java.nio.file.Path pathToDb)
The database must already exist.static javax.sql.DataSource
createEmbeddedDerbyDataSource(java.nio.file.Path pathToDb, boolean create)
static javax.sql.DataSource
createEmbeddedDerbyDataSource(java.nio.file.Path pathToDb, java.lang.String user, java.lang.String pWord, boolean create)
static DatabaseIfc
createSQLiteDatabase(java.lang.String dbName)
If the database already exists it is deleted.static DatabaseIfc
createSQLiteDatabase(java.lang.String dbName, java.nio.file.Path dbDir)
If the database already exists it is deletedstatic DatabaseIfc
createSQLiteDatabase(java.lang.String dbLabel, org.sqlite.SQLiteDataSource dataSource)
static org.sqlite.SQLiteDataSource
createSQLiteDataSource(java.nio.file.Path pathToDb)
static void
deleteEmbeddedDerbyDatabase(java.nio.file.Path pathToDb)
This does not check if the database is shutdown.static void
deleteSQLiteDatabase(java.nio.file.Path pathToDb)
Deletes a SQLite database Strategy: - simply deletes the file at the end of the path - it may or not be a valid SQLiteDatabasestatic javax.sql.DataSource
getDataSource(java.nio.file.Path pathToPropertiesFile)
static javax.sql.DataSource
getDataSource(java.util.Properties properties)
Assumes that the properties are appropriately configured to create a DataSource via HikariCPstatic DatabaseIfc
getEmbeddedDerbyDatabase(java.lang.String dbName)
The database must already exist.static DatabaseIfc
getEmbeddedDerbyDatabase(java.lang.String dbName, java.nio.file.Path dbDir)
The database must already exist.static DatabaseIfc
getEmbeddedDerbyDatabase(java.nio.file.Path pathToDb)
The database must already exist.static javax.sql.DataSource
getPostGresDataSource(java.lang.String dbServerName, java.lang.String dbName, java.lang.String user, java.lang.String pWord)
Assumes standard PostGres portstatic javax.sql.DataSource
getPostGresDataSource(java.lang.String dbServerName, java.lang.String dbName, java.lang.String user, java.lang.String pWord, int portNumber)
static javax.sql.DataSource
getPostGresDataSourceWithLocalHost(java.lang.String dbName, java.lang.String user, java.lang.String pWord)
static DatabaseIfc
getSQLiteDatabase(java.lang.String fileName)
The database file must already exist within the JSLDatabase.dbDir directory It is opened for reading and writing.static DatabaseIfc
getSQLiteDatabase(java.nio.file.Path pathToDb)
The database file must already exist at the path.static DatabaseIfc
getSQLiteDatabase(java.nio.file.Path pathToDb, boolean readOnly)
The database file must already exist at the pathstatic boolean
isEmbeddedDerbyDatabase(java.io.File file)
A convenience method for those that use File instead of Path.static boolean
isEmbeddedDerbyDatabase(java.nio.file.Path path)
There is no way to guarantee with 100 percent certainty that the path is in fact a embedded derby database because someone could be faking the directory structure.static boolean
isEmbeddedDerbyDatabaseExists(java.lang.String dbName)
Checks if the database exists in in JSLDatabase.dbDirstatic boolean
isEmbeddedDerbyDatabaseExists(java.lang.String dbName, java.nio.file.Path dbDir)
static boolean
isEmbeddedDerbyDatabaseExists(java.nio.file.Path fullPath)
static boolean
isSQLiteDatabase(java.nio.file.Path pathToFile)
Checks if a file is a valid SQLite database Strategy: - path must reference a regular file - if file exists, check if it is larger than 100 bytes (SQLite header size) - then check if a database operation worksstatic java.util.Properties
makePostGresProperties(java.lang.String dbServerName, java.lang.String dbName, java.lang.String user, java.lang.String pWord)
static boolean
shutDownEmbeddedDerbyDatabase(java.nio.file.Path pathToDb)
Sends a shutdown connection to the database.static boolean
shutDownEmbeddedDerbyDatabase(java.nio.file.Path pathToDb, java.lang.String user, java.lang.String pWord)
Sends a shutdown connection to the database.static javax.sql.DataSource
shutDownEmbeddedDerbyDataSource(java.lang.String dbName)
Creates a data source that can be used to shut down an embedded derby database upon first connection.static javax.sql.DataSource
shutDownEmbeddedDerbyDataSource(java.lang.String dbName, java.lang.String user, java.lang.String pWord)
Creates a data source that can be used to shut down an embedded derby database upon first connection.static javax.sql.DataSource
shutDownEmbeddedDerbyDataSource(java.nio.file.Path pathToDb)
Creates a data source that can be used to shut down an embedded derby database upon first connection.static javax.sql.DataSource
shutDownEmbeddedDerbyDataSource(java.nio.file.Path pathToDb, java.lang.String user, java.lang.String pWord)
Creates a data source that can be used to shut down an embedded derby database upon first connection.
-
-
-
Method Detail
-
createEmbeddedDerbyDatabase
public static DatabaseIfc createEmbeddedDerbyDatabase(java.lang.String dbName)
If the database already exists it is deleted. Creates the database in JSLDatabase.dbDir- Parameters:
dbName
- the name of the database- Returns:
- the created database
-
createEmbeddedDerbyDatabase
public static DatabaseIfc createEmbeddedDerbyDatabase(java.lang.String dbName, java.nio.file.Path dbDir)
If the database already exists it is deleted- Parameters:
dbName
- the name of the embedded database. Must not be nulldbDir
- a path to the directory to hold the database. Must not be null- Returns:
- the created database
-
createEmbeddedDerbyDatabase
public static DatabaseIfc createEmbeddedDerbyDatabase(java.lang.String dbLabel, javax.sql.DataSource dataSource)
- Parameters:
dbLabel
- a label for the databasedataSource
- the data source for connections- Returns:
- the created database
-
getEmbeddedDerbyDatabase
public static DatabaseIfc getEmbeddedDerbyDatabase(java.lang.String dbName)
The database must already exist. It is not created. An exception is thrown if it does not exist. Assumes that the named database is in JSLDatabase.dbDir- Parameters:
dbName
- the name of the embedded database, must not be null- Returns:
- the created database
-
getEmbeddedDerbyDatabase
public static DatabaseIfc getEmbeddedDerbyDatabase(java.lang.String dbName, java.nio.file.Path dbDir)
The database must already exist. It is not created. An exception is thrown if it does not exist.- Parameters:
dbName
- the name of the embedded database, must not be nulldbDir
- a path to the directory that holds the database, must not be null- Returns:
- the created database
-
getEmbeddedDerbyDatabase
public static DatabaseIfc getEmbeddedDerbyDatabase(java.nio.file.Path pathToDb)
The database must already exist. It is not created. An exception is thrown if it does not exist.- Parameters:
pathToDb
- the full path to the directory that is the database, must not be null- Returns:
- the database
-
isEmbeddedDerbyDatabaseExists
public static boolean isEmbeddedDerbyDatabaseExists(java.lang.String dbName)
Checks if the database exists in in JSLDatabase.dbDir- Parameters:
dbName
- the name of the database- Returns:
- true if it exists false if not
-
isEmbeddedDerbyDatabaseExists
public static boolean isEmbeddedDerbyDatabaseExists(java.lang.String dbName, java.nio.file.Path dbDir)
- Parameters:
dbName
- the name of the databasedbDir
- the directory to the database- Returns:
- true if it exists false if not
-
isEmbeddedDerbyDatabaseExists
public static boolean isEmbeddedDerbyDatabaseExists(java.nio.file.Path fullPath)
- Parameters:
fullPath
- the full path to the database including its name (because derby stores the database in a directory- Returns:
- true if it exists
-
deleteEmbeddedDerbyDatabase
public static void deleteEmbeddedDerbyDatabase(java.nio.file.Path pathToDb)
This does not check if the database is shutdown. It simply removes the database from the file system. If it doesn't exist, then nothing happens.- Parameters:
pathToDb
- the path to the embedded database on disk
-
createEmbeddedDerbyDataSource
public static javax.sql.DataSource createEmbeddedDerbyDataSource(java.nio.file.Path pathToDb)
The database must already exist. It will not be created- Parameters:
pathToDb
- a path to the database, must not be null- Returns:
- the created DataSource
-
createEmbeddedDerbyDataSource
public static javax.sql.DataSource createEmbeddedDerbyDataSource(java.nio.file.Path pathToDb, boolean create)
- Parameters:
pathToDb
- a path to the database, must not be nullcreate
- a flag to indicate if the database should be created upon first connection- Returns:
- the created DataSource
-
createEmbeddedDerbyDataSource
public static javax.sql.DataSource createEmbeddedDerbyDataSource(java.nio.file.Path pathToDb, java.lang.String user, java.lang.String pWord, boolean create)
- Parameters:
pathToDb
- a path to the database, must not be nulluser
- a user name, can be nullpWord
- a password, can be nullcreate
- a flag to indicate if the database should be created upon first connection- Returns:
- the created DataSource
-
createEmbeddedDerbyDataSource
public static javax.sql.DataSource createEmbeddedDerbyDataSource(java.lang.String dbName)
Assumes that the database exists- Parameters:
dbName
- the path to the database, must not be null- Returns:
- the created DataSource
-
createEmbeddedDerbyDataSource
public static javax.sql.DataSource createEmbeddedDerbyDataSource(java.lang.String dbName, boolean create)
- Parameters:
dbName
- the path to the database, must not be nullcreate
- a flag to indicate if the database should be created upon first connection- Returns:
- the created DataSource
-
createEmbeddedDerbyDataSource
public static javax.sql.DataSource createEmbeddedDerbyDataSource(java.lang.String dbName, java.lang.String user, java.lang.String pWord, boolean create)
- Parameters:
dbName
- the path to the database, must not be nulluser
- a user name, can be nullpWord
- a password, can be nullcreate
- a flag to indicate if the database should be created upon first connection- Returns:
- the created DataSource
-
shutDownEmbeddedDerbyDatabase
public static boolean shutDownEmbeddedDerbyDatabase(java.nio.file.Path pathToDb)
Sends a shutdown connection to the database.- Parameters:
pathToDb
- a path to the database, must not be null- Returns:
- true if successfully shutdown
-
shutDownEmbeddedDerbyDatabase
public static boolean shutDownEmbeddedDerbyDatabase(java.nio.file.Path pathToDb, java.lang.String user, java.lang.String pWord)
Sends a shutdown connection to the database.- Parameters:
pathToDb
- a path to the database, must not be nulluser
- a user name, can be nullpWord
- a password, can be null- Returns:
- true if successfully shutdown
-
shutDownEmbeddedDerbyDataSource
public static javax.sql.DataSource shutDownEmbeddedDerbyDataSource(java.nio.file.Path pathToDb)
Creates a data source that can be used to shut down an embedded derby database upon first connection.- Parameters:
pathToDb
- a path to the database, must not be null- Returns:
- the created DataSource
-
shutDownEmbeddedDerbyDataSource
public static javax.sql.DataSource shutDownEmbeddedDerbyDataSource(java.nio.file.Path pathToDb, java.lang.String user, java.lang.String pWord)
Creates a data source that can be used to shut down an embedded derby database upon first connection.- Parameters:
pathToDb
- a path to the database, must not be nulluser
- a user name, can be nullpWord
- a password, can be null- Returns:
- the created DataSource
-
shutDownEmbeddedDerbyDataSource
public static javax.sql.DataSource shutDownEmbeddedDerbyDataSource(java.lang.String dbName)
Creates a data source that can be used to shut down an embedded derby database upon first connection.- Parameters:
dbName
- the path to the database, must not be null- Returns:
- the created DataSource
-
shutDownEmbeddedDerbyDataSource
public static javax.sql.DataSource shutDownEmbeddedDerbyDataSource(java.lang.String dbName, java.lang.String user, java.lang.String pWord)
Creates a data source that can be used to shut down an embedded derby database upon first connection.- Parameters:
dbName
- the path to the database, must not be nulluser
- a user name, can be nullpWord
- a password, can be null- Returns:
- the created DataSource
-
createClientDerbyDataSourceWithLocalHost
public static javax.sql.DataSource createClientDerbyDataSourceWithLocalHost(java.lang.String dbName, java.lang.String user, java.lang.String pWord, boolean create)
- Parameters:
dbName
- the path to the database, must not be nulluser
- a user name, can be nullpWord
- a password, can be nullcreate
- a flag to indicate if the database should be created upon first connection- Returns:
- the created DataSource
-
getPostGresDataSourceWithLocalHost
public static javax.sql.DataSource getPostGresDataSourceWithLocalHost(java.lang.String dbName, java.lang.String user, java.lang.String pWord)
- Parameters:
dbName
- the name of the database, must not be nulluser
- the userpWord
- the password- Returns:
- the DataSource for getting connections
-
getPostGresDataSource
public static javax.sql.DataSource getPostGresDataSource(java.lang.String dbServerName, java.lang.String dbName, java.lang.String user, java.lang.String pWord)
Assumes standard PostGres port- Parameters:
dbServerName
- the name of the database server, must not be nulldbName
- the name of the database, must not be nulluser
- the userpWord
- the password- Returns:
- the DataSource for getting connections
-
getPostGresDataSource
public static javax.sql.DataSource getPostGresDataSource(java.lang.String dbServerName, java.lang.String dbName, java.lang.String user, java.lang.String pWord, int portNumber)
- Parameters:
dbServerName
- the name of the database server, must not be nulldbName
- the name of the database, must not be nulluser
- the userpWord
- the passwordportNumber
- a valid port number- Returns:
- the DataSource for getting connections
-
makePostGresProperties
public static java.util.Properties makePostGresProperties(java.lang.String dbServerName, java.lang.String dbName, java.lang.String user, java.lang.String pWord)
- Parameters:
dbServerName
- the database server name, must not be nulldbName
- the database name, must not be nulluser
- the user, must not be nullpWord
- the password, must not be null- Returns:
- the Properties instance
-
getDataSource
public static javax.sql.DataSource getDataSource(java.util.Properties properties)
Assumes that the properties are appropriately configured to create a DataSource via HikariCP- Parameters:
properties
- the properties- Returns:
- a pooled connection DataSource
-
getDataSource
public static javax.sql.DataSource getDataSource(java.nio.file.Path pathToPropertiesFile)
- Parameters:
pathToPropertiesFile
- must not be null- Returns:
- a DataSource for making a database
-
copyEmbeddedDerbyDatabase
public static void copyEmbeddedDerbyDatabase(java.nio.file.Path sourceDB, java.lang.String dupName, java.nio.file.Path directory) throws java.io.IOException
Duplicates the database into a new database with the supplied name and directory. Assumes that the source database has no active connections and performs a file system copy- Parameters:
sourceDB
- the path to the database that needs duplicatingdupName
- the name of the duplicate databasedirectory
- the directory to place the database in- Throws:
java.io.IOException
- thrown if the system file copy commands fail
-
copyEmbeddedDerbyDatabase
public static final void copyEmbeddedDerbyDatabase(javax.sql.DataSource ds, java.nio.file.Path sourceDB, java.lang.String dupName, java.nio.file.Path directory) throws java.sql.SQLException, java.io.IOException
Uses an active database connection and derby system commands to freeze the database, uses system OS commands to copy the database, and then unfreezes the database. The duplicate name and directory path must not already exist- Parameters:
ds
- a DataSource to the embedded derby database, obviously it must point to the derby databasesourceDB
- the path to the source databasedupName
- the name of the duplicate database, obviously it must reference the same database that is referenced by the DataSourcedirectory
- the directory to place the database in- Throws:
java.sql.SQLException
- thrown if the derby commands failjava.io.IOException
- thrown if the system file copy commands fail
-
isEmbeddedDerbyDatabase
public static boolean isEmbeddedDerbyDatabase(java.nio.file.Path path)
There is no way to guarantee with 100 percent certainty that the path is in fact a embedded derby database because someone could be faking the directory structure. The database directory of an embedded derby database must have a service.properties file, a log directory, and a seg0 directory. If these exist and the supplied path is a directory, then the method returns true.- Parameters:
path
- the path to check, must not be null- Returns:
- true if it could be an embedded derby database
-
isEmbeddedDerbyDatabase
public static boolean isEmbeddedDerbyDatabase(java.io.File file)
A convenience method for those that use File instead of Path. Calls isEmbeddedDerbyDatabase(file.toPath())- Parameters:
file
- the file to check, must not be null- Returns:
- true if it could be an embedded derby database
-
isSQLiteDatabase
public static boolean isSQLiteDatabase(java.nio.file.Path pathToFile)
Checks if a file is a valid SQLite database Strategy: - path must reference a regular file - if file exists, check if it is larger than 100 bytes (SQLite header size) - then check if a database operation works- Parameters:
pathToFile
- the path to the database file, must not be null- Returns:
- true if the path points to a valid SQLite database file
-
deleteSQLiteDatabase
public static void deleteSQLiteDatabase(java.nio.file.Path pathToDb)
Deletes a SQLite database Strategy: - simply deletes the file at the end of the path - it may or not be a valid SQLiteDatabase- Parameters:
pathToDb
- the path to the database file, must not be null
-
createSQLiteDataSource
public static org.sqlite.SQLiteDataSource createSQLiteDataSource(java.nio.file.Path pathToDb)
- Parameters:
pathToDb
- the path to the database file, must not be null- Returns:
- the data source
-
createDefaultSQLiteConfiguration
public static org.sqlite.SQLiteConfig createDefaultSQLiteConfiguration()
Creates a recommended non-read only configuration that has been tested for performance. https://ericdraken.com/sqlite-performance-testing/- Returns:
- the configuration
-
createDefaultSQLiteConfiguration
public static org.sqlite.SQLiteConfig createDefaultSQLiteConfiguration(boolean readOnly)
Creates a recommended configuration that has been tested for performance. https://ericdraken.com/sqlite-performance-testing/- Parameters:
readOnly
- indicates read only mode- Returns:
- the configuration
-
createSQLiteDatabase
public static DatabaseIfc createSQLiteDatabase(java.lang.String dbLabel, org.sqlite.SQLiteDataSource dataSource)
- Parameters:
dbLabel
- a label for the databasedataSource
- the data source for connections- Returns:
- the created database
-
createSQLiteDatabase
public static DatabaseIfc createSQLiteDatabase(java.lang.String dbName, java.nio.file.Path dbDir)
If the database already exists it is deleted- Parameters:
dbName
- the name of the SQLite database. Must not be nulldbDir
- a path to the directory to hold the database. Must not be null- Returns:
- the created database
-
createSQLiteDatabase
public static DatabaseIfc createSQLiteDatabase(java.lang.String dbName)
If the database already exists it is deleted. Created within JSLDatabase.dbDir- Parameters:
dbName
- the name of the SQLite database. Must not be null- Returns:
- the created database
-
getSQLiteDatabase
public static DatabaseIfc getSQLiteDatabase(java.nio.file.Path pathToDb, boolean readOnly)
The database file must already exist at the path- Parameters:
pathToDb
- the path to the database file, must not be nullreadOnly
- true indicates that the database is read only- Returns:
- the database
-
getSQLiteDatabase
public static DatabaseIfc getSQLiteDatabase(java.nio.file.Path pathToDb)
The database file must already exist at the path. It is opened for reading and writing- Parameters:
pathToDb
- the path to the database file, must not be null- Returns:
- the database
-
getSQLiteDatabase
public static DatabaseIfc getSQLiteDatabase(java.lang.String fileName)
The database file must already exist within the JSLDatabase.dbDir directory It is opened for reading and writing.- Parameters:
fileName
- the name of database file, must not be null- Returns:
- the database
-
-