Class EPSGInstaller

java.lang.Object
org.apache.sis.internal.metadata.sql.ScriptRunner
org.apache.sis.referencing.factory.sql.EPSGInstaller
All Implemented Interfaces:
AutoCloseable

final class EPSGInstaller extends ScriptRunner
Runs the SQL scripts for creating an EPSG database. See org.apache.sis.referencing.factory.sql.epsg.DataScriptFormatter in the test directory for more information about how the scripts are formatted.
Since:
0.7
Version:
1.2
  • Field Details

    • REPLACE_STATEMENT

      static final String REPLACE_STATEMENT
      The pattern for an "UPDATE … SET … REPLACE" instruction. Example: Note: this regular expression use a capturing group.
      See Also:
    • replacePilcrow

      private final boolean replacePilcrow
      true if the Pilcrow character (¶ - decimal code 182) should be replaced by Line Feed (LF - decimal code 10). This is a possible workaround when the database does not support the REPLACE(column, CHAR(182), CHAR(10)) SQL statement, but accepts LF.
  • Constructor Details

    • EPSGInstaller

      public EPSGInstaller(Connection connection) throws SQLException
      Creates a new runner which will execute the statements using the given connection. The encoding is "ISO-8859-1", which is the encoding used for the files provided by EPSG.
      Parameters:
      connection - the connection to the database.
      Throws:
      SQLException - if an error occurred while executing a SQL statement.
  • Method Details

    • setSchema

      public void setSchema(String schema) throws SQLException, IOException
      Creates immediately a schema of the given name in the database and remember that the "epsg_" prefix in table names will need to be replaced by path to that schema.

      This method should be invoked only once. It does nothing if the database does not supports schema.

      Parameters:
      schema - the schema (usually "epsg").
      Throws:
      SQLException - if the schema cannot be created.
      IOException - if an I/O operation was required and failed.
    • prependNamespace

      final void prependNamespace(String schema)
      Prepends the given schema or catalog to all table names.
    • editText

      protected void editText(StringBuilder sql, int lower, int upper)
      Invoked for each text found in a SQL statement. This method replaces '' by Null. The intent is to consistently use the null value for meaning "no information", which is not the same than "information is an empty string". This replacement is okay in this particular case since there is no field in the EPSG database for which we really want an empty string.
      Overrides:
      editText in class ScriptRunner
      Parameters:
      sql - the whole SQL statement.
      lower - index of the first character of the text in sql.
      upper - index after the last character of the text in sql.
    • execute

      protected int execute(StringBuilder sql) throws SQLException, IOException
      Modifies the SQL statement before to execute it, or omit unsupported statements.
      Overrides:
      execute in class ScriptRunner
      Parameters:
      sql - the SQL statement to execute.
      Returns:
      the number of rows added or modified as a result of the statement execution.
      Throws:
      SQLException - if an error occurred while executing the SQL statement.
      IOException - if an I/O operation was required and failed.
    • run

      public void run(InstallationResources scriptProvider, Locale locale) throws SQLException, IOException
      Processes to the creation of the EPSG database using the SQL scripts from the given provider.
      Parameters:
      scriptProvider - user-provided scripts, or null for automatic lookup.
      locale - the locale for information or warning messages, if any.
      Throws:
      FileNotFoundException - if a SQL script has not been found.
      IOException - if another error occurred while reading an input.
      SQLException - if an error occurred while executing a SQL statement.
    • lookupProvider

      private static InstallationResources lookupProvider(Locale locale) throws IOException
      Searches for a SQL script provider on the classpath before to fallback on the default provider. The returned provider will be, in preference order:
      1. A provider from a publicly supported dependency such as sis-epsg.jar or sis-embedded.jar. Users have to put one of those dependencies in the classpath themselves. This action is interpreted as an acceptance of EPSG terms of use, so no license agreement window will popup.
      2. A provider using the SQL scripts in the $SIS_DATA/Databases/ExternalSources directory. Users have to put those scripts in that directory manually. This action is interpreted as an acceptance of EPSG terms of use, so no license agreement window will popup.
      3. A provider offering users to automatically download the data. Those providers are defined by sis-console and sis-javafx modules. Users must accept EPSG terms of use before the database can be installed.
      Parameters:
      locale - the locale for information or warning messages, if any.
      Throws:
      IOException
    • failure

      final String failure(Locale locale, Exception cause)
      Creates a message reporting the failure to create EPSG database. This method is invoked when EPSGFactory caught an exception. This method completes the exception message with the file name and line number where the error occurred, if such information is available.