Class SQLUtils


  • public class SQLUtils
    extends Object
    SQL Utility methods
    Author:
    osbornb
    • Constructor Detail

      • SQLUtils

        public SQLUtils()
    • Method Detail

      • execSQL

        public static void execSQL​(Connection connection,
                                   String sql)
        Execute the SQL
        Parameters:
        connection - connection
        sql - sql statement
      • query

        public static ResultSet query​(Connection connection,
                                      String sql,
                                      String[] selectionArgs)
        Query for results
        Parameters:
        connection - connection
        sql - sql statement
        selectionArgs - selection arguments
        Returns:
        result set
      • count

        public static int count​(ResultSet resultSet,
                                String sql,
                                String[] selectionArgs)
        Attempt to count the results of the query
        Parameters:
        resultSet - result set
        sql - SQL statement
        selectionArgs - selection arguments
        Returns:
        count if known, -1 if not able to determine
        Since:
        4.0.0
      • count

        public static int count​(Connection connection,
                                String sql,
                                String[] selectionArgs)
        Attempt to count the results of the query
        Parameters:
        connection - connection
        sql - SQL statement
        selectionArgs - selection arguments
        Returns:
        count if known, -1 if not able to determine
      • querySingleResult

        public static Object querySingleResult​(Connection connection,
                                               String sql,
                                               String[] args,
                                               int column,
                                               GeoPackageDataType dataType)
        Query the SQL for a single result object with the expected data type
        Parameters:
        connection - connection
        sql - sql statement
        args - arguments
        column - column index
        dataType - GeoPackage data type
        Returns:
        result, null if no result
        Since:
        3.1.0
      • querySingleColumnResults

        public static List<Object> querySingleColumnResults​(Connection connection,
                                                            String sql,
                                                            String[] args,
                                                            int column,
                                                            GeoPackageDataType dataType,
                                                            Integer limit)
        Query for values from a single column up to the limit
        Parameters:
        connection - connection
        sql - sql statement
        args - arguments
        column - column index
        dataType - GeoPackage data type
        limit - result row limit
        Returns:
        single column results
        Since:
        3.1.0
      • queryResults

        public static List<List<Object>> queryResults​(Connection connection,
                                                      String sql,
                                                      String[] args,
                                                      GeoPackageDataType[] dataTypes,
                                                      Integer limit)
        Query for values up to the limit
        Parameters:
        connection - connection
        sql - sql statement
        args - arguments
        dataTypes - column data types
        limit - result row limit
        Returns:
        results
        Since:
        3.1.0
      • delete

        public static int delete​(Connection connection,
                                 String table,
                                 String where,
                                 String[] args)
        Execute a deletion
        Parameters:
        connection - connection
        table - table name
        where - where clause
        args - where arguments
        Returns:
        deleted count
      • update

        public static int update​(Connection connection,
                                 String table,
                                 ContentValues values,
                                 String whereClause,
                                 String[] whereArgs)
        Update table rows
        Parameters:
        connection - connection
        table - table name
        values - content values
        whereClause - where clause
        whereArgs - where arguments
        Returns:
        updated count
      • insert

        public static long insert​(Connection connection,
                                  String table,
                                  ContentValues values)
        Insert a new row
        Parameters:
        connection - connection
        table - table name
        values - content values
        Returns:
        row id or -1 on an exception
      • insertOrThrow

        public static long insertOrThrow​(Connection connection,
                                         String table,
                                         ContentValues values)
        Insert a new row
        Parameters:
        connection - connection
        table - table name
        values - content values
        Returns:
        row id
      • insert

        public static long insert​(Connection connection,
                                  String table,
                                  String pkColumn,
                                  ContentValues values)
        Insert a new row
        Parameters:
        connection - connection
        table - table name
        pkColumn - primary key id column
        values - content values
        Returns:
        row id or -1 on an exception
        Since:
        6.6.2
      • insertOrThrow

        public static long insertOrThrow​(Connection connection,
                                         String table,
                                         String pkColumn,
                                         ContentValues values)
        Insert a new row
        Parameters:
        connection - connection
        table - table name
        pkColumn - primary key id column
        values - content values
        Returns:
        row id
        Since:
        6.6.2
      • setArguments

        public static void setArguments​(PreparedStatement statement,
                                        Object[] selectionArgs)
                                 throws SQLException
        Set the prepared statement arguments
        Parameters:
        statement - prepared statement
        selectionArgs - selection arguments
        Throws:
        SQLException - upon failure
      • closeStatement

        public static void closeStatement​(Statement statement,
                                          String sql)
        Close the statement
        Parameters:
        statement - statement
        sql - sql statement
      • closeResultSet

        public static void closeResultSet​(ResultSet resultSet,
                                          String sql)
        Close the ResultSet
        Parameters:
        resultSet - result set
        sql - sql statement
      • closeResultSetStatement

        public static void closeResultSetStatement​(ResultSet resultSet,
                                                   String sql)
        Close the ResultSet Statement from which it was created, which closes all ResultSets as well
        Parameters:
        resultSet - result set
        sql - sql statement
      • wrapQuery

        public static ResultSetResult wrapQuery​(Connection connection,
                                                String sql,
                                                String[] selectionArgs)
        Perform the query and wrap as a result
        Parameters:
        connection - connection
        sql - sql statement
        selectionArgs - selection arguments
        Returns:
        result
        Since:
        3.1.0
      • beginTransaction

        public static boolean beginTransaction​(Connection connection)
        Begin a transaction for the connection
        Parameters:
        connection - connection
        Returns:
        pre-transaction auto commit value
        Since:
        3.3.0
      • endTransaction

        public static void endTransaction​(Connection connection,
                                          boolean successful)
        End a transaction for the connection
        Parameters:
        connection - connection
        successful - true to commit, false to rollback
        Since:
        3.3.0
      • endTransaction

        public static void endTransaction​(Connection connection,
                                          boolean successful,
                                          Boolean autoCommit)
        End a transaction for the connection
        Parameters:
        connection - connection
        successful - true to commit, false to rollback
        autoCommit - pre-transaction auto commit value
        Since:
        3.3.0