X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fdao%2FAccountDAO.java;h=0c581787a30a2fac55c605674904eebdd7441995;hp=bb8d4ec2333d9e34a647d83c28ded648cdfd1762;hb=833544eb24cb630dc1ce221e4aa3dedb3f6341e3;hpb=93545c6fbf1244fbd96ecfc50e1115dbdc25f9ae diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java index bb8d4ec2..0c581787 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java @@ -23,14 +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 { static public List unbox(List list) { @@ -41,17 +47,46 @@ public abstract class AccountDAO extends BaseDAO { return result; } - @Insert + + @Insert(onConflict = OnConflictStrategy.REPLACE) public abstract long insertSync(Account item); + @Insert(onConflict = OnConflictStrategy.REPLACE) + public abstract void insertSync(List 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); @Delete public abstract void deleteSync(Account item); - @Query("SELECT * FROM accounts") - public abstract LiveData> getAll(); + @Delete + public abstract void deleteSync(List items); + + @Query("DELETE FROM accounts") + public abstract void deleteAllSync(); + + @Query("SELECT * FROM accounts WHERE profile_id=:profileId ORDER BY name") + public abstract LiveData> getAll(long profileId); + + @Transaction + @Query("SELECT * FROM accounts WHERE profile_id = :profileId ORDER BY name") + public abstract LiveData> getAllWithAmounts(long profileId); + + @Query("SELECT * FROM accounts WHERE id=:id") + public abstract Account getByIdSync(long id); // not useful for now // @Transaction @@ -60,15 +95,23 @@ public abstract class AccountDAO extends BaseDAO { @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName") public abstract LiveData 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 getByNameWithAmounts(long profileId, + @NonNull String accountName); + @Query("SELECT 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 profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ") - public abstract LiveData> lookupInProfileByName(long profileId, - @NonNull - String term); + public abstract LiveData> lookupNamesInProfileByName(long profileId, + @NonNull + String term); @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " + " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " + @@ -76,22 +119,65 @@ public abstract class AccountDAO extends BaseDAO { " 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 lookupInProfileByNameSync(long profileId, - @NonNull String term); + public abstract List 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 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> lookupByName(@NonNull String term); + public abstract LiveData> 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 lookupByNameSync(@NonNull String term); + public abstract List lookupNamesByNameSync(@NonNull String term); + + @Query("SELECT * FROM accounts WHERE profile_id = :profileId") + public abstract List 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 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 @@ -99,4 +185,12 @@ public abstract class AccountDAO extends BaseDAO { @ColumnInfo public int ordering; } + + static class AccountGenerationContainer { + @ColumnInfo + long generation; + public AccountGenerationContainer(long generation) { + this.generation = generation; + } + } }