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