]> git.ktnx.net Git - mobile-ledger.git/commitdiff
describe transaction_accounts to Room
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 19 Feb 2021 16:37:56 +0000 (16:37 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 1 Mar 2021 06:00:42 +0000 (06:00 +0000)
app/schemas/net.ktnx.mobileledger.db.DB/58.json
app/src/main/java/net/ktnx/mobileledger/db/DB.java
app/src/main/java/net/ktnx/mobileledger/db/TransactionAccount.java [new file with mode: 0644]
app/src/main/res/raw/create_db.sql
app/src/main/res/raw/sql_58.sql

index c4e6131319a40aeef18ebde15693ef0afe3f6125..fd3239de6983b4a168f518739052e91490a21984 100644 (file)
@@ -2,7 +2,7 @@
   "formatVersion": 1,
   "database": {
     "version": 58,
-    "identityHash": "00d9b541473347c65a90e47ce9072436",
+    "identityHash": "249d4507f837a17defe32f1bc9dc2de6",
     "entities": [
       {
         "tableName": "templates",
           }
         ],
         "foreignKeys": []
+      },
+      {
+        "tableName": "transaction_accounts",
+        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`profile` TEXT NOT NULL, `transaction_id` INTEGER NOT NULL, `order_no` INTEGER NOT NULL, `account_name` TEXT NOT NULL, `currency` TEXT NOT NULL DEFAULT '', `amount` REAL NOT NULL, `comment` TEXT, `generation` INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(`profile`, `transaction_id`, `order_no`), FOREIGN KEY(`profile`, `transaction_id`) REFERENCES `transactions`(`profile`, `id`) ON UPDATE RESTRICT ON DELETE CASCADE , FOREIGN KEY(`profile`, `account_name`) REFERENCES `accounts`(`profile`, `name`) ON UPDATE RESTRICT ON DELETE CASCADE )",
+        "fields": [
+          {
+            "fieldPath": "profile",
+            "columnName": "profile",
+            "affinity": "TEXT",
+            "notNull": true
+          },
+          {
+            "fieldPath": "transactionId",
+            "columnName": "transaction_id",
+            "affinity": "INTEGER",
+            "notNull": true
+          },
+          {
+            "fieldPath": "orderNo",
+            "columnName": "order_no",
+            "affinity": "INTEGER",
+            "notNull": true
+          },
+          {
+            "fieldPath": "accountName",
+            "columnName": "account_name",
+            "affinity": "TEXT",
+            "notNull": true
+          },
+          {
+            "fieldPath": "currency",
+            "columnName": "currency",
+            "affinity": "TEXT",
+            "notNull": true,
+            "defaultValue": "''"
+          },
+          {
+            "fieldPath": "amount",
+            "columnName": "amount",
+            "affinity": "REAL",
+            "notNull": true
+          },
+          {
+            "fieldPath": "comment",
+            "columnName": "comment",
+            "affinity": "TEXT",
+            "notNull": false
+          },
+          {
+            "fieldPath": "generation",
+            "columnName": "generation",
+            "affinity": "INTEGER",
+            "notNull": true,
+            "defaultValue": "0"
+          }
+        ],
+        "primaryKey": {
+          "columnNames": [
+            "profile",
+            "transaction_id",
+            "order_no"
+          ],
+          "autoGenerate": false
+        },
+        "indices": [],
+        "foreignKeys": [
+          {
+            "table": "transactions",
+            "onDelete": "CASCADE",
+            "onUpdate": "RESTRICT",
+            "columns": [
+              "profile",
+              "transaction_id"
+            ],
+            "referencedColumns": [
+              "profile",
+              "id"
+            ]
+          },
+          {
+            "table": "accounts",
+            "onDelete": "CASCADE",
+            "onUpdate": "RESTRICT",
+            "columns": [
+              "profile",
+              "account_name"
+            ],
+            "referencedColumns": [
+              "profile",
+              "name"
+            ]
+          }
+        ]
       }
     ],
     "views": [],
     "setupQueries": [
       "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
-      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '00d9b541473347c65a90e47ce9072436')"
+      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '249d4507f837a17defe32f1bc9dc2de6')"
     ]
   }
 }
\ No newline at end of file
index 630c5b272a64002061908d5db2cac881021b6f72..e5cfb5d415c294dd43a6d515608d6715dedf3421 100644 (file)
@@ -34,7 +34,7 @@ import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 @Database(version = 58,
           entities = {TemplateHeader.class, TemplateAccount.class, Currency.class, Account.class,
                       Profile.class, Option.class, AccountValue.class, DescriptionHistory.class,
-                      Transaction.class
+                      Transaction.class, TransactionAccount.class
           })
 abstract public class DB extends RoomDatabase {
     private static DB instance;
diff --git a/app/src/main/java/net/ktnx/mobileledger/db/TransactionAccount.java b/app/src/main/java/net/ktnx/mobileledger/db/TransactionAccount.java
new file mode 100644 (file)
index 0000000..671e5d0
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.db;
+
+import androidx.annotation.NonNull;
+import androidx.room.ColumnInfo;
+import androidx.room.Entity;
+import androidx.room.ForeignKey;
+
+@Entity(tableName = "transaction_accounts", primaryKeys = {"profile", "transaction_id", "order_no"},
+        foreignKeys = {@ForeignKey(entity = Transaction.class, parentColumns = {"profile", "id"},
+                                   childColumns = {"profile", "transaction_id"},
+                                   onDelete = ForeignKey.CASCADE, onUpdate = ForeignKey.RESTRICT),
+                       @ForeignKey(entity = Account.class, parentColumns = {"profile", "name"},
+                                   childColumns = {"profile", "account_name"},
+                                   onDelete = ForeignKey.CASCADE, onUpdate = ForeignKey.RESTRICT)
+        })
+public class TransactionAccount {
+    @ColumnInfo
+    @NonNull
+    private String profile;
+    @ColumnInfo(name = "transaction_id")
+    private int transactionId;
+    @ColumnInfo(name = "order_no")
+    private int orderNo;
+    @ColumnInfo(name = "account_name")
+    @NonNull
+    private String accountName;
+    @ColumnInfo(defaultValue = "")
+    @NonNull
+    private String currency = "";
+    @ColumnInfo
+    private float amount;
+    @ColumnInfo
+    private String comment;
+    @ColumnInfo(defaultValue = "0")
+    private int generation = 0;
+    @NonNull
+    public String getProfile() {
+        return profile;
+    }
+    public void setProfile(@NonNull String profile) {
+        this.profile = profile;
+    }
+    public int getTransactionId() {
+        return transactionId;
+    }
+    public void setTransactionId(int transactionId) {
+        this.transactionId = transactionId;
+    }
+    public int getOrderNo() {
+        return orderNo;
+    }
+    public void setOrderNo(int orderNo) {
+        this.orderNo = orderNo;
+    }
+    @NonNull
+    public String getAccountName() {
+        return accountName;
+    }
+    public void setAccountName(@NonNull String accountName) {
+        this.accountName = accountName;
+    }
+    @NonNull
+    public String getCurrency() {
+        return currency;
+    }
+    public void setCurrency(@NonNull String currency) {
+        this.currency = currency;
+    }
+    public float getAmount() {
+        return amount;
+    }
+    public void setAmount(float amount) {
+        this.amount = amount;
+    }
+    public String getComment() {
+        return comment;
+    }
+    public void setComment(String comment) {
+        this.comment = comment;
+    }
+    public int getGeneration() {
+        return generation;
+    }
+    public void setGeneration(int generation) {
+        this.generation = generation;
+    }
+}
index 625826727ef24bb929102a1e7b7e76f2829ef6b5..5df18e0bcf0af9838b8d73ac7e4eda033ec18498 100644 (file)
@@ -66,8 +66,21 @@ create table transactions(
 create unique index un_transactions_data_hash on transactions(profile,data_hash);
 create index idx_transaction_description on transactions(description);
 
-create table transaction_accounts(profile varchar not null, transaction_id integer not null, order_no integer not null, account_name varchar not null, currency varchar not null default '', amount decimal not null, comment varchar, generation integer default 0, constraint fk_transaction_accounts_acc foreign key(profile,account_name) references accounts(profile,name), constraint fk_transaction_accounts_trn foreign key(profile, transaction_id) references transactions(profile,id));
-create unique index un_transaction_accounts_order on transaction_accounts(profile, transaction_id, order_no);
+create table transaction_accounts(
+ profile varchar not null,
+ transaction_id integer not null,
+ order_no integer not null,
+ account_name varchar not null,
+ currency varchar not null default '',
+ amount real not null,
+ comment varchar,
+ generation integer not null default 0,
+ primary key(profile, transaction_id, order_no),
+ foreign key (profile,account_name) references accounts(profile,name)
+  on delete cascade on update restrict,
+ foreign key(profile, transaction_id) references transactions(profile,id)
+  on delete cascade on update restrict);
+
 create table currencies(id integer not null primary key, name varchar not null, position varchar not null, has_gap integer not null);
 
 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, is_fallback INTEGER NOT NULL DEFAULT 0);
index b2b687c587e1158c0f3c4bfa575d724b87bd5085..ad28d1a5ba010b6fd2fe005084b1088ed0c6fb55 100644 (file)
@@ -119,6 +119,31 @@ alter table transactions_new rename to transactions;
 create unique index un_transactions_data_hash on transactions(profile,data_hash);
 create index idx_transaction_description on transactions(description);
 
+-- transaction_accounts
+
+create table transaction_accounts_new(
+ profile varchar not null,
+ transaction_id integer not null,
+ order_no integer not null,
+ account_name varchar not null,
+ currency varchar not null default '',
+ amount real not null,
+ comment varchar,
+ generation integer not null default 0,
+ primary key(profile, transaction_id, order_no),
+ foreign key (profile,account_name) references accounts(profile,name)
+  on delete cascade on update restrict,
+ foreign key(profile, transaction_id) references transactions(profile,id)
+  on delete cascade on update restrict);
+
+insert into transaction_accounts_new(profile, transaction_id, order_no, account_name,
+ currency, amount, comment, generation)
+select profile, transaction_id, order_no, account_name,
+       currency, amount, comment, generation
+from transaction_accounts;
+
+drop table transaction_accounts;
+alter table transaction_accounts_new rename to transaction_accounts;
 
 COMMIT TRANSACTION;