1 /*
2  * Copyright (C) 2017 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 android.inputmethodservice.cts.db;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.database.sqlite.SQLiteOpenHelper;
24 
25 import androidx.annotation.NonNull;
26 
27 import java.util.List;
28 
29 /**
30  * Abstraction of SQLite database.
31  */
32 public abstract class Database {
33 
34     private final SQLiteOpenHelper mHelper;
35 
36     /**
37      * Create {@link Database}.
38      *
39      * @param context to use for locating paths to the the database.
40      * @param name of the database file, or null for an in-memory database.
41      * @param version number of the database (starting at 1).
42      */
Database(Context context, String name, int version)43     public Database(Context context, String name, int version) {
44         mHelper = new SQLiteOpenHelper(context, name, null /* factory */, version) {
45             @Override
46             public void onCreate(SQLiteDatabase db) {
47                 db.beginTransaction();
48                 try {
49                     for (Table table : getTables()) {
50                         db.execSQL(table.createTableSql());
51                     }
52                     db.setTransactionSuccessful();
53                 } finally {
54                     db.endTransaction();
55                 }
56             }
57 
58             @Override
59             public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
60                 // nothing to do so far.
61             }
62         };
63     }
64 
65     @NonNull
getTables()66     protected abstract List<Table> getTables();
67 
68     /**
69      * A wrapper method for {@link SQLiteDatabase#query(String, String[], String, String[], String,
70      * String, String)}.
71      *
72      * @param table the table name to compile the query against.
73      * @param projection a list of which columns to return. Passing {@code null} will return all
74      *                   columns, which is discouraged to prevent reading data from storage that
75      *                   isn't going to be used.
76      * @param selection a filter declaring which rows to return, formatted as an SQL WHERE clause
77      *                  (excluding the WHERE itself). Passing {@code null} will return all rows for
78      *                  the given table.
79      * @param selectionArgs you may include {@code ?}s in selection, which will be replaced by the
80      *                      values from selectionArgs, in order that they appear in the selection.
81      *                      The values will be bound as Strings.
82      * @param orderBy how to order the rows, formatted as an SQL ORDER BY clause (excluding the
83      *                ORDER BY itself). Passing null will use the default sort order, which may be
84      *                unordered.
85      * @return A {@link Cursor} object, which is positioned before the first entry. Note that
86      *         {@link Cursor}s are not synchronized, see the documentation for more details.
87      */
query(String table, String[] projection, String selection, String[] selectionArgs, String orderBy)88     public Cursor query(String table, String[] projection, String selection, String[] selectionArgs,
89             String orderBy) {
90         return mHelper.getReadableDatabase()
91                 .query(table, projection, selection, selectionArgs, null /* groupBy */,
92                         null /* having */, orderBy);
93     }
94 
95     /**
96      * A wrapper method for {@link SQLiteDatabase#insert(String, String, ContentValues)}.
97      *
98      * @param table the table to insert the row into.
99      * @param values this map contains the initial column values for the row. The keys should be the
100      *               column names and the values the column values.
101      * @return the row ID of the newly inserted row, or {@code -1} if an error occurred.
102      */
insert(String table, ContentValues values)103     public long insert(String table, ContentValues values) {
104         return mHelper.getWritableDatabase().insert(table, null /* nullColumnHack */, values);
105     }
106 
107     /**
108      * A wrapper method for {@link SQLiteDatabase#delete(String, String, String[])}.
109      *
110      * @param table the table to delete from.
111      * @param selection the optional WHERE clause to apply when deleting.  Passing {@code null} will
112      *                  delete all rows.
113      * @param selectionArgs You may include {@code ?}s in the where clause, which will be replaced
114      *                      by the values from whereArgs. The values will be bound as Strings.
115      * @return the number of rows affected if a whereClause is passed in, {@code 0} otherwise. To
116      *         remove all rows and get a count pass {@code 1} as the {@code selection}.
117      */
delete(String table, String selection, String[] selectionArgs)118     public int delete(String table, String selection, String[] selectionArgs) {
119         return mHelper.getWritableDatabase().delete(table, selection, selectionArgs);
120     }
121 
122     /**
123      * A wrapper method for {@link SQLiteDatabase#update(String, ContentValues, String, String[])}.
124      *
125      * @param table the table to update in
126      * @param values a map from column names to new column values. {@code null} is a valid value
127      *               that will be translated to NULL.
128      * @param selection the optional WHERE clause to apply when updating. Passing {@code null} will
129      *                  update all rows.
130      * @param selectionArgs You may include {@code ?}s in the where clause, which will be replaced
131      *                      by the values from whereArgs. The values will be bound as Strings.
132      * @return the number of rows affected
133      */
update(String table, ContentValues values, String selection, String[] selectionArgs)134     public int update(String table, ContentValues values, String selection,
135             String[] selectionArgs) {
136         return mHelper.getWritableDatabase().update(table, values, selection, selectionArgs);
137     }
138 
139     /**
140      * Close any open database object.
141      */
close()142     public void close() {
143         mHelper.close();
144     }
145 }
146