Package 

Class SQLiteQueryBuilder

    • Constructor Detail

      • SQLiteQueryBuilder

        SQLiteQueryBuilder()
    • Method Detail

      • setDistinct

         void setDistinct(boolean distinct)

        Mark the query as DISTINCT.

        Parameters:
        distinct - if true the query is DISTINCT, otherwise it isn't
      • 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 surroundedby parenthesis and ANDed with the selection passed to query. The finalWHERE 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 surroundedby parenthesis and ANDed with the selection passed to query. The finalWHERE 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 mapsfrom column names that the caller passes into query to databasecolumn names. This is useful for renaming columns as well asdisambiguating column names when doing joins. For example youcould map "name" to "people.name". If a projection map is setit must contain all column names the user may request, even ifthe 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 useone factory for all queries on a database but it is normallyeasier 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, fieldsnot 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 examplecontent 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
        By default, this value is false.
      • 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 SQLWHERE clause (excluding the WHERE itself).
        groupBy - A filter declaring how to group rows, formatted as an SQLGROUP 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 HAVINGclause (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.
      • 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 theinformation passed into this method.

        Parameters:
        db - the database to query on
        projectionIn - A list of which columns to return.
        selection - A filter declaring which rows to return,formatted as an SQL WHERE clause (excluding the WHEREitself).
        selectionArgs - You may include ?s in selection, whichwill be replaced by the values from selectionArgs, in orderthat they appear in the selection.
        groupBy - A filter declaring how to group rows, formattedas an SQL GROUP BY clause (excluding the GROUP BYitself).
        having - A filter declare which row groups to include inthe cursor, if row grouping is being used, formatted as anSQL HAVING clause (excluding the HAVING itself).
        sortOrder - How to order the rows, formatted as an SQLORDER BY clause (excluding the ORDER BY itself).
      • 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 theinformation passed into this method.

        Parameters:
        db - the database to query on
        projectionIn - A list of which columns to return.
        selection - A filter declaring which rows to return,formatted as an SQL WHERE clause (excluding the WHEREitself).
        selectionArgs - You may include ?s in selection, whichwill be replaced by the values from selectionArgs, in orderthat they appear in the selection.
        groupBy - A filter declaring how to group rows, formattedas an SQL GROUP BY clause (excluding the GROUP BYitself).
        having - A filter declare which row groups to include inthe cursor, if row grouping is being used, formatted as anSQL HAVING clause (excluding the HAVING itself).
        sortOrder - How to order the rows, formatted as an SQLORDER BY clause (excluding the ORDER BY itself).
        limit - Limits the number of rows returned by the query,formatted as LIMIT clause.
      • 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 theinformation passed into this method.

        Parameters:
        db - the database to query on
        projectionIn - A list of which columns to return.
        selection - A filter declaring which rows to return,formatted as an SQL WHERE clause (excluding the WHEREitself).
        selectionArgs - You may include ?s in selection, whichwill be replaced by the values from selectionArgs, in orderthat they appear in the selection.
        groupBy - A filter declaring how to group rows, formattedas an SQL GROUP BY clause (excluding the GROUP BYitself).
        having - A filter declare which row groups to include inthe cursor, if row grouping is being used, formatted as anSQL HAVING clause (excluding the HAVING itself).
        sortOrder - How to order the rows, formatted as an SQLORDER 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.
      • 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 ofSELECT statements that will be joined through UNION operatorsin 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 WHEREitself).
        groupBy - A filter declaring how to group rows, formattedas 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 inthe cursor, if row grouping is being used, formatted as anSQL HAVING clause (excluding the HAVING itself).
        sortOrder - How to order the rows, formatted as an SQLORDER BY clause (excluding the ORDER BY itself).
        limit - Limits the number of rows returned by the query,formatted as LIMIT clause.
      • 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 ofSELECT statements that will be joined through UNION operatorsin buildUnionQuery.

        Parameters:
        typeDiscriminatorColumn - the name of the result columnwhose cells will contain the name of the table from whicheach row was drawn.
        unionColumns - the names of the columns to appear in theresult.
        columnsPresentInTable - a Set of the names of the columnsthat appear in this table (i.e.
        computedColumnsOffset - all columns in unionColumns beforethis index are included under the assumption that they'recomputed and therefore won't appear in columnsPresentInTable,e.g.
        typeDiscriminatorValue - the value used for thetype-discriminator column in this subquery
        selection - A filter declaring which rows to return,formatted as an SQL WHERE clause (excluding the WHEREitself).
        groupBy - A filter declaring how to group rows, formattedas 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 inthe cursor, if row grouping is being used, formatted as anSQL HAVING clause (excluding the HAVING itself).
      • 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 thosesubqueries return.

        Parameters:
        subQueries - an array of SQL SELECT statements, all ofwhich must have the same columns as the same positions intheir results
        sortOrder - How to order the rows, formatted as an SQLORDER BY clause (excluding the ORDER BY itself).
        limit - The limit clause, which applies to the entire union result set