Class BaseJdbcOutputStore

java.lang.Object
nz.org.riskscape.engine.defaults.data.jdbc.BaseJdbcOutputStore
Direct Known Subclasses:
GeoPackageOutputStore

public abstract class BaseJdbcOutputStore extends Object

Base class for output formats that write to a JDBC database.

  • Field Details

    • DEFAULT_BATCH_INSERT_SIZE

      public static final int DEFAULT_BATCH_INSERT_SIZE

      The default number of insert statements that will be committed as a batch by the RiskscapeWriter.

      See Also:
    • PROBLEMS

      public static final BaseJdbcOutputStore.LocalProblems PROBLEMS
    • ds

      protected final DataSource ds
    • replaceExistingTables

      protected final boolean replaceExistingTables
  • Constructor Details

    • BaseJdbcOutputStore

      public BaseJdbcOutputStore(DataSource ds, boolean replaceExistingTables)
  • Method Details

    • writerFor

      public nz.org.riskscape.problem.ResultOrProblems<BaseJdbcOutputStore.JdbcRiskscapeWriter> writerFor(nz.org.riskscape.engine.types.Struct type, String name)

      Get a writer that can write the given Struct.

      Parameters:
      type - the type that the writer will write
      name - the desired table name to write tuples to
      Returns:
    • appendingWriterFor

      public nz.org.riskscape.problem.ResultOrProblems<BaseJdbcOutputStore.JdbcRiskscapeWriter> appendingWriterFor(nz.org.riskscape.engine.types.Struct type, String name)

      Return a writer for the given type and name that will append to existing tables should they exist and have the same shape.

      If the table already exists but with a different shape (schema) then BaseJdbcOutputStore.LocalProblems.cannotAppendTableStructureMismatch(String, String, String) will result.

      Note, that replaceExistingTables has no effect when appending.

    • doWriterFor

      protected nz.org.riskscape.problem.ResultOrProblems<BaseJdbcOutputStore.JdbcRiskscapeWriter> doWriterFor(nz.org.riskscape.engine.types.Struct type, String name, boolean appending)
    • getConnection

      protected Connection getConnection() throws SQLException

      Get a Connection that will be used to both create and then populate the table.

      Note that if this method is overridden to return a shared connection then createJdbcWriter(java.lang.String, java.util.List, java.sql.Connection, java.net.URI) should also be overridden to return a BaseJdbcOutputStore.JdbcRiskscapeWriter that will not close the shared connection. In this case the implementation will need to take other steps to ensure that the connection is closed.

      Throws:
      SQLException
    • createJdbcWriter

      protected BaseJdbcOutputStore.JdbcRiskscapeWriter createJdbcWriter(String tableName, List<BaseJdbcOutputStore.StructMappingToColumnMapping> mappings, Connection conn, URI storedAt) throws SQLException
      Throws:
      SQLException
    • urlEncode

      protected String urlEncode(String value)
    • quoteIdentifier

      protected String quoteIdentifier(String identifier, DatabaseMetaData metaData) throws SQLException

      Quotes a database identifier (table or column name) with the database's quote character, replacing any quote characters that might appear within the identifier itself with an underscore (_)

      Throws:
      SQLException
    • getTableURI

      public abstract URI getTableURI(String urlEncodedtableName)

      Get a URI that represents the given table at the JDBC data source. Will be returned from RiskscapeWriter.getStoredAt() when writing is complete.

      Parameters:
      urlEncodedtableName - the table that is being written with URL encoding
      Returns:
      URI representing the table
    • initDbIfNecessary

      protected void initDbIfNecessary(Connection conn) throws SQLException

      Allows implementations to initialize the DB with any format required tables should this be necessary.

      Throws:
      SQLException
    • toColumnMapping

      protected abstract nz.org.riskscape.problem.ResultOrProblems<BaseJdbcOutputStore.StructMappingToColumnMapping> toColumnMapping(nz.org.riskscape.engine.output.StructFlattener.StructMapping structMapping, nz.org.riskscape.engine.output.StructFlattener.StructMapping firstGeomMapping, Connection conn) throws nz.org.riskscape.problem.ProblemException

      Provide a BaseJdbcOutputStore.StructMappingToColumnMapping for the given structMapping.

      firstGeomMapping is also provided to allow implementations that only allow one spatial entry to map subsequent geometries, maybe to text WKT.

      Parameters:
      structMapping - the struct mapping to map
      firstGeomMapping - the first geometry mapping, if one exists
      Returns:
      a mapper
      Throws:
      nz.org.riskscape.problem.ProblemException
    • getExistingTableNames

      protected List<String> getExistingTableNames(DatabaseMetaData metaData) throws SQLException

      Get a list of names of the existing tables and views that exist in the DB.

      Parameters:
      metaData - DB metadata to find existing tables in
      Returns:
      list of existing table and view names
      Throws:
      SQLException - if database metadata cannot be obtained
    • deleteTable

      protected void deleteTable(String tableName, Connection conn) throws SQLException
      Throws:
      SQLException
    • runScript

      protected void runScript(InputStream is, Connection conn) throws IOException, SQLException

      Run the sql script content from the given InputStream.

      SQL statements may span multiple lines but must be terminated by a ';'. Comment lines starting with '--' are allowed.

      Throws:
      IOException
      SQLException
    • normalizeSridAndAxisOrder

      protected static org.locationtech.jts.geom.Geometry normalizeSridAndAxisOrder(org.locationtech.jts.geom.Geometry geom, int writeSRID, Optional<nz.org.riskscape.engine.output.AxisSwapper> axisSwapper)

      Returns a geometry with the correct SRID and axis order for insertion in to the database, or the original geometry if no change is required.

      These steps are performed on a copy of the input geometry to ensure that is it not altered as that could have bad affects should it be used in later RiskScape processing.

      Parameters:
      geom - geometry to prepare
      writeSRID - The SRID that the prepared geometry should have. Note that this is intended to be the database SRID (which usually matches the EPSG code) not the ID we put in the SRIDSet.
      axisSwapper - Axis swapper if required
      Returns:
      the prepared geometry