• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.database.Cursor;
22 import android.util.Log;
23 
24 /**
25  * Static methods for database operations.
26  */
27 public class MoreDatabaseUtils {
28 
29     /**
30      * Builds a CREATE INDEX ddl statement for a given table and field.
31      *
32      * @param table The table name.
33      * @param field The field to index.
34      * @return The create index sql statement.
35      */
buildCreateIndexSql(String table, String field)36     public static String buildCreateIndexSql(String table, String field) {
37         return "CREATE INDEX " + buildIndexName(table, field) + " ON " + table
38                 + "(" + field + ")";
39     }
40 
41     /**
42      * Builds a DROP INDEX ddl statement for a given table and field.
43      *
44      * @param table The table name that was originally used to create the index.
45      * @param field The field that was originally used to create the index.
46      * @return The drop index sql statement.
47      */
48     @NeededForTesting
buildDropIndexSql(String table, String field)49     public static String buildDropIndexSql(String table, String field) {
50         return "DROP INDEX IF EXISTS " + buildIndexName(table, field);
51     }
52 
53     /**
54      * The index is created with a name using the following convention:
55      * <p>
56      * [table name]_[field name]_index
57      */
buildIndexName(String table, String field)58     public static String buildIndexName(String table, String field) {
59         return table + "_" + field + "_index";
60     }
61 
62     /**
63      * Build a bind arg where clause.
64      * <p>
65      * e.g. Calling this method with value of 4 results in:
66      * <p>
67      * "?,?,?,?"
68      *
69      * @param numArgs The number of arguments.
70      * @return A string that can be used for bind args in a sql where clause.
71      */
72     @NeededForTesting
buildBindArgString(int numArgs)73     public static String buildBindArgString(int numArgs) {
74         final StringBuilder sb = new StringBuilder();
75         String delimiter = "";
76         for (int i = 0; i < numArgs; i++) {
77             sb.append(delimiter).append("?");
78             delimiter = ",";
79         }
80         return sb.toString();
81     }
82 
83     /** Debug utility that dumps a cursor on logcat. */
dumpCursor(String logTag, String name, Cursor c)84     public static final void dumpCursor(String logTag, String name, Cursor c) {
85         Log.d(logTag, "Dumping cursor " + name + " containing " + c.getCount() + " rows");
86 
87         // Dump the column names.
88         final StringBuilder sb = new StringBuilder();
89         for (int i = 0; i < c.getColumnCount(); i++) {
90             if (sb.length() > 0) sb.append(" ");
91             sb.append(c.getColumnName(i));
92         }
93         Log.d(logTag, sb.toString());
94 
95         // Dump the values.
96         c.moveToPosition(-1);
97         while (c.moveToNext()) {
98             sb.setLength(0);
99             sb.append("row#");
100             sb.append(c.getPosition());
101 
102             for (int i = 0; i < c.getColumnCount(); i++) {
103                 sb.append(" ");
104 
105                 String s = c.getString(i);
106                 sb.append(s == null ? "{null}" : s.replaceAll("\\s", "{space}"));
107             }
108             Log.d(logTag, sb.toString());
109         }
110     }
111 }
112