Home | History | Annotate | Download | only in database
      1 /*
      2  * Copyright (C) 2013 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.android.providers.contacts.database;
     18 
     19 import com.android.providers.contacts.util.NeededForTesting;
     20 
     21 /**
     22  * Static methods for database operations.
     23  */
     24 public class MoreDatabaseUtils {
     25 
     26     /**
     27      * Builds a CREATE INDEX ddl statement for a given table and field.
     28      *
     29      * @param table The table name.
     30      * @param field The field to index.
     31      * @return The create index sql statement.
     32      */
     33     public static String buildCreateIndexSql(String table, String field) {
     34         return "CREATE INDEX " + buildIndexName(table, field) + " ON " + table
     35                 + "(" + field + ")";
     36     }
     37 
     38     /**
     39      * Builds a DROP INDEX ddl statement for a given table and field.
     40      *
     41      * @param table The table name that was originally used to create the index.
     42      * @param field The field that was originally used to create the index.
     43      * @return The drop index sql statement.
     44      */
     45     @NeededForTesting
     46     public static String buildDropIndexSql(String table, String field) {
     47         return "DROP INDEX IF EXISTS " + buildIndexName(table, field);
     48     }
     49 
     50     /**
     51      * The index is created with a name using the following convention:
     52      * <p>
     53      * [table name]_[field name]_index
     54      */
     55     public static String buildIndexName(String table, String field) {
     56         return table + "_" + field + "_index";
     57     }
     58 
     59     /**
     60      * Build a bind arg where clause.
     61      * <p>
     62      * e.g. Calling this method with value of 4 results in:
     63      * <p>
     64      * "?,?,?,?"
     65      *
     66      * @param numArgs The number of arguments.
     67      * @return A string that can be used for bind args in a sql where clause.
     68      */
     69     @NeededForTesting
     70     public static String buildBindArgString(int numArgs) {
     71         final StringBuilder sb = new StringBuilder();
     72         String delimiter = "";
     73         for (int i = 0; i < numArgs; i++) {
     74             sb.append(delimiter).append("?");
     75             delimiter = ",";
     76         }
     77         return sb.toString();
     78     }
     79 }
     80