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.
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.
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/>.
18 package net.ktnx.mobileledger.dao;
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;
29 import net.ktnx.mobileledger.db.DescriptionHistory;
31 import java.util.ArrayList;
32 import java.util.List;
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);
45 public abstract long insertSync(DescriptionHistory item);
48 public abstract void updateSync(DescriptionHistory item);
51 public abstract void deleteSync(DescriptionHistory item);
54 public abstract void deleteSync(List<DescriptionHistory> items);
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();
60 @Query("SELECT * FROM description_history")
61 public abstract LiveData<List<DescriptionHistory>> getAll();
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);
71 static public class DescriptionContainer {
73 public String description;