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.Transaction;
31 import java.util.ArrayList;
32 import java.util.List;
35 public abstract class TransactionDAO extends BaseDAO<Transaction> {
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(Transaction item);
48 public abstract void updateSync(Transaction item);
51 public abstract void deleteSync(Transaction item);
53 @Query("SELECT * FROM transactions")
54 public abstract LiveData<List<Transaction>> getAll();
58 // @Query("SELECT * FROM patterns")
59 // List<PatternWithAccounts> getPatternsWithAccounts();
60 @Query("SELECT * FROM transactions WHERE id = :id")
61 public abstract LiveData<Transaction> getById(long id);
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> lookupDescriptionSync(@NonNull String term);
71 static public class DescriptionContainer {
73 public String description;