SQLiteQueryBuilder.java revision e5360fbf3efe85427f7e7f59afe7bbeddb4949ac
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.database.sqlite;
18
19import android.database.Cursor;
20import android.database.DatabaseUtils;
21import android.provider.BaseColumns;
22import android.text.TextUtils;
23import android.util.Log;
24
25import java.util.Iterator;
26import java.util.Map;
27import java.util.Map.Entry;
28import java.util.Set;
29import java.util.regex.Pattern;
30
31/**
32 * This is a convience class that helps build SQL queries to be sent to
33 * {@link SQLiteDatabase} objects.
34 */
35public class SQLiteQueryBuilder
36{
37    private static final String TAG = "SQLiteQueryBuilder";
38    private static final Pattern sLimitPattern =
39            Pattern.compile("\\s*\\d+\\s*(,\\s*\\d+\\s*)?");
40
41    private Map<String, String> mProjectionMap = null;
42    private String mTables = "";
43    private StringBuilder mWhereClause = null;  // lazily created
44    private boolean mDistinct;
45    private SQLiteDatabase.CursorFactory mFactory;
46    private boolean mStrict;
47
48    public SQLiteQueryBuilder() {
49        mDistinct = false;
50        mFactory = null;
51    }
52
53    /**
54     * Mark the query as DISTINCT.
55     *
56     * @param distinct if true the query is DISTINCT, otherwise it isn't
57     */
58    public void setDistinct(boolean distinct) {
59        mDistinct = distinct;
60    }
61
62    /**
63     * Returns the list of tables being queried
64     *
65     * @return the list of tables being queried
66     */
67    public String getTables() {
68        return mTables;
69    }
70
71    /**
72     * Sets the list of tables to query. Multiple tables can be specified to perform a join.
73     * For example:
74     *   setTables("foo, bar")
75     *   setTables("foo LEFT OUTER JOIN bar ON (foo.id = bar.foo_id)")
76     *
77     * @param inTables the list of tables to query on
78     */
79    public void setTables(String inTables) {
80        mTables = inTables;
81    }
82
83    /**
84     * Append a chunk to the WHERE clause of the query. All chunks appended are surrounded
85     * by parenthesis and ANDed with the selection passed to {@link #query}. The final
86     * WHERE clause looks like:
87     *
88     * WHERE (&lt;append chunk 1>&lt;append chunk2>) AND (&lt;query() selection parameter>)
89     *
90     * @param inWhere the chunk of text to append to the WHERE clause.
91     */
92    public void appendWhere(CharSequence inWhere) {
93        if (mWhereClause == null) {
94            mWhereClause = new StringBuilder(inWhere.length() + 16);
95        }
96        if (mWhereClause.length() == 0) {
97            mWhereClause.append('(');
98        }
99        mWhereClause.append(inWhere);
100    }
101
102    /**
103     * Append a chunk to the WHERE clause of the query. All chunks appended are surrounded
104     * by parenthesis and ANDed with the selection passed to {@link #query}. The final
105     * WHERE clause looks like:
106     *
107     * WHERE (&lt;append chunk 1>&lt;append chunk2>) AND (&lt;query() selection parameter>)
108     *
109     * @param inWhere the chunk of text to append to the WHERE clause. it will be escaped
110     * to avoid SQL injection attacks
111     */
112    public void appendWhereEscapeString(String inWhere) {
113        if (mWhereClause == null) {
114            mWhereClause = new StringBuilder(inWhere.length() + 16);
115        }
116        if (mWhereClause.length() == 0) {
117            mWhereClause.append('(');
118        }
119        DatabaseUtils.appendEscapedSQLString(mWhereClause, inWhere);
120    }
121
122    /**
123     * Sets the projection map for the query.  The projection map maps
124     * from column names that the caller passes into query to database
125     * column names. This is useful for renaming columns as well as
126     * disambiguating column names when doing joins. For example you
127     * could map "name" to "people.name".  If a projection map is set
128     * it must contain all column names the user may request, even if
129     * the key and value are the same.
130     *
131     * @param columnMap maps from the user column names to the database column names
132     */
133    public void setProjectionMap(Map<String, String> columnMap) {
134        mProjectionMap = columnMap;
135    }
136
137    /**
138     * Sets the cursor factory to be used for the query.  You can use
139     * one factory for all queries on a database but it is normally
140     * easier to specify the factory when doing this query.  @param
141     * factory the factor to use
142     */
143    public void setCursorFactory(SQLiteDatabase.CursorFactory factory) {
144        mFactory = factory;
145    }
146
147    /**
148     * When set, the selection is verified against malicious arguments.
149     * When using this class to create a statement using
150     * {@link #buildQueryString(boolean, String, String[], String, String, String, String, String)},
151     * non-numeric limits will raise an exception. If a projection map is specified, fields
152     * not in that map will be ignored.
153     * If this class is used to execute the statement directly using
154     * {@link #query(SQLiteDatabase, String[], String, String[], String, String, String)}
155     * or
156     * {@link #query(SQLiteDatabase, String[], String, String[], String, String, String, String)},
157     * additionally also parenthesis escaping selection are caught.
158     *
159     * To summarize: To get maximum protection against malicious third party apps (for example
160     * content provider consumers), make sure to do the following:
161     * <ul>
162     * <li>Set this value to true</li>
163     * <li>Use a projection map</li>
164     * <li>Use one of the query overloads instead of getting the statement as a sql string</li>
165     * </ul>
166     * By default, this value is false.
167     */
168    public void setStrict(boolean flag) {
169        mStrict = flag;
170    }
171
172    /**
173     * Build an SQL query string from the given clauses.
174     *
175     * @param distinct true if you want each row to be unique, false otherwise.
176     * @param tables The table names to compile the query against.
177     * @param columns A list of which columns to return. Passing null will
178     *            return all columns, which is discouraged to prevent reading
179     *            data from storage that isn't going to be used.
180     * @param where A filter declaring which rows to return, formatted as an SQL
181     *            WHERE clause (excluding the WHERE itself). Passing null will
182     *            return all rows for the given URL.
183     * @param groupBy A filter declaring how to group rows, formatted as an SQL
184     *            GROUP BY clause (excluding the GROUP BY itself). Passing null
185     *            will cause the rows to not be grouped.
186     * @param having A filter declare which row groups to include in the cursor,
187     *            if row grouping is being used, formatted as an SQL HAVING
188     *            clause (excluding the HAVING itself). Passing null will cause
189     *            all row groups to be included, and is required when row
190     *            grouping is not being used.
191     * @param orderBy How to order the rows, formatted as an SQL ORDER BY clause
192     *            (excluding the ORDER BY itself). Passing null will use the
193     *            default sort order, which may be unordered.
194     * @param limit Limits the number of rows returned by the query,
195     *            formatted as LIMIT clause. Passing null denotes no LIMIT clause.
196     * @return the SQL query string
197     */
198    public static String buildQueryString(
199            boolean distinct, String tables, String[] columns, String where,
200            String groupBy, String having, String orderBy, String limit) {
201        if (TextUtils.isEmpty(groupBy) && !TextUtils.isEmpty(having)) {
202            throw new IllegalArgumentException(
203                    "HAVING clauses are only permitted when using a groupBy clause");
204        }
205        if (!TextUtils.isEmpty(limit) && !sLimitPattern.matcher(limit).matches()) {
206            throw new IllegalArgumentException("invalid LIMIT clauses:" + limit);
207        }
208
209        StringBuilder query = new StringBuilder(120);
210
211        query.append("SELECT ");
212        if (distinct) {
213            query.append("DISTINCT ");
214        }
215        if (columns != null && columns.length != 0) {
216            appendColumns(query, columns);
217        } else {
218            query.append("* ");
219        }
220        query.append("FROM ");
221        query.append(tables);
222        appendClause(query, " WHERE ", where);
223        appendClause(query, " GROUP BY ", groupBy);
224        appendClause(query, " HAVING ", having);
225        appendClause(query, " ORDER BY ", orderBy);
226        appendClause(query, " LIMIT ", limit);
227
228        return query.toString();
229    }
230
231    private static void appendClause(StringBuilder s, String name, String clause) {
232        if (!TextUtils.isEmpty(clause)) {
233            s.append(name);
234            s.append(clause);
235        }
236    }
237
238    /**
239     * Add the names that are non-null in columns to s, separating
240     * them with commas.
241     */
242    public static void appendColumns(StringBuilder s, String[] columns) {
243        int n = columns.length;
244
245        for (int i = 0; i < n; i++) {
246            String column = columns[i];
247
248            if (column != null) {
249                if (i > 0) {
250                    s.append(", ");
251                }
252                s.append(column);
253            }
254        }
255        s.append(' ');
256    }
257
258    /**
259     * Perform a query by combining all current settings and the
260     * information passed into this method.
261     *
262     * @param db the database to query on
263     * @param projectionIn A list of which columns to return. Passing
264     *   null will return all columns, which is discouraged to prevent
265     *   reading data from storage that isn't going to be used.
266     * @param selection A filter declaring which rows to return,
267     *   formatted as an SQL WHERE clause (excluding the WHERE
268     *   itself). Passing null will return all rows for the given URL.
269     * @param selectionArgs You may include ?s in selection, which
270     *   will be replaced by the values from selectionArgs, in order
271     *   that they appear in the selection. The values will be bound
272     *   as Strings.
273     * @param groupBy A filter declaring how to group rows, formatted
274     *   as an SQL GROUP BY clause (excluding the GROUP BY
275     *   itself). Passing null will cause the rows to not be grouped.
276     * @param having A filter declare which row groups to include in
277     *   the cursor, if row grouping is being used, formatted as an
278     *   SQL HAVING clause (excluding the HAVING itself).  Passing
279     *   null will cause all row groups to be included, and is
280     *   required when row grouping is not being used.
281     * @param sortOrder How to order the rows, formatted as an SQL
282     *   ORDER BY clause (excluding the ORDER BY itself). Passing null
283     *   will use the default sort order, which may be unordered.
284     * @return a cursor over the result set
285     * @see android.content.ContentResolver#query(android.net.Uri, String[],
286     *      String, String[], String)
287     */
288    public Cursor query(SQLiteDatabase db, String[] projectionIn,
289            String selection, String[] selectionArgs, String groupBy,
290            String having, String sortOrder) {
291        return query(db, projectionIn, selection, selectionArgs, groupBy, having, sortOrder,
292                null /* limit */);
293    }
294
295    /**
296     * Perform a query by combining all current settings and the
297     * information passed into this method.
298     *
299     * @param db the database to query on
300     * @param projectionIn A list of which columns to return. Passing
301     *   null will return all columns, which is discouraged to prevent
302     *   reading data from storage that isn't going to be used.
303     * @param selection A filter declaring which rows to return,
304     *   formatted as an SQL WHERE clause (excluding the WHERE
305     *   itself). Passing null will return all rows for the given URL.
306     * @param selectionArgs You may include ?s in selection, which
307     *   will be replaced by the values from selectionArgs, in order
308     *   that they appear in the selection. The values will be bound
309     *   as Strings.
310     * @param groupBy A filter declaring how to group rows, formatted
311     *   as an SQL GROUP BY clause (excluding the GROUP BY
312     *   itself). Passing null will cause the rows to not be grouped.
313     * @param having A filter declare which row groups to include in
314     *   the cursor, if row grouping is being used, formatted as an
315     *   SQL HAVING clause (excluding the HAVING itself).  Passing
316     *   null will cause all row groups to be included, and is
317     *   required when row grouping is not being used.
318     * @param sortOrder How to order the rows, formatted as an SQL
319     *   ORDER BY clause (excluding the ORDER BY itself). Passing null
320     *   will use the default sort order, which may be unordered.
321     * @param limit Limits the number of rows returned by the query,
322     *   formatted as LIMIT clause. Passing null denotes no LIMIT clause.
323     * @return a cursor over the result set
324     * @see android.content.ContentResolver#query(android.net.Uri, String[],
325     *      String, String[], String)
326     */
327    public Cursor query(SQLiteDatabase db, String[] projectionIn,
328            String selection, String[] selectionArgs, String groupBy,
329            String having, String sortOrder, String limit) {
330        if (mTables == null) {
331            return null;
332        }
333
334        if (mStrict && selection != null && selection.length() > 0) {
335            // Validate the user-supplied selection to detect syntactic anomalies
336            // in the selection string that could indicate a SQL injection attempt.
337            // The idea is to ensure that the selection clause is a valid SQL expression
338            // by compiling it twice: once wrapped in parentheses and once as
339            // originally specified. An attacker cannot create an expression that
340            // would escape the SQL expression while maintaining balanced parentheses
341            // in both the wrapped and original forms.
342            String sqlForValidation = buildQuery(projectionIn, "(" + selection + ")", groupBy,
343                    having, sortOrder, limit);
344            validateQuerySql(db, sqlForValidation); // will throw if query is invalid
345        }
346
347        String sql = buildQuery(
348                projectionIn, selection, groupBy, having,
349                sortOrder, limit);
350
351        if (Log.isLoggable(TAG, Log.DEBUG)) {
352            Log.d(TAG, "Performing query: " + sql);
353        }
354        return db.rawQueryWithFactory(
355                mFactory, sql, selectionArgs,
356                SQLiteDatabase.findEditTable(mTables)); // will throw if query is invalid
357    }
358
359    /**
360     * Verifies that a SQL SELECT statement is valid by compiling it.
361     * If the SQL statement is not valid, this method will throw a {@link SQLiteException}.
362     */
363    private void validateQuerySql(SQLiteDatabase db, String sql) {
364        db.getThreadSession().prepare(sql,
365                db.getThreadDefaultConnectionFlags(true /*readOnly*/), null);
366    }
367
368    /**
369     * Construct a SELECT statement suitable for use in a group of
370     * SELECT statements that will be joined through UNION operators
371     * in buildUnionQuery.
372     *
373     * @param projectionIn A list of which columns to return. Passing
374     *    null will return all columns, which is discouraged to
375     *    prevent reading data from storage that isn't going to be
376     *    used.
377     * @param selection A filter declaring which rows to return,
378     *   formatted as an SQL WHERE clause (excluding the WHERE
379     *   itself).  Passing null will return all rows for the given
380     *   URL.
381     * @param groupBy A filter declaring how to group rows, formatted
382     *   as an SQL GROUP BY clause (excluding the GROUP BY itself).
383     *   Passing null will cause the rows to not be grouped.
384     * @param having A filter declare which row groups to include in
385     *   the cursor, if row grouping is being used, formatted as an
386     *   SQL HAVING clause (excluding the HAVING itself).  Passing
387     *   null will cause all row groups to be included, and is
388     *   required when row grouping is not being used.
389     * @param sortOrder How to order the rows, formatted as an SQL
390     *   ORDER BY clause (excluding the ORDER BY itself). Passing null
391     *   will use the default sort order, which may be unordered.
392     * @param limit Limits the number of rows returned by the query,
393     *   formatted as LIMIT clause. Passing null denotes no LIMIT clause.
394     * @return the resulting SQL SELECT statement
395     */
396    public String buildQuery(
397            String[] projectionIn, String selection, String groupBy,
398            String having, String sortOrder, String limit) {
399        String[] projection = computeProjection(projectionIn);
400
401        StringBuilder where = new StringBuilder();
402        boolean hasBaseWhereClause = mWhereClause != null && mWhereClause.length() > 0;
403
404        if (hasBaseWhereClause) {
405            where.append(mWhereClause.toString());
406            where.append(')');
407        }
408
409        // Tack on the user's selection, if present.
410        if (selection != null && selection.length() > 0) {
411            if (hasBaseWhereClause) {
412                where.append(" AND ");
413            }
414
415            where.append('(');
416            where.append(selection);
417            where.append(')');
418        }
419
420        return buildQueryString(
421                mDistinct, mTables, projection, where.toString(),
422                groupBy, having, sortOrder, limit);
423    }
424
425    /**
426     * @deprecated This method's signature is misleading since no SQL parameter
427     * substitution is carried out.  The selection arguments parameter does not get
428     * used at all.  To avoid confusion, call
429     * {@link #buildQuery(String[], String, String, String, String, String)} instead.
430     */
431    @Deprecated
432    public String buildQuery(
433            String[] projectionIn, String selection, String[] selectionArgs,
434            String groupBy, String having, String sortOrder, String limit) {
435        return buildQuery(projectionIn, selection, groupBy, having, sortOrder, limit);
436    }
437
438    /**
439     * Construct a SELECT statement suitable for use in a group of
440     * SELECT statements that will be joined through UNION operators
441     * in buildUnionQuery.
442     *
443     * @param typeDiscriminatorColumn the name of the result column
444     *   whose cells will contain the name of the table from which
445     *   each row was drawn.
446     * @param unionColumns the names of the columns to appear in the
447     *   result.  This may include columns that do not appear in the
448     *   table this SELECT is querying (i.e. mTables), but that do
449     *   appear in one of the other tables in the UNION query that we
450     *   are constructing.
451     * @param columnsPresentInTable a Set of the names of the columns
452     *   that appear in this table (i.e. in the table whose name is
453     *   mTables).  Since columns in unionColumns include columns that
454     *   appear only in other tables, we use this array to distinguish
455     *   which ones actually are present.  Other columns will have
456     *   NULL values for results from this subquery.
457     * @param computedColumnsOffset all columns in unionColumns before
458     *   this index are included under the assumption that they're
459     *   computed and therefore won't appear in columnsPresentInTable,
460     *   e.g. "date * 1000 as normalized_date"
461     * @param typeDiscriminatorValue the value used for the
462     *   type-discriminator column in this subquery
463     * @param selection A filter declaring which rows to return,
464     *   formatted as an SQL WHERE clause (excluding the WHERE
465     *   itself).  Passing null will return all rows for the given
466     *   URL.
467     * @param groupBy A filter declaring how to group rows, formatted
468     *   as an SQL GROUP BY clause (excluding the GROUP BY itself).
469     *   Passing null will cause the rows to not be grouped.
470     * @param having A filter declare which row groups to include in
471     *   the cursor, if row grouping is being used, formatted as an
472     *   SQL HAVING clause (excluding the HAVING itself).  Passing
473     *   null will cause all row groups to be included, and is
474     *   required when row grouping is not being used.
475     * @return the resulting SQL SELECT statement
476     */
477    public String buildUnionSubQuery(
478            String typeDiscriminatorColumn,
479            String[] unionColumns,
480            Set<String> columnsPresentInTable,
481            int computedColumnsOffset,
482            String typeDiscriminatorValue,
483            String selection,
484            String groupBy,
485            String having) {
486        int unionColumnsCount = unionColumns.length;
487        String[] projectionIn = new String[unionColumnsCount];
488
489        for (int i = 0; i < unionColumnsCount; i++) {
490            String unionColumn = unionColumns[i];
491
492            if (unionColumn.equals(typeDiscriminatorColumn)) {
493                projectionIn[i] = "'" + typeDiscriminatorValue + "' AS "
494                        + typeDiscriminatorColumn;
495            } else if (i <= computedColumnsOffset
496                       || columnsPresentInTable.contains(unionColumn)) {
497                projectionIn[i] = unionColumn;
498            } else {
499                projectionIn[i] = "NULL AS " + unionColumn;
500            }
501        }
502        return buildQuery(
503                projectionIn, selection, groupBy, having,
504                null /* sortOrder */,
505                null /* limit */);
506    }
507
508    /**
509     * @deprecated This method's signature is misleading since no SQL parameter
510     * substitution is carried out.  The selection arguments parameter does not get
511     * used at all.  To avoid confusion, call
512     * {@link #buildUnionSubQuery}
513     * instead.
514     */
515    @Deprecated
516    public String buildUnionSubQuery(
517            String typeDiscriminatorColumn,
518            String[] unionColumns,
519            Set<String> columnsPresentInTable,
520            int computedColumnsOffset,
521            String typeDiscriminatorValue,
522            String selection,
523            String[] selectionArgs,
524            String groupBy,
525            String having) {
526        return buildUnionSubQuery(
527                typeDiscriminatorColumn, unionColumns, columnsPresentInTable,
528                computedColumnsOffset, typeDiscriminatorValue, selection,
529                groupBy, having);
530    }
531
532    /**
533     * Given a set of subqueries, all of which are SELECT statements,
534     * construct a query that returns the union of what those
535     * subqueries return.
536     * @param subQueries an array of SQL SELECT statements, all of
537     *   which must have the same columns as the same positions in
538     *   their results
539     * @param sortOrder How to order the rows, formatted as an SQL
540     *   ORDER BY clause (excluding the ORDER BY itself).  Passing
541     *   null will use the default sort order, which may be unordered.
542     * @param limit The limit clause, which applies to the entire union result set
543     *
544     * @return the resulting SQL SELECT statement
545     */
546    public String buildUnionQuery(String[] subQueries, String sortOrder, String limit) {
547        StringBuilder query = new StringBuilder(128);
548        int subQueryCount = subQueries.length;
549        String unionOperator = mDistinct ? " UNION " : " UNION ALL ";
550
551        for (int i = 0; i < subQueryCount; i++) {
552            if (i > 0) {
553                query.append(unionOperator);
554            }
555            query.append(subQueries[i]);
556        }
557        appendClause(query, " ORDER BY ", sortOrder);
558        appendClause(query, " LIMIT ", limit);
559        return query.toString();
560    }
561
562    private String[] computeProjection(String[] projectionIn) {
563        if (projectionIn != null && projectionIn.length > 0) {
564            if (mProjectionMap != null) {
565                String[] projection = new String[projectionIn.length];
566                int length = projectionIn.length;
567
568                for (int i = 0; i < length; i++) {
569                    String userColumn = projectionIn[i];
570                    String column = mProjectionMap.get(userColumn);
571
572                    if (column != null) {
573                        projection[i] = column;
574                        continue;
575                    }
576
577                    if (!mStrict &&
578                            ( userColumn.contains(" AS ") || userColumn.contains(" as "))) {
579                        /* A column alias already exist */
580                        projection[i] = userColumn;
581                        continue;
582                    }
583
584                    throw new IllegalArgumentException("Invalid column "
585                            + projectionIn[i]);
586                }
587                return projection;
588            } else {
589                return projectionIn;
590            }
591        } else if (mProjectionMap != null) {
592            // Return all columns in projection map.
593            Set<Entry<String, String>> entrySet = mProjectionMap.entrySet();
594            String[] projection = new String[entrySet.size()];
595            Iterator<Entry<String, String>> entryIter = entrySet.iterator();
596            int i = 0;
597
598            while (entryIter.hasNext()) {
599                Entry<String, String> entry = entryIter.next();
600
601                // Don't include the _count column when people ask for no projection.
602                if (entry.getKey().equals(BaseColumns._COUNT)) {
603                    continue;
604                }
605                projection[i++] = entry.getValue();
606            }
607            return projection;
608        }
609        return null;
610    }
611}
612