Class BaseJdbcOutputStore
- Direct Known Subclasses:
GeoPackageOutputStore
Base class for output formats that write to a JDBC database.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassstatic interfacestatic classData to allow the mapping of aStructFlattener.StructMappingto a DB column. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default number of insert statements that will be committed as a batch by theRiskscapeWriter.protected final DataSourcestatic final BaseJdbcOutputStore.LocalProblemsprotected final boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionnz.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.protected BaseJdbcOutputStore.JdbcRiskscapeWritercreateJdbcWriter(String tableName, List<BaseJdbcOutputStore.StructMappingToColumnMapping> mappings, Connection conn, URI storedAt) protected voiddeleteTable(String tableName, Connection conn) protected nz.org.riskscape.problem.ResultOrProblems<BaseJdbcOutputStore.JdbcRiskscapeWriter>doWriterFor(nz.org.riskscape.engine.types.Struct type, String name, boolean appending) protected ConnectionGet aConnectionthat will be used to both create and then populate the table.getExistingTableNames(DatabaseMetaData metaData) Get a list of names of the existing tables and views that exist in the DB.abstract URIgetTableURI(String urlEncodedtableName) Get a URI that represents the given table at the JDBC data source.protected voidinitDbIfNecessary(Connection conn) Allows implementations to initialize the DB with any format required tables should this be necessary.protected voidpostTableCreate(String tableName, nz.org.riskscape.engine.output.StructFlattener.StructMapping firstGeomMapping, Connection conn) Allow implementations to perform any other actions that may be required now that a feature table has been created.protected org.locationtech.jts.geom.GeometryprepareGeometry(org.locationtech.jts.geom.Geometry geom, int writeSRID, Optional<nz.org.riskscape.engine.output.AxisSwapper> axisSwapper) Prepare aGeometryfor writing, which means to swap the axis if necessary and set the writeSRID.protected StringquoteIdentifier(String identifier, DatabaseMetaData metaData) 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 (_)protected voidrunScript(InputStream is, Connection conn) Run the sql script content from the givenInputStream.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) Provide aBaseJdbcOutputStore.StructMappingToColumnMappingfor the given structMapping.protected Stringnz.org.riskscape.problem.ResultOrProblems<BaseJdbcOutputStore.JdbcRiskscapeWriter>Get a writer that can write the givenStruct.
-
Field Details
-
DEFAULT_BATCH_INSERT_SIZE
public static final int DEFAULT_BATCH_INSERT_SIZEThe default number of insert statements that will be committed as a batch by the
RiskscapeWriter.- See Also:
-
PROBLEMS
-
ds
-
replaceExistingTables
protected final boolean replaceExistingTables
-
-
Constructor Details
-
BaseJdbcOutputStore
-
-
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 writename- 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
replaceExistingTableshas 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
Get a
Connectionthat 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 aBaseJdbcOutputStore.JdbcRiskscapeWriterthat 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
-
quoteIdentifier
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
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
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.StructMappingToColumnMappingfor 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 mapfirstGeomMapping- the first geometry mapping, if one exists- Returns:
- a mapper
- Throws:
nz.org.riskscape.problem.ProblemException
-
postTableCreate
protected void postTableCreate(String tableName, nz.org.riskscape.engine.output.StructFlattener.StructMapping firstGeomMapping, Connection conn) throws SQLException, nz.org.riskscape.problem.ProblemException Allow implementations to perform any other actions that may be required now that a feature table has been created. This could be to register the table such as a GeoPackage will register a table in 'gpkgcontents' and 'gpkggeometry_columns' for a geometry column.
- Parameters:
tableName- the name of the table that has been createdfirstGeomMapping- the first geometry mapping from that table, or null if none existconn- a connection that can be used for further sql statements.- Throws:
SQLExceptionnz.org.riskscape.problem.ProblemException
-
getExistingTableNames
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
- Throws:
SQLException
-
runScript
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:
IOExceptionSQLException
-
prepareGeometry
protected org.locationtech.jts.geom.Geometry prepareGeometry(org.locationtech.jts.geom.Geometry geom, int writeSRID, Optional<nz.org.riskscape.engine.output.AxisSwapper> axisSwapper) Prepare a
Geometryfor writing, which means to swap the axis if necessary and set the writeSRID.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 preparewriteSRID- The SRID that the prepared geometry should haveaxisSwapper- Axis swapper if required- Returns:
- the prepared geometry
-