Interface DatabaseEngine

    • Method Detail

      • close

        void close()
        Closes the connection to the database.
        Specified by:
        close in interface AutoCloseable
      • addEntity

        void addEntity​(DbEntity entity)
                throws DatabaseEngineException
        Adds an entity to the engine. It will create tables and everything necessary so persistence can work.
        Parameters:
        entity - The entity to add.
        Throws:
        DatabaseEngineException - If something goes wrong while creating the structures.
      • loadEntity

        void loadEntity​(DbEntity entity)
                 throws DatabaseEngineException
        Loads an entity into the engine.

        No DDL commands will be executed, only prepared statements will be created in order to persist data into the entities.

        Parameters:
        entity - The entity to load into the connection.
        Throws:
        DatabaseEngineException - If something goes wrong while loading the entity.
        Since:
        2.1.2
      • updateEntity

        void updateEntity​(DbEntity entity)
                   throws DatabaseEngineException

        Updates an entity in the engine.

        If the entity does not exists 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. Primary Keys, Foreign Keys, Indexes and column types changes will not be updated.

        Parameters:
        entity - The entity to update.
        Throws:
        DatabaseEngineException - If something goes wrong while updating the entity.
        Since:
        2.0.0
      • removeEntity

        DbEntity removeEntity​(String name)
        Removes the entity given the name. If the schema is drop-create then the entity will be dropped.
        Parameters:
        name - The name of the entity to remove.
        Returns:
        The entity removed or null if there's no entity with the given name.
      • containsEntity

        boolean containsEntity​(String name)
        Returns if the current engine contains the entity.
        Parameters:
        name - The entity name.
        Returns:
        True if the engine has the entity, false otherwise.
      • persist

        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.

        Parameters:
        name - The entity name.
        entry - The entry to persist.
        Returns:
        The ID of the auto generated value, null if there's no auto generated value.
        Throws:
        DatabaseEngineException - If something goes wrong while persisting data.
      • persist

        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.

        Parameters:
        name - The entity name.
        entry - The entry to persist.
        useAutoInc - Use or not the autoinc.
        Returns:
        The ID of the auto generated value, null if there's no auto generated value.
        Throws:
        DatabaseEngineException - If something goes wrong while persisting data.
      • executeUpdate

        int executeUpdate​(String query)
                   throws DatabaseEngineException
        Executes a native query.
        Parameters:
        query - The query to execute.
        Returns:
        The number of rows updated.
        Throws:
        DatabaseEngineException - If something goes wrong executing the native query.
      • translate

        String translate​(Expression query)
        Translates the given expression to the current dialect.
        Parameters:
        query - The query to translate.
        Returns:
        The translation result.
      • getDialect

        Dialect getDialect()
        Gets the dialect being used.
        Returns:
        The dialect being used.
      • createBatch

        AbstractBatch createBatch​(int batchSize,
                                  long batchTimeout)
        Creates a new batch that periodically flushes a batch. A flush will also occur when the maximum number of statements in the batch is reached.

        Please be sure to call AbstractBatch.destroy() before closing the session with the database.

        Parameters:
        batchSize - The batch size.
        batchTimeout - If inserts do not occur after the specified time, a flush will be performed.
        Returns:
        The batch.
      • createBatch

        AbstractBatch createBatch​(int batchSize,
                                  long batchTimeout,
                                  String batchName)
        Creates a new batch that periodically flushes a batch. A flush will also occur when the maximum number of statements in the batch is reached.

        Please be sure to call AbstractBatch.destroy() before closing the session with the database.

        Parameters:
        batchSize - The batch size.
        batchTimeout - If inserts do not occur after the specified time, a flush will be performed.
        batchName - The batch name.
        Returns:
        The batch.
      • createBatch

        @Deprecated
        AbstractBatch createBatch​(int batchSize,
                                  long batchTimeout,
                                  String batchName,
                                  FailureListener failureListener)
        Creates a new batch that periodically flushes a batch. A flush will also occur when the maximum number of statements in the batch is reached.

        If desired you may provide an Optional FailureListener that will be invoked when the batch fails to persist data.

        Please be sure to call AbstractBatch.destroy() before closing the session with the database.

        Parameters:
        batchSize - The batch size.
        batchTimeout - If inserts do not occur after the specified time, a flush will be performed.
        batchName - The batch name.
        failureListener - Failure listener to execute custom behavior when the batch fails to persist.
        Returns:
        The batch.
        Since:
        2.1.11
      • createBatch

        default AbstractBatch createBatch​(int batchSize,
                                          long batchTimeout,
                                          String batchName,
                                          @Nullable
                                          BatchListener batchListener)
        Creates a new batch that periodically flushes a batch. A flush will also occur when the maximum number of statements in the batch is reached.

        If desired you may provide a BatchListener that will be invoked when a batch operation fails or succeeds to persist data.

        Please be sure to call AbstractBatch.destroy() before closing the session with the database.

        Parameters:
        batchSize - The batch size.
        batchTimeout - If inserts do not occur after the specified time, a flush will be performed.
        batchName - The batch name.
        batchListener - Batch listener to execute custom behavior when the batch fails or succeeds to persist.
        Returns:
        The batch.
        Since:
        2.8.1
      • checkConnection

        boolean checkConnection​(boolean forceReconnect)
        Checks if the connection is alive.
        Parameters:
        forceReconnect - true to force the connection in case of failure, false otherwise.
        Returns:
        true if the connection is valid, false otherwise.
      • checkConnection

        boolean checkConnection()
        Checks if the connection is alive.
        Returns:
        true if the connection is valid, false otherwise.
      • getEntities

        Map<String,​DbEntityType> getEntities​(String schemaPattern)
                                            throws DatabaseEngineException
        Gets the database entities for the specified schema.
        Parameters:
        schemaPattern - A schema name pattern; must match the schema name as it is stored in the database; "" retrieves those without a schema; null means that the schema name should not be used to narrow the search.
        Returns:
        The list of database entities and types (tables, views, and so on).
        Throws:
        DatabaseEngineException - If something occurs getting the existing tables.
        Since:
        2.1.13
      • getMetadata

        Map<String,​DbColumnType> getMetadata​(String tableNamePattern)
                                            throws DatabaseEngineException
        Gets the table metadata (table must be in the current schema).
        Parameters:
        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.
      • getMetadata

        Map<String,​DbColumnType> getMetadata​(String schemaPattern,
                                                   String tableNamePattern)
                                            throws DatabaseEngineException
        Gets the table metadata (table must be in the current schema).
        Parameters:
        tableNamePattern - A table name pattern; must match the table name as it is stored in the database.
        schemaPattern - A schema name pattern; must match the schema name as it is stored in the database; "" retrieves those without a schema; null means that the schema name should not be used to narrow the search.
        Returns:
        A representation of the table columns and types.
        Throws:
        DatabaseEngineException - If something occurs getting the metadata.
        Since:
        2.1.13
      • duplicate

        DatabaseEngine duplicate​(Properties mergeProperties,
                                 boolean copyEntities)
                          throws DuplicateEngineException
        Duplicates a connection.
        Parameters:
        mergeProperties - Merge properties with the ones already existing.
        copyEntities - true to include the entities in the new connection, false otherwise.
        Returns:
        The new connection.
        Throws:
        DuplicateEngineException - If policy is set to other than 'create' or 'none' or duplication fails for some reason.
      • getProperties

        PdbProperties getProperties()
        Gets the properties in use.
        Returns:
        A clone of the properties in use.
      • removePreparedStatement

        void removePreparedStatement​(String name)
        Removes the given prepared statement.
        Parameters:
        name - The prepared statement name.
      • setParameter

        void setParameter​(String name,
                          int index,
                          Object param,
                          DbColumnType paramType)
                   throws DatabaseEngineException,
                          ConnectionResetException
        Sets the parameter on the specified index given its type. This is for situations where the java type of the parameter alone is not enough to determine the corresponding database type; for example, Strings can be used to represent both actual Strings and Json values, so if we have an update statement that updates a json column we need to specify that the bind parameter is of type json.
        Parameters:
        name - The prepared statement name.
        index - The index to set.
        param - The parameter to set.
        paramType - The type of the parameter being set.
        Throws:
        DatabaseEngineException - If something occurs setting the parameters.
        ConnectionResetException - If the connection was reset while trying to set the parameter.
        Since:
        2.1.5
      • preparedStatementExists

        boolean preparedStatementExists​(String name)
        Checks if there's a prepared statement with the given name.
        Parameters:
        name - The name to test.
        Returns:
        true if it exists, false otherwise.
      • commentCharacter

        String commentCharacter()
        Returns:
        The comment character of this engine implementation.
      • escapeCharacter

        String escapeCharacter()
        Returns:
        The escape character of this engine implementation.
      • iterator

        ResultIterator iterator​(String query,
                                int fetchSize)
                         throws DatabaseEngineException
        Creates an iterator for the given SQL sentence.
        Parameters:
        query - The query.
        fetchSize - The number of rows to fetch each time.
        Returns:
        An iterator for the results of the given SQL query.
        Throws:
        DatabaseEngineException - If a database access error occurs.
      • iterator

        default ResultIterator iterator​(String query,
                                        int fetchSize,
                                        int readTimeoutOverride)
                                 throws DatabaseEngineException
        Creates an iterator for the given SQL sentence overriding the configured query timeout (see PdbProperties.getSelectQueryTimeout()) if the underlying engine supports query timeouts.
        Parameters:
        query - The query.
        fetchSize - The number of rows to fetch each time.
        readTimeoutOverride - The query timeout to use.
        Returns:
        An iterator for the results of the given SQL query.
        Throws:
        DatabaseEngineException - If a database access error occurs.
      • iterator

        ResultIterator iterator​(Expression query,
                                int fetchSize)
                         throws DatabaseEngineException
        Creates an iterator for the given SQL expression.
        Parameters:
        query - The expression that represents the query.
        fetchSize - The number of rows to fetch each time.
        Returns:
        An iterator for the results of the given SQL expression.
        Throws:
        DatabaseEngineException - If a database access error occurs.
      • iterator

        default ResultIterator iterator​(Expression query,
                                        int fetchSize,
                                        int readTimeoutOverride)
                                 throws DatabaseEngineException
        Creates an iterator for the given SQL expression overriding the configured query timeout (see PdbProperties.getSelectQueryTimeout()) if the underlying engine supports query timeouts.
        Parameters:
        query - The expression that represents the query.
        fetchSize - The number of rows to fetch each time.
        readTimeoutOverride - The query timeout to use.
        Returns:
        An iterator for the results of the given SQL expression.
        Throws:
        DatabaseEngineException - If a database access error occurs.
      • setExceptionHandler

        void setExceptionHandler​(ExceptionHandler eh)
        Sets the given exception handler in the engine.
        Parameters:
        eh - The reference for exception callbacks.
      • isStringAggDistinctCapable

        boolean isStringAggDistinctCapable()
        Checks if the engine supports using DISTINCT inside a string aggregation.
        Returns:
        true if the engine supports using DISTINCT inside a string aggregation, false otherwise.