]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/db/DB.java
rename Patterns to Templates
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / DB.java
index 80652ed1ecb8cf6642b79ff579e73345805dabe6..f65f503fccfc49583690d1a864c4a28575e7ce59 100644 (file)
@@ -26,11 +26,11 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
 
 import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.dao.CurrencyDAO;
-import net.ktnx.mobileledger.dao.PatternAccountDAO;
-import net.ktnx.mobileledger.dao.PatternHeaderDAO;
+import net.ktnx.mobileledger.dao.TemplateAccountDAO;
+import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 
-@Database(version = 53, entities = {TemplateHeader.class, TemplateAccount.class, Currency.class})
+@Database(version = 54, entities = {TemplateHeader.class, TemplateAccount.class, Currency.class})
 abstract public class DB extends RoomDatabase {
     private static DB instance;
     public static DB get() {
@@ -63,14 +63,70 @@ abstract public class DB extends RoomDatabase {
                                 db.execSQL(
                                         "alter table pattern_accounts add negate_amount boolean;");
                             }
+                        }, new Migration(53, 54) {
+                            @Override
+                            public void migrate(@NonNull SupportSQLiteDatabase db) {
+                                db.execSQL("CREATE TABLE templates (id INTEGER PRIMARY KEY " +
+                                           "AUTOINCREMENT NOT NULL, name TEXT NOT NULL, " +
+                                           "regular_expression TEXT NOT NULL, test_text TEXT, " +
+                                           "transaction_description TEXT, " +
+                                           "transaction_description_match_group INTEGER, " +
+                                           "transaction_comment TEXT, " +
+                                           "transaction_comment_match_group INTEGER, date_year " +
+                                           "INTEGER, date_year_match_group INTEGER, date_month " +
+                                           "INTEGER, date_month_match_group INTEGER, date_day " +
+                                           "INTEGER, date_day_match_group INTEGER)");
+                                db.execSQL(
+                                        "CREATE TABLE template_accounts (id INTEGER PRIMARY KEY " +
+                                        "AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, " +
+                                        "acc TEXT, position INTEGER NOT NULL, acc_match_group " +
+                                        "INTEGER, currency INTEGER, currency_match_group INTEGER," +
+                                        " amount REAL, amount_match_group INTEGER, comment TEXT, " +
+                                        "comment_match_group INTEGER, negate_amount INTEGER, " +
+                                        "FOREIGN KEY(template_id) REFERENCES templates(id) ON " +
+                                        "UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY" +
+                                        "(currency) REFERENCES currencies(id) ON UPDATE NO ACTION" +
+                                        " ON DELETE NO ACTION )");
+                                db.execSQL("insert into templates(id, name, regular_expression, " +
+                                           "test_text, transaction_description, " +
+                                           "transaction_description_match_group, " +
+                                           "transaction_comment, transaction_comment_match_group," +
+                                           " date_year, date_year_match_group, date_month, " +
+                                           "date_month_match_group, date_day, " +
+                                           "date_day_match_group)" +
+                                           " select id, name, regular_expression, test_text, " +
+                                           "transaction_description, " +
+                                           "transaction_description_match_group, " +
+                                           "transaction_comment, transaction_comment_match_group," +
+                                           " date_year, date_year_match_group, date_month, " +
+                                           "date_month_match_group, date_day, " +
+                                           "date_day_match_group from patterns");
+                                db.execSQL("insert into template_accounts(id, template_id, acc, " +
+                                           "position, acc_match_group, currency, " +
+                                           "currency_match_group, amount, amount_match_group, " +
+                                           "amount, amount_match_group, comment, " +
+                                           "comment_match_group, negate_amount) select id, " +
+                                           "pattern_id, acc, position, acc_match_group, " +
+                                           "currency, " +
+                                           "currency_match_group, amount, amount_match_group, " +
+                                           "amount, amount_match_group, comment, " +
+                                           "comment_match_group, negate_amount from " +
+                                           "pattern_accounts");
+                                db.execSQL("create index fk_template_accounts_template on " +
+                                           "template_accounts(template_id)");
+                                db.execSQL("create index fk_template_accounts_currency on " +
+                                           "template_accounts(currency)");
+                                db.execSQL("drop table pattern_accounts");
+                                db.execSQL("drop table patterns");
+                            }
                         }
                         })
                         .build();
         }
     }
-    public abstract PatternHeaderDAO getPatternDAO();
+    public abstract TemplateHeaderDAO getPatternDAO();
 
-    public abstract PatternAccountDAO getPatternAccountDAO();
+    public abstract TemplateAccountDAO getPatternAccountDAO();
 
     public abstract CurrencyDAO getCurrencyDAO();
 }