public class SQLiteQueryBuilder extends Object
SQLiteDatabase
objects.Constructor and Description |
---|
SQLiteQueryBuilder() |
Modifier and Type | Method and Description |
---|---|
static void |
appendColumns(StringBuilder s,
String[] columns)
Add the names that are non-null in columns to s, separating
them with commas.
|
void |
appendWhere(CharSequence inWhere)
Append a chunk to the WHERE clause of the query.
|
void |
appendWhereEscapeString(String inWhere)
Append a chunk to the WHERE clause of the query.
|
String |
buildQuery(String[] projectionIn,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String sortOrder,
String limit)
Deprecated.
This method's signature is misleading since no SQL parameter
substitution is carried out. The selection arguments parameter does not get
used at all. To avoid confusion, call
buildQuery(String[], String, String, String, String, String) instead. |
String |
buildQuery(String[] projectionIn,
String selection,
String groupBy,
String having,
String sortOrder,
String limit)
Construct a SELECT statement suitable for use in a group of
SELECT statements that will be joined through UNION operators
in buildUnionQuery.
|
static String |
buildQueryString(boolean distinct,
String tables,
String[] columns,
String where,
String groupBy,
String having,
String orderBy,
String limit)
Build an SQL query string from the given clauses.
|
String |
buildUnionQuery(String[] subQueries,
String sortOrder,
String limit)
Given a set of subqueries, all of which are SELECT statements,
construct a query that returns the union of what those
subqueries return.
|
String |
buildUnionSubQuery(String typeDiscriminatorColumn,
String[] unionColumns,
Set<String> columnsPresentInTable,
int computedColumnsOffset,
String typeDiscriminatorValue,
String selection,
String[] selectionArgs,
String groupBy,
String having)
Deprecated.
This method's signature is misleading since no SQL parameter
substitution is carried out. The selection arguments parameter does not get
used at all. To avoid confusion, call
buildUnionSubQuery(java.lang.String, java.lang.String[], java.util.Set<java.lang.String>, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
instead. |
String |
buildUnionSubQuery(String typeDiscriminatorColumn,
String[] unionColumns,
Set<String> columnsPresentInTable,
int computedColumnsOffset,
String typeDiscriminatorValue,
String selection,
String groupBy,
String having)
Construct a SELECT statement suitable for use in a group of
SELECT statements that will be joined through UNION operators
in buildUnionQuery.
|
String |
getTables()
Returns the list of tables being queried
|
Cursor |
query(SQLiteDatabase db,
String[] projectionIn,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String sortOrder)
Perform a query by combining all current settings and the
information passed into this method.
|
Cursor |
query(SQLiteDatabase db,
String[] projectionIn,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String sortOrder,
String limit)
Perform a query by combining all current settings and the
information passed into this method.
|
Cursor |
query(SQLiteDatabase db,
String[] projectionIn,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String sortOrder,
String limit,
CancellationSignal cancellationSignal)
Perform a query by combining all current settings and the
information passed into this method.
|
void |
setCursorFactory(SQLiteDatabase.CursorFactory factory)
Sets the cursor factory to be used for the query.
|
void |
setDistinct(boolean distinct)
Mark the query as DISTINCT.
|
void |
setProjectionMap(Map<String,String> columnMap)
Sets the projection map for the query.
|
void |
setStrict(boolean flag)
When set, the selection is verified against malicious arguments.
|
void |
setTables(String inTables)
Sets the list of tables to query.
|
public void setDistinct(boolean distinct)
distinct
- if true the query is DISTINCT, otherwise it isn'tpublic String getTables()
public void setTables(String inTables)
inTables
- the list of tables to query onpublic void appendWhere(CharSequence inWhere)
query(android.database.sqlite.SQLiteDatabase, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)
. The final
WHERE clause looks like:
WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>)inWhere
- the chunk of text to append to the WHERE clause.public void appendWhereEscapeString(String inWhere)
query(android.database.sqlite.SQLiteDatabase, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)
. The final
WHERE clause looks like:
WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>)inWhere
- the chunk of text to append to the WHERE clause. it will be escaped
to avoid SQL injection attackspublic void setProjectionMap(Map<String,String> columnMap)
columnMap
- maps from the user column names to the database column namespublic void setCursorFactory(SQLiteDatabase.CursorFactory factory)
factory
- the factory to use.public void setStrict(boolean flag)
buildQueryString(boolean, String, String[], String, String, String, String, String)
,
non-numeric limits will raise an exception. If a projection map is specified, fields
not in that map will be ignored.
If this class is used to execute the statement directly using
query(SQLiteDatabase, String[], String, String[], String, String, String)
or
query(SQLiteDatabase, String[], String, String[], String, String, String, String)
,
additionally also parenthesis escaping selection are caught.
To summarize: To get maximum protection against malicious third party apps (for example
content provider consumers), make sure to do the following:
public static String buildQueryString(boolean distinct, String tables, String[] columns, String where, String groupBy, String having, String orderBy, String limit)
distinct
- true if you want each row to be unique, false otherwise.tables
- The table names to compile the query against.columns
- A list of which columns to return. Passing null will
return all columns, which is discouraged to prevent reading
data from storage that isn't going to be used.where
- A filter declaring which rows to return, formatted as an SQL
WHERE clause (excluding the WHERE itself). Passing null will
return all rows for the given URL.groupBy
- A filter declaring how to group rows, formatted as an SQL
GROUP BY clause (excluding the GROUP BY itself). Passing null
will cause the rows to not be grouped.having
- A filter declare which row groups to include in the cursor,
if row grouping is being used, formatted as an SQL HAVING
clause (excluding the HAVING itself). Passing null will cause
all row groups to be included, and is required when row
grouping is not being used.orderBy
- How to order the rows, formatted as an SQL ORDER BY clause
(excluding the ORDER BY itself). Passing null will use the
default sort order, which may be unordered.limit
- Limits the number of rows returned by the query,
formatted as LIMIT clause. Passing null denotes no LIMIT clause.public static void appendColumns(StringBuilder s, String[] columns)
public Cursor query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)
db
- the database to query onprojectionIn
- A list of which columns to return. Passing
null will return all columns, which is discouraged to prevent
reading data from storage that isn't going to be used.selection
- A filter declaring which rows to return,
formatted as an SQL WHERE clause (excluding the WHERE
itself). Passing null will return all rows for the given URL.selectionArgs
- You may include ?s in selection, which
will be replaced by the values from selectionArgs, in order
that they appear in the selection. The values will be bound
as Strings.groupBy
- A filter declaring how to group rows, formatted
as an SQL GROUP BY clause (excluding the GROUP BY
itself). Passing null will cause the rows to not be grouped.having
- A filter declare which row groups to include in
the cursor, if row grouping is being used, formatted as an
SQL HAVING clause (excluding the HAVING itself). Passing
null will cause all row groups to be included, and is
required when row grouping is not being used.sortOrder
- How to order the rows, formatted as an SQL
ORDER BY clause (excluding the ORDER BY itself). Passing null
will use the default sort order, which may be unordered.ContentResolver.query(android.net.Uri, String[],
String, String[], String)
public Cursor query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)
db
- the database to query onprojectionIn
- A list of which columns to return. Passing
null will return all columns, which is discouraged to prevent
reading data from storage that isn't going to be used.selection
- A filter declaring which rows to return,
formatted as an SQL WHERE clause (excluding the WHERE
itself). Passing null will return all rows for the given URL.selectionArgs
- You may include ?s in selection, which
will be replaced by the values from selectionArgs, in order
that they appear in the selection. The values will be bound
as Strings.groupBy
- A filter declaring how to group rows, formatted
as an SQL GROUP BY clause (excluding the GROUP BY
itself). Passing null will cause the rows to not be grouped.having
- A filter declare which row groups to include in
the cursor, if row grouping is being used, formatted as an
SQL HAVING clause (excluding the HAVING itself). Passing
null will cause all row groups to be included, and is
required when row grouping is not being used.sortOrder
- How to order the rows, formatted as an SQL
ORDER BY clause (excluding the ORDER BY itself). Passing null
will use the default sort order, which may be unordered.limit
- Limits the number of rows returned by the query,
formatted as LIMIT clause. Passing null denotes no LIMIT clause.ContentResolver.query(android.net.Uri, String[],
String, String[], String)
public Cursor query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit, CancellationSignal cancellationSignal)
db
- the database to query onprojectionIn
- A list of which columns to return. Passing
null will return all columns, which is discouraged to prevent
reading data from storage that isn't going to be used.selection
- A filter declaring which rows to return,
formatted as an SQL WHERE clause (excluding the WHERE
itself). Passing null will return all rows for the given URL.selectionArgs
- You may include ?s in selection, which
will be replaced by the values from selectionArgs, in order
that they appear in the selection. The values will be bound
as Strings.groupBy
- A filter declaring how to group rows, formatted
as an SQL GROUP BY clause (excluding the GROUP BY
itself). Passing null will cause the rows to not be grouped.having
- A filter declare which row groups to include in
the cursor, if row grouping is being used, formatted as an
SQL HAVING clause (excluding the HAVING itself). Passing
null will cause all row groups to be included, and is
required when row grouping is not being used.sortOrder
- How to order the rows, formatted as an SQL
ORDER BY clause (excluding the ORDER BY itself). Passing null
will use the default sort order, which may be unordered.limit
- Limits the number of rows returned by the query,
formatted as LIMIT clause. Passing null denotes no LIMIT clause.cancellationSignal
- A signal to cancel the operation in progress, or null if none.
If the operation is canceled, then OperationCanceledException
will be thrown
when the query is executed.ContentResolver.query(android.net.Uri, String[],
String, String[], String)
public String buildQuery(String[] projectionIn, String selection, String groupBy, String having, String sortOrder, String limit)
projectionIn
- A list of which columns to return. Passing
null will return all columns, which is discouraged to
prevent reading data from storage that isn't going to be
used.selection
- A filter declaring which rows to return,
formatted as an SQL WHERE clause (excluding the WHERE
itself). Passing null will return all rows for the given
URL.groupBy
- A filter declaring how to group rows, formatted
as an SQL GROUP BY clause (excluding the GROUP BY itself).
Passing null will cause the rows to not be grouped.having
- A filter declare which row groups to include in
the cursor, if row grouping is being used, formatted as an
SQL HAVING clause (excluding the HAVING itself). Passing
null will cause all row groups to be included, and is
required when row grouping is not being used.sortOrder
- How to order the rows, formatted as an SQL
ORDER BY clause (excluding the ORDER BY itself). Passing null
will use the default sort order, which may be unordered.limit
- Limits the number of rows returned by the query,
formatted as LIMIT clause. Passing null denotes no LIMIT clause.@Deprecated public String buildQuery(String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)
buildQuery(String[], String, String, String, String, String)
instead.public String buildUnionSubQuery(String typeDiscriminatorColumn, String[] unionColumns, Set<String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, String groupBy, String having)
typeDiscriminatorColumn
- the name of the result column
whose cells will contain the name of the table from which
each row was drawn.unionColumns
- the names of the columns to appear in the
result. This may include columns that do not appear in the
table this SELECT is querying (i.e. mTables), but that do
appear in one of the other tables in the UNION query that we
are constructing.columnsPresentInTable
- a Set of the names of the columns
that appear in this table (i.e. in the table whose name is
mTables). Since columns in unionColumns include columns that
appear only in other tables, we use this array to distinguish
which ones actually are present. Other columns will have
NULL values for results from this subquery.computedColumnsOffset
- all columns in unionColumns before
this index are included under the assumption that they're
computed and therefore won't appear in columnsPresentInTable,
e.g. "date * 1000 as normalized_date"typeDiscriminatorValue
- the value used for the
type-discriminator column in this subqueryselection
- A filter declaring which rows to return,
formatted as an SQL WHERE clause (excluding the WHERE
itself). Passing null will return all rows for the given
URL.groupBy
- A filter declaring how to group rows, formatted
as an SQL GROUP BY clause (excluding the GROUP BY itself).
Passing null will cause the rows to not be grouped.having
- A filter declare which row groups to include in
the cursor, if row grouping is being used, formatted as an
SQL HAVING clause (excluding the HAVING itself). Passing
null will cause all row groups to be included, and is
required when row grouping is not being used.@Deprecated public String buildUnionSubQuery(String typeDiscriminatorColumn, String[] unionColumns, Set<String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, String[] selectionArgs, String groupBy, String having)
buildUnionSubQuery(java.lang.String, java.lang.String[], java.util.Set<java.lang.String>, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
instead.public String buildUnionQuery(String[] subQueries, String sortOrder, String limit)
subQueries
- an array of SQL SELECT statements, all of
which must have the same columns as the same positions in
their resultssortOrder
- How to order the rows, formatted as an SQL
ORDER BY clause (excluding the ORDER BY itself). Passing
null will use the default sort order, which may be unordered.limit
- The limit clause, which applies to the entire union result set