Class SQLiteQueryBuilder
-
- All Implemented Interfaces:
public class SQLiteQueryBuilderThis is a convience class that helps build SQL queries to be sent to SQLiteDatabase objects.
-
-
Constructor Summary
Constructors Constructor Description SQLiteQueryBuilder()
-
Method Summary
Modifier and Type Method Description voidsetDistinct(boolean distinct)Mark the query as DISTINCT. StringgetTables()Returns the list of tables being queried voidsetTables(String inTables)Sets the list of tables to query. voidappendWhere(CharSequence inWhere)Append a chunk to the WHERE clause of the query. voidappendWhereEscapeString(String inWhere)Append a chunk to the WHERE clause of the query. voidsetProjectionMap(Map<String, String> columnMap)Sets the projection map for the query. voidsetCursorFactory(SQLiteDatabase.CursorFactory factory)Sets the cursor factory to be used for the query. voidsetStrict(boolean flag)When set, the selection is verified against malicious arguments. static StringbuildQueryString(boolean distinct, String tables, Array<String> columns, String where, String groupBy, String having, String orderBy, String limit)Build an SQL query string from the given clauses. static voidappendColumns(StringBuilder s, Array<String> columns)Add the names that are non-null in columns to s, separating them with commas. Cursorquery(SQLiteDatabase db, Array<String> projectionIn, String selection, Array<String> selectionArgs, String groupBy, String having, String sortOrder)Perform a query by combining all current settings and the information passed into this method. Cursorquery(SQLiteDatabase db, Array<String> projectionIn, String selection, Array<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. Cursorquery(SQLiteDatabase db, Array<String> projectionIn, String selection, Array<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. StringbuildQuery(Array<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. StringbuildQuery(Array<String> projectionIn, String selection, Array<String> selectionArgs, String groupBy, String having, String sortOrder, String limit)StringbuildUnionSubQuery(String typeDiscriminatorColumn, Array<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. StringbuildUnionSubQuery(String typeDiscriminatorColumn, Array<String> unionColumns, Set<String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, Array<String> selectionArgs, String groupBy, String having)StringbuildUnionQuery(Array<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. -
-
Method Detail
-
setDistinct
void setDistinct(boolean distinct)
Mark the query as DISTINCT.
- Parameters:
distinct- if true the query is DISTINCT, otherwise it isn't
-
getTables
String getTables()
Returns the list of tables being queried
- Returns:
the list of tables being queried
-
setTables
void setTables(String inTables)
Sets the list of tables to query. Multiple tables can be specified to perform a join. For example: setTables("foo, bar") setTables("foo LEFT OUTER JOIN bar ON (foo.id = bar.foo_id)")
- Parameters:
inTables- the list of tables to query on
-
appendWhere
void appendWhere(CharSequence inWhere)
Append a chunk to the WHERE clause of the query. All chunks appended are surrounded by parenthesis and ANDed with the selection passed to query. The final WHERE clause looks like: WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>)
- Parameters:
inWhere- the chunk of text to append to the WHERE clause.
-
appendWhereEscapeString
void appendWhereEscapeString(String inWhere)
Append a chunk to the WHERE clause of the query. All chunks appended are surrounded by parenthesis and ANDed with the selection passed to query. The final WHERE clause looks like: WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>)
- Parameters:
inWhere- the chunk of text to append to the WHERE clause.
-
setProjectionMap
void setProjectionMap(Map<String, String> columnMap)
Sets the projection map for the query. The projection map maps from column names that the caller passes into query to database column names. This is useful for renaming columns as well as disambiguating column names when doing joins. For example you could map "name" to "people.name". If a projection map is set it must contain all column names the user may request, even if the key and value are the same.
- Parameters:
columnMap- maps from the user column names to the database column names
-
setCursorFactory
void setCursorFactory(SQLiteDatabase.CursorFactory factory)
Sets the cursor factory to be used for the query. You can use one factory for all queries on a database but it is normally easier to specify the factory when doing this query.
- Parameters:
factory- the factory to use.
-
setStrict
void setStrict(boolean flag)
When set, the selection is verified against malicious arguments. When using this class to create a statement using buildQueryString, 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 or query, 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:
- Set this value to true
- Use a projection map
- Use one of the query overloads instead of getting the statement as a sql string
-
buildQueryString
static String buildQueryString(boolean distinct, String tables, Array<String> columns, String where, String groupBy, String having, String orderBy, String limit)
Build an SQL query string from the given clauses.
- Parameters:
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.where- A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself).groupBy- A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself).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).orderBy- How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself).limit- Limits the number of rows returned by the query, formatted as LIMIT clause.- Returns:
the SQL query string
-
appendColumns
static void appendColumns(StringBuilder s, Array<String> columns)
Add the names that are non-null in columns to s, separating them with commas.
-
query
Cursor query(SQLiteDatabase db, Array<String> projectionIn, String selection, Array<String> selectionArgs, String groupBy, String having, String sortOrder)
Perform a query by combining all current settings and the information passed into this method.
- Parameters:
db- the database to query onprojectionIn- A list of which columns to return.selection- A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself).selectionArgs- You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection.groupBy- A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself).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).sortOrder- How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself).- Returns:
a cursor over the result set
-
query
Cursor query(SQLiteDatabase db, Array<String> projectionIn, String selection, Array<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.
- Parameters:
db- the database to query onprojectionIn- A list of which columns to return.selection- A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself).selectionArgs- You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection.groupBy- A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself).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).sortOrder- How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself).limit- Limits the number of rows returned by the query, formatted as LIMIT clause.- Returns:
a cursor over the result set
-
query
Cursor query(SQLiteDatabase db, Array<String> projectionIn, String selection, Array<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.
- Parameters:
db- the database to query onprojectionIn- A list of which columns to return.selection- A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself).selectionArgs- You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection.groupBy- A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself).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).sortOrder- How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself).limit- Limits the number of rows returned by the query, formatted as LIMIT clause.cancellationSignal- A signal to cancel the operation in progress, or null if none.- Returns:
a cursor over the result set
-
buildQuery
String buildQuery(Array<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.
- Parameters:
projectionIn- A list of which columns to return.selection- A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself).groupBy- A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself).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).sortOrder- How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself).limit- Limits the number of rows returned by the query, formatted as LIMIT clause.- Returns:
the resulting SQL SELECT statement
-
buildQuery
@Deprecated() String buildQuery(Array<String> projectionIn, String selection, Array<String> selectionArgs, String groupBy, String having, String sortOrder, String limit)
-
buildUnionSubQuery
String buildUnionSubQuery(String typeDiscriminatorColumn, Array<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.
- Parameters:
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.columnsPresentInTable- a Set of the names of the columns that appear in this table (i.e.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.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).groupBy- A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself).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).- Returns:
the resulting SQL SELECT statement
-
buildUnionSubQuery
@Deprecated() String buildUnionSubQuery(String typeDiscriminatorColumn, Array<String> unionColumns, Set<String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, Array<String> selectionArgs, String groupBy, String having)
-
buildUnionQuery
String buildUnionQuery(Array<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.
- Parameters:
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).limit- The limit clause, which applies to the entire union result set- Returns:
the resulting SQL SELECT statement
-
-
-
-