]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
3bdf431d3189f52a17f69c3ae8ec0ad2b28912ed
[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.Dao;
23 import androidx.room.Delete;
24 import androidx.room.Insert;
25 import androidx.room.Query;
26 import androidx.room.RoomWarnings;
27 import androidx.room.Update;
28
29 import net.ktnx.mobileledger.db.Account;
30
31 import java.util.List;
32
33 @Dao
34 public abstract class AccountDAO extends BaseDAO<Account> {
35     @Insert
36     public abstract void insertSync(Account item);
37
38     @Update
39     public abstract void updateSync(Account item);
40
41     @Delete
42     public abstract void deleteSync(Account item);
43
44     @Query("SELECT * FROM accounts")
45     public abstract LiveData<List<Account>> getAll();
46
47     @Query("SELECT * FROM accounts WHERE profile = :profileUUID AND name = :accountName")
48     public abstract LiveData<Account> getByName(@NonNull String profileUUID,
49                                                 @NonNull String accountName);
50
51 //    not useful for now
52 //    @Transaction
53 //    @Query("SELECT * FROM patterns")
54 //    List<PatternWithAccounts> getPatternsWithAccounts();
55
56     @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
57            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
58            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
59            "               ELSE 9 END AS ordering " + "FROM accounts " +
60            "WHERE profile=:profileUUID AND name_upper LIKE '%%'||:term||'%%' " +
61            "ORDER BY ordering, name_upper, rowid ")
62     @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
63     public abstract LiveData<List<Account>> lookupInProfileByName(@NonNull String profileUUID,
64                                                                   @NonNull String term);
65
66     @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
67            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
68            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
69            "               ELSE 9 END AS ordering " + "FROM accounts " +
70            "WHERE profile=:profileUUID AND name_upper LIKE '%%'||:term||'%%' " +
71            "ORDER BY ordering, name_upper, rowid ")
72     @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
73     public abstract List<Account> lookupInProfileByNameSync(@NonNull String profileUUID,
74                                                             @NonNull String term);
75
76     @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
77            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
78            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
79            "               ELSE 9 END AS ordering " + "FROM accounts " +
80            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
81     @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
82     public abstract LiveData<List<Account>> lookupByName(@NonNull String term);
83
84     @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
85            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
86            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
87            "               ELSE 9 END AS ordering " + "FROM accounts " +
88            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
89     @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
90     public abstract List<Account> lookupByNameSync(@NonNull String term);
91 }