Package co.aikar.idb
Class DbStatement
- java.lang.Object
-
- co.aikar.idb.DbStatement
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class DbStatement extends java.lang.Object implements java.lang.AutoCloseableManages a connection to the database db and lets you work with an active prepared statement. Must close after you are done with it, preferably wrapping in a try/catch/finally DbStatement statement = null; try { statement = new DbStatement(); // use it } catch (Exception e) { // handle exception } finally { if (statement != null) { statement.close(); } }
-
-
Field Summary
Fields Modifier and Type Field Description java.lang.Stringquery
-
Constructor Summary
Constructors Constructor Description DbStatement()DbStatement(Database db)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes all resources associated with this statement and returns the connection to the db.voidcommit()Commits a pending transaction on this connectionDbStatementexecute(java.lang.Object... params)Executes the prepared statement with the supplied parameters.<T> TexecuteQueryGetFirstColumn(java.lang.String query, java.lang.Object... params)Helper to query, execute and get first column<T> java.util.List<T>executeQueryGetFirstColumnResults(java.lang.String query, java.lang.Object... params)Helper to query, execute and get first column of all resultsDbRowexecuteQueryGetFirstRow(java.lang.String query, java.lang.Object... params)Helper method to query, execute and get first rowjava.util.ArrayList<DbRow>executeQueryGetResults(java.lang.String query, java.lang.Object... params)Helper method to query, execute and getResultsintexecuteUpdate(java.lang.Object... params)Execute an update query with the supplied parametersintexecuteUpdateQuery(java.lang.String query, java.lang.Object... params)Helper method to query and execute update<T> TgetFirstColumn()java.lang.LonggetLastInsertId()Gets the Id of last insertDbRowgetNextRow()Gets the next DbRow from the result set.java.util.ArrayList<DbRow>getResults()Gets all results as an array of DbRowbooleaninTransaction()booleanisClosed()voidonCommit(java.util.function.Consumer<DbStatement> run)When this connection is rolled back, run this method.voidonRollback(java.util.function.Consumer<DbStatement> run)When this connection is rolled back, run this method.DbStatementquery(java.lang.String query)Initiates a new prepared statement on this connection.voidrollback()Rollsback a pending transaction on this connection.voidstartTransaction()Starts a transaction on this connection
-
-
-
Constructor Detail
-
DbStatement
public DbStatement() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
DbStatement
public DbStatement(Database db) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
-
Method Detail
-
startTransaction
public void startTransaction() throws java.sql.SQLExceptionStarts a transaction on this connection- Throws:
java.sql.SQLException
-
commit
public void commit() throws java.sql.SQLExceptionCommits a pending transaction on this connection- Throws:
java.sql.SQLException
-
rollback
public void rollback() throws java.sql.SQLExceptionRollsback a pending transaction on this connection.- Throws:
java.sql.SQLException
-
inTransaction
public boolean inTransaction()
-
onCommit
public void onCommit(java.util.function.Consumer<DbStatement> run)
When this connection is rolled back, run this method. If not in a transaction, the method will run immediately after the next executeUpdate. It will not run on non update execute queries when not in a transaction
-
onRollback
public void onRollback(java.util.function.Consumer<DbStatement> run)
When this connection is rolled back, run this method. If not in a transaction, the method will run if the next executeUpdate has an error. No guarantee is made about the state of the connection when this runnable is called.
-
query
public DbStatement query(java.lang.String query) throws java.sql.SQLException
Initiates a new prepared statement on this connection.- Throws:
java.sql.SQLException
-
executeQueryGetResults
public java.util.ArrayList<DbRow> executeQueryGetResults(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException
Helper method to query, execute and getResults- Throws:
java.sql.SQLException
-
executeUpdateQuery
public int executeUpdateQuery(java.lang.String query, java.lang.Object... params) throws java.sql.SQLExceptionHelper method to query and execute update- Throws:
java.sql.SQLException
-
executeQueryGetFirstRow
public DbRow executeQueryGetFirstRow(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException
Helper method to query, execute and get first row- Throws:
java.sql.SQLException
-
executeQueryGetFirstColumn
public <T> T executeQueryGetFirstColumn(java.lang.String query, java.lang.Object... params) throws java.sql.SQLExceptionHelper to query, execute and get first column- Throws:
java.sql.SQLException
-
executeQueryGetFirstColumnResults
public <T> java.util.List<T> executeQueryGetFirstColumnResults(java.lang.String query, java.lang.Object... params) throws java.sql.SQLExceptionHelper to query, execute and get first column of all results- Throws:
java.sql.SQLException
-
executeUpdate
public int executeUpdate(java.lang.Object... params) throws java.sql.SQLExceptionExecute an update query with the supplied parameters- Throws:
java.sql.SQLException
-
execute
public DbStatement execute(java.lang.Object... params) throws java.sql.SQLException
Executes the prepared statement with the supplied parameters.- Throws:
java.sql.SQLException
-
getLastInsertId
public java.lang.Long getLastInsertId() throws java.sql.SQLExceptionGets the Id of last insert- Returns:
- Long
- Throws:
java.sql.SQLException
-
getResults
public java.util.ArrayList<DbRow> getResults() throws java.sql.SQLException
Gets all results as an array of DbRow- Throws:
java.sql.SQLException
-
getNextRow
public DbRow getNextRow() throws java.sql.SQLException
Gets the next DbRow from the result set.- Returns:
- DbRow containing a hashmap of the columns
- Throws:
java.sql.SQLException
-
getFirstColumn
public <T> T getFirstColumn() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
close
public void close()
Closes all resources associated with this statement and returns the connection to the db.- Specified by:
closein interfacejava.lang.AutoCloseable
-
isClosed
public boolean isClosed() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
-