aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/providers/contacts/sqlite/DatabaseAnalyzer.java
blob: facd02e2b87cf31661dc55454e1d9d9b4e6b9ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.providers.contacts.sqlite;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.android.providers.contacts.AbstractContactsProvider;

import com.google.common.annotations.VisibleForTesting;

import java.util.ArrayList;
import java.util.List;

/**
 * Class to extract table/view/column names from databases.
 */
@VisibleForTesting
public class DatabaseAnalyzer {
    private static final String TAG = "DatabaseAnalyzer";

    private static final boolean VERBOSE_LOGGING = AbstractContactsProvider.VERBOSE_LOGGING;

    private DatabaseAnalyzer() {
    }

    /**
     * Find and return all table/view names in a db.
     */
    private static List<String> findTablesAndViews(SQLiteDatabase db) {
        final List<String> ret = new ArrayList<>();
        try (final Cursor c = db.rawQuery(
                "SELECT name FROM sqlite_master WHERE type in (\"table\", \"view\")", null)) {
            while (c.moveToNext()) {
                ret.add(c.getString(0).toLowerCase());
            }
        }
        return ret;
    }

    /**
     * Find all columns in a table/view.
     */
    private static List<String> findColumns(SQLiteDatabase db, String table) {
        final List<String> ret = new ArrayList<>();

        // Open the table/view but requests 0 rows.
        final Cursor c = db.rawQuery("SELECT * FROM " + table + " WHERE 0 LIMIT 0", null);
        try {
            // Collect the column names.
            for (int i = 0; i < c.getColumnCount(); i++) {
                ret.add(c.getColumnName(i).toLowerCase());
            }
        } finally {
            c.close();
        }
        return ret;
    }

    /**
     * Return all table/view names that clients shouldn't use in their queries -- basically the
     * result contains all table/view names, except for the names that are column names of any
     * tables.
     */
    @VisibleForTesting
    public static List<String> findTableViewsAllowingColumns(SQLiteDatabase db) {
        final List<String> tables = findTablesAndViews(db);
        if (VERBOSE_LOGGING) {
            Log.d(TAG, "Tables and views:");
        }
        final List<String> ret = new ArrayList<>(tables); // Start with the table/view list.
        for (String name : tables) {
            if (VERBOSE_LOGGING) {
                Log.d(TAG, "  " + name);
            }
            final List<String> columns = findColumns(db, name);
            if (VERBOSE_LOGGING) {
                Log.d(TAG, "    Columns: " + columns);
            }
            for (String c : columns) {
                if (ret.remove(c)) {
                    Log.d(TAG, "Removing [" + c + "] from disallow list");
                }
            }
        }
        return ret;
    }
}