2 * Copyright © 2021 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 ORDER BY name")
82 public abstract LiveData<List<Account>> getAll(long profileId);
85 @Query("SELECT * FROM accounts WHERE profile_id = :profileId ORDER BY name")
86 public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId);
88 @Query("SELECT * FROM accounts WHERE id=:id")
89 public abstract Account getByIdSync(long id);
93 // @Query("SELECT * FROM patterns")
94 // List<PatternWithAccounts> getPatternsWithAccounts();
95 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
96 public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
98 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
99 public abstract Account getByNameSync(long profileId, @NonNull String accountName);
102 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
103 public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
104 @NonNull String accountName);
106 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
107 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
108 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
109 " ELSE 9 END AS ordering " + "FROM accounts " +
110 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
111 "ORDER BY ordering, name_upper, rowid ")
112 public abstract LiveData<List<AccountNameContainer>> lookupNamesInProfileByName(long profileId,
116 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
117 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
118 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
119 " ELSE 9 END AS ordering " + "FROM accounts " +
120 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
121 "ORDER BY ordering, name_upper, rowid ")
122 public abstract List<AccountNameContainer> lookupNamesInProfileByNameSync(long profileId,
123 @NonNull String term);
126 @Query("SELECT * FROM accounts " +
127 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
128 "ORDER BY CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
129 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
130 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
131 " ELSE 9 END, name_upper, rowid ")
132 public abstract List<AccountWithAmounts> lookupWithAmountsInProfileByNameSync(long profileId,
133 @NonNull String term);
135 @Query("SELECT DISTINCT name, 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 AS ordering " + "FROM accounts " +
139 "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
140 public abstract LiveData<List<AccountNameContainer>> lookupNamesByName(@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 List<AccountNameContainer> lookupNamesByNameSync(@NonNull String term);
149 @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
150 public abstract List<Account> allForProfileSync(long profileId);
152 @Query("SELECT generation FROM accounts WHERE profile_id = :profileId LIMIT 1")
153 protected abstract AccountGenerationContainer getGenerationPOJOSync(long profileId);
154 public long getGenerationSync(long profileId) {
155 AccountGenerationContainer result = getGenerationPOJOSync(profileId);
159 return result.generation;
161 @Query("DELETE FROM accounts WHERE profile_id = :profileId AND generation <> " +
162 ":currentGeneration")
163 public abstract void purgeOldAccountsSync(long profileId, long currentGeneration);
165 @Query("DELETE FROM account_values WHERE EXISTS (SELECT 1 FROM accounts a WHERE a" +
166 ".id=account_values.account_id AND a.profile_id=:profileId) AND generation <> " +
167 ":currentGeneration")
168 public abstract void purgeOldAccountValuesSync(long profileId, long currentGeneration);
170 public void storeAccountsSync(List<AccountWithAmounts> accounts, long profileId) {
171 long generation = getGenerationSync(profileId) + 1;
173 for (AccountWithAmounts rec : accounts) {
174 rec.account.setGeneration(generation);
175 rec.account.setProfileId(profileId);
178 purgeOldAccountsSync(profileId, generation);
179 purgeOldAccountValuesSync(profileId, generation);
182 static public class AccountNameContainer {
189 static class AccountGenerationContainer {
192 public AccountGenerationContainer(long generation) {
193 this.generation = generation;