Class Table<T>


  • public class Table<T>
    extends Object
    Java representation and helper methods for a database table.
    • Constructor Detail

      • Table

        public Table​(String fullTableName,
                     Class<T> beanClass)
    • Method Detail

      • getSupportsGeneratedKeys

        public boolean getSupportsGeneratedKeys()
      • setSupportsGeneratedKeys

        public void setSupportsGeneratedKeys​(boolean value)
      • setStatementCacheEnabled

        public Table setStatementCacheEnabled​(boolean enabled)
        Enable (default to avoid breaking changes) or disable the StatementCache.

        If using a jdbc driver with Statement caching (e.g. PG, Oracle, MySQL) or for fine-grain control over memory, consider disabling and closing Statements with try-resource or finally.

        Parameters:
        enabled - should be set prior to setConnection
        See Also:
        for a detailed description, https://github.com/brettwooldridge/HikariCP/issues/488
      • getStatementCacheEnabled

        public boolean getStatementCacheEnabled()
        Return true if the StatementCache is enabled (default).
      • setStatementCacheConfig

        public Table setStatementCacheConfig​(List<StatementCacheListener> listeners,
                                             Integer perConCacheMaxEntries,
                                             Long perConCacheMaxAgeMillis)
        Sets configuration passed to StatementCache if it is or becomes enabled.

        Enables logging and aids diagnosing affinity of cache, thread, connection and its sql statements. The last two params guard against leaks caused by sql concatenation instead of using parameters.

        Parameters:
        listeners - (optional) will be notified of key StatementCache events such as for logging
        perConCacheMaxEntries - (optional) max stmts before the LRU Statement is evicted (1000 default)
        perConCacheMaxAgeMillis - (optional) max time to live before the LRU Statement will be evicted (30 minutes default)
        See Also:
        setConnection(Connection)
      • getConnection

        protected Connection getConnection()
      • setConnection

        public boolean setConnection​(Connection conn)
                              throws SQLException
        Return true if stmtCache is enabled and the Connection is the same as last.

        Otherwise, clears the StatementCache and creates a new StatementCache. The internal connection reference is set for both cases because conn may be a Connection facade such as if from HikariCP.getConnection. net.jextra.fauxjo.StatementCache#getConnKey(Connection) is used to determine if conn is the same as the last one set.

        Parameters:
        conn - may be a real db Connection or one wrapped in a Connection facade
        Throws:
        SQLException
        See Also:
        StatementCache.getConnKey(Connection)
      • getSchemaName

        public String getSchemaName()
      • getTableName

        public String getTableName()
      • getFullTableName

        public String getFullTableName()
      • getCoercer

        public Coercer getCoercer()
      • prepareStatement

        protected PreparedStatement prepareStatement​(String sql)
                                              throws SQLException
        Return a new PreparedStatement from StatementCache if enabled else from connection.

        If getStatementCacheEnabled is false, ensure to close in a try-resource or finally after use.

        Parameters:
        sql - to prepare
        Throws:
        SQLException
      • buildBasicSelectStatement

        public String buildBasicSelectStatement​(String clause)
      • insert

        public int insert​(T bean)
                   throws SQLException
        Convert the bean into an insert statement and execute it.

        If StatementCache is enabled, the PreparedStatement will be closed upon the next new Connection else is closed here in a finally block.

        Parameters:
        bean - to insert
        Throws:
        SQLException
      • insert

        public int insert​(Collection<T> beans)
                   throws SQLException
        Insert multiple beans into the database at the same time using a fast multi-row insert statement. If StatementCache is enabled, the PreparedStatement will be closed upon the next new Connection else is closed here in a finally block.
        Throws:
        SQLException
      • insertBatch

        public int[] insertBatch​(Collection<T> beans)
                          throws SQLException
        Return the result of preparedStatement.executeBatch. Insert multiple beans into the database at the same time using a fast multi-row insert statement. If StatementCache is enabled, the PreparedStatement will be closed upon the next new Connection else is closed here in a finally block.
        Parameters:
        beans - to insert
        Throws:
        SQLException
        See Also:
        Statement.executeBatch()
      • update

        public int update​(T bean)
                   throws SQLException
        Convert the bean into an update statement and execute it.

        If StatementCache is enabled, the PreparedStatement will be closed upon the next new Connection else is closed here in a finally block.

        Throws:
        SQLException
      • delete

        public boolean delete​(T bean)
                       throws SQLException
        Convert the bean into an delete statement and execute it.

        If StatementCache is enabled, the PreparedStatement will be closed upon the next new Connection else is closed here in a finally block.

        Throws:
        SQLException
      • getStatementCacheCsvForPrepStmts

        public void getStatementCacheCsvForPrepStmts​(StringBuilder sb)
                                              throws Exception
        Append csv contents of the statement cache if enabled else throws an exception.
        Parameters:
        sb - to append cache entries as csv
        Throws:
        Exception
      • getInsertDef

        protected net.jextra.fauxjo.Table.InsertDef getInsertDef​(T bean)
                                                          throws SQLException
        Optionally passing in a actual bean instant allows the insert statement to be exclude columns that can have defaulted values and are also null in the bean.
        Throws:
        SQLException