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 IDBConnectionPool.Flavor 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 IDBConnectionPool.Flavor FLAVOR_* values.

      Returns:
      A flavor value.
    • 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.
    • getDatabaseDirectory

      public static String getDatabaseDirectory()
      Gets the database directory configuration for Derby (if used).
      Returns:
      The directory configured, or null for none.
    • 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.
    • isTableCreated

      public static boolean isTableCreated(Connection conn, String schema, String table)
      Checks if the table 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(IDBConnectionPool.Flavor 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.