Class ForeignDBViews

  • All Implemented Interfaces:
    OptionalTool

    public class ForeignDBViews
    extends java.lang.Object
    implements OptionalTool

    OptionalTool to create wrapper functions and views for all of the user tables in a foreign database.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String[] SAFE_DROP_SQLSTATES  
      private static int XML_TYPE  
    • Constructor Summary

      Constructors 
      Constructor Description
      ForeignDBViews()
      0-arg constructor required by the OptionalTool contract
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void createDerbySchema​(java.sql.Connection derbyConn, java.lang.String derbySchemaName)
      Create a Derby schema if it does not already exist.
      private java.lang.String delimitedID​(java.lang.String text)  
      private java.lang.String dotSeparatedSchemaName​(java.lang.String rawName)
      Turn a Derby schema name into a schema name suitable for use in a dot-separated object name.
      private void dropDerbySchema​(java.sql.Connection derbyConn, java.lang.String derbySchemaName)
      Drop a Derby schema.
      private void dropObject​(java.sql.Connection conn, java.lang.String schemaName, java.lang.String objectName, java.lang.String objectType, boolean restrict)
      Drop a schema object.
      private void executeDDL​(java.sql.Connection conn, java.lang.String text)  
      private java.sql.Connection getDerbyConnection()  
      private java.lang.String getDerbySchemaName​(java.lang.String schemaPrefix, java.lang.String foreignSchemaName)
      Get the name of the local Derby schema corresponding to a foreign schema name.
      private java.sql.Connection getForeignConnection​(java.lang.String connectionURL)  
      private java.sql.ResultSet getForeignTables​(java.sql.DatabaseMetaData foreignDBMD)
      Get a cursor through the user tables in the foreign database.
      void loadTool​(java.lang.String... configurationParameters)
      Creates a local Derby schema for every foreign schema which contains a user table.
      private java.lang.String mapType​(int jdbcType, int precision, int scale, java.lang.String foreignTypeName)
      Get the type of an external database's column as a Derby type name.
      private java.lang.String precisionAndScale​(int precision, int scale)
      Build a precision and scale designator.
      private java.lang.String precisionToLength​(int precision)
      Turns precision into a length designator.
      private java.sql.PreparedStatement prepareStatement​(java.sql.Connection conn, java.lang.String text)  
      private void registerForeignTable​(java.sql.DatabaseMetaData foreignDBMD, java.lang.String foreignSchemaName, java.lang.String foreignTableName, java.lang.String foreignConnectionURL, java.lang.String schemaPrefix, java.sql.Connection derbyConn)  
      private java.lang.String stringLiteral​(java.lang.String text)  
      void unloadTool​(java.lang.String... configurationParameters)
      Removes the schemas, table functions, and views created by loadTool().
      private java.sql.SQLException wrap​(java.lang.String errorMessage)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SAFE_DROP_SQLSTATES

        private static final java.lang.String[] SAFE_DROP_SQLSTATES
    • Constructor Detail

      • ForeignDBViews

        public ForeignDBViews()
        0-arg constructor required by the OptionalTool contract
    • Method Detail

      • loadTool

        public void loadTool​(java.lang.String... configurationParameters)
                      throws java.sql.SQLException

        Creates a local Derby schema for every foreign schema which contains a user table. Then creates a table function and convenience view for every user table found in the foreign database. The parameters to this method are:

        • foreignConnectionURL (required) - URL to connect to the foreign database
        • schemaPrefix (optional) - If not specified, then the local Derby schema which is created has the same name as the foreign schema. Otherwise, this prefix is prepended to the names of the local Derby schemas which are created.
        Specified by:
        loadTool in interface OptionalTool
        Throws:
        java.sql.SQLException
      • unloadTool

        public void unloadTool​(java.lang.String... configurationParameters)
                        throws java.sql.SQLException

        Removes the schemas, table functions, and views created by loadTool().

        • connectionURL (required) - URL to connect to the foreign database
        • schemaPrefix (optional) - See loadTool() for more information on this argument.
        Specified by:
        unloadTool in interface OptionalTool
        Throws:
        java.sql.SQLException
      • registerForeignTable

        private void registerForeignTable​(java.sql.DatabaseMetaData foreignDBMD,
                                          java.lang.String foreignSchemaName,
                                          java.lang.String foreignTableName,
                                          java.lang.String foreignConnectionURL,
                                          java.lang.String schemaPrefix,
                                          java.sql.Connection derbyConn)
                                   throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getForeignTables

        private java.sql.ResultSet getForeignTables​(java.sql.DatabaseMetaData foreignDBMD)
                                             throws java.sql.SQLException

        Get a cursor through the user tables in the foreign database.

        Throws:
        java.sql.SQLException
      • createDerbySchema

        private void createDerbySchema​(java.sql.Connection derbyConn,
                                       java.lang.String derbySchemaName)
                                throws java.sql.SQLException

        Create a Derby schema if it does not already exist.

        Throws:
        java.sql.SQLException
      • dropDerbySchema

        private void dropDerbySchema​(java.sql.Connection derbyConn,
                                     java.lang.String derbySchemaName)
                              throws java.sql.SQLException

        Drop a Derby schema.

        Throws:
        java.sql.SQLException
      • getDerbySchemaName

        private java.lang.String getDerbySchemaName​(java.lang.String schemaPrefix,
                                                    java.lang.String foreignSchemaName)

        Get the name of the local Derby schema corresponding to a foreign schema name. Returns null if the default (current) schema is to be used.

      • dotSeparatedSchemaName

        private java.lang.String dotSeparatedSchemaName​(java.lang.String rawName)

        Turn a Derby schema name into a schema name suitable for use in a dot-separated object name.

      • mapType

        private java.lang.String mapType​(int jdbcType,
                                         int precision,
                                         int scale,
                                         java.lang.String foreignTypeName)
                                  throws java.sql.SQLException

        Get the type of an external database's column as a Derby type name.

        Throws:
        java.sql.SQLException
      • precisionToLength

        private java.lang.String precisionToLength​(int precision)

        Turns precision into a length designator.

      • precisionAndScale

        private java.lang.String precisionAndScale​(int precision,
                                                   int scale)

        Build a precision and scale designator.

      • dropObject

        private void dropObject​(java.sql.Connection conn,
                                java.lang.String schemaName,
                                java.lang.String objectName,
                                java.lang.String objectType,
                                boolean restrict)
                         throws java.sql.SQLException

        Drop a schema object. If the object does not exist, silently swallow the error.

        Throws:
        java.sql.SQLException
      • getForeignConnection

        private java.sql.Connection getForeignConnection​(java.lang.String connectionURL)
                                                  throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getDerbyConnection

        private java.sql.Connection getDerbyConnection()
                                                throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • delimitedID

        private java.lang.String delimitedID​(java.lang.String text)
      • stringLiteral

        private java.lang.String stringLiteral​(java.lang.String text)
      • executeDDL

        private void executeDDL​(java.sql.Connection conn,
                                java.lang.String text)
                         throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • prepareStatement

        private java.sql.PreparedStatement prepareStatement​(java.sql.Connection conn,
                                                            java.lang.String text)
                                                     throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • wrap

        private java.sql.SQLException wrap​(java.lang.String errorMessage)