Skip to content

Commit

Permalink
Merge pull request codinguser#439 from fefe982/patches
Browse files Browse the repository at this point in the history
fix the bug that modify account name will delete all transaction under the account
  • Loading branch information
codinguser committed Nov 27, 2015
2 parents a3ad8c2 + de6afbc commit c2df4e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/src/main/java/org/gnucash/android/db/DatabaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public abstract class DatabaseAdapter<Model extends BaseModel> {

protected SQLiteStatement mReplaceStatement;

public enum UpdateMethod {
insert, update, replace
};

/**
* Opens the database adapter with an existing database
* @param db SQLiteDatabase object
Expand Down Expand Up @@ -620,6 +624,20 @@ public void setTransactionSuccessful() {
mDb.setTransactionSuccessful();
}

/// Foreign key constraits should be enabled in general.
/// But if it affects speed (check constraints takes time)
/// and the constrained can be assured by the program,
/// or if some SQL exec will cause deletion of records
/// (like use replace in accounts update will delete all transactions)
/// that need not be deleted, then it can be disabled temporarily
public void enableForeignKey(boolean enable) {
if (enable){
mDb.execSQL("PRAGMA foreign_keys=ON");
} else {
mDb.execSQL("PRAGMA foreign_keys=OFF");
}
}

/**
* Expose mDb.endTransaction()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,9 @@ private void saveAccount() {
if (mAccountsDbAdapter == null)
mAccountsDbAdapter = AccountsDbAdapter.getInstance();
// bulk update, will not update transactions
mAccountsDbAdapter.enableForeignKey(false);
mAccountsDbAdapter.bulkAddRecords(accountsToUpdate);
mAccountsDbAdapter.enableForeignKey(true);

finishFragment();
}
Expand Down

0 comments on commit c2df4e5

Please sign in to comment.