Class OracleEngine
- java.lang.Object
-
- com.feedzai.commons.sql.abstraction.engine.AbstractDatabaseEngine
-
- com.feedzai.commons.sql.abstraction.engine.impl.OracleEngine
-
- All Implemented Interfaces:
DatabaseEngine,AutoCloseable
public class OracleEngine extends AbstractDatabaseEngine
Oracle specific database implementation.- Since:
- 2.0.0
- Author:
- Rui Vilao (rui.vilao@feedzai.com)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceOracleEngine.Action0Represents an action that can throw any exception.
-
Field Summary
Fields Modifier and Type Field Description static StringFOREIGN_ALREADY_EXISTSForeign key already existsstatic StringNAME_ALREADY_EXISTSName is already used by an existing object.protected static StringORACLE_DRIVERThe Oracle JDBC driver.static QueryExceptionHandlerORACLE_QUERY_EXCEPTION_HANDLERAn instance ofQueryExceptionHandlerspecific for Oracle engine, to be used in disambiguating SQL exceptions.static StringSEQUENCE_DOES_NOT_EXISTSequence does not exist.static StringTABLE_CAN_ONLY_HAVE_ONE_PRIMARY_KEYTable can have only one primary key.static StringTABLE_OR_VIEW_DOES_NOT_EXISTTable or view does not exist.-
Fields inherited from class com.feedzai.commons.sql.abstraction.engine.AbstractDatabaseEngine
conn, currentSchema, DEFAULT_QUERY_EXCEPTION_HANDLER, dev, dialect, eh, entities, injector, logger, notificationLogger, properties, socketTimeoutExecutor, stmts, translator
-
-
Constructor Summary
Constructors Constructor Description OracleEngine(PdbProperties properties)Creates a new Oracle connection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddColumn(DbEntity entity, DbColumn... columns)Adds the column to an existent table.protected voidaddFks(DbEntity entity, Set<DbFk> fks)Adds foreign key constraints to the table in the database that the provided entity represents.protected voidaddIndexes(DbEntity entity)Add the desired indexes.protected voidaddPrimaryKey(DbEntity entity)Add a primary key to the entity.protected voidaddSequences(DbEntity entity)Adds the necessary sequences.protected voidconnect()Connects to the database.voidcreatePreparedStatement(String name, Expression query)Creates a prepared statement.protected MappedEntitycreatePreparedStatementForInserts(DbEntity entity)Creates and gets the prepared statement that will be used for insertions.protected ResultIteratorcreateResultIterator(PreparedStatement ps)Creates a specificResultIteratorfor the engine in place given a prepared statement.protected ResultIteratorcreateResultIterator(Statement statement, String sql)Creates a specificResultIteratorgiven the engine implementation.protected voidcreateTable(DbEntity entity)Creates the table.protected longdoPersist(PreparedStatement ps, MappedEntity me, boolean useAutoInc, int lastBindPosition)DB engine specific auxiliary method forAbstractDatabaseEngine.persist(String, EntityEntry, boolean)to effectively perform the persist action.protected voiddropColumn(DbEntity entity, String... columns)Drops the column.protected voiddropSequences(DbEntity entity)Drops the sequences of the entity.protected voiddropTable(DbEntity entity)Drops the table.protected intentityToPreparedStatement(DbEntity entity, PreparedStatement originalPs, EntityEntry entry, boolean useAutoInc)Translates the given entry entity to the prepared statement.protected intentityToPreparedStatementForBatch(DbEntity entity, PreparedStatement originalPs, EntityEntry entry, boolean useAutoInc)Translates the given entry entity to the prepared statement when used in the context of batch updates.voidflush()Flushes the batches for all the registered entities.protected PropertiesgetDBProperties()Get the properties to be passed to the database connection that will be created.protected ResultSetgetImportedKeys(String entityName)Gets a description of the columns that are referenced by the given table's foreign key columns.Map<String,DbColumnType>getMetadata(String schemaPattern, String tableNamePattern)Gets the table metadata (table must be in the current schema).protected PreparedStatementgetPreparedStatementForPersist(boolean useAutoInc, MappedEntity mappedEntity)Gets thePreparedStatementto use in a persist operation, depending on whether autoInc is to be used or not.protected QueryExceptionHandlergetQueryExceptionHandler()Gets the instance ofQueryExceptionHandlerto be used in disambiguating SQL exceptions.Class<? extends AbstractTranslator>getTranslatorClass()Gets the class that translates SQL bound to this engine.booleanisStringAggDistinctCapable()Checks if the engine supports using DISTINCT inside a string aggregation.voidsetParameter(String name, int index, Object param)Sets the parameter on the specified index.voidsetParameters(String name, Object... params)Sets the parameters on the specified prepared statement.protected voidsetParameterValues(PreparedStatement ps, int index, Object param)Sets the value of a parameter inindexto the value provided inparam.protected voidsetTransactionIsolation()OverridesAbstractDatabaseEngine.setTransactionIsolation()This is because Oracle does not support READ_UNCOMMITTED e REPEATABLE_READ.-
Methods inherited from class com.feedzai.commons.sql.abstraction.engine.AbstractDatabaseEngine
addBatch, addEntity, addFks, beginTransaction, checkConnection, checkConnection, checkConnection, clearParameters, close, commentCharacter, commit, containsEntity, createBatch, createBatch, createBatch, createBatch, createPreparedStatement, createPreparedStatement, createPreparedStatement, createSelectStatement, dropEntity, dropEntity, dropFkQuery, dropFks, dropFks, dropView, duplicate, escapeCharacter, executePS, executePSUpdate, executeUpdate, executeUpdate, executeUpdateSilently, getConnection, getDialect, getEntities, getEntities, getFinalJdbcConnection, getMetadata, getPrivateKey, getProperties, getPSIterator, getPSIterator, getPSResultSet, getQueryMetadata, getQueryMetadata, getSchema, handleOperation, hasIdentityColumn, inject, isTransactionActive, iterator, iterator, iterator, iterator, iterator, iterator, loadEntity, objectToArray, persist, persist, preparedStatementExists, processResultIterator, query, query, query, query, reconnectExceptionally, removeEntity, removePreparedStatement, rollback, setExceptionHandler, setParameter, setSchema, toPdbType, translate, translateType, updateEntity
-
-
-
-
Field Detail
-
ORACLE_DRIVER
protected static final String ORACLE_DRIVER
The Oracle JDBC driver.
-
NAME_ALREADY_EXISTS
public static final String NAME_ALREADY_EXISTS
Name is already used by an existing object.- See Also:
- Constant Field Values
-
TABLE_CAN_ONLY_HAVE_ONE_PRIMARY_KEY
public static final String TABLE_CAN_ONLY_HAVE_ONE_PRIMARY_KEY
Table can have only one primary key.- See Also:
- Constant Field Values
-
SEQUENCE_DOES_NOT_EXIST
public static final String SEQUENCE_DOES_NOT_EXIST
Sequence does not exist.- See Also:
- Constant Field Values
-
TABLE_OR_VIEW_DOES_NOT_EXIST
public static final String TABLE_OR_VIEW_DOES_NOT_EXIST
Table or view does not exist.- See Also:
- Constant Field Values
-
FOREIGN_ALREADY_EXISTS
public static final String FOREIGN_ALREADY_EXISTS
Foreign key already exists- See Also:
- Constant Field Values
-
ORACLE_QUERY_EXCEPTION_HANDLER
public static final QueryExceptionHandler ORACLE_QUERY_EXCEPTION_HANDLER
An instance ofQueryExceptionHandlerspecific for Oracle engine, to be used in disambiguating SQL exceptions.- Since:
- 2.5.1
-
-
Constructor Detail
-
OracleEngine
public OracleEngine(PdbProperties properties) throws DatabaseEngineException
Creates a new Oracle connection.- Parameters:
properties- The properties for the database connection.- Throws:
DatabaseEngineException- When the connection fails.
-
-
Method Detail
-
getDBProperties
protected Properties getDBProperties()
Description copied from class:AbstractDatabaseEngineGet the properties to be passed to the database connection that will be created. Since these properties are specific to the database engine, it is up to theAbstractDatabaseEngineimplementation to specify them. This is used, for instance, to set the database connection timeout.- Overrides:
getDBPropertiesin classAbstractDatabaseEngine- Returns:
- the properties of the connection to the database.
-
connect
protected void connect() throws ExceptionDescription copied from class:AbstractDatabaseEngineConnects to the database.- Overrides:
connectin classAbstractDatabaseEngine- Throws:
Exception- If connection is not possible, or failed to decrypt username/password if encryption was provided.
-
setParameters
public void setParameters(String name, Object... params) throws DatabaseEngineException, ConnectionResetException
Description copied from interface:DatabaseEngineSets the parameters on the specified prepared statement.- Specified by:
setParametersin interfaceDatabaseEngine- Overrides:
setParametersin classAbstractDatabaseEngine- Parameters:
name- The prepared statement name.params- The parameters to set.- Throws:
DatabaseEngineException- If something occurs setting the parameters.ConnectionResetException- If the connection was reset while trying to set parameters.
-
setParameter
public void setParameter(String name, int index, Object param) throws DatabaseEngineException, ConnectionResetException
Description copied from interface:DatabaseEngineSets the parameter on the specified index.- Specified by:
setParameterin interfaceDatabaseEngine- Overrides:
setParameterin classAbstractDatabaseEngine- Parameters:
name- The prepared statement name.index- The index to set.param- The parameter to set.- Throws:
DatabaseEngineException- If something occurs setting the parameters.ConnectionResetException- If the connection was reset while trying to set the parameter.
-
isStringAggDistinctCapable
public boolean isStringAggDistinctCapable()
Description copied from interface:DatabaseEngineChecks if the engine supports using DISTINCT inside a string aggregation.- Returns:
- true if the engine supports using DISTINCT inside a string aggregation, false otherwise.
-
flush
public void flush() throws DatabaseEngineExceptionDescription copied from class:AbstractDatabaseEngineFlushes the batches for all the registered entities.- Specified by:
flushin interfaceDatabaseEngine- Overrides:
flushin classAbstractDatabaseEngine- Throws:
DatabaseEngineException- If something goes wrong while persisting data.
-
entityToPreparedStatementForBatch
protected int entityToPreparedStatementForBatch(DbEntity entity, PreparedStatement originalPs, EntityEntry entry, boolean useAutoInc) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineTranslates the given entry entity to the prepared statement when used in the context of batch updates. This is to be overriden on engines where a distinct treatment is required for these situations.- Overrides:
entityToPreparedStatementForBatchin classAbstractDatabaseEngine- Parameters:
entity- The entity.originalPs- The prepared statement.entry- The entry.- Returns:
- The position (1-based) of the last bind parameter that was filled in a prepared statement.
- Throws:
DatabaseEngineException- if something occurs during the translation.
-
entityToPreparedStatement
protected int entityToPreparedStatement(DbEntity entity, PreparedStatement originalPs, EntityEntry entry, boolean useAutoInc) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineTranslates the given entry entity to the prepared statement.- Specified by:
entityToPreparedStatementin classAbstractDatabaseEngine- Parameters:
entity- The entity.originalPs- The prepared statement.entry- The entry.- Returns:
- The position (1-based) of the last bind parameter that was filled in a prepared statement.
- Throws:
DatabaseEngineException- if something occurs during the translation.
-
setParameterValues
protected void setParameterValues(PreparedStatement ps, int index, Object param) throws SQLException
Description copied from class:AbstractDatabaseEngineSets the value of a parameter inindexto the value provided inparam.- Overrides:
setParameterValuesin classAbstractDatabaseEngine- Parameters:
ps- ThePreparedStatementto insert the association between index and the param.index- The index to set the value to.param- The value to be set at the provided index.- Throws:
SQLException- If something goes wrong accessing the database.
-
setTransactionIsolation
protected void setTransactionIsolation() throws SQLExceptionOverridesAbstractDatabaseEngine.setTransactionIsolation()This is because Oracle does not support READ_UNCOMMITTED e REPEATABLE_READ.- Overrides:
setTransactionIsolationin classAbstractDatabaseEngine- Throws:
SQLException- If a database access error occurs.
-
getTranslatorClass
public Class<? extends AbstractTranslator> getTranslatorClass()
Description copied from class:AbstractDatabaseEngineGets the class that translates SQL bound to this engine.- Specified by:
getTranslatorClassin classAbstractDatabaseEngine- Returns:
- The class that translates SQL bound to this engine.
-
createTable
protected void createTable(DbEntity entity) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineCreates the table.- Specified by:
createTablein classAbstractDatabaseEngine- Parameters:
entity- The entity.- Throws:
DatabaseEngineException- If something goes wrong while creating the table.
-
addPrimaryKey
protected void addPrimaryKey(DbEntity entity) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineAdd a primary key to the entity.- Specified by:
addPrimaryKeyin classAbstractDatabaseEngine- Parameters:
entity- The entity.- Throws:
DatabaseEngineException- If something goes wrong while creating the table.
-
addIndexes
protected void addIndexes(DbEntity entity) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineAdd the desired indexes.- Specified by:
addIndexesin classAbstractDatabaseEngine- Parameters:
entity- The entity.- Throws:
DatabaseEngineException- If something goes wrong while creating the table.
-
addSequences
protected void addSequences(DbEntity entity) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineAdds the necessary sequences.- Specified by:
addSequencesin classAbstractDatabaseEngine- Parameters:
entity- The entity.- Throws:
DatabaseEngineException- If something goes wrong while creating the table.
-
createPreparedStatementForInserts
protected MappedEntity createPreparedStatementForInserts(DbEntity entity) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineCreates and gets the prepared statement that will be used for insertions.- Specified by:
createPreparedStatementForInsertsin classAbstractDatabaseEngine- Parameters:
entity- The entity.- Throws:
DatabaseEngineException- If something goes wrong while creating the table.
-
dropSequences
protected void dropSequences(DbEntity entity) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineDrops the sequences of the entity.- Specified by:
dropSequencesin classAbstractDatabaseEngine- Parameters:
entity- The entity.- Throws:
DatabaseEngineException- If something goes wrong dropping the sequences.
-
dropTable
protected void dropTable(DbEntity entity) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineDrops the table.- Specified by:
dropTablein classAbstractDatabaseEngine- Parameters:
entity- The entity.- Throws:
DatabaseEngineException- If something goes wrong dropping the sequences.
-
dropColumn
protected void dropColumn(DbEntity entity, String... columns) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineDrops the column.- Specified by:
dropColumnin classAbstractDatabaseEngine- Parameters:
entity- The entity.columns- The column name to drop.- Throws:
DatabaseEngineException- If something goes wrong dropping the sequences.
-
addColumn
protected void addColumn(DbEntity entity, DbColumn... columns) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineAdds the column to an existent table.- Specified by:
addColumnin classAbstractDatabaseEngine- Parameters:
entity- The entity that represents the table.columns- The db column to add.- Throws:
DatabaseEngineException- If something goes wrong adding columns.
-
createPreparedStatement
public void createPreparedStatement(String name, Expression query) throws NameAlreadyExistsException, DatabaseEngineException
Description copied from interface:DatabaseEngineCreates a prepared statement.- Specified by:
createPreparedStatementin interfaceDatabaseEngine- Overrides:
createPreparedStatementin classAbstractDatabaseEngine- Parameters:
name- The prepared statement name.query- The query.- Throws:
NameAlreadyExistsException- If the name already exists.DatabaseEngineException- If something goes wrong creating the statement.
-
getPreparedStatementForPersist
protected PreparedStatement getPreparedStatementForPersist(boolean useAutoInc, MappedEntity mappedEntity)
Description copied from class:AbstractDatabaseEngineGets thePreparedStatementto use in a persist operation, depending on whether autoInc is to be used or not.- Overrides:
getPreparedStatementForPersistin classAbstractDatabaseEngine- Parameters:
useAutoInc- Whether to use autoInc.mappedEntity- The mapped entity for which to get the prepared statement.- Returns:
- the
PreparedStatement.
-
doPersist
protected long doPersist(PreparedStatement ps, MappedEntity me, boolean useAutoInc, int lastBindPosition) throws Exception
Description copied from class:AbstractDatabaseEngineDB engine specific auxiliary method forAbstractDatabaseEngine.persist(String, EntityEntry, boolean)to effectively perform the persist action.- Specified by:
doPersistin classAbstractDatabaseEngine- Parameters:
ps- ThePreparedStatementto use in the persist operationme- The mapped entity on which to persist.useAutoInc- Whether to use autoInc.lastBindPosition- The position (1-based) of the last bind parameter that was filled in the prepared statement.- Returns:
- The ID of the auto generated value (
0if there's no auto generated value). If the table has more than 1 column with auto generated values, then it will return the first column found. - Throws:
Exception- if any problem occurs while persisting.
-
addFks
protected void addFks(DbEntity entity, Set<DbFk> fks) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineAdds foreign key constraints to the table in the database that the provided entity represents.- Overrides:
addFksin classAbstractDatabaseEngine- Parameters:
entity- The entity.fks- The foreign keys to be added.- Throws:
DatabaseEngineException- If something goes wrong creating the FKs.
-
getImportedKeys
protected ResultSet getImportedKeys(String entityName) throws SQLException
Description copied from class:AbstractDatabaseEngineGets a description of the columns that are referenced by the given table's foreign key columns.- Overrides:
getImportedKeysin classAbstractDatabaseEngine- Parameters:
entityName- The entity name.- Returns:
- A
ResultSetcontaining information about columns referenced by a foreign key. - Throws:
SQLException- If a database access error occurs when getting key information.
-
getMetadata
public Map<String,DbColumnType> getMetadata(String schemaPattern, String tableNamePattern) throws DatabaseEngineException
Description copied from interface:DatabaseEngineGets the table metadata (table must be in the current schema).- Specified by:
getMetadatain interfaceDatabaseEngine- Overrides:
getMetadatain classAbstractDatabaseEngine- Parameters:
schemaPattern- A schema name pattern; must match the schema name as it is stored in the database;""retrieves those without a schema;nullmeans that the schema name should not be used to narrow the search.tableNamePattern- A table name pattern; must match the table name as it is stored in the database.- Returns:
- A representation of the table columns and types.
- Throws:
DatabaseEngineException- If something occurs getting the metadata.
-
createResultIterator
protected ResultIterator createResultIterator(Statement statement, String sql) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineCreates a specificResultIteratorgiven the engine implementation.- Specified by:
createResultIteratorin classAbstractDatabaseEngine- Parameters:
statement- The statement.sql- The SQL sentence.- Returns:
- An iterator for the specific engine.
- Throws:
DatabaseEngineException- If a database access error occurs.
-
createResultIterator
protected ResultIterator createResultIterator(PreparedStatement ps) throws DatabaseEngineException
Description copied from class:AbstractDatabaseEngineCreates a specificResultIteratorfor the engine in place given a prepared statement.- Specified by:
createResultIteratorin classAbstractDatabaseEngine- Parameters:
ps- The prepared statement.- Returns:
- The result iterator.
- Throws:
DatabaseEngineException- If a database access error occurs.
-
getQueryExceptionHandler
protected QueryExceptionHandler getQueryExceptionHandler()
Description copied from class:AbstractDatabaseEngineGets the instance ofQueryExceptionHandlerto be used in disambiguating SQL exceptions.- Overrides:
getQueryExceptionHandlerin classAbstractDatabaseEngine- Returns:
- the
QueryExceptionHandler.
-
-