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 androidx.annotation.NonNull;
21 import androidx.room.Database;
22 import androidx.room.Room;
23 import androidx.room.RoomDatabase;
24 import androidx.room.migration.Migration;
25 import androidx.sqlite.db.SupportSQLiteDatabase;
27 import net.ktnx.mobileledger.App;
28 import net.ktnx.mobileledger.dao.AccountDAO;
29 import net.ktnx.mobileledger.dao.CurrencyDAO;
30 import net.ktnx.mobileledger.dao.TemplateAccountDAO;
31 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
32 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
34 @Database(version = 58,
35 entities = {TemplateHeader.class, TemplateAccount.class, Currency.class, Account.class,
36 Profile.class, Option.class, AccountValue.class, DescriptionHistory.class,
39 abstract public class DB extends RoomDatabase {
40 private static DB instance;
41 public static DB get() {
44 synchronized (DB.class) {
49 Room.databaseBuilder(App.instance, DB.class, MobileLedgerDatabase.DB_NAME)
50 .addMigrations(new Migration[]{new Migration(51, 52) {
52 public void migrate(@NonNull SupportSQLiteDatabase db) {
53 db.beginTransaction();
55 db.execSQL("create index fk_pattern_accounts_pattern on " +
56 "pattern_accounts(pattern_id);");
57 db.execSQL("create index fk_pattern_accounts_currency on " +
58 "pattern_accounts(currency);");
59 db.setTransactionSuccessful();
65 }, new Migration(52, 53) {
67 public void migrate(@NonNull SupportSQLiteDatabase db) {
69 "alter table pattern_accounts add negate_amount boolean;");
71 }, new Migration(53, 54) {
73 public void migrate(@NonNull SupportSQLiteDatabase db) {
74 db.execSQL("CREATE TABLE templates (id INTEGER PRIMARY KEY " +
75 "AUTOINCREMENT NOT NULL, name TEXT NOT NULL, " +
76 "regular_expression TEXT NOT NULL, test_text TEXT, " +
77 "transaction_description TEXT, " +
78 "transaction_description_match_group INTEGER, " +
79 "transaction_comment TEXT, " +
80 "transaction_comment_match_group INTEGER, date_year " +
81 "INTEGER, date_year_match_group INTEGER, date_month " +
82 "INTEGER, date_month_match_group INTEGER, date_day " +
83 "INTEGER, date_day_match_group INTEGER)");
85 "CREATE TABLE template_accounts (id INTEGER PRIMARY KEY " +
86 "AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, " +
87 "acc TEXT, position INTEGER NOT NULL, acc_match_group " +
88 "INTEGER, currency INTEGER, currency_match_group INTEGER," +
89 " amount REAL, amount_match_group INTEGER, comment TEXT, " +
90 "comment_match_group INTEGER, negate_amount INTEGER, " +
91 "FOREIGN KEY(template_id) REFERENCES templates(id) ON " +
92 "UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY" +
93 "(currency) REFERENCES currencies(id) ON UPDATE NO ACTION" +
94 " ON DELETE NO ACTION )");
95 db.execSQL("insert into templates(id, name, regular_expression, " +
96 "test_text, transaction_description, " +
97 "transaction_description_match_group, " +
98 "transaction_comment, transaction_comment_match_group," +
99 " date_year, date_year_match_group, date_month, " +
100 "date_month_match_group, date_day, " +
101 "date_day_match_group)" +
102 " select id, name, regular_expression, test_text, " +
103 "transaction_description, " +
104 "transaction_description_match_group, " +
105 "transaction_comment, transaction_comment_match_group," +
106 " date_year, date_year_match_group, date_month, " +
107 "date_month_match_group, date_day, " +
108 "date_day_match_group from patterns");
109 db.execSQL("insert into template_accounts(id, template_id, acc, " +
110 "position, acc_match_group, currency, " +
111 "currency_match_group, amount, amount_match_group, " +
112 "amount, amount_match_group, comment, " +
113 "comment_match_group, negate_amount) select id, " +
114 "pattern_id, acc, position, acc_match_group, " +
116 "currency_match_group, amount, amount_match_group, " +
117 "amount, amount_match_group, comment, " +
118 "comment_match_group, negate_amount from " +
120 db.execSQL("create index fk_template_accounts_template on " +
121 "template_accounts(template_id)");
122 db.execSQL("create index fk_template_accounts_currency on " +
123 "template_accounts(currency)");
124 db.execSQL("drop table pattern_accounts");
125 db.execSQL("drop table patterns");
127 }, new Migration(54, 55) {
129 public void migrate(@NonNull SupportSQLiteDatabase db) {
131 "CREATE TABLE template_accounts_new (id INTEGER PRIMARY " +
133 "AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, " +
134 "acc TEXT, position INTEGER NOT NULL, acc_match_group " +
135 "INTEGER, currency INTEGER, currency_match_group INTEGER," +
136 " amount REAL, amount_match_group INTEGER, comment TEXT, " +
137 "comment_match_group INTEGER, negate_amount INTEGER, " +
138 "FOREIGN KEY(template_id) REFERENCES templates(id) ON " +
139 "UPDATE RESTRICT ON DELETE CASCADE , FOREIGN KEY" +
140 "(currency) REFERENCES currencies(id) ON UPDATE RESTRICT" +
141 " ON DELETE RESTRICT)");
143 "insert into template_accounts_new(id, template_id, acc, " +
144 "position, acc_match_group, currency, " +
145 "currency_match_group, amount, amount_match_group, " +
146 "amount, amount_match_group, comment, " +
147 "comment_match_group, negate_amount) select id, " +
148 "template_id, acc, position, acc_match_group, " +
150 "currency_match_group, amount, amount_match_group, " +
151 "amount, amount_match_group, comment, " +
152 "comment_match_group, negate_amount from " +
153 "template_accounts");
154 db.execSQL("drop table template_accounts");
155 db.execSQL("alter table template_accounts_new rename to " +
156 "template_accounts");
157 db.execSQL("create index fk_template_accounts_template on " +
158 "template_accounts(template_id)");
159 db.execSQL("create index fk_template_accounts_currency on " +
160 "template_accounts(currency)");
164 .addCallback(new Callback() {
166 public void onOpen(@NonNull SupportSQLiteDatabase db) {
168 db.execSQL("PRAGMA foreign_keys = ON");
174 public abstract TemplateHeaderDAO getTemplateDAO();
176 public abstract TemplateAccountDAO getTemplateAccountDAO();
178 public abstract CurrencyDAO getCurrencyDAO();
180 public abstract AccountDAO getAccountDAO();