Home | History | Annotate | Download | only in util
      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 package com.android.providers.contacts.util;
     17 
     18 import android.content.ContentValues;
     19 import android.database.DatabaseUtils;
     20 import android.text.TextUtils;
     21 
     22 import java.util.HashMap;
     23 
     24 /**
     25  * Static methods for helping us build database query selection strings.
     26  */
     27 public 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 in-equality of a field to a value. */
     38     public static String getInequalityClause(String field, String value) {
     39         return getClauseWithOperator(field, "!=", value);
     40     }
     41 
     42     private static String getClauseWithOperator(String field, String operator, String value) {
     43         StringBuilder clause = new StringBuilder();
     44         clause.append("(");
     45         clause.append(field);
     46         clause.append(" ").append(operator).append(" ");
     47         DatabaseUtils.appendEscapedSQLString(clause, value);
     48         clause.append(")");
     49         return clause.toString();
     50     }
     51 
     52     /** Concatenates any number of clauses using "AND". */
     53     public static String concatenateClauses(String... clauses) {
     54         StringBuilder builder = new StringBuilder();
     55         for (String clause : clauses) {
     56             if (!TextUtils.isEmpty(clause)) {
     57                 if (builder.length() > 0) {
     58                     builder.append(" AND ");
     59                 }
     60                 builder.append("(");
     61                 builder.append(clause);
     62                 builder.append(")");
     63             }
     64         }
     65         return builder.toString();
     66     }
     67 
     68     /**
     69      * Checks if the given ContentValues contains values within the projection
     70      * map.
     71      * @throws IllegalArgumentException if any value in values is not found in
     72      * the projection map.
     73      */
     74     public static void checkForSupportedColumns(HashMap<String, String> projectionMap,
     75             ContentValues values) {
     76         for (String requestedColumn : values.keySet()) {
     77             if (!projectionMap.keySet().contains(requestedColumn)) {
     78                 throw new IllegalArgumentException("Column '" + requestedColumn + "' is invalid.");
     79             }
     80         }
     81     }
     82 }
     83