]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
methods for deleting all DB tables
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / AccountDAO.java
index 3b401b669ef9e303e2b91d1aa5348acf05cdd0bf..0c581787a30a2fac55c605674904eebdd7441995 100644 (file)
@@ -23,16 +23,20 @@ import androidx.room.ColumnInfo;
 import androidx.room.Dao;
 import androidx.room.Delete;
 import androidx.room.Insert;
+import androidx.room.OnConflictStrategy;
 import androidx.room.Query;
 import androidx.room.Transaction;
 import androidx.room.Update;
 
 import net.ktnx.mobileledger.db.Account;
+import net.ktnx.mobileledger.db.AccountValue;
 import net.ktnx.mobileledger.db.AccountWithAmounts;
+import net.ktnx.mobileledger.db.DB;
 
 import java.util.ArrayList;
 import java.util.List;
 
+
 @Dao
 public abstract class AccountDAO extends BaseDAO<Account> {
     static public List<String> unbox(List<AccountNameContainer> list) {
@@ -43,9 +47,25 @@ public abstract class AccountDAO extends BaseDAO<Account> {
 
         return result;
     }
-    @Insert
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
     public abstract long insertSync(Account item);
 
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    public abstract void insertSync(List<Account> items);
+
+    @Transaction
+    public void insertSync(@NonNull AccountWithAmounts accountWithAmounts) {
+        final AccountValueDAO valueDAO = DB.get()
+                                           .getAccountValueDAO();
+        Account account = accountWithAmounts.account;
+        account.setId(insertSync(account));
+        for (AccountValue value : accountWithAmounts.amounts) {
+            value.setAccountId(account.getId());
+            value.setGeneration(account.getGeneration());
+            value.setId(valueDAO.insertSync(value));
+        }
+    }
     @Update
     public abstract void updateSync(Account item);
 
@@ -55,11 +75,14 @@ public abstract class AccountDAO extends BaseDAO<Account> {
     @Delete
     public abstract void deleteSync(List<Account> items);
 
-    @Query("SELECT * FROM accounts WHERE profile_id=:profileId")
+    @Query("DELETE FROM accounts")
+    public abstract void deleteAllSync();
+
+    @Query("SELECT * FROM accounts WHERE profile_id=:profileId ORDER BY name")
     public abstract LiveData<List<Account>> getAll(long profileId);
 
     @Transaction
-    @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
+    @Query("SELECT * FROM accounts WHERE profile_id = :profileId ORDER BY name")
     public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId);
 
     @Query("SELECT * FROM accounts WHERE id=:id")
@@ -72,6 +95,9 @@ public abstract class AccountDAO extends BaseDAO<Account> {
     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
     public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
 
+    @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
+    public abstract Account getByNameSync(long profileId, @NonNull String accountName);
+
     @Transaction
     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
     public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
@@ -83,9 +109,9 @@ public abstract class AccountDAO extends BaseDAO<Account> {
            "               ELSE 9 END AS ordering " + "FROM accounts " +
            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
            "ORDER BY ordering, name_upper, rowid ")
-    public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(long profileId,
-                                                                               @NonNull
-                                                                                       String term);
+    public abstract LiveData<List<AccountNameContainer>> lookupNamesInProfileByName(long profileId,
+                                                                                    @NonNull
+                                                                                            String term);
 
     @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
@@ -93,30 +119,78 @@ public abstract class AccountDAO extends BaseDAO<Account> {
            "               ELSE 9 END AS ordering " + "FROM accounts " +
            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
            "ORDER BY ordering, name_upper, rowid ")
-    public abstract List<AccountNameContainer> lookupInProfileByNameSync(long profileId,
-                                                                         @NonNull String term);
+    public abstract List<AccountNameContainer> lookupNamesInProfileByNameSync(long profileId,
+                                                                              @NonNull String term);
+
+    @Transaction
+    @Query("SELECT * FROM accounts " +
+           "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
+           "ORDER BY  CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END, name_upper, rowid ")
+    public abstract List<AccountWithAmounts> lookupWithAmountsInProfileByNameSync(long profileId,
+                                                                                  @NonNull String term);
 
     @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
            "               ELSE 9 END AS ordering " + "FROM accounts " +
            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
-    public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
+    public abstract LiveData<List<AccountNameContainer>> lookupNamesByName(@NonNull String term);
 
     @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
            "               ELSE 9 END AS ordering " + "FROM accounts " +
            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
-    public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
+    public abstract List<AccountNameContainer> lookupNamesByNameSync(@NonNull String term);
 
     @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
     public abstract List<Account> allForProfileSync(long profileId);
 
+    @Query("SELECT generation FROM accounts WHERE profile_id = :profileId LIMIT 1")
+    protected abstract AccountGenerationContainer getGenerationPOJOSync(long profileId);
+    public long getGenerationSync(long profileId) {
+        AccountGenerationContainer result = getGenerationPOJOSync(profileId);
+
+        if (result == null)
+            return 0;
+        return result.generation;
+    }
+    @Query("DELETE FROM accounts WHERE profile_id = :profileId AND generation <> " +
+           ":currentGeneration")
+    public abstract void purgeOldAccountsSync(long profileId, long currentGeneration);
+
+    @Query("DELETE FROM account_values WHERE EXISTS (SELECT 1 FROM accounts a WHERE a" +
+           ".id=account_values.account_id AND a.profile_id=:profileId) AND generation <> " +
+           ":currentGeneration")
+    public abstract void purgeOldAccountValuesSync(long profileId, long currentGeneration);
+    @Transaction
+    public void storeAccountsSync(List<AccountWithAmounts> accounts, long profileId) {
+        long generation = getGenerationSync(profileId) + 1;
+
+        for (AccountWithAmounts rec : accounts) {
+            rec.account.setGeneration(generation);
+            rec.account.setProfileId(profileId);
+            insertSync(rec);
+        }
+        purgeOldAccountsSync(profileId, generation);
+        purgeOldAccountValuesSync(profileId, generation);
+    }
+
     static public class AccountNameContainer {
         @ColumnInfo
         public String name;
         @ColumnInfo
         public int ordering;
     }
+
+    static class AccountGenerationContainer {
+        @ColumnInfo
+        long generation;
+        public AccountGenerationContainer(long generation) {
+            this.generation = generation;
+        }
+    }
 }