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 = 54, entities = {TemplateHeader.class, TemplateAccount.class, Currency.class})
34 abstract public class DB extends RoomDatabase {
35 private static DB instance;
36 public static DB get() {
39 synchronized (DB.class) {
44 Room.databaseBuilder(App.instance, DB.class, MobileLedgerDatabase.DB_NAME)
45 .addMigrations(new Migration[]{new Migration(51, 52) {
47 public void migrate(@NonNull SupportSQLiteDatabase db) {
48 db.beginTransaction();
50 db.execSQL("create index fk_pattern_accounts_pattern on " +
51 "pattern_accounts(pattern_id);");
52 db.execSQL("create index fk_pattern_accounts_currency on " +
53 "pattern_accounts(currency);");
54 db.setTransactionSuccessful();
60 }, new Migration(52, 53) {
62 public void migrate(@NonNull SupportSQLiteDatabase db) {
64 "alter table pattern_accounts add negate_amount boolean;");
66 }, new Migration(53, 54) {
68 public void migrate(@NonNull SupportSQLiteDatabase db) {
69 db.execSQL("CREATE TABLE templates (id INTEGER PRIMARY KEY " +
70 "AUTOINCREMENT NOT NULL, name TEXT NOT NULL, " +
71 "regular_expression TEXT NOT NULL, test_text TEXT, " +
72 "transaction_description TEXT, " +
73 "transaction_description_match_group INTEGER, " +
74 "transaction_comment TEXT, " +
75 "transaction_comment_match_group INTEGER, date_year " +
76 "INTEGER, date_year_match_group INTEGER, date_month " +
77 "INTEGER, date_month_match_group INTEGER, date_day " +
78 "INTEGER, date_day_match_group INTEGER)");
80 "CREATE TABLE template_accounts (id INTEGER PRIMARY KEY " +
81 "AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, " +
82 "acc TEXT, position INTEGER NOT NULL, acc_match_group " +
83 "INTEGER, currency INTEGER, currency_match_group INTEGER," +
84 " amount REAL, amount_match_group INTEGER, comment TEXT, " +
85 "comment_match_group INTEGER, negate_amount INTEGER, " +
86 "FOREIGN KEY(template_id) REFERENCES templates(id) ON " +
87 "UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY" +
88 "(currency) REFERENCES currencies(id) ON UPDATE NO ACTION" +
89 " ON DELETE NO ACTION )");
90 db.execSQL("insert into templates(id, name, regular_expression, " +
91 "test_text, transaction_description, " +
92 "transaction_description_match_group, " +
93 "transaction_comment, transaction_comment_match_group," +
94 " date_year, date_year_match_group, date_month, " +
95 "date_month_match_group, date_day, " +
96 "date_day_match_group)" +
97 " select id, name, regular_expression, test_text, " +
98 "transaction_description, " +
99 "transaction_description_match_group, " +
100 "transaction_comment, transaction_comment_match_group," +
101 " date_year, date_year_match_group, date_month, " +
102 "date_month_match_group, date_day, " +
103 "date_day_match_group from patterns");
104 db.execSQL("insert into template_accounts(id, template_id, acc, " +
105 "position, acc_match_group, currency, " +
106 "currency_match_group, amount, amount_match_group, " +
107 "amount, amount_match_group, comment, " +
108 "comment_match_group, negate_amount) select id, " +
109 "pattern_id, acc, position, acc_match_group, " +
111 "currency_match_group, amount, amount_match_group, " +
112 "amount, amount_match_group, comment, " +
113 "comment_match_group, negate_amount from " +
115 db.execSQL("create index fk_template_accounts_template on " +
116 "template_accounts(template_id)");
117 db.execSQL("create index fk_template_accounts_currency on " +
118 "template_accounts(currency)");
119 db.execSQL("drop table pattern_accounts");
120 db.execSQL("drop table patterns");
124 .addCallback(new Callback() {
126 public void onOpen(@NonNull SupportSQLiteDatabase db) {
128 db.execSQL("PRAGMA foreign_keys = ON");
134 public abstract TemplateHeaderDAO getPatternDAO();
136 public abstract TemplateAccountDAO getPatternAccountDAO();
138 public abstract CurrencyDAO getCurrencyDAO();