Skip to content

Commit

Permalink
Cannot show transaction details if not added to database.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Jan 21, 2025
1 parent a70b39f commit d53e7eb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.text.TextUtils;
Expand Down Expand Up @@ -211,7 +212,7 @@ public boolean isOpen() {
*
* @param model Model to be saved to the database
*/
public void addRecord(@NonNull final Model model) {
public void addRecord(@NonNull final Model model) throws SQLException {
addRecord(model, UpdateMethod.replace);
}

Expand All @@ -222,14 +223,14 @@ public void addRecord(@NonNull final Model model) {
* @param model Subclass of {@link BaseModel} to be added
* @param updateMethod Method to use for adding the record
*/
public void addRecord(@NonNull final Model model, UpdateMethod updateMethod) {
public void addRecord(@NonNull final Model model, UpdateMethod updateMethod) throws SQLException {
Timber.d("Adding %s record to database: ", model.getClass().getSimpleName());
final SQLiteStatement statement;
switch (updateMethod) {
case insert:
statement = getInsertStatement();
synchronized (statement) {
setBindings(statement, model).execute();
model.id = setBindings(statement, model).executeInsert();
}
break;
case update:
Expand All @@ -254,29 +255,33 @@ public void addRecord(@NonNull final Model model, UpdateMethod updateMethod) {
* @param updateMethod Method to use when persisting them
* @return Number of rows affected in the database
*/
private long doAddModels(@NonNull final List<Model> modelList, UpdateMethod updateMethod) {
private long doAddModels(@NonNull final List<Model> modelList, UpdateMethod updateMethod) throws SQLException {
long nRow = 0;
final SQLiteStatement statement;
switch (updateMethod) {
case update:
synchronized (getUpdateStatement()) {
statement = getUpdateStatement();
synchronized (statement) {
for (Model model : modelList) {
setBindings(getUpdateStatement(), model).execute();
setBindings(statement, model).execute();
nRow++;
}
}
break;
case insert:
synchronized (getInsertStatement()) {
statement = getInsertStatement();
synchronized (statement) {
for (Model model : modelList) {
setBindings(getInsertStatement(), model).execute();
setBindings(statement, model).execute();
nRow++;
}
}
break;
default:
synchronized (getReplaceStatement()) {
statement = getReplaceStatement();
synchronized (statement) {
for (Model model : modelList) {
setBindings(getReplaceStatement(), model).execute();
setBindings(statement, model).execute();
nRow++;
}
}
Expand All @@ -296,14 +301,14 @@ public long bulkAddRecords(@NonNull List<Model> modelList) {
return bulkAddRecords(modelList, UpdateMethod.replace);
}

public long bulkAddRecords(@NonNull List<Model> modelList, UpdateMethod updateMethod) {
public long bulkAddRecords(@NonNull List<Model> modelList, UpdateMethod updateMethod) throws SQLException {
if (modelList.isEmpty()) {
Timber.w("Empty model list. Cannot bulk add records, returning 0");
return 0;
}

Timber.i("Bulk adding %d %s records to the database", modelList.size(),
modelList.isEmpty() ? "null" : modelList.get(0).getClass().getSimpleName());
modelList.get(0).getClass().getSimpleName());
long nRow;
try {
beginTransaction();
Expand Down Expand Up @@ -719,7 +724,7 @@ public int updateRecord(@NonNull String uid, @NonNull ContentValues contentValue
return mDb.update(mTableName, contentValues, CommonColumns.COLUMN_UID + "=?", new String[]{uid});
}

public void updateRecord(Model model) {
public void updateRecord(Model model) throws SQLException {
addRecord(model, UpdateMethod.update);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public SplitsDbAdapter getSplitDbAdapter() {
* @param transaction {@link Transaction} to be inserted to database
*/
@Override
public void addRecord(@NonNull Transaction transaction, UpdateMethod updateMethod) {
public void addRecord(@NonNull Transaction transaction, UpdateMethod updateMethod) throws SQLException {
Timber.d("Adding transaction to the db via %s", updateMethod.name());
try {
beginTransaction();
Expand All @@ -118,7 +118,7 @@ public void addRecord(@NonNull Transaction transaction, UpdateMethod updateMetho
super.addRecord(transaction, updateMethod);

Timber.d("Adding splits for transaction");
ArrayList<String> splitUIDs = new ArrayList<>(transaction.getSplits().size());
List<String> splitUIDs = new ArrayList<>(transaction.getSplits().size());
for (Split split : transaction.getSplits()) {
Timber.d("Replace transaction split in db");
if (imbalanceSplit == split) {
Expand All @@ -137,8 +137,6 @@ public void addRecord(@NonNull Transaction transaction, UpdateMethod updateMetho
Timber.d("%d splits deleted", deleted);

setTransactionSuccessful();
} catch (SQLException e) {
Timber.e(e);
} finally {
endTransaction();
}
Expand All @@ -155,7 +153,7 @@ public void addRecord(@NonNull Transaction transaction, UpdateMethod updateMetho
* @return Number of transactions inserted
*/
@Override
public long bulkAddRecords(@NonNull List<Transaction> transactionList, UpdateMethod updateMethod) {
public long bulkAddRecords(@NonNull List<Transaction> transactionList, UpdateMethod updateMethod) throws SQLException {
long start = System.nanoTime();
long rowInserted = super.bulkAddRecords(transactionList, updateMethod);
long end = System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -43,6 +44,8 @@

import java.util.MissingFormatArgumentException;

import timber.log.Timber;

/**
* Activity for displaying transaction information
*
Expand Down Expand Up @@ -275,7 +278,13 @@ private void duplicateTransaction(@Nullable String transactionUID) {
Transaction transaction = dbAdapter.getRecord(transactionUID);
Transaction duplicate = new Transaction(transaction, true);
duplicate.setTime(System.currentTimeMillis());
dbAdapter.addRecord(duplicate, DatabaseAdapter.UpdateMethod.insert);
try {
dbAdapter.addRecord(duplicate, DatabaseAdapter.UpdateMethod.insert);
if (duplicate.id <= 0) return;
} catch (SQLException e) {
Timber.e(e);
return;
}

// Show the new transaction
Intent intent = new Intent(getIntent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -440,11 +441,15 @@ private void deleteTransaction(long transactionId) {
}

private void duplicateTransaction(long transactionId) {
Transaction transaction = mTransactionsDbAdapter.getRecord(transactionId);
Transaction duplicate = new Transaction(transaction, true);
duplicate.setTime(System.currentTimeMillis());
mTransactionsDbAdapter.addRecord(duplicate, DatabaseAdapter.UpdateMethod.insert);
refresh();
try {
Transaction transaction = mTransactionsDbAdapter.getRecord(transactionId);
Transaction duplicate = new Transaction(transaction, true);
duplicate.setTime(System.currentTimeMillis());
mTransactionsDbAdapter.addRecord(duplicate, DatabaseAdapter.UpdateMethod.insert);
refresh();
} catch (SQLException e) {
Timber.e(e);
}
}

private void moveTransaction(long transactionId) {
Expand Down

0 comments on commit d53e7eb

Please sign in to comment.