Package mil.nga.geopackage.db
Class SQLUtils
- java.lang.Object
-
- mil.nga.geopackage.db.SQLUtils
-
public class SQLUtils extends Object
SQL Utility methods- Author:
- osbornb
-
-
Constructor Summary
Constructors Constructor Description SQLUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
beginTransaction(Connection connection)
Begin a transaction for the connectionstatic void
closeResultSet(ResultSet resultSet, String sql)
Close the ResultSetstatic void
closeResultSetStatement(ResultSet resultSet, String sql)
Close the ResultSet Statement from which it was created, which closes all ResultSets as wellstatic void
closeStatement(Statement statement, String sql)
Close the statementstatic int
count(Connection connection, String sql, String[] selectionArgs)
Attempt to count the results of the querystatic int
count(ResultSet resultSet, String sql, String[] selectionArgs)
Attempt to count the results of the querystatic int
delete(Connection connection, String table, String where, String[] args)
Execute a deletionstatic void
endTransaction(Connection connection, boolean successful)
End a transaction for the connectionstatic void
endTransaction(Connection connection, boolean successful, Boolean autoCommit)
End a transaction for the connectionstatic void
execSQL(Connection connection, String sql)
Execute the SQLstatic long
insert(Connection connection, String table, String pkColumn, ContentValues values)
Insert a new rowstatic long
insert(Connection connection, String table, ContentValues values)
Insert a new rowstatic long
insertOrThrow(Connection connection, String table, String pkColumn, ContentValues values)
Insert a new rowstatic long
insertOrThrow(Connection connection, String table, ContentValues values)
Insert a new rowstatic ResultSet
query(Connection connection, String sql, String[] selectionArgs)
Query for resultsstatic List<List<Object>>
queryResults(Connection connection, String sql, String[] args, GeoPackageDataType[] dataTypes, Integer limit)
Query for values up to the limitstatic 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 limitstatic Object
querySingleResult(Connection connection, String sql, String[] args, int column, GeoPackageDataType dataType)
Query the SQL for a single result object with the expected data typestatic void
setArguments(PreparedStatement statement, Object[] selectionArgs)
Set the prepared statement argumentsstatic int
update(Connection connection, String table, ContentValues values, String whereClause, String[] whereArgs)
Update table rowsstatic ResultSetResult
wrapQuery(Connection connection, String sql, String[] selectionArgs)
Perform the query and wrap as a result
-
-
-
Method Detail
-
execSQL
public static void execSQL(Connection connection, String sql)
Execute the SQL- Parameters:
connection
- connectionsql
- sql statement
-
query
public static ResultSet query(Connection connection, String sql, String[] selectionArgs)
Query for results- Parameters:
connection
- connectionsql
- sql statementselectionArgs
- 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 setsql
- SQL statementselectionArgs
- 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
- connectionsql
- SQL statementselectionArgs
- 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
- connectionsql
- sql statementargs
- argumentscolumn
- column indexdataType
- 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
- connectionsql
- sql statementargs
- argumentscolumn
- column indexdataType
- GeoPackage data typelimit
- 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
- connectionsql
- sql statementargs
- argumentsdataTypes
- column data typeslimit
- 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
- connectiontable
- table namewhere
- where clauseargs
- 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
- connectiontable
- table namevalues
- content valueswhereClause
- where clausewhereArgs
- where arguments- Returns:
- updated count
-
insert
public static long insert(Connection connection, String table, ContentValues values)
Insert a new row- Parameters:
connection
- connectiontable
- table namevalues
- 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
- connectiontable
- table namevalues
- content values- Returns:
- row id
-
insert
public static long insert(Connection connection, String table, String pkColumn, ContentValues values)
Insert a new row- Parameters:
connection
- connectiontable
- table namepkColumn
- primary key id columnvalues
- 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
- connectiontable
- table namepkColumn
- primary key id columnvalues
- 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 statementselectionArgs
- selection arguments- Throws:
SQLException
- upon failure
-
closeStatement
public static void closeStatement(Statement statement, String sql)
Close the statement- Parameters:
statement
- statementsql
- sql statement
-
closeResultSet
public static void closeResultSet(ResultSet resultSet, String sql)
Close the ResultSet- Parameters:
resultSet
- result setsql
- 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 setsql
- sql statement
-
wrapQuery
public static ResultSetResult wrapQuery(Connection connection, String sql, String[] selectionArgs)
Perform the query and wrap as a result- Parameters:
connection
- connectionsql
- sql statementselectionArgs
- 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
- connectionsuccessful
- 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
- connectionsuccessful
- true to commit, false to rollbackautoCommit
- pre-transaction auto commit value- Since:
- 3.3.0
-
-