Package com.iizix.server.db
Class DBUtilities
- java.lang.Object
- com.iizix.server.db.DBUtilities
public class DBUtilities extends java.lang.ObjectThe utilities class is used e.g. to create the database and the database tables used by the stand-alone or development servers.- Author:
- Christopher Mindus
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_DATABASE_DIRECTORYThe default database directory: "db".
Constructor Summary
Constructors Constructor Description DBUtilities()
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanareTablesCreated(java.sql.Connection conn, java.lang.String schema, java.lang.String table)Checks if the tables already are created.static voidcreateTables(int jdbcFlavor, java.sql.Connection conn, java.io.InputStream in, java.nio.charset.Charset charSet, int textLength)Creates tables.static voidcreateTables(java.sql.Connection conn, java.io.InputStream in)Creates tables.static voidcreateTables(java.sql.Connection conn, java.io.InputStream in, java.lang.String originalSchema, java.lang.String newSchema)Creates the tables with a change of the schema from "original" to "new".static IDBConnectionPoolgetConnectionPool(IFileURLResolver fileURLResolver)Creates the connection pool.static intgetJDBCFlavor()Gets the flavor of the JDBC used in the internal server database.static java.sql.ConnectiongetPooledConnection()Gets a pooled connection for user processing to the internal server database.static intgetServerDatabaseReady()Returns whether the Server internal database is ready for use, i.e.static java.lang.StringgetServerHost()Gets the Derby network database server host name for connections.static intgetServerPort()Gets the Derby network database server port for connections.static voidregisterPoolProxy(IDBPoolProxy proxy)Registers a database pool proxy.static voidsetAutoCreate(boolean on)Sets the database auto-create flag.static voidsetDBConfDir(java.lang.String dirName)Processes the database configuration directive.static voidsetDirectory(java.lang.String dir)Sets the database directory for the internal server.static voidsetFileURLResolver(IFileURLResolver resolver)Interface used to resolve URL's to files for the Designer.static voidsetServerAutoStart(boolean on)Sets the Derby network database server auto-start flag.static voidsetServerHost(java.lang.String host)Sets the Derby network database server host name for connections.static voidsetServerPort(int port)Sets the Derby network database server port for connections.
Field Detail
DEFAULT_DATABASE_DIRECTORY
public static final java.lang.String DEFAULT_DATABASE_DIRECTORY
The default database directory: "db".- See Also:
- Constant Field Values
Method Detail
getJDBCFlavor
public static int getJDBCFlavor()
Gets the flavor of the JDBC used in the internal server database. This value is computed when the connection pool is created.See the FLAVOR_* values.
- Returns:
- A flavor value as:
- -1 = Undefined
- 0 = Derby
- 1 = MySQL
- 2 = PostgreSQL
- 3 = SQL Server
- 4 = DB2
setDBConfDir
public static void setDBConfDir(java.lang.String dirName) throws java.io.FileNotFoundExceptionProcesses the database configuration directive. The directory is searched for a file named "dbconf.properties", and if it is missing, aFileNotFoundExceptionis thrown.- Parameters:
dirName- The database configuration directory.- Throws:
java.io.FileNotFoundException- ifdirNameis not found or not a directory, or if one of the filesdirName/dbconf.propertiesis not found.
setFileURLResolver
public static void setFileURLResolver(IFileURLResolver resolver)
Interface used to resolve URL's to files for the Designer. In other products, the same URL is returned unchanged.- Parameters:
resolver- The resolver instance.- Throws:
java.lang.NullPointerException- If the resolver is null.
setAutoCreate
public static void setAutoCreate(boolean on)
Sets the database auto-create flag.- Parameters:
on- The automatic start flag.
setServerAutoStart
public static void setServerAutoStart(boolean on)
Sets the Derby network database server auto-start flag.- Parameters:
on- The automatic start flag.
setServerHost
public static void setServerHost(java.lang.String host)
Sets the Derby network database server host name for connections.- Parameters:
host- The host for the Client Derby driver.- Throws:
java.lang.NullPointerException- If the host name is null.java.lang.IllegalArgumentException- If the host is invalid.
getServerHost
public static java.lang.String getServerHost()
Gets the Derby network database server host name for connections.- Returns:
- The host for the Client Derby driver, default "localhost".
setServerPort
public static void setServerPort(int port)
Sets the Derby network database server port for connections.- Parameters:
port- The port number for the Client Derby driver.- Throws:
java.lang.IllegalArgumentException- If the number is invalid.
getServerPort
public static int getServerPort()
Gets the Derby network database server port for connections.- Returns:
- The port number for the Client Derby driver, default 1527.
setDirectory
public static void setDirectory(java.lang.String dir) throws java.io.FileNotFoundException, java.io.IOExceptionSets the database directory for the internal server. The default directory is "db" (it is already appended with 'iiziServer').- Parameters:
dir- The new directory. The database name will be "iiziServer"- Throws:
java.io.FileNotFoundException- If the parent directory does not exist.java.io.IOException- For file system errors.
registerPoolProxy
public static void registerPoolProxy(IDBPoolProxy proxy) throws java.sql.SQLException
Registers a database pool proxy.- Parameters:
proxy- The proxy.- Throws:
java.sql.SQLException- For connection, class loader or instantiation failures.java.lang.SecurityException- If caller is not allowed.
getConnectionPool
public static IDBConnectionPool getConnectionPool(IFileURLResolver fileURLResolver) throws java.sql.SQLException
Creates the connection pool.- Parameters:
fileURLResolver- File resolver from an URL.- Returns:
- The connection pool.
- Throws:
java.sql.SQLException- For connection, class loader or instantiation failures.java.lang.SecurityException- If the caller is not allowed to access the server database connection pool.
areTablesCreated
public static boolean areTablesCreated(java.sql.Connection conn, java.lang.String schema, java.lang.String table)Checks if the tables already are created.- Parameters:
conn- The connection.schema- The schema.table- The table.- Returns:
- true if the table exist, false otherwise.
getServerDatabaseReady
public static int getServerDatabaseReady()
Returns whether the Server internal database is ready for use, i.e. database server potentially started and the connection pool ready.- Returns:
- The codes are:
1 = started and ready,
0 = starting or will start and not ready; and
-1 = failed starting and ready won't start, i.e. server database has failed and server will exit.
getPooledConnection
public static java.sql.Connection getPooledConnection() throws java.sql.SQLExceptionGets a pooled connection for user processing to the internal server database.- Returns:
- The pooled connection that MUST be released and not closed.
- Throws:
java.sql.SQLException- For SQL errors.
createTables
public static void createTables(java.sql.Connection conn, java.io.InputStream in) throws java.sql.SQLExceptionCreates tables.The length of a MySQL TEXT when creating a Derby table using VARCHAR instead is set to 4096. Use the method
{@link #createTables(int, Connection, InputStream, Charset, int)}to specify another length.The Database Flavor is assumed to be the one from the IIZI Internal Database pool.
- Parameters:
conn- The connection.in- Input stream to a creation of tables SQL file. The input stream is closed regardless of exceptions. The input stream is processed using UTF-8.- Throws:
java.sql.SQLException- For SQL errors.
createTables
public static void createTables(int jdbcFlavor, java.sql.Connection conn, java.io.InputStream in, java.nio.charset.Charset charSet, int textLength) throws java.sql.SQLExceptionCreates tables.- Parameters:
jdbcFlavor- The JDBC flavor used to convert SQL statements between Database Flavors, accepted values:IDBConnectionPool.FLAVOR_DERBY,IDBConnectionPool.FLAVOR_MYSQL,- others values are ignored and makes no change in the SQL.
conn- The connection.in- Input stream to a "createTables.sql" file. The input stream is closed regardless of exceptions. The input stream is processed using UTF-8.textLength- The length of a MySQL TEXT when creating a Derby table using VARCHAR instead.- Throws:
java.sql.SQLException- For SQL errors.
createTables
public static void createTables(java.sql.Connection conn, java.io.InputStream in, java.lang.String originalSchema, java.lang.String newSchema) throws java.sql.SQLExceptionCreates the tables with a change of the schema from "original" to "new".- Parameters:
conn- The connection.in- Input stream to a "createTables.sql" file. The input stream is closed regardless of exceptions.originalSchema- The original schema name (upper case).newSchema- The new schema name (upper case).- Throws:
java.sql.SQLException- For SQL errors.