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 WHERE id = :id")
59 public abstract LiveData<Transaction> getById(long id);
61 @androidx.room.Transaction
62 @Query("SELECT * FROM transactions WHERE id = :transactionId")
63 public abstract LiveData<TransactionWithAccounts> getByIdWithAccounts(long transactionId);
65 @androidx.room.Transaction
66 @Query("SELECT * FROM transactions WHERE id = :transactionId")
67 public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
69 @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
70 " WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
71 " WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
72 " ELSE 9 END AS ordering " + "FROM description_history " +
73 "WHERE description_upper LIKE '%%'||:term||'%%' " +
74 "ORDER BY ordering, description_upper, rowid ")
75 public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
77 @androidx.room.Transaction
78 @Query("SELECT * from transactions WHERE description = :description ORDER BY year desc, month" +
79 " desc, day desc LIMIT 1")
80 public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
82 @androidx.room.Transaction
83 @Query("SELECT * from transactions tr JOIN transaction_accounts t_a ON t_a.transaction_id = " +
84 "tr.id WHERE tr.description = :description AND t_a.account_name LIKE " +
85 "'%'||:accountTerm||'%' ORDER BY year desc, month desc, day desc LIMIT 1")
86 public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
87 @NonNull String description, @NonNull String accountTerm);
89 @Query("SELECT * from transactions WHERE profile_id = :profileId ORDER BY " +
90 "year desc, month desc, day desc, id desc")
91 public abstract List<Transaction> allForProfileSync(long profileId);
93 @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
94 protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
96 @androidx.room.Transaction
97 @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
98 public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
100 @androidx.room.Transaction
101 @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
102 " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
103 "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
104 ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
105 "desc, tr.month desc, tr.day desc, tr.id desc")
106 public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
109 public long getGenerationSync(long profileId) {
110 TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
114 return result.generation;
116 @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
117 ":currentGeneration")
118 public abstract void purgeOldTransactionsSync(long profileId, long currentGeneration);
119 static class TransactionGenerationContainer {
122 public TransactionGenerationContainer(long generation) {
123 this.generation = generation;
127 static public class DescriptionContainer {
129 public String description;