]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
methods for deleting all DB tables
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TransactionDAO.java
index 869e88c2d28e0217d534a040cc6a7adebe963cb0..001976d3b1ee08393c2ac48d6fbc5d634566a403 100644 (file)
@@ -17,8 +17,6 @@
 
 package net.ktnx.mobileledger.dao;
 
-import android.os.AsyncTask;
-
 import androidx.annotation.NonNull;
 import androidx.lifecycle.LiveData;
 import androidx.room.ColumnInfo;
@@ -68,6 +66,9 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     @Delete
     public abstract void deleteSync(List<Transaction> items);
 
+    @Query("DELETE FROM transactions")
+    public abstract void deleteAllSync();
+
     @Query("SELECT * FROM transactions WHERE id = :id")
     public abstract LiveData<Transaction> getById(long id);
 
@@ -79,11 +80,11 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     @Query("SELECT * FROM transactions WHERE id = :transactionId")
     public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
 
-    @Query("SELECT DISTINCT description, CASE WHEN description LIKE :term||'%%' THEN 1 " +
-           "               WHEN description LIKE '%%:'||:term||'%%' THEN 2 " +
-           "               WHEN description LIKE '%% '||:term||'%%' THEN 3 " +
+    @Query("SELECT DISTINCT description, CASE WHEN description_uc LIKE :term||'%%' THEN 1 " +
+           "               WHEN description_uc LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN description_uc LIKE '%% '||:term||'%%' THEN 3 " +
            "               ELSE 9 END AS ordering FROM transactions " +
-           "WHERE description LIKE '%%'||:term||'%%' " + "ORDER BY ordering, description, rowid ")
+           "WHERE description_uc LIKE '%%'||:term||'%%' ORDER BY ordering, description_uc, rowid ")
     public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
 
     @androidx.room.Transaction
@@ -92,9 +93,9 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
 
     @androidx.room.Transaction
-    @Query("SELECT tr.id, tr.profile_id, tr.ledger_id, tr.description, tr.data_hash, tr.comment, " +
-           "tr.year, tr.month, tr.day, tr.generation from transactions tr JOIN " +
-           "transaction_accounts t_a ON t_a.transaction_id = tr.id WHERE tr.description = " +
+    @Query("SELECT tr.id, tr.profile_id, tr.ledger_id, tr.description, tr.description_uc, tr" +
+           ".data_hash, tr.comment, tr.year, tr.month, tr.day, tr.generation from transactions tr" +
+           " JOIN transaction_accounts t_a ON t_a.transaction_id = tr.id WHERE tr.description = " +
            ":description AND t_a.account_name LIKE '%'||:accountTerm||'%' ORDER BY year desc, " +
            "month desc, day desc, tr.ledger_id desc LIMIT 1")
     public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
@@ -108,29 +109,15 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
 
     @androidx.room.Transaction
     @Query("SELECT * FROM transactions WHERE profile_id = :profileId ORDER BY year " +
-           " desc, month desc, day desc, ledger_id desc")
-    public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
-
-    @androidx.room.Transaction
-    @Query("SELECT * FROM transactions WHERE profile_id = :profileId ORDER BY year " +
-           " desc, month desc, day desc, ledger_id desc")
+           " asc, month asc, day asc, ledger_id asc")
     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 " +
-           "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 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")
+           " tr.day, tr.description, tr.description_uc, 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 asc, tr.month asc, tr.day asc, tr.ledger_id asc")
     public abstract LiveData<List<TransactionWithAccounts>> getAllWithAccountsFiltered(
             long profileId, String accountName);
 
@@ -231,7 +218,7 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
         }
     }
     public void storeLast(TransactionWithAccounts rec) {
-        AsyncTask.execute(() -> appendSync(rec));
+        BaseDAO.runAsync(() -> appendSync(rec));
     }
     @androidx.room.Transaction
     public void appendSync(TransactionWithAccounts rec) {
@@ -253,32 +240,36 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
             trAcc.setGeneration(transaction.getGeneration());
             trAcc.setId(trAccDao.insertSync(trAcc));
 
-            Account acc = accDao.getByNameSync(profileId, trAcc.getAccountName());
-            if (acc == null) {
-                acc = new Account();
-                acc.setProfileId(profileId);
-                acc.setName(trAcc.getAccountName());
-                acc.setNameUpper(acc.getName()
-                                    .toUpperCase());
-                acc.setParentName(LedgerAccount.extractParentName(acc.getName()));
-                acc.setLevel(LedgerAccount.determineLevel(acc.getName()));
-                acc.setGeneration(trAcc.getGeneration());
-
-                acc.setId(accDao.insertSync(acc));
-            }
-
-            AccountValue accVal = accValDao.getByCurrencySync(acc.getId(), trAcc.getCurrency());
-            if (accVal == null) {
-                accVal = new AccountValue();
-                accVal.setAccountId(acc.getId());
-                accVal.setGeneration(trAcc.getGeneration());
-                accVal.setCurrency(trAcc.getCurrency());
-                accVal.setValue(trAcc.getAmount());
-                accVal.setId(accValDao.insertSync(accVal));
-            }
-            else {
-                accVal.setValue(accVal.getValue() + trAcc.getAmount());
-                accValDao.updateSync(accVal);
+            String accName = trAcc.getAccountName();
+            while (accName != null) {
+                Account acc = accDao.getByNameSync(profileId, accName);
+                if (acc == null) {
+                    acc = new Account();
+                    acc.setProfileId(profileId);
+                    acc.setName(accName);
+                    acc.setNameUpper(accName.toUpperCase());
+                    acc.setParentName(LedgerAccount.extractParentName(accName));
+                    acc.setLevel(LedgerAccount.determineLevel(acc.getName()));
+                    acc.setGeneration(trAcc.getGeneration());
+
+                    acc.setId(accDao.insertSync(acc));
+                }
+
+                AccountValue accVal = accValDao.getByCurrencySync(acc.getId(), trAcc.getCurrency());
+                if (accVal == null) {
+                    accVal = new AccountValue();
+                    accVal.setAccountId(acc.getId());
+                    accVal.setGeneration(trAcc.getGeneration());
+                    accVal.setCurrency(trAcc.getCurrency());
+                    accVal.setValue(trAcc.getAmount());
+                    accVal.setId(accValDao.insertSync(accVal));
+                }
+                else {
+                    accVal.setValue(accVal.getValue() + trAcc.getAmount());
+                    accValDao.updateSync(accVal);
+                }
+
+                accName = LedgerAccount.extractParentName(accName);
             }
         }
     }