]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java
enable the foreign_keys PRAGMA only in debug builds
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / MobileLedgerDatabase.java
1 /*
2  * Copyright © 2020 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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
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;
26
27 import net.ktnx.mobileledger.BuildConfig;
28
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.util.Locale;
34
35 import static net.ktnx.mobileledger.utils.Logger.debug;
36
37 public class MobileLedgerDatabase extends SQLiteOpenHelper {
38     private static final String DB_NAME = "MoLe.db";
39     private static final int LATEST_REVISION = 40;
40     private static final String CREATE_DB_SQL = "create_db";
41
42     private final Application mContext;
43
44     public MobileLedgerDatabase(Application context) {
45         super(context, DB_NAME, null, LATEST_REVISION);
46         debug("db", "creating helper instance");
47         mContext = context;
48         super.setWriteAheadLoggingEnabled(true);
49     }
50
51     @Override
52     public void onCreate(SQLiteDatabase db) {
53         debug("db", "onCreate called");
54         applyRevisionFile(db, CREATE_DB_SQL);
55     }
56
57     @Override
58     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
59         debug("db",
60                 String.format(Locale.US, "needs upgrade from version %d to version %d", oldVersion,
61                         newVersion));
62         for (int i = oldVersion + 1; i <= newVersion; i++)
63             applyRevision(db, i);
64     }
65     @Override
66     public void onOpen(SQLiteDatabase db) {
67         super.onOpen(db);
68         db.execSQL("pragma case_sensitive_like=ON;");
69         if (BuildConfig.DEBUG)
70             db.execSQL("PRAGMA foreign_keys=ON");
71     }
72
73     private void applyRevision(SQLiteDatabase db, int rev_no) {
74         String rev_file = String.format(Locale.US, "sql_%d", rev_no);
75
76         applyRevisionFile(db, rev_file);
77     }
78     private void applyRevisionFile(SQLiteDatabase db, String rev_file) {
79         final Resources rm = mContext.getResources();
80         int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
81         if (res_id == 0)
82             throw new SQLException(String.format(Locale.US, "No resource for %s", rev_file));
83         db.beginTransaction();
84         try (InputStream res = rm.openRawResource(res_id)) {
85             debug("db", "Applying " + rev_file);
86             InputStreamReader isr = new InputStreamReader(res);
87             BufferedReader reader = new BufferedReader(isr);
88
89             String line;
90             int line_no = 1;
91             while ((line = reader.readLine()) != null) {
92                 if (line.startsWith("--")) {
93                     line_no++;
94                     continue;
95                 }
96                 if (line.isEmpty()) {
97                     line_no++;
98                     continue;
99                 }
100                 try {
101                     db.execSQL(line);
102                 }
103                 catch (Exception e) {
104                     throw new RuntimeException(
105                             String.format("Error applying %s, line %d", rev_file, line_no), e);
106                 }
107                 line_no++;
108             }
109
110             db.setTransactionSuccessful();
111         }
112         catch (IOException e) {
113             Log.e("db", String.format("Error opening raw resource for %s", rev_file));
114             e.printStackTrace();
115         }
116         finally {
117             db.endTransaction();
118         }
119     }
120 }