Class Table<T>
- java.lang.Object
-
- net.jextra.fauxjo.Table<T>
-
public class Table<T> extends Object
Java representation and helper methods for a database table.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTable.ColumnInfo
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringbuildBasicSelectStatement(String clause)booleandelete(T bean)Convert the bean into an delete statement and execute it.CoercergetCoercer()protected ConnectiongetConnection()StringgetDeleteSql()StringgetFullTableName()protected net.jextra.fauxjo.Table.InsertDefgetInsertDef(T bean)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.StringgetSchemaName()protected StatementCachegetStatementCache()voidgetStatementCacheCsvForPrepStmts(StringBuilder sb)Append csv contents of the statement cache if enabled else throws an exception.booleangetStatementCacheEnabled()Return true if the StatementCache is enabled (default).voidgetStatementCacheStats(StringBuilder strBldrToAppend, DateTimeFormatter optionalDtFormat)Append json statistics about the statement cache if enabled else throws an exception.booleangetSupportsGeneratedKeys()StringgetTableName()StringgetUpdateSql()intinsert(Collection<T> beans)Insert multiple beans into the database at the same time using a fast multi-row insert statement.intinsert(T bean)Convert the bean into an insert statement and execute it.int[]insertBatch(Collection<T> beans)Return the result of preparedStatement.executeBatch.protected PreparedStatementprepareStatement(String sql)Return a new PreparedStatement from StatementCache if enabled else from connection.protected voidretrieveGeneratedKeys(PreparedStatement insStatement, net.jextra.fauxjo.Table.InsertDef insertDef, T bean)booleansetConnection(Connection conn)Return true if stmtCache is enabled and the Connection is the same as last.voidsetDeleteValues(PreparedStatement statement, T bean)protected intsetInsertValues(PreparedStatement insStatement, net.jextra.fauxjo.Table.InsertDef insertDef, int paramIndex, T bean)TablesetStatementCacheConfig(List<StatementCacheListener> listeners, Integer perConCacheMaxEntries, Long perConCacheMaxAgeMillis)Sets configuration passed to StatementCache if it is or becomes enabled.TablesetStatementCacheEnabled(boolean enabled)Enable (default to avoid breaking changes) or disable the StatementCache.voidsetSupportsGeneratedKeys(boolean value)voidsetUpdateValues(PreparedStatement statement, T bean)intupdate(T bean)Convert the bean into an update statement and execute it.
-
-
-
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 loggingperConCacheMaxEntries- (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
-
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()
-
getStatementCache
protected StatementCache getStatementCache()
-
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
-
getUpdateSql
public String getUpdateSql() throws SQLException
- Throws:
SQLException
-
setUpdateValues
public void setUpdateValues(PreparedStatement statement, T bean) throws SQLException
- Throws:
SQLException
-
getDeleteSql
public String getDeleteSql() throws SQLException
- Throws:
SQLException
-
setDeleteValues
public void setDeleteValues(PreparedStatement statement, T bean) throws SQLException
- Throws:
SQLException
-
getStatementCacheStats
public void getStatementCacheStats(StringBuilder strBldrToAppend, DateTimeFormatter optionalDtFormat) throws SQLException
Append json statistics about the statement cache if enabled else throws an exception.- 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
-
setInsertValues
protected int setInsertValues(PreparedStatement insStatement, net.jextra.fauxjo.Table.InsertDef insertDef, int paramIndex, T bean) throws SQLException
- Throws:
SQLException
-
retrieveGeneratedKeys
protected void retrieveGeneratedKeys(PreparedStatement insStatement, net.jextra.fauxjo.Table.InsertDef insertDef, T bean) throws SQLException
- Throws:
SQLException
-
-