]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/DescriptionHistoryDAO.java
more Room adoption - accounts@100%, some profiles/transactions
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / DescriptionHistoryDAO.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.DescriptionHistory;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 @Dao
35 public abstract class DescriptionHistoryDAO extends BaseDAO<DescriptionHistory> {
36     static public List<String> unbox(List<DescriptionContainer> list) {
37         ArrayList<String> result = new ArrayList<>(list.size());
38         for (DescriptionContainer item : list) {
39             result.add(item.description);
40         }
41
42         return result;
43     }
44     @Insert
45     public abstract long insertSync(DescriptionHistory item);
46
47     @Update
48     public abstract void updateSync(DescriptionHistory item);
49
50     @Delete
51     public abstract void deleteSync(DescriptionHistory item);
52
53     @Delete
54     public abstract void deleteSync(List<DescriptionHistory> items);
55
56     @Query("DELETE FROM description_history where not exists (select 1 from transactions tr where" +
57            " upper(tr.description)=description_history.description_upper)")
58     public abstract void sweepSync();
59
60     @Query("SELECT * FROM description_history")
61     public abstract LiveData<List<DescriptionHistory>> getAll();
62
63     @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
64            "               WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
65            "               WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
66            "               ELSE 9 END AS ordering " + "FROM description_history " +
67            "WHERE description_upper LIKE '%%'||:term||'%%' " +
68            "ORDER BY ordering, description_upper, rowid ")
69     public abstract List<DescriptionContainer> lookupSync(@NonNull String term);
70
71     static public class DescriptionContainer {
72         @ColumnInfo
73         public String description;
74         @ColumnInfo
75         public int ordering;
76     }
77 }