2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.utils;
20 import android.app.Application;
21 import android.content.res.Resources;
22 import android.database.SQLException;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.database.sqlite.SQLiteOpenHelper;
25 import android.util.Log;
27 import androidx.lifecycle.MutableLiveData;
29 import net.ktnx.mobileledger.BuildConfig;
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.util.Locale;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 import static net.ktnx.mobileledger.utils.Logger.debug;
41 public class MobileLedgerDatabase extends SQLiteOpenHelper {
42 public static final MutableLiveData<Boolean> initComplete = new MutableLiveData<>(false);
43 public static final String DB_NAME = "MoLe.db";
44 private static final int LATEST_REVISION = 55;
45 private static final String CREATE_DB_SQL = "create_db";
46 private final Application mContext;
48 public MobileLedgerDatabase(Application context) {
49 super(context, DB_NAME, null, LATEST_REVISION);
50 debug("db", "creating helper instance");
52 super.setWriteAheadLoggingEnabled(true);
56 public void onCreate(SQLiteDatabase db) {
57 debug("db", "onCreate called");
58 applyRevisionFile(db, CREATE_DB_SQL);
62 public void onConfigure(SQLiteDatabase db) {
63 super.onConfigure(db);
64 db.execSQL("pragma case_sensitive_like=ON;");
65 if (BuildConfig.DEBUG)
66 db.execSQL("PRAGMA foreign_keys=ON");
69 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
71 String.format(Locale.US, "needs upgrade from version %d to version %d", oldVersion,
73 for (int i = oldVersion + 1; i <= newVersion; i++)
76 private void applyRevision(SQLiteDatabase db, int rev_no) {
77 String rev_file = String.format(Locale.US, "sql_%d", rev_no);
79 applyRevisionFile(db, rev_file);
81 private void applyRevisionFile(SQLiteDatabase db, String rev_file) {
82 final Resources rm = mContext.getResources();
83 int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
85 throw new SQLException(String.format(Locale.US, "No resource for %s", rev_file));
86 try (InputStream res = rm.openRawResource(res_id)) {
87 debug("db", "Applying " + rev_file);
88 InputStreamReader isr = new InputStreamReader(res);
89 BufferedReader reader = new BufferedReader(isr);
91 Pattern continuation = Pattern.compile("\\\\\\s*$");
94 String sqlStatement = null;
96 while ((line = reader.readLine()) != null) {
97 if (line.startsWith("--")) {
101 if (line.isEmpty()) {
105 if (sqlStatement == null)
108 sqlStatement = sqlStatement.concat(line);
110 Matcher m = continuation.matcher(line);
117 db.execSQL(sqlStatement);
120 catch (Exception e) {
121 throw new RuntimeException(
122 String.format("Error applying %s, line %d", rev_file, line_no), e);
127 if (sqlStatement != null)
128 throw new RuntimeException(
129 String.format("Error applying %s: EOF after continuation", rev_file));
132 catch (IOException e) {
133 Log.e("db", String.format("Error opening raw resource for %s", rev_file));