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("SELECT * FROM accounts WHERE profile_id=:profileId")
79 public abstract LiveData<List<Account>> getAll(long profileId);
82 @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
83 public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId);
85 @Query("SELECT * FROM accounts WHERE id=:id")
86 public abstract Account getByIdSync(long id);
90 // @Query("SELECT * FROM patterns")
91 // List<PatternWithAccounts> getPatternsWithAccounts();
92 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
93 public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
95 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
96 public abstract Account getByNameSync(long profileId, @NonNull String accountName);
99 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
100 public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
101 @NonNull String accountName);
103 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
104 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
105 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
106 " ELSE 9 END AS ordering " + "FROM accounts " +
107 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
108 "ORDER BY ordering, name_upper, rowid ")
109 public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(long profileId,
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 List<AccountNameContainer> lookupInProfileByNameSync(long profileId,
120 @NonNull String term);
122 @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
123 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
124 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
125 " ELSE 9 END AS ordering " + "FROM accounts " +
126 "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
127 public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
129 @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
130 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
131 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
132 " ELSE 9 END AS ordering " + "FROM accounts " +
133 "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
134 public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
136 @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
137 public abstract List<Account> allForProfileSync(long profileId);
139 @Query("SELECT generation FROM accounts WHERE profile_id = :profileId LIMIT 1")
140 protected abstract AccountGenerationContainer getGenerationPOJOSync(long profileId);
141 public long getGenerationSync(long profileId) {
142 AccountGenerationContainer result = getGenerationPOJOSync(profileId);
146 return result.generation;
148 @Query("DELETE FROM accounts WHERE profile_id = :profileId AND generation <> " +
149 ":currentGeneration")
150 public abstract void purgeOldAccountsSync(long profileId, long currentGeneration);
152 @Query("DELETE FROM account_values WHERE EXISTS (SELECT 1 FROM accounts a WHERE a" +
153 ".id=account_values.account_id AND a.profile_id=:profileId) AND generation <> " +
154 ":currentGeneration")
155 public abstract void purgeOldAccountValuesSync(long profileId, long currentGeneration);
157 public void storeAccountsSync(List<AccountWithAmounts> accounts, long profileId) {
158 long generation = getGenerationSync(profileId) + 1;
160 for (AccountWithAmounts rec : accounts) {
161 rec.account.setGeneration(generation);
162 rec.account.setProfileId(profileId);
165 purgeOldAccountsSync(profileId, generation);
166 purgeOldAccountValuesSync(profileId, generation);
169 static public class AccountNameContainer {
176 static class AccountGenerationContainer {
179 public AccountGenerationContainer(long generation) {
180 this.generation = generation;