Class DBUtilities

java.lang.Object
com.iizix.server.db.DBUtilities

public class DBUtilities extends Object
The 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 Details

    • DEFAULT_DATABASE_DIRECTORY

      public static final String DEFAULT_DATABASE_DIRECTORY
      The default database directory: "db".
      See Also:
  • Constructor Details

    • DBUtilities

      public DBUtilities()
  • Method Details

    • 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(String dirName) throws FileNotFoundException
      Processes the database configuration directive. The directory is searched for a file named "dbconf.properties", and if it is missing, a FileNotFoundException is thrown.
      Parameters:
      dirName - The database configuration directory.
      Throws:
      FileNotFoundException - if dirName is not found or not a directory, or if one of the files dirName/dbconf.properties is 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:
      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(String host)
      Sets the Derby network database server host name for connections.
      Parameters:
      host - The host for the Client Derby driver.
      Throws:
      NullPointerException - If the host name is null.
      IllegalArgumentException - If the host is invalid.
    • getServerHost

      public static 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:
      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(String dir) throws FileNotFoundException, IOException
      Sets 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:
      FileNotFoundException - If the parent directory does not exist.
      IOException - For file system errors.
    • registerPoolProxy

      public static void registerPoolProxy(IDBPoolProxy proxy) throws SQLException
      Registers a database pool proxy.
      Parameters:
      proxy - The proxy.
      Throws:
      SQLException - For connection, class loader or instantiation failures.
      SecurityException - If caller is not allowed.
    • getConnectionPool

      public static IDBConnectionPool getConnectionPool(IFileURLResolver fileURLResolver) throws SQLException
      Creates the connection pool.
      Parameters:
      fileURLResolver - File resolver from an URL.
      Returns:
      The connection pool.
      Throws:
      SQLException - For connection, class loader or instantiation failures.
      SecurityException - If the caller is not allowed to access the server database connection pool.
    • areTablesCreated

      public static boolean areTablesCreated(Connection conn, String schema, 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 Connection getPooledConnection() throws SQLException
      Gets a pooled connection for user processing to the internal server database.
      Returns:
      The pooled connection that MUST be released and not closed.
      Throws:
      SQLException - For SQL errors.
    • createTables

      public static void createTables(Connection conn, InputStream in) throws SQLException
      Creates 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:
      SQLException - For SQL errors.
    • createTables

      public static void createTables(int jdbcFlavor, Connection conn, InputStream in, Charset charSet, int textLength) throws SQLException
      Creates tables.
      Parameters:
      jdbcFlavor - The JDBC flavor used to convert SQL statements between Database Flavors, accepted values:
      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:
      SQLException - For SQL errors.
    • createTables

      public static void createTables(Connection conn, InputStream in, String originalSchema, String newSchema) throws SQLException
      Creates 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:
      SQLException - For SQL errors.