]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
adopt Room for displaying account lists
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / AccountDAO.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.dao;
19
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;
29
30 import net.ktnx.mobileledger.db.Account;
31 import net.ktnx.mobileledger.db.AccountWithAmounts;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 @Dao
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);
42         }
43
44         return result;
45     }
46     @Insert
47     public abstract long insertSync(Account item);
48
49     @Update
50     public abstract void updateSync(Account item);
51
52     @Delete
53     public abstract void deleteSync(Account item);
54
55     @Delete
56     public abstract void deleteSync(List<Account> items);
57
58     @Query("SELECT * FROM accounts WHERE profile_id=:profileId")
59     public abstract LiveData<List<Account>> getAll(long profileId);
60
61     @Transaction
62     @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
63     public abstract LiveData<List<AccountWithAmounts>> getAllWithAmounts(long profileId);
64
65     @Query("SELECT * FROM accounts WHERE id=:id")
66     public abstract Account getByIdSync(long id);
67
68     //    not useful for now
69 //    @Transaction
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);
74
75     @Transaction
76     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
77     public abstract LiveData<AccountWithAmounts> getByNameWithAmounts(long profileId,
78                                                                       @NonNull String accountName);
79
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,
87                                                                                @NonNull
88                                                                                        String term);
89
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);
98
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);
105
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);
112
113     @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
114     public abstract List<Account> allForProfileSync(long profileId);
115
116     static public class AccountNameContainer {
117         @ColumnInfo
118         public String name;
119         @ColumnInfo
120         public int ordering;
121     }
122 }