DbQueryUtils.java revision 9978b26dd17bb2b20b91101f1e4682604336b5f6
1/*
2 * Copyright (C) 2011 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 */
16package com.android.providers.contacts.util;
17
18import android.content.ContentValues;
19import android.database.DatabaseUtils;
20import android.text.TextUtils;
21
22import java.util.HashMap;
23
24/**
25 * Static methods for helping us build database query selection strings.
26 */
27public class DbQueryUtils {
28    // Static class with helper methods, so private constructor.
29    private DbQueryUtils() {
30    }
31
32    /** Returns a WHERE clause asserting equality of a field to a value. */
33    public static String getEqualityClause(String field, String value) {
34        return getClauseWithOperator(field, "=", value);
35    }
36
37    /** Returns a WHERE clause asserting equality of a field to a value. */
38    public static String getEqualityClause(String field, long value) {
39        return getClauseWithOperator(field, "=", value);
40    }
41
42    /** Returns a WHERE clause asserting in-equality of a field to a value. */
43    public static String getInequalityClause(String field, long value) {
44        return getClauseWithOperator(field, "!=", value);
45    }
46
47    private static String getClauseWithOperator(String field, String operator, String value) {
48        StringBuilder clause = new StringBuilder();
49        clause.append("(");
50        clause.append(field);
51        clause.append(" ").append(operator).append(" ");
52        DatabaseUtils.appendEscapedSQLString(clause, value);
53        clause.append(")");
54        return clause.toString();
55    }
56
57    private static String getClauseWithOperator(String field, String operator, long value) {
58        StringBuilder clause = new StringBuilder();
59        clause.append("(");
60        clause.append(field);
61        clause.append(" ").append(operator).append(" ");
62        clause.append(value);
63        clause.append(")");
64        return clause.toString();
65    }
66
67    /** Concatenates any number of clauses using "AND". */
68    public static String concatenateClauses(String... clauses) {
69        StringBuilder builder = new StringBuilder();
70        for (String clause : clauses) {
71            if (!TextUtils.isEmpty(clause)) {
72                if (builder.length() > 0) {
73                    builder.append(" AND ");
74                }
75                builder.append("(");
76                builder.append(clause);
77                builder.append(")");
78            }
79        }
80        return builder.toString();
81    }
82
83    /**
84     * Checks if the given ContentValues contains values within the projection
85     * map.
86     * @throws IllegalArgumentException if any value in values is not found in
87     * the projection map.
88     */
89    public static void checkForSupportedColumns(HashMap<String, String> projectionMap,
90            ContentValues values) {
91        for (String requestedColumn : values.keySet()) {
92            if (!projectionMap.keySet().contains(requestedColumn)) {
93                throw new IllegalArgumentException("Column '" + requestedColumn + "' is invalid.");
94            }
95        }
96    }
97}
98