Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DatabaseIndexUtil.java « util « shared « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8506c7133f452eaa11a8cc3ce49bc51cd050b8e9 (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
package it.niedermann.owncloud.notes.shared.util;

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

import androidx.annotation.NonNull;
import androidx.sqlite.db.SupportSQLiteDatabase;

public class DatabaseIndexUtil {

    private static final String TAG = DatabaseIndexUtil.class.getSimpleName();

    private DatabaseIndexUtil() {

    }

    public static void createIndex(@NonNull SupportSQLiteDatabase db, @NonNull String table, @NonNull String... columns) {
        for (String column : columns) {
            createIndex(db, table, column);
        }
    }

    public static void createIndex(@NonNull SupportSQLiteDatabase db, @NonNull String table, @NonNull String column) {
        String indexName = table + "_" + column + "_idx";
        Log.v(TAG, "Creating database index: CREATE INDEX IF NOT EXISTS " + indexName + " ON " + table + "(" + column + ")");
        db.execSQL("CREATE INDEX " + indexName + " ON " + table + "(" + column + ")");
    }

    public static void dropIndexes(@NonNull SupportSQLiteDatabase db) {
        try (Cursor c = db.query("SELECT name, sql FROM sqlite_master WHERE type = 'index'")) {
            while (c.moveToNext()) {
                // Skip automatic indexes which we can't drop manually
                if (c.getString(1) != null) {
                    Log.v(TAG, "Deleting database index: DROP INDEX " + c.getString(0));
                    db.execSQL("DROP INDEX " + c.getString(0));
                }
            }
        }
    }
}