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.Query;
27 import androidx.room.Transaction;
28 import androidx.room.Update;
30 import net.ktnx.mobileledger.db.Account;
31 import net.ktnx.mobileledger.db.AccountWithAmounts;
33 import java.util.ArrayList;
34 import java.util.List;
37 public abstract class AccountDAO extends BaseDAO<Account> {
38 static public List<String> unbox(List<AccountNameContainer> list) {
39 ArrayList<String> result = new ArrayList<>(list.size());
40 for (AccountNameContainer item : list) {
41 result.add(item.name);
47 public abstract long insertSync(Account item);
50 public abstract void updateSync(Account item);
53 public abstract void deleteSync(Account item);
56 public abstract void deleteSync(List<Account> items);
58 @Query("SELECT * FROM accounts WHERE profile_id=:profileId")
59 public abstract LiveData<List<Account>> getAll(long profileId);
62 @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
63 public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId);
65 @Query("SELECT * FROM accounts WHERE id=:id")
66 public abstract Account getByIdSync(long id);
70 // @Query("SELECT * FROM patterns")
71 // List<PatternWithAccounts> getPatternsWithAccounts();
72 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
73 public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
76 @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
77 public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
78 @NonNull String accountName);
80 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
81 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
82 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
83 " ELSE 9 END AS ordering " + "FROM accounts " +
84 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
85 "ORDER BY ordering, name_upper, rowid ")
86 public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(long profileId,
90 @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
91 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
92 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
93 " ELSE 9 END AS ordering " + "FROM accounts " +
94 "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
95 "ORDER BY ordering, name_upper, rowid ")
96 public abstract List<AccountNameContainer> lookupInProfileByNameSync(long profileId,
97 @NonNull String term);
99 @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
100 " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
101 " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
102 " ELSE 9 END AS ordering " + "FROM accounts " +
103 "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
104 public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
106 @Query("SELECT DISTINCT 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 name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
111 public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
113 @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
114 public abstract List<Account> allForProfileSync(long profileId);
116 static public class AccountNameContainer {