]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
selects from joined tables need explicit column list :(
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TransactionDAO.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.OnConflictStrategy;
27 import androidx.room.Query;
28 import androidx.room.Update;
29
30 import net.ktnx.mobileledger.db.DB;
31 import net.ktnx.mobileledger.db.Transaction;
32 import net.ktnx.mobileledger.db.TransactionAccount;
33 import net.ktnx.mobileledger.db.TransactionWithAccounts;
34 import net.ktnx.mobileledger.utils.Logger;
35
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Locale;
39
40 @Dao
41 public abstract class TransactionDAO extends BaseDAO<Transaction> {
42     static public List<String> unbox(List<DescriptionContainer> list) {
43         ArrayList<String> result = new ArrayList<>(list.size());
44         for (DescriptionContainer item : list) {
45             result.add(item.description);
46         }
47
48         return result;
49     }
50     @Insert(onConflict = OnConflictStrategy.REPLACE)
51     public abstract long insertSync(Transaction item);
52
53     @Update
54     public abstract void updateSync(Transaction item);
55
56     @Delete
57     public abstract void deleteSync(Transaction item);
58
59     @Delete
60     public abstract void deleteSync(List<Transaction> items);
61
62     @Query("SELECT * FROM transactions WHERE id = :id")
63     public abstract LiveData<Transaction> getById(long id);
64
65     @androidx.room.Transaction
66     @Query("SELECT * FROM transactions WHERE id = :transactionId")
67     public abstract LiveData<TransactionWithAccounts> getByIdWithAccounts(long transactionId);
68
69     @androidx.room.Transaction
70     @Query("SELECT * FROM transactions WHERE id = :transactionId")
71     public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
72
73     @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
74            "               WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
75            "               WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
76            "               ELSE 9 END AS ordering " + "FROM description_history " +
77            "WHERE description_upper LIKE '%%'||:term||'%%' " +
78            "ORDER BY ordering, description_upper, rowid ")
79     public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
80
81     @androidx.room.Transaction
82     @Query("SELECT * from transactions WHERE description = :description ORDER BY year desc, month" +
83            " desc, day desc LIMIT 1")
84     public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
85
86     @androidx.room.Transaction
87     @Query("SELECT tr.id, tr.profile_id, tr.ledger_id, tr.description, tr.data_hash, tr.comment, " +
88            "tr.year, tr.month, tr.day, tr.generation from transactions tr JOIN " +
89            "transaction_accounts t_a ON t_a.transaction_id = tr.id WHERE tr.description = " +
90            ":description AND t_a.account_name LIKE '%'||:accountTerm||'%' ORDER BY year desc, " +
91            "month desc, day desc, tr.ledger_id desc LIMIT 1")
92     public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
93             @NonNull String description, @NonNull String accountTerm);
94
95     @Query("SELECT * from transactions WHERE profile_id = :profileId ORDER BY " +
96            "year desc, month desc, day desc, ledger_id desc")
97     public abstract List<Transaction> allForProfileSync(long profileId);
98
99     @Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
100     protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
101
102     @androidx.room.Transaction
103     @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
104     public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
105
106     @androidx.room.Transaction
107     @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
108            " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
109            "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
110            ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
111            "desc, tr.month desc, tr.day desc, tr.ledger_id desc")
112     public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
113                                                                                  String accountName);
114
115     public long getGenerationSync(long profileId) {
116         TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
117
118         if (result == null)
119             return 0;
120         return result.generation;
121     }
122     @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
123            ":currentGeneration")
124     public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
125
126     @Query("DELETE FROM transaction_accounts WHERE EXISTS (SELECT 1 FROM transactions tr WHERE tr" +
127            ".id=transaction_accounts.transaction_id AND tr.profile_id=:profileId) AND generation " +
128            "<> :currentGeneration")
129     public abstract int purgeOldTransactionAccountsSync(long profileId, long currentGeneration);
130     public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
131         long generation = getGenerationSync(profileId) + 1;
132
133         for (TransactionWithAccounts tr : list) {
134             tr.transaction.setGeneration(generation);
135             tr.transaction.setProfileId(profileId);
136
137             storeSync(tr);
138         }
139
140         Logger.debug("Transaction", "Purging old transactions");
141         int removed = purgeOldTransactionsSync(profileId, generation);
142         Logger.debug("Transaction", String.format(Locale.ROOT, "Purged %d transactions", removed));
143
144         removed = purgeOldTransactionAccountsSync(profileId, generation);
145         Logger.debug("Transaction",
146                 String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
147     }
148     private void storeSync(TransactionWithAccounts rec) {
149         TransactionAccountDAO trAccDao = DB.get()
150                                            .getTransactionAccountDAO();
151
152         Transaction transaction = rec.transaction;
153         Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
154         if (existing != null) {
155             existing.copyDataFrom(transaction);
156             updateSync(existing);
157             transaction = existing;
158         }
159         else
160             transaction.setId(insertSync(transaction));
161
162         for (TransactionAccount trAcc : rec.accounts) {
163             trAcc.setTransactionId(transaction.getId());
164             trAcc.setGeneration(transaction.getGeneration());
165             TransactionAccount existingAcc =
166                     trAccDao.getByOrderNoSync(trAcc.getTransactionId(), trAcc.getOrderNo());
167             if (existingAcc != null) {
168                 existingAcc.copyDataFrom(trAcc);
169                 trAccDao.updateSync(trAcc);
170                 trAcc = existingAcc;
171             }
172             else
173                 trAcc.setId(trAccDao.insertSync(trAcc));
174         }
175     }
176     @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
177     public abstract Transaction getByLedgerId(long profileId, long ledgerId);
178     static class TransactionGenerationContainer {
179         @ColumnInfo
180         long generation;
181         public TransactionGenerationContainer(long generation) {
182             this.generation = generation;
183         }
184     }
185
186     static public class DescriptionContainer {
187         @ColumnInfo
188         public String description;
189         @ColumnInfo
190         public int ordering;
191     }
192 }