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