import androidx.room.Query;
import androidx.room.Update;
+import net.ktnx.mobileledger.db.DB;
import net.ktnx.mobileledger.db.Transaction;
+import net.ktnx.mobileledger.db.TransactionAccount;
import net.ktnx.mobileledger.db.TransactionWithAccounts;
+import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
import java.util.ArrayList;
import java.util.List;
+import java.util.Locale;
@Dao
public abstract class TransactionDAO extends BaseDAO<Transaction> {
@Delete
public abstract void deleteSync(Transaction item);
+ @Delete
+ public abstract void deleteSync(Transaction... items);
+
@Delete
public abstract void deleteSync(List<Transaction> items);
public abstract TransactionWithAccounts getFirstByDescriptionSync(@NonNull String description);
@androidx.room.Transaction
- @Query("SELECT * from transactions tr JOIN transaction_accounts t_a ON t_a.transaction_id = " +
- "tr.id WHERE tr.description = :description AND t_a.account_name LIKE " +
- "'%'||:accountTerm||'%' ORDER BY year desc, month desc, day desc, tr.ledger_id desc " +
- "LIMIT 1")
+ @Query("SELECT tr.id, tr.profile_id, tr.ledger_id, tr.description, tr.data_hash, tr.comment, " +
+ "tr.year, tr.month, tr.day, tr.generation from transactions tr JOIN " +
+ "transaction_accounts t_a ON t_a.transaction_id = tr.id WHERE tr.description = " +
+ ":description AND t_a.account_name LIKE '%'||:accountTerm||'%' ORDER BY year desc, " +
+ "month desc, day desc, tr.ledger_id desc LIMIT 1")
public abstract TransactionWithAccounts getFirstByDescriptionHavingAccountSync(
@NonNull String description, @NonNull String accountTerm);
- @Query("SELECT * from transactions WHERE profile_id = :profileId ORDER BY " +
- "year desc, month desc, day desc, ledger_id desc")
- public abstract List<Transaction> allForProfileSync(long profileId);
+ @Query("SELECT * from transactions WHERE profile_id = :profileId")
+ public abstract List<Transaction> getAllForProfileUnorderedSync(long profileId);
@Query("SELECT generation FROM transactions WHERE profile_id = :profileId LIMIT 1")
protected abstract TransactionGenerationContainer getGenerationPOJOSync(long profileId);
@Query("SELECT * FROM transactions WHERE profile_id = :profileId")
public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
+ @androidx.room.Transaction
+ @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
+ public abstract LiveData<List<TransactionWithAccounts>> getAllWithAccounts(long profileId);
+
@androidx.room.Transaction
@Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
" tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
public abstract List<TransactionWithAccounts> getAllWithAccountsFilteredSync(long profileId,
String accountName);
+ @androidx.room.Transaction
+ @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
+ " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
+ "transaction_accounts ta ON ta.transaction_id=tr.id WHERE ta.account_name LIKE " +
+ ":accountName||'%' AND ta.amount <> 0 AND tr.profile_id = :profileId ORDER BY tr.year " +
+ "desc, tr.month desc, tr.day desc, tr.ledger_id desc")
+ public abstract LiveData<List<TransactionWithAccounts>> getAllWithAccountsFiltered(
+ long profileId, String accountName);
+
+ @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
+ ":currentGeneration")
+ public abstract int purgeOldTransactionsSync(long profileId, long currentGeneration);
+
+ @Query("DELETE FROM transaction_accounts WHERE EXISTS (SELECT 1 FROM transactions tr WHERE tr" +
+ ".id=transaction_accounts.transaction_id AND tr.profile_id=:profileId) AND generation " +
+ "<> :currentGeneration")
+ public abstract int purgeOldTransactionAccountsSync(long profileId, long currentGeneration);
+
+ @Query("DELETE FROM transactions WHERE profile_id = :profileId")
+ public abstract int deleteAllSync(long profileId);
+
+ @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId")
+ public abstract Transaction getByLedgerId(long profileId, long ledgerId);
+
+ @Query("UPDATE transactions SET generation = :newGeneration WHERE id = :transactionId")
+ public abstract int updateGeneration(long transactionId, long newGeneration);
+
+ @Query("UPDATE transaction_accounts SET generation = :newGeneration WHERE transaction_id = " +
+ ":transactionId")
+ public abstract int updateAccountsGeneration(long transactionId, long newGeneration);
+ @androidx.room.Transaction
+ public void updateGenerationWithAccounts(long transactionId, long newGeneration) {
+ updateGeneration(transactionId, newGeneration);
+ updateAccountsGeneration(transactionId, newGeneration);
+ }
public long getGenerationSync(long profileId) {
TransactionGenerationContainer result = getGenerationPOJOSync(profileId);
return 0;
return result.generation;
}
- @Query("DELETE FROM transactions WHERE profile_id = :profileId AND generation <> " +
- ":currentGeneration")
- public abstract void purgeOldTransactionsSync(long profileId, long currentGeneration);
+ public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
+ long generation = getGenerationSync(profileId) + 1;
+
+ for (TransactionWithAccounts tr : list) {
+ tr.transaction.setGeneration(generation);
+ tr.transaction.setProfileId(profileId);
+
+ storeSync(tr);
+ }
+
+ Logger.debug("Transaction", "Purging old transactions");
+ int removed = purgeOldTransactionsSync(profileId, generation);
+ Logger.debug("Transaction", String.format(Locale.ROOT, "Purged %d transactions", removed));
+
+ removed = purgeOldTransactionAccountsSync(profileId, generation);
+ Logger.debug("Transaction",
+ String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
+ }
+ private void storeSync(TransactionWithAccounts rec) {
+ TransactionAccountDAO trAccDao = DB.get()
+ .getTransactionAccountDAO();
+
+ Transaction transaction = rec.transaction;
+ Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
+ if (existing != null) {
+ if (Misc.equalStrings(transaction.getDataHash(), existing.getDataHash())) {
+ updateGenerationWithAccounts(existing.getId(), rec.transaction.getGeneration());
+ return;
+ }
+
+ existing.copyDataFrom(transaction);
+ updateSync(existing);
+
+ transaction = existing;
+ }
+ else
+ transaction.setId(insertSync(transaction));
+
+ for (TransactionAccount trAcc : rec.accounts) {
+ trAcc.setTransactionId(transaction.getId());
+ trAcc.setGeneration(transaction.getGeneration());
+ TransactionAccount existingAcc =
+ trAccDao.getByOrderNoSync(trAcc.getTransactionId(), trAcc.getOrderNo());
+ if (existingAcc != null) {
+ existingAcc.copyDataFrom(trAcc);
+ trAccDao.updateSync(existingAcc);
+ }
+ else
+ trAcc.setId(trAccDao.insertSync(trAcc));
+ }
+ }
static class TransactionGenerationContainer {
@ColumnInfo
long generation;