Package co.aikar.idb

Class DbStatement

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class DbStatement
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Manages 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.String query  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes all resources associated with this statement and returns the connection to the db.
      void commit()
      Commits a pending transaction on this connection
      DbStatement execute​(java.lang.Object... params)
      Executes the prepared statement with the supplied parameters.
      <T> T executeQueryGetFirstColumn​(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 results
      DbRow executeQueryGetFirstRow​(java.lang.String query, java.lang.Object... params)
      Helper method to query, execute and get first row
      java.util.ArrayList<DbRow> executeQueryGetResults​(java.lang.String query, java.lang.Object... params)
      Helper method to query, execute and getResults
      int executeUpdate​(java.lang.Object... params)
      Execute an update query with the supplied parameters
      int executeUpdateQuery​(java.lang.String query, java.lang.Object... params)
      Helper method to query and execute update
      <T> T getFirstColumn()  
      java.lang.Long getLastInsertId()
      Gets the Id of last insert
      DbRow getNextRow()
      Gets the next DbRow from the result set.
      java.util.ArrayList<DbRow> getResults()
      Gets all results as an array of DbRow
      boolean inTransaction()  
      boolean isClosed()  
      void onCommit​(java.util.function.Consumer<DbStatement> run)
      When this connection is rolled back, run this method.
      void onRollback​(java.util.function.Consumer<DbStatement> run)
      When this connection is rolled back, run this method.
      DbStatement query​(java.lang.String query)
      Initiates a new prepared statement on this connection.
      void rollback()
      Rollsback a pending transaction on this connection.
      void startTransaction()
      Starts a transaction on this connection
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • query

        public java.lang.String query
    • 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.SQLException
        Starts a transaction on this connection
        Throws:
        java.sql.SQLException
      • commit

        public void commit()
                    throws java.sql.SQLException
        Commits a pending transaction on this connection
        Throws:
        java.sql.SQLException
      • rollback

        public void rollback()
                      throws java.sql.SQLException
        Rollsback 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.SQLException
        Helper 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.SQLException
        Helper 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.SQLException
        Helper 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.SQLException
        Execute 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.SQLException
        Gets 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:
        close in interface java.lang.AutoCloseable
      • isClosed

        public boolean isClosed()
                         throws java.sql.SQLException
        Throws:
        java.sql.SQLException