]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
migrate to surrogate IDs for all database objects
[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.Update;
28
29 import net.ktnx.mobileledger.db.Account;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 @Dao
35 public abstract class AccountDAO extends BaseDAO<Account> {
36     static public List<String> unbox(List<AccountNameContainer> list) {
37         ArrayList<String> result = new ArrayList<>(list.size());
38         for (AccountNameContainer item : list) {
39             result.add(item.name);
40         }
41
42         return result;
43     }
44     @Insert
45     public abstract long insertSync(Account item);
46
47     @Update
48     public abstract void updateSync(Account item);
49
50     @Delete
51     public abstract void deleteSync(Account item);
52
53     @Query("SELECT * FROM accounts")
54     public abstract LiveData<List<Account>> getAll();
55
56     //    not useful for now
57 //    @Transaction
58 //    @Query("SELECT * FROM patterns")
59 //    List<PatternWithAccounts> getPatternsWithAccounts();
60     @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName")
61     public abstract LiveData<Account> getByName(long profileId, @NonNull String accountName);
62
63     @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
64            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
65            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
66            "               ELSE 9 END AS ordering " + "FROM accounts " +
67            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
68            "ORDER BY ordering, name_upper, rowid ")
69     public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(long profileId,
70                                                                                @NonNull
71                                                                                        String term);
72
73     @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
74            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
75            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
76            "               ELSE 9 END AS ordering " + "FROM accounts " +
77            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
78            "ORDER BY ordering, name_upper, rowid ")
79     public abstract List<AccountNameContainer> lookupInProfileByNameSync(long profileId,
80                                                                          @NonNull String term);
81
82     @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
83            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
84            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
85            "               ELSE 9 END AS ordering " + "FROM accounts " +
86            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
87     public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
88
89     @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
90            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
91            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
92            "               ELSE 9 END AS ordering " + "FROM accounts " +
93            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
94     public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
95
96     static public class AccountNameContainer {
97         @ColumnInfo
98         public String name;
99         @ColumnInfo
100         public int ordering;
101     }
102 }