2 * Copyright © 2024 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.dao;
20 import androidx.annotation.NonNull;
21 import androidx.lifecycle.LiveData;
22 import androidx.room.ColumnInfo;
23 import androidx.room.Dao;
24 import androidx.room.Delete;
25 import androidx.room.Insert;
26 import androidx.room.OnConflictStrategy;
27 import androidx.room.Query;
28 import androidx.room.Transaction;
29 import androidx.room.Update;
31 import net.ktnx.mobileledger.db.Account;
32 import net.ktnx.mobileledger.db.AccountValue;
33 import net.ktnx.mobileledger.db.AccountWithAmounts;
34 import net.ktnx.mobileledger.db.DB;
36 import java.util.ArrayList;
37 import java.util.List;
41 public abstract class AccountDAO extends BaseDAO<Account> {
42 static public List<String> unbox(List<AccountNameContainer> list) {
43 ArrayList<String> result = new ArrayList<>(list.size());
44 for (AccountNameContainer item : list) {
45 result.add(item.name);
51 @Insert(onConflict = OnConflictStrategy.REPLACE)
52 public abstract long insertSync(Account item);
54 @Insert(onConflict = OnConflictStrategy.REPLACE)
55 public abstract void insertSync(List<Account> items);
58 public void insertSync(@NonNull AccountWithAmounts accountWithAmounts) {
59 final AccountValueDAO valueDAO = DB.get()
60 .getAccountValueDAO();
61 Account account = accountWithAmounts.account;
62 account.setId(insertSync(account));
63 for (AccountValue value : accountWithAmounts.amounts) {
64 value.setAccountId(account.getId());
65 value.setGeneration(account.getGeneration());
66 value.setId(valueDAO.insertSync(value));
70 public abstract void updateSync(Account item);
73 public abstract void deleteSync(Account item);
76 public abstract void deleteSync(List<Account> items);
78 @Query("DELETE FROM accounts")
79 public abstract void deleteAllSync();
81 @Query("SELECT * FROM accounts WHERE profile_id=:profileId AND IIF(:includeZeroBalances=1, 1," +
82 " (EXISTS(SELECT 1 FROM account_values av WHERE av.account_id=accounts.id AND av.value" +
83 " <> 0) OR EXISTS(SELECT 1 FROM accounts a WHERE a.parent_name = accounts.name))) " +
85 public abstract LiveData<List<Account>> getAll(long profileId, boolean includeZeroBalances);
88 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND IIF(:includeZeroBalances=1, " +
89 "1, (EXISTS(SELECT 1 FROM account_values av WHERE av.account_id=accounts.id AND av" +
90 ".value <> 0) OR EXISTS(SELECT 1 FROM accounts a WHERE a.parent_name = accounts.name))" +
92 public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId,
93 boolean includeZeroBalances);
95 @Query("SELECT * FROM accounts WHERE id=:id")
96 public abstract Account getByIdSync(long id);
100 // @Query("SELECT * FROM patterns")
101 // List<PatternWithAccounts> getPatternsWithAccounts();
102 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
103 public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
105 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
106 public abstract Account getByNameSync(long profileId, @NonNull String accountName);
109 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
110 public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
111 @NonNull String accountName);
113 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
114 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
115 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
116 " ELSE 9 END AS ordering " + "FROM accounts " +
117 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
118 "ORDER BY ordering, name_upper, rowid ")
119 public abstract LiveData<List<AccountNameContainer>> lookupNamesInProfileByName(long profileId,
123 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
124 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
125 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
126 " ELSE 9 END AS ordering " + "FROM accounts " +
127 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
128 "ORDER BY ordering, name_upper, rowid ")
129 public abstract List<AccountNameContainer> lookupNamesInProfileByNameSync(long profileId,
130 @NonNull String term);
133 @Query("SELECT * FROM accounts " +
134 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
135 "ORDER BY CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
136 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
137 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
138 " ELSE 9 END, name_upper, rowid ")
139 public abstract List<AccountWithAmounts> lookupWithAmountsInProfileByNameSync(long profileId,
140 @NonNull String term);
142 @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
143 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
144 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
145 " ELSE 9 END AS ordering " + "FROM accounts " +
146 "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
147 public abstract LiveData<List<AccountNameContainer>> lookupNamesByName(@NonNull String term);
149 @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
150 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
151 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
152 " ELSE 9 END AS ordering " + "FROM accounts " +
153 "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
154 public abstract List<AccountNameContainer> lookupNamesByNameSync(@NonNull String term);
156 @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
157 public abstract List<Account> allForProfileSync(long profileId);
159 @Query("SELECT generation FROM accounts WHERE profile_id = :profileId LIMIT 1")
160 protected abstract AccountGenerationContainer getGenerationPOJOSync(long profileId);
161 public long getGenerationSync(long profileId) {
162 AccountGenerationContainer result = getGenerationPOJOSync(profileId);
166 return result.generation;
168 @Query("DELETE FROM accounts WHERE profile_id = :profileId AND generation <> " +
169 ":currentGeneration")
170 public abstract void purgeOldAccountsSync(long profileId, long currentGeneration);
172 @Query("DELETE FROM account_values WHERE EXISTS (SELECT 1 FROM accounts a WHERE a" +
173 ".id=account_values.account_id AND a.profile_id=:profileId) AND generation <> " +
174 ":currentGeneration")
175 public abstract void purgeOldAccountValuesSync(long profileId, long currentGeneration);
177 public void storeAccountsSync(List<AccountWithAmounts> accounts, long profileId) {
178 long generation = getGenerationSync(profileId) + 1;
180 for (AccountWithAmounts rec : accounts) {
181 rec.account.setGeneration(generation);
182 rec.account.setProfileId(profileId);
185 purgeOldAccountsSync(profileId, generation);
186 purgeOldAccountValuesSync(profileId, generation);
189 static public class AccountNameContainer {
196 static class AccountGenerationContainer {
199 public AccountGenerationContainer(long generation) {
200 this.generation = generation;