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.CurrencyDAO;
29 import net.ktnx.mobileledger.dao.TemplateAccountDAO;
30 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
31 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
33 @Database(version = 56, entities = {TemplateHeader.class, TemplateAccount.class, Currency.class,
34 net.ktnx.mobileledger.db.Account.class
36 abstract public class DB extends RoomDatabase {
37 private static DB instance;
38 public static DB get() {
41 synchronized (DB.class) {
46 Room.databaseBuilder(App.instance, DB.class, MobileLedgerDatabase.DB_NAME)
47 .addMigrations(new Migration[]{new Migration(51, 52) {
49 public void migrate(@NonNull SupportSQLiteDatabase db) {
50 db.beginTransaction();
52 db.execSQL("create index fk_pattern_accounts_pattern on " +
53 "pattern_accounts(pattern_id);");
54 db.execSQL("create index fk_pattern_accounts_currency on " +
55 "pattern_accounts(currency);");
56 db.setTransactionSuccessful();
62 }, new Migration(52, 53) {
64 public void migrate(@NonNull SupportSQLiteDatabase db) {
66 "alter table pattern_accounts add negate_amount boolean;");
68 }, new Migration(53, 54) {
70 public void migrate(@NonNull SupportSQLiteDatabase db) {
71 db.execSQL("CREATE TABLE templates (id INTEGER PRIMARY KEY " +
72 "AUTOINCREMENT NOT NULL, name TEXT NOT NULL, " +
73 "regular_expression TEXT NOT NULL, test_text TEXT, " +
74 "transaction_description TEXT, " +
75 "transaction_description_match_group INTEGER, " +
76 "transaction_comment TEXT, " +
77 "transaction_comment_match_group INTEGER, date_year " +
78 "INTEGER, date_year_match_group INTEGER, date_month " +
79 "INTEGER, date_month_match_group INTEGER, date_day " +
80 "INTEGER, date_day_match_group INTEGER)");
82 "CREATE TABLE template_accounts (id INTEGER PRIMARY KEY " +
83 "AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, " +
84 "acc TEXT, position INTEGER NOT NULL, acc_match_group " +
85 "INTEGER, currency INTEGER, currency_match_group INTEGER," +
86 " amount REAL, amount_match_group INTEGER, comment TEXT, " +
87 "comment_match_group INTEGER, negate_amount INTEGER, " +
88 "FOREIGN KEY(template_id) REFERENCES templates(id) ON " +
89 "UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY" +
90 "(currency) REFERENCES currencies(id) ON UPDATE NO ACTION" +
91 " ON DELETE NO ACTION )");
92 db.execSQL("insert into templates(id, name, regular_expression, " +
93 "test_text, transaction_description, " +
94 "transaction_description_match_group, " +
95 "transaction_comment, transaction_comment_match_group," +
96 " date_year, date_year_match_group, date_month, " +
97 "date_month_match_group, date_day, " +
98 "date_day_match_group)" +
99 " select id, name, regular_expression, test_text, " +
100 "transaction_description, " +
101 "transaction_description_match_group, " +
102 "transaction_comment, transaction_comment_match_group," +
103 " date_year, date_year_match_group, date_month, " +
104 "date_month_match_group, date_day, " +
105 "date_day_match_group from patterns");
106 db.execSQL("insert into template_accounts(id, template_id, acc, " +
107 "position, acc_match_group, currency, " +
108 "currency_match_group, amount, amount_match_group, " +
109 "amount, amount_match_group, comment, " +
110 "comment_match_group, negate_amount) select id, " +
111 "pattern_id, acc, position, acc_match_group, " +
113 "currency_match_group, amount, amount_match_group, " +
114 "amount, amount_match_group, comment, " +
115 "comment_match_group, negate_amount from " +
117 db.execSQL("create index fk_template_accounts_template on " +
118 "template_accounts(template_id)");
119 db.execSQL("create index fk_template_accounts_currency on " +
120 "template_accounts(currency)");
121 db.execSQL("drop table pattern_accounts");
122 db.execSQL("drop table patterns");
124 }, new Migration(54, 55) {
126 public void migrate(@NonNull SupportSQLiteDatabase db) {
128 "CREATE TABLE template_accounts_new (id INTEGER PRIMARY " +
130 "AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, " +
131 "acc TEXT, position INTEGER NOT NULL, acc_match_group " +
132 "INTEGER, currency INTEGER, currency_match_group INTEGER," +
133 " amount REAL, amount_match_group INTEGER, comment TEXT, " +
134 "comment_match_group INTEGER, negate_amount INTEGER, " +
135 "FOREIGN KEY(template_id) REFERENCES templates(id) ON " +
136 "UPDATE RESTRICT ON DELETE CASCADE , FOREIGN KEY" +
137 "(currency) REFERENCES currencies(id) ON UPDATE RESTRICT" +
138 " ON DELETE RESTRICT)");
140 "insert into template_accounts_new(id, template_id, acc, " +
141 "position, acc_match_group, currency, " +
142 "currency_match_group, amount, amount_match_group, " +
143 "amount, amount_match_group, comment, " +
144 "comment_match_group, negate_amount) select id, " +
145 "template_id, acc, position, acc_match_group, " +
147 "currency_match_group, amount, amount_match_group, " +
148 "amount, amount_match_group, comment, " +
149 "comment_match_group, negate_amount from " +
150 "template_accounts");
151 db.execSQL("drop table template_accounts");
152 db.execSQL("alter table template_accounts_new rename to " +
153 "template_accounts");
154 db.execSQL("create index fk_template_accounts_template on " +
155 "template_accounts(template_id)");
156 db.execSQL("create index fk_template_accounts_currency on " +
157 "template_accounts(currency)");
161 .addCallback(new Callback() {
163 public void onOpen(@NonNull SupportSQLiteDatabase db) {
165 db.execSQL("PRAGMA foreign_keys = ON");
171 public abstract TemplateHeaderDAO getTemplateDAO();
173 public abstract TemplateAccountDAO getTemplateAccountDAO();
175 public abstract CurrencyDAO getCurrencyDAO();