]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
more Room adoption - accounts@100%, some profiles/transactions
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TransactionDAO.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.Transaction;
30 import net.ktnx.mobileledger.db.TransactionWithAccounts;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 @Dao
36 public abstract class TransactionDAO extends BaseDAO<Transaction> {
37     static public List<String> unbox(List<DescriptionContainer> list) {
38         ArrayList<String> result = new ArrayList<>(list.size());
39         for (DescriptionContainer item : list) {
40             result.add(item.description);
41         }
42
43         return result;
44     }
45     @Insert
46     public abstract long insertSync(Transaction item);
47
48     @Update
49     public abstract void updateSync(Transaction item);
50
51     @Delete
52     public abstract void deleteSync(Transaction item);
53
54     @Delete
55     public abstract void deleteSync(List<Transaction> items);
56
57     @Query("SELECT * FROM transactions")
58     public abstract LiveData<List<Transaction>> getAll();
59
60     //    not useful for now
61 //    @Transaction
62 //    @Query("SELECT * FROM patterns")
63 //    List<PatternWithAccounts> getPatternsWithAccounts();
64     @Query("SELECT * FROM transactions WHERE id = :id")
65     public abstract LiveData<Transaction> getById(long id);
66
67     @androidx.room.Transaction
68     @Query("SELECT * FROM transactions WHERE id = :transactionId")
69     public abstract LiveData<TransactionWithAccounts> getByIdWithAccounts(long transactionId);
70
71     @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
72            "               WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
73            "               WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
74            "               ELSE 9 END AS ordering " + "FROM description_history " +
75            "WHERE description_upper LIKE '%%'||:term||'%%' " +
76            "ORDER BY ordering, description_upper, rowid ")
77     public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
78
79     @Query("SELECT * from transactions WHERE profile_id = :profileId")
80     public abstract List<Transaction> allForProfileSync(long profileId);
81
82     static public class DescriptionContainer {
83         @ColumnInfo
84         public String description;
85         @ColumnInfo
86         public int ordering;
87     }
88 }