]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
store transaction list in a db transaction
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TransactionDAO.java
index 31319f34b9f2be522273b3a5ade9a74f2ecdc3b9..715d6ea1d72272e9c6ba736697432e8ba4cd3794 100644 (file)
@@ -32,6 +32,7 @@ import net.ktnx.mobileledger.db.Transaction;
 import net.ktnx.mobileledger.db.TransactionAccount;
 import net.ktnx.mobileledger.db.TransactionWithAccounts;
 import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -105,6 +106,10 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
     public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
 
+    @androidx.room.Transaction
+    @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
+    public abstract LiveData<List<TransactionWithAccounts>> getAllWithAccounts(long profileId);
+
     @androidx.room.Transaction
     @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
            " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
@@ -114,6 +119,15 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
                                                                                  String accountName);
 
+    @androidx.room.Transaction
+    @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
+           " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
+           "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
+           ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
+           "desc, tr.month desc, tr.day desc, tr.ledger_id desc")
+    public abstract LiveData<List<TransactionWithAccounts>> getAllWithAccountsFiltered(
+            long profileId, String accountName);
+
     @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
            ":currentGeneration")
     public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
@@ -128,6 +142,18 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
 
     @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
     public abstract Transaction getByLedgerId(long profileId, long ledgerId);
+
+    @Query("UPDATE transactions SET generation = :newGeneration WHERE id = :transactionId")
+    public abstract int updateGeneration(long transactionId, long newGeneration);
+
+    @Query("UPDATE transaction_accounts SET generation = :newGeneration WHERE transaction_id = " +
+           ":transactionId")
+    public abstract int updateAccountsGeneration(long transactionId, long newGeneration);
+    @androidx.room.Transaction
+    public void updateGenerationWithAccounts(long transactionId, long newGeneration) {
+        updateGeneration(transactionId, newGeneration);
+        updateAccountsGeneration(transactionId, newGeneration);
+    }
     public long getGenerationSync(long profileId) {
         TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
 
@@ -135,6 +161,7 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
             return 0;
         return result.generation;
     }
+    @androidx.room.Transaction
     public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
         long generation = getGenerationSync(profileId) + 1;
 
@@ -153,15 +180,22 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
         Logger.debug("Transaction",
                 String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
     }
-    private void storeSync(TransactionWithAccounts rec) {
+    @androidx.room.Transaction
+    void storeSync(TransactionWithAccounts rec) {
         TransactionAccountDAO trAccDao = DB.get()
                                            .getTransactionAccountDAO();
 
         Transaction transaction = rec.transaction;
         Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
         if (existing != null) {
+            if (Misc.equalStrings(transaction.getDataHash(), existing.getDataHash())) {
+                updateGenerationWithAccounts(existing.getId(), rec.transaction.getGeneration());
+                return;
+            }
+
             existing.copyDataFrom(transaction);
             updateSync(existing);
+
             transaction = existing;
         }
         else