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.db;
20 import android.content.res.Resources;
21 import android.database.SQLException;
23 import androidx.annotation.NonNull;
24 import androidx.room.Database;
25 import androidx.room.Room;
26 import androidx.room.RoomDatabase;
27 import androidx.room.migration.Migration;
28 import androidx.sqlite.db.SupportSQLiteDatabase;
30 import net.ktnx.mobileledger.App;
31 import net.ktnx.mobileledger.dao.AccountDAO;
32 import net.ktnx.mobileledger.dao.AccountValueDAO;
33 import net.ktnx.mobileledger.dao.CurrencyDAO;
34 import net.ktnx.mobileledger.dao.OptionDAO;
35 import net.ktnx.mobileledger.dao.TemplateAccountDAO;
36 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
37 import net.ktnx.mobileledger.dao.TransactionDAO;
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.InputStreamReader;
43 import java.util.Locale;
44 import java.util.regex.Matcher;
45 import java.util.regex.Pattern;
47 import static net.ktnx.mobileledger.utils.Logger.debug;
49 @Database(version = DB.REVISION,
50 entities = {TemplateHeader.class, TemplateAccount.class, Currency.class, Account.class,
51 Profile.class, Option.class, AccountValue.class, DescriptionHistory.class,
52 Transaction.class, TransactionAccount.class
54 abstract public class DB extends RoomDatabase {
55 public static final int REVISION = 59;
56 public static final String DB_NAME = "MoLe.db";
57 private static DB instance;
58 public static DB get() {
61 synchronized (DB.class) {
65 return instance = Room.databaseBuilder(App.instance, DB.class, DB_NAME)
66 .addMigrations(new Migration[]{singleVersionMigration(17),
67 singleVersionMigration(18),
68 singleVersionMigration(19),
69 singleVersionMigration(20),
70 multiVersionMigration(20, 22),
71 multiVersionMigration(22, 30),
72 multiVersionMigration(30, 32),
73 multiVersionMigration(32, 34),
74 multiVersionMigration(34, 40),
75 singleVersionMigration(41),
76 multiVersionMigration(41, 58),
77 singleVersionMigration(59)
79 .addCallback(new Callback() {
81 public void onOpen(@NonNull SupportSQLiteDatabase db) {
83 db.execSQL("PRAGMA foreign_keys = ON");
84 db.execSQL("pragma case_sensitive_like=ON;");
91 private static Migration singleVersionMigration(int toVersion) {
92 return new Migration(toVersion - 1, toVersion) {
94 public void migrate(@NonNull SupportSQLiteDatabase db) {
95 String fileName = String.format(Locale.US, "db_%d", toVersion);
97 applyRevisionFile(db, fileName);
101 private static Migration multiVersionMigration(int fromVersion, int toVersion) {
102 return new Migration(fromVersion, toVersion) {
104 public void migrate(@NonNull SupportSQLiteDatabase db) {
105 String fileName = String.format(Locale.US, "db_%d_%d", fromVersion, toVersion);
107 applyRevisionFile(db, fileName);
111 public static void applyRevisionFile(@NonNull SupportSQLiteDatabase db, String fileName) {
112 final Resources rm = App.instance.getResources();
113 int res_id = rm.getIdentifier(fileName, "raw", App.instance.getPackageName());
115 throw new SQLException(String.format(Locale.US, "No resource for %s", fileName));
117 try (InputStream res = rm.openRawResource(res_id)) {
118 debug("db", "Applying " + fileName);
119 InputStreamReader isr = new InputStreamReader(res);
120 BufferedReader reader = new BufferedReader(isr);
122 Pattern endOfStatement = Pattern.compile(";\\s*(?:--.*)?$");
125 String sqlStatement = null;
127 while ((line = reader.readLine()) != null) {
129 if (line.startsWith("--"))
134 if (sqlStatement == null)
137 sqlStatement = sqlStatement.concat(" " + line);
139 Matcher m = endOfStatement.matcher(line);
144 db.execSQL(sqlStatement);
147 catch (Exception e) {
148 throw new RuntimeException(
149 String.format("Error applying %s, line %d, statement: %s", fileName,
150 lineNo, sqlStatement), e);
154 if (sqlStatement != null)
155 throw new RuntimeException(String.format(
156 "Error applying %s: EOF after continuation. Line %s, Incomplete " +
157 "statement: %s", fileName, lineNo, sqlStatement));
160 catch (IOException e) {
161 throw new RuntimeException(String.format("Error opening raw resource for %s", fileName),
165 public abstract TemplateHeaderDAO getTemplateDAO();
167 public abstract TemplateAccountDAO getTemplateAccountDAO();
169 public abstract CurrencyDAO getCurrencyDAO();
171 public abstract AccountDAO getAccountDAO();
173 public abstract AccountValueDAO getAccountValueDAO();
175 public abstract TransactionDAO getTransactionDAO();
177 public abstract OptionDAO getOptionDAO();