public abstract class AbstractDatabaseEngine extends Object implements DatabaseEngine
Modifier and Type | Field and Description |
---|---|
protected Connection |
conn
The database connection.
|
protected static org.slf4j.Marker |
dev
The dev Marker.
|
protected Dialect |
diaclect
The dialect.
|
protected ExceptionHandler |
eh
The exception handler to control the flow when defining new entities.
|
protected Map<String,MappedEntity> |
entities
Map of entities.
|
protected com.google.inject.Injector |
injector
The Guice injector.
|
protected org.slf4j.Logger |
logger
The logger that will be used.
|
protected org.slf4j.Logger |
notificationLogger
The notification logger for administration.
|
protected PdbProperties |
properties
The configuration.
|
protected Map<String,PreparedStatementCapsule> |
stmts
Map of prepared statements.
|
protected AbstractTranslator |
translator
The translator in place.
|
Constructor and Description |
---|
AbstractDatabaseEngine(String driver,
PdbProperties properties,
Dialect dialect)
Creates a new instance of
AbstractDatabaseEngine . |
Modifier and Type | Method and Description |
---|---|
void |
addBatch(String name,
EntityEntry entry)
Add an entry to the batch.
|
protected abstract void |
addColumn(DbEntity entity,
DbColumn... columns)
Adds the column to an existent table.
|
void |
addEntity(DbEntity entity)
Adds an entity to the engine.
|
protected abstract void |
addFks(DbEntity entity)
Adds the FKs.
|
protected abstract void |
addIndexes(DbEntity entity)
Add the desired indexes.
|
protected abstract void |
addPrimaryKey(DbEntity entity)
Add a primary key to the entity.
|
protected abstract void |
addSequences(DbEntity entity)
Adds the necessary sequences.
|
void |
beginTransaction()
Starts a transaction.
|
boolean |
checkConnection()
Checks if the connection is alive.
|
boolean |
checkConnection(boolean forceReconnect)
Checks if the connection is alive.
|
protected abstract boolean |
checkConnection(Connection conn)
Checks if the connection is alive.
|
void |
clearParameters(String name)
Clears the prepared statement parameters.
|
void |
close()
Closes the connection to the database.
|
String |
commentCharacter() |
void |
commit()
Commits the current transaction.
|
protected void |
connect()
Connects to the database.
|
boolean |
containsEntity(String name)
Returns if the current engine contains the entity.
|
AbstractBatch |
createBatch(int batchSize,
long batchTimeout)
Creates a new batch that periodically flushes a batch.
|
AbstractBatch |
createBatch(int batchSize,
long batchTimeout,
String batchName)
Creates a new batch that periodically flushes a batch.
|
void |
createPreparedStatement(String name,
Expression query)
Creates a prepared statement.
|
void |
createPreparedStatement(String name,
Expression query,
int timeout)
Creates a prepared statement.
|
void |
createPreparedStatement(String name,
String query)
Creates a prepared statement.
|
void |
createPreparedStatement(String name,
String query,
int timeout)
Creates a prepared statement.
|
protected abstract MappedEntity |
createPreparedStatementForInserts(DbEntity entity)
Creates and gets the prepared statement that will be used for insertions.
|
protected abstract ResultIterator |
createResultIterator(PreparedStatement ps)
Creates a specific
ResultIterator for the engine in place given given prepared statement. |
protected abstract ResultIterator |
createResultIterator(Statement statement,
String sql)
Creates a specific
ResultIterator given the engine implementation. |
protected abstract void |
createTable(DbEntity entity)
Creates the table.
|
protected abstract void |
dropColumn(DbEntity entity,
String... columns)
Drops the column.
|
void |
dropEntity(DbEntity entity)
Drops everything that belongs to the entity.
|
void |
dropEntity(String entity)
Drops an entity.
|
protected void |
dropFks(String table)
Drops this table foreign keys.
|
protected abstract void |
dropSequences(DbEntity entity)
Drops the sequences of the entity.
|
protected abstract void |
dropTable(DbEntity entity)
Drops the table.
|
DatabaseEngine |
duplicate(Properties mergeProperties,
boolean copyEntities)
Duplicates a connection.
|
protected abstract int |
entityToPreparedStatement(DbEntity entity,
PreparedStatement ps,
EntityEntry entry,
boolean useAutoIncs)
Translates the given entry entity to the prepared statement.
|
String |
escapeCharacter() |
void |
executePS(String name)
Executes the specified prepared statement.
|
Integer |
executePSUpdate(String name)
Executes update on the specified prepared statement.
|
int |
executeUpdate(Expression query)
Executes the given update.
|
int |
executeUpdate(String query)
Executes a native query.
|
protected void |
executeUpdateSilently(String statement)
Executes the given statement.
|
void |
flush()
Flushes the batches for all the registered entities.
|
Connection |
getConnection()
Checks if the connection is available and returns it.
|
Dialect |
getDialect()
Gets the dialect being used.
|
Map<String,DbEntityType> |
getEntities()
Gets the database entities.
|
protected String |
getFinalJdbcConnection(String jdbc)
Gets the final JDBC connection.
|
Map<String,DbColumnType> |
getMetadata(String name)
Gets the table metadata.
|
PdbProperties |
getProperties()
Gets the properties in use.
|
ResultIterator |
getPSIterator(String name)
Creates an iterator for the
PreparedStatement bound to the given name. |
List<Map<String,ResultColumn>> |
getPSResultSet(String name)
Gets the result set of the specified prepared statement.
|
Map<String,DbColumnType> |
getQueryMetadata(Expression query)
Gets the query metadata.
|
Map<String,DbColumnType> |
getQueryMetadata(String query)
Gets the query metadata.
|
protected String |
getSchema()
Gets the schema.
|
abstract Class<? extends AbstractTranslator> |
getTranslatorClass()
Gets the class that translates SQL bound to this engine.
|
protected void |
handleOperation(OperationFault op,
Exception e)
Controls if the given faulty operation must be silenced or not.
|
boolean |
hasIdentityColumn(DbEntity entity)
Check if the entity has an identity column.
|
protected void |
inject(Expression... objs) |
boolean |
isTransactionActive()
Checks if a transaction is active.
|
ResultIterator |
iterator(Expression query)
Creates an iterator for the given SQL expression.
|
ResultIterator |
iterator(String query)
Creates an iterator for the given SQL sentence.
|
protected byte[] |
objectToArray(Object val)
Converts an object to byte array.
|
abstract Long |
persist(String name,
EntityEntry entry)
Persists a given entry.
|
abstract Long |
persist(String name,
EntityEntry entry,
boolean useAutoInc)
Persists a given entry.
|
boolean |
preparedStatementExists(String name)
Checks if there's a prepared statement with the given name.
|
protected List<Map<String,ResultColumn>> |
processResultIterator(ResultIterator it)
Process a whole
ResultIterator . |
List<Map<String,ResultColumn>> |
query(Expression query)
Executes the given query.
|
List<Map<String,ResultColumn>> |
query(String query)
Executes the given query.
|
DbEntity |
removeEntity(String name)
Removes the entity given the name.
|
void |
removePreparedStatement(String name)
Removes the given prepared statement.
|
void |
rollback()
Rolls back a transaction.
|
void |
setExceptionHandler(ExceptionHandler eh)
Sets the given exception handler in the engine.
|
void |
setParameter(String name,
int index,
Object param)
Sets the parameter on the specified index.
|
void |
setParameters(String name,
Object... params)
Sets the parameters on the specified prepared statement.
|
protected void |
setTransactionIsolation()
Sets the transaction isolation level.
|
protected DbColumnType |
toPdbType(int type)
Maps the database type to
DbColumnType . |
String |
translate(Expression query)
Translates the given expression to the current dialect.
|
protected abstract String |
translateType(DbColumn c)
Translates the type present in the given column.
|
void |
updateEntity(DbEntity entity)
Updates an entity in the engine.
|
@Inject protected com.google.inject.Injector injector
@Inject protected AbstractTranslator translator
protected Connection conn
protected static final org.slf4j.Marker dev
protected org.slf4j.Logger logger
protected org.slf4j.Logger notificationLogger
protected final Map<String,MappedEntity> entities
protected final Map<String,PreparedStatementCapsule> stmts
protected final PdbProperties properties
protected final Dialect diaclect
protected ExceptionHandler eh
public AbstractDatabaseEngine(String driver, PdbProperties properties, Dialect dialect) throws DatabaseEngineException
AbstractDatabaseEngine
.driver
- The driver to connect to the database.properties
- The properties of the connection.dialect
- The dialect in use.DatabaseEngineException
- When errors occur trying to set up the database connection.protected void connect() throws Exception
Exception
- If connection is not possible, or failed to decrypt username/password if encryption was provided.protected String getFinalJdbcConnection(String jdbc)
jdbc
- The current JDBC connection.protected void setTransactionIsolation() throws SQLException
SQLException
- If a database access error occurs.public abstract Class<? extends AbstractTranslator> getTranslatorClass()
public Connection getConnection() throws RetryLimitExceededException, InterruptedException, RecoveryException
getConnection
in interface DatabaseEngine
RetryLimitExceededException
- If the retry limit is exceeded.InterruptedException
- If the thread is interrupted during reconnection.RecoveryException
public void close()
close
in interface DatabaseEngine
public void beginTransaction() throws DatabaseEngineRuntimeException
Connection.getAutoCommit()
).beginTransaction
in interface DatabaseEngine
DatabaseEngineException
- If something goes wrong while persisting data.DatabaseEngineRuntimeException
public void addEntity(DbEntity entity) throws DatabaseEngineException
addEntity
in interface DatabaseEngine
entity
- The entity to add.DatabaseEngineException
- If something goes wrong while creating the structures.public void updateEntity(DbEntity entity) throws DatabaseEngineException
Updates an entity in the engine.
If the entity does not exist in the instance, the method addEntity(com.feedzai.commons.sql.abstraction.ddl.DbEntity)
will be invoked.
The engine will compare the entity with the getMetadata(String)
information and update the schema of the table.
ATTENTION: This method will only add new columns or drop removed columns in the database table. It will also drop and create foreign keys. Primary Keys, Indexes and column types changes will not be updated.
updateEntity
in interface DatabaseEngine
entity
- The entity to update.DatabaseEngineException
public DbEntity removeEntity(String name)
DatabaseEngine
removeEntity
in interface DatabaseEngine
name
- The name of the entity to remove.public boolean containsEntity(String name)
DatabaseEngine
containsEntity
in interface DatabaseEngine
name
- The entity name.public void dropEntity(String entity) throws DatabaseEngineException
dropEntity
in interface DatabaseEngine
entity
- The entity name.DatabaseEngineException
public void dropEntity(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong while dropping the structures.public abstract Long persist(String name, EntityEntry entry) throws DatabaseEngineException
Persists a given entry. Persisting a query implies executing the statement.
If you are inside of an explicit transaction, changes will only be visible upon explicit commit, otherwise a commit will immediately take place.
persist
in interface DatabaseEngine
name
- The entity name.entry
- The entry to persist.DatabaseEngineException
- If something goes wrong while persisting data.public abstract Long persist(String name, EntityEntry entry, boolean useAutoInc) throws DatabaseEngineException
Persists a given entry. Persisting a query implies executing the statement. If define useAutoInc as false, PDB will disable the auto increments for the current insert and advance the sequences if needed.
If you are inside of an explicit transaction, changes will only be visible upon explicit commit, otherwise a commit will immediately take place.
persist
in interface DatabaseEngine
name
- The entity name.entry
- The entry to persist.useAutoInc
- Use or not the autoinc.DatabaseEngineException
- If something goes wrong while persisting data.public void flush() throws DatabaseEngineException
flush
in interface DatabaseEngine
DatabaseEngineException
- If something goes wrong while persisting data.public void commit() throws DatabaseEngineRuntimeException
beginTransaction()
.commit
in interface DatabaseEngine
DatabaseEngineException
- If something goes wrong while persisting data.DatabaseEngineRuntimeException
public void rollback() throws DatabaseEngineRuntimeException
beginTransaction()
.rollback
in interface DatabaseEngine
DatabaseEngineRuntimeException
- If the rollback fails.public boolean isTransactionActive() throws DatabaseEngineRuntimeException
DatabaseEngine
isTransactionActive
in interface DatabaseEngine
DatabaseEngineRuntimeException
- If the access to the database fails.public int executeUpdate(String query) throws DatabaseEngineException
executeUpdate
in interface DatabaseEngine
query
- The query to execute.DatabaseEngineException
- If something goes wrong executing the native query.public int executeUpdate(Expression query) throws DatabaseEngineException
executeUpdate
in interface DatabaseEngine
query
- The update to execute.DatabaseEngineException
- If something goes wrong executing the update.public String translate(Expression query)
translate
in interface DatabaseEngine
query
- The query to translate.public Dialect getDialect()
DatabaseEngine
getDialect
in interface DatabaseEngine
public AbstractBatch createBatch(int batchSize, long batchTimeout)
AbstractBatch.destroy()
before closing the session with the databasecreateBatch
in interface DatabaseEngine
batchSize
- The batch size.batchTimeout
- If inserts do not occur after the specified time, a flush will be performed.public AbstractBatch createBatch(int batchSize, long batchTimeout, String batchName)
DatabaseEngine
AbstractBatch.destroy()
before closing the session with the database.createBatch
in interface DatabaseEngine
batchSize
- The batch size.batchTimeout
- If inserts do not occur after the specified time, a flush will be performed.batchName
- The batch name.protected abstract boolean checkConnection(Connection conn)
conn
- The connection to test.public boolean checkConnection(boolean forceReconnect)
checkConnection
in interface DatabaseEngine
forceReconnect
- True to force the connection in case of failure.public boolean checkConnection()
checkConnection
in interface DatabaseEngine
public void addBatch(String name, EntityEntry entry) throws DatabaseEngineException
addBatch
in interface DatabaseEngine
name
- The entity name.entry
- The entry to persist.DatabaseEngineException
- If something goes wrong while persisting data.protected abstract int entityToPreparedStatement(DbEntity entity, PreparedStatement ps, EntityEntry entry, boolean useAutoIncs) throws DatabaseEngineException
entity
- The entity.ps
- The prepared statement.entry
- The entry.DatabaseEngineException
- if something occurs during the translation.public List<Map<String,ResultColumn>> query(Expression query) throws DatabaseEngineException
query
in interface DatabaseEngine
query
- The query to execute.DatabaseEngineException
- If something goes wrong executing the query.public List<Map<String,ResultColumn>> query(String query) throws DatabaseEngineException
query
in interface DatabaseEngine
query
- The query to execute.DatabaseEngineException
- If something goes wrong executing the query.protected List<Map<String,ResultColumn>> processResultIterator(ResultIterator it) throws DatabaseEngineException
ResultIterator
.it
- The iterator.Map
.DatabaseEngineException
- If a database access error occurs.public ResultIterator iterator(String query) throws DatabaseEngineException
DatabaseEngine
iterator
in interface DatabaseEngine
query
- The query.DatabaseEngineException
- If a database access error occurs.public ResultIterator iterator(Expression query) throws DatabaseEngineException
DatabaseEngine
iterator
in interface DatabaseEngine
query
- The expression that represents the query.DatabaseEngineException
- If a database access error occurs.protected abstract ResultIterator createResultIterator(Statement statement, String sql) throws DatabaseEngineException
ResultIterator
given the engine implementation.statement
- The statement.sql
- The SQL sentence.DatabaseEngineException
- If a database access error occurs.protected abstract void createTable(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong while creating the table.protected abstract void addPrimaryKey(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong while creating the table.protected abstract void addIndexes(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong while creating the table.protected abstract void addSequences(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong while creating the table.protected abstract void dropSequences(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong dropping the sequences.protected abstract void dropTable(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong dropping the sequences.protected abstract void dropColumn(DbEntity entity, String... columns) throws DatabaseEngineException
entity
- The entity.columns
- The column name to drop.DatabaseEngineException
- If something goes wrong dropping the sequences.protected abstract void addColumn(DbEntity entity, DbColumn... columns) throws DatabaseEngineException
entity
- The entity that represents the table.columns
- The db column to add.DatabaseEngineException
protected abstract void addFks(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong creating the FKs.protected abstract MappedEntity createPreparedStatementForInserts(DbEntity entity) throws DatabaseEngineException
entity
- The entity.DatabaseEngineException
- If something goes wrong while creating the table.protected abstract String translateType(DbColumn c) throws DatabaseEngineException
c
- The column.DatabaseEngineException
- If the type cannot be found.protected abstract ResultIterator createResultIterator(PreparedStatement ps) throws DatabaseEngineException
ResultIterator
for the engine in place given given prepared statement.ps
- The prepared statement.DatabaseEngineException
- If a database access error occurs.protected void dropFks(String table) throws DatabaseEngineException
table
- The table name.Exception
DatabaseEngineException
public Map<String,DbEntityType> getEntities() throws DatabaseEngineException
getEntities
in interface DatabaseEngine
DatabaseEngineException
- If something occurs getting the existing tables.protected void executeUpdateSilently(String statement)
statement
- The statement.protected String getSchema()
public Map<String,DbColumnType> getMetadata(String name) throws DatabaseEngineException
getMetadata
in interface DatabaseEngine
DatabaseEngineException
- If something occurs getting the metadata.public Map<String,DbColumnType> getQueryMetadata(Expression query) throws DatabaseEngineException
getQueryMetadata
in interface DatabaseEngine
query
- The query to retrieve the metadata from.DatabaseEngineException
- If something occurs getting the metadata.public Map<String,DbColumnType> getQueryMetadata(String query) throws DatabaseEngineException
getQueryMetadata
in interface DatabaseEngine
query
- The query to retrieve the metadata from.DatabaseEngineException
- If something occurs getting the metadata.protected DbColumnType toPdbType(int type)
DbColumnType
. If there's no mapping a DbColumnType.UNMAPPED
is returned.type
- The SQL type from Types
.DbColumnType
.public DatabaseEngine duplicate(Properties mergeProperties, boolean copyEntities) throws DuplicateEngineException
duplicate
in interface DatabaseEngine
mergeProperties
- Merge properties with the ones already existing.copyEntities
- True to include the entities in the new connection, false otherwise.DuplicateEngineException
- If policy is set to other than 'create' or 'none' or duplication fails for some reason.public PdbProperties getProperties()
DatabaseEngine
getProperties
in interface DatabaseEngine
public void createPreparedStatement(String name, Expression query) throws NameAlreadyExistsException, DatabaseEngineException
DatabaseEngine
createPreparedStatement
in interface DatabaseEngine
name
- The prepared statement name.query
- The query.NameAlreadyExistsException
- If the name already exists.DatabaseEngineException
- If something goes wrong creating the statement.public void createPreparedStatement(String name, String query) throws NameAlreadyExistsException, DatabaseEngineException
DatabaseEngine
createPreparedStatement
in interface DatabaseEngine
name
- The prepared statement name.query
- The query.NameAlreadyExistsException
- If the name already exists.DatabaseEngineException
- If something goes wrong creating the statement.public void createPreparedStatement(String name, Expression query, int timeout) throws NameAlreadyExistsException, DatabaseEngineException
DatabaseEngine
createPreparedStatement
in interface DatabaseEngine
name
- The prepared statement name.query
- The query.timeout
- The timeout (in seconds) for the query to execute.NameAlreadyExistsException
- If the name already exists.DatabaseEngineException
- If something goes wrong creating the statement.public void createPreparedStatement(String name, String query, int timeout) throws NameAlreadyExistsException, DatabaseEngineException
DatabaseEngine
createPreparedStatement
in interface DatabaseEngine
name
- The prepared statement name.query
- The query.timeout
- The timeout (in seconds) for the query to execute.NameAlreadyExistsException
- If the name already exists.DatabaseEngineException
- If something goes wrong creating the statement.public void removePreparedStatement(String name)
DatabaseEngine
removePreparedStatement
in interface DatabaseEngine
name
- The prepared statement name.public List<Map<String,ResultColumn>> getPSResultSet(String name) throws DatabaseEngineException
getPSResultSet
in interface DatabaseEngine
name
- The prepared statement name.DatabaseEngineException
- If something occurs getting the result.public ResultIterator getPSIterator(String name) throws DatabaseEngineException
DatabaseEngine
PreparedStatement
bound to the given name.getPSIterator
in interface DatabaseEngine
name
- The name of the prepared statement.DatabaseEngineException
public void setParameters(String name, Object... params) throws DatabaseEngineException, ConnectionResetException
DatabaseEngine
setParameters
in interface DatabaseEngine
name
- The prepared statement name.params
- The parameters to set.DatabaseEngineException
- If something occurs setting the parameters.ConnectionResetException
- If the connection was reset while trying to set parameters.public void setParameter(String name, int index, Object param) throws DatabaseEngineException, ConnectionResetException
DatabaseEngine
setParameter
in interface DatabaseEngine
name
- The prepared statement name.index
- The index to set.param
- The parameter to set.DatabaseEngineException
- If something occurs setting the parameters.ConnectionResetException
- If the connection was reset while trying to set the parameter.public void executePS(String name) throws DatabaseEngineException, ConnectionResetException
executePS
in interface DatabaseEngine
name
- The prepared statement name.DatabaseEngineException
- If something goes wrong while executing.ConnectionResetException
- If the connection is down and reestablishment occurs. If this happens, the user must reset the parameters and re-execute
the query.public void clearParameters(String name) throws DatabaseEngineException, ConnectionResetException
clearParameters
in interface DatabaseEngine
name
- The prepared statement name.DatabaseEngineException
- If something occurs while clearing the parameters.ConnectionResetException
- If the connection was reset.public boolean preparedStatementExists(String name)
preparedStatementExists
in interface DatabaseEngine
name
- The name to test.public Integer executePSUpdate(String name) throws DatabaseEngineException, ConnectionResetException
executePSUpdate
in interface DatabaseEngine
name
- The prepared statement name.DatabaseEngineException
- If the prepared statement does not exist or something goes wrong while executing.ConnectionResetException
- If the connection is down and reestablishment occurs. If this happens, the user must reset the parameters and re-execute
the query.public String commentCharacter()
commentCharacter
in interface DatabaseEngine
public String escapeCharacter()
escapeCharacter
in interface DatabaseEngine
public void setExceptionHandler(ExceptionHandler eh)
DatabaseEngine
setExceptionHandler
in interface DatabaseEngine
eh
- The reference for exception callbacks.protected void handleOperation(OperationFault op, Exception e) throws DatabaseEngineException
setExceptionHandler(ExceptionHandler)
method to provide a
specific implementation.op
- The operation that originated the exception.e
- The exception.DatabaseEngineException
- If the faulty operation must stop the execution.protected final byte[] objectToArray(Object val) throws IOException
val
- The object to convert.IOException
- If the buffer is not enough to make the conversion.public boolean hasIdentityColumn(DbEntity entity)
entity
- The entity to check.protected void inject(Expression... objs)
Copyright © 2014 Feedzai. All Rights Reserved.