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.CurrencyDAO;
33 import net.ktnx.mobileledger.dao.TemplateAccountDAO;
34 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
35 import net.ktnx.mobileledger.dao.TransactionDAO;
37 import java.io.BufferedReader;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.InputStreamReader;
41 import java.util.Locale;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
45 import static net.ktnx.mobileledger.utils.Logger.debug;
47 @Database(version = DB.REVISION,
48 entities = {TemplateHeader.class, TemplateAccount.class, Currency.class, Account.class,
49 Profile.class, Option.class, AccountValue.class, DescriptionHistory.class,
50 Transaction.class, TransactionAccount.class
52 abstract public class DB extends RoomDatabase {
53 public static final int REVISION = 59;
54 public static final String DB_NAME = "MoLe.db";
55 private static DB instance;
56 public static DB get() {
59 synchronized (DB.class) {
63 return instance = Room.databaseBuilder(App.instance, DB.class, DB_NAME)
64 .addMigrations(new Migration[]{singleVersionMigration(17),
65 singleVersionMigration(18),
66 singleVersionMigration(19),
67 singleVersionMigration(20),
68 multiVersionMigration(20, 22),
69 multiVersionMigration(22, 30),
70 multiVersionMigration(30, 32),
71 multiVersionMigration(32, 34),
72 multiVersionMigration(34, 40),
73 singleVersionMigration(41),
74 multiVersionMigration(41, 58),
76 .addCallback(new Callback() {
78 public void onOpen(@NonNull SupportSQLiteDatabase db) {
80 db.execSQL("PRAGMA foreign_keys = ON");
81 db.execSQL("pragma case_sensitive_like=ON;");
88 private static Migration singleVersionMigration(int toVersion) {
89 return new Migration(toVersion - 1, toVersion) {
91 public void migrate(@NonNull SupportSQLiteDatabase db) {
92 String fileName = String.format(Locale.US, "db_%d", toVersion);
94 applyRevisionFile(db, fileName);
98 private static Migration multiVersionMigration(int fromVersion, int toVersion) {
99 return new Migration(fromVersion, toVersion) {
101 public void migrate(@NonNull SupportSQLiteDatabase db) {
102 String fileName = String.format(Locale.US, "db_%d_%d", fromVersion, toVersion);
104 applyRevisionFile(db, fileName);
108 public static void applyRevisionFile(@NonNull SupportSQLiteDatabase db, String fileName) {
109 final Resources rm = App.instance.getResources();
110 int res_id = rm.getIdentifier(fileName, "raw", App.instance.getPackageName());
112 throw new SQLException(String.format(Locale.US, "No resource for %s", fileName));
114 try (InputStream res = rm.openRawResource(res_id)) {
115 debug("db", "Applying " + fileName);
116 InputStreamReader isr = new InputStreamReader(res);
117 BufferedReader reader = new BufferedReader(isr);
119 Pattern endOfStatement = Pattern.compile(";\\s*(?:--.*)?$");
122 String sqlStatement = null;
124 while ((line = reader.readLine()) != null) {
126 if (line.startsWith("--"))
131 if (sqlStatement == null)
134 sqlStatement = sqlStatement.concat(" " + line);
136 Matcher m = endOfStatement.matcher(line);
141 db.execSQL(sqlStatement);
144 catch (Exception e) {
145 throw new RuntimeException(
146 String.format("Error applying %s, line %d, statement: %s", fileName,
147 lineNo, sqlStatement), e);
151 if (sqlStatement != null)
152 throw new RuntimeException(String.format(
153 "Error applying %s: EOF after continuation. Line %s, Incomplete " +
154 "statement: %s", fileName, lineNo, sqlStatement));
157 catch (IOException e) {
158 throw new RuntimeException(String.format("Error opening raw resource for %s", fileName),
162 public abstract TemplateHeaderDAO getTemplateDAO();
164 public abstract TemplateAccountDAO getTemplateAccountDAO();
166 public abstract CurrencyDAO getCurrencyDAO();
168 public abstract AccountDAO getAccountDAO();
170 public abstract TransactionDAO getTransactionDAO();