DbQueryUtils.java revision 52e8d24f8492116f0b49b147576ce13a5f913aa2
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.database.DatabaseUtils;
19import android.text.TextUtils;
20
21/**
22 * Static methods for helping us build database query selection strings.
23 */
24public class DbQueryUtils {
25    // Static class with helper methods, so private constructor.
26    private DbQueryUtils() {
27    }
28
29    /** Returns a WHERE clause asserting equality of a field to a value. */
30    public static String getEqualityClause(String field, String value) {
31        StringBuilder clause = new StringBuilder();
32        clause.append("(");
33        clause.append(field);
34        clause.append(" = ");
35        DatabaseUtils.appendEscapedSQLString(clause, value);
36        clause.append(")");
37        return clause.toString();
38    }
39
40    /** Concatenates any number of clauses using "AND". */
41    public static String concatenateClauses(String... clauses) {
42        StringBuilder builder = new StringBuilder();
43        for (String clause : clauses) {
44            if (!TextUtils.isEmpty(clause)) {
45                if (builder.length() > 0) {
46                    builder.append(" AND ");
47                }
48                builder.append("(");
49                builder.append(clause);
50                builder.append(")");
51            }
52        }
53        return builder.toString();
54    }
55}
56