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.OnConflictStrategy;
27 import androidx.room.Query;
28 import androidx.room.Update;
30 import net.ktnx.mobileledger.db.Transaction;
31 import net.ktnx.mobileledger.db.TransactionWithAccounts;
33 import java.util.ArrayList;
34 import java.util.List;
37 public abstract class TransactionDAO extends BaseDAO<Transaction> {
38 static public List<String> unbox(List<DescriptionContainer> list) {
39 ArrayList<String> result = new ArrayList<>(list.size());
40 for (DescriptionContainer item : list) {
41 result.add(item.description);
46 @Insert(onConflict = OnConflictStrategy.REPLACE)
47 public abstract long insertSync(Transaction item);
50 public abstract void updateSync(Transaction item);
53 public abstract void deleteSync(Transaction item);
56 public abstract void deleteSync(List<Transaction> items);
58 @Query("SELECT * FROM transactions")
59 public abstract LiveData<List<Transaction>> getAll();
63 // @Query("SELECT * FROM patterns")
64 // List<PatternWithAccounts> getPatternsWithAccounts();
65 @Query("SELECT * FROM transactions WHERE id = :id")
66 public abstract LiveData<Transaction> getById(long id);
68 @androidx.room.Transaction
69 @Query("SELECT * FROM transactions WHERE id = :transactionId")
70 public abstract LiveData<TransactionWithAccounts> getByIdWithAccounts(long transactionId);
72 @androidx.room.Transaction
73 @Query("SELECT * FROM transactions WHERE id = :transactionId")
74 public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
76 @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
77 " WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
78 " WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
79 " ELSE 9 END AS ordering " + "FROM description_history " +
80 "WHERE description_upper LIKE '%%'||:term||'%%' " +
81 "ORDER BY ordering, description_upper, rowid ")
82 public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
84 @androidx.room.Transaction
85 @Query("SELECT * from transactions WHERE description = :description ORDER BY year desc, month" +
86 " desc, day desc LIMIT 1")
87 public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
89 @androidx.room.Transaction
90 @Query("SELECT * from transactions tr JOIN transaction_accounts t_a ON t_a.transaction_id = " +
91 "tr.id WHERE tr.description = :description AND t_a.account_name LIKE " +
92 "'%'||:accountTerm||'%' ORDER BY year desc, month desc, day desc LIMIT 1")
93 public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
94 @NonNull String description, @NonNull String accountTerm);
96 @Query("SELECT * from transactions WHERE profile_id = :profileId")
97 public abstract List<Transaction> allForProfileSync(long profileId);
99 @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
100 protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
101 public long getGenerationSync(long profileId) {
102 TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
106 return result.generation;
108 @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
109 ":currentGeneration")
110 public abstract void purgeOldTransactionsSync(long profileId, long currentGeneration);
111 static class TransactionGenerationContainer {
114 public TransactionGenerationContainer(long generation) {
115 this.generation = generation;
119 static public class DescriptionContainer {
121 public String description;