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  */
16 
17 package com.example.android.voicemail.common.utils;
18 
19 import android.database.DatabaseUtils;
20 import android.text.TextUtils;
21 
22 /**
23  * Static methods for helping us build database query selection strings.
24  */
25 public class DbQueryUtils {
26     // Static class with helper methods, so private constructor.
DbQueryUtils()27     private DbQueryUtils() {
28     }
29 
30     /** Returns a WHERE clause assert equality of a field to a value for the specified table . */
getEqualityClause(String table, String field, String value)31     public static String getEqualityClause(String table, String field, String value) {
32         return getEqualityClause(table + "." + field, value);
33     }
34 
35     /** Returns a WHERE clause assert equality of a field to a value. */
getEqualityClause(String field, String value)36     public static String getEqualityClause(String field, String value) {
37         StringBuilder clause = new StringBuilder();
38         clause.append(field);
39         clause.append(" = ");
40         DatabaseUtils.appendEscapedSQLString(clause, value);
41         return clause.toString();
42     }
43 
44     /** Concatenates any number of clauses using "AND". */
45     // TODO: 0. It worries me that I can change the following "AND" to "OR" and the provider tests
46     // all pass. I can also remove the braces, and the tests all pass.
concatenateClausesWithAnd(String... clauses)47     public static String concatenateClausesWithAnd(String... clauses) {
48         return concatenateClausesWithOperation("AND", clauses);
49     }
50 
51     /** Concatenates any number of clauses using "OR". */
concatenateClausesWithOr(String... clauses)52     public static String concatenateClausesWithOr(String... clauses) {
53         return concatenateClausesWithOperation("OR", clauses);
54     }
55 
56     /** Concatenates any number of clauses using the specified operation. */
concatenateClausesWithOperation(String operation, String... clauses)57     public static String concatenateClausesWithOperation(String operation, String... clauses) {
58         // Nothing to concatenate.
59         if (clauses.length == 1) {
60             return clauses[0];
61         }
62 
63         StringBuilder builder = new StringBuilder();
64 
65         for (String clause : clauses) {
66             if (!TextUtils.isEmpty(clause)) {
67                 if (builder.length() > 0) {
68                     builder.append(" ").append(operation).append(" ");
69                 }
70                 builder.append("(");
71                 builder.append(clause);
72                 builder.append(")");
73             }
74         }
75         return builder.toString();
76     }
77 }
78