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    /**
99     * Escape values to be used in LIKE sqlite clause.
100     *
101     * The LIKE clause has two special characters: '%' and '_'.  If either of these
102     * characters need to be matched literally, then they must be escaped like so:
103     *
104     * WHERE value LIKE 'android\_%' ESCAPE '\'
105     *
106     * The ESCAPE clause is required and no default exists as the escape character in this context.
107     * Since the escape character needs to be defined as part of the sql string, it must be
108     * provided to this method so the escape characters match.
109     *
110     * @param sb The StringBuilder to append the escaped value to.
111     * @param value The value to be escaped.
112     * @param escapeChar The escape character to be defined in the sql ESCAPE clause.
113     */
114    public static void escapeLikeValue(StringBuilder sb, String value, char escapeChar) {
115        for (int i = 0; i < value.length(); i++) {
116            char ch = value.charAt(i);
117            if (ch == '%' || ch == '_') {
118                sb.append(escapeChar);
119            }
120            sb.append(ch);
121        }
122    }
123
124}
125