@@ -279,10 +279,25 @@ public boolean destructiveDeleteAccount(long rowId){
279279 }
280280 }
281281 }
282+ // TODO: with "ON DELETE CASCADE", the first two delete will not be necessary.
283+ // deleteRecord(AccountEntry.TABLE_NAME, rowId); will delete related
284+ // transactions and splits
282285 //delete splits in this account
283286 mDb .delete (SplitEntry .TABLE_NAME ,
284- SplitEntry .COLUMN_ACCOUNT_UID + "=?" ,
287+ SplitEntry .COLUMN_TRANSACTION_UID + " IN ( SELECT DISTINCT "
288+ + TransactionEntry .TABLE_NAME + "_" + TransactionEntry .COLUMN_UID
289+ + " FROM trans_split_acct WHERE "
290+ + AccountEntry .TABLE_NAME + "_" + AccountEntry .COLUMN_UID
291+ + " = ? )" ,
285292 new String []{getAccountUID (rowId )});
293+ // delete empty transactions
294+ // trans_split_acct is an inner joint, empty transactions will
295+ // not be selected in this view
296+ mDb .delete (TransactionEntry .TABLE_NAME ,
297+ TransactionEntry .COLUMN_UID + " NOT IN ( SELECT DISTINCT "
298+ + TransactionEntry .TABLE_NAME + "_" + TransactionEntry .COLUMN_UID
299+ + " FROM trans_split_acct )" ,
300+ null );
286301 deleteRecord (AccountEntry .TABLE_NAME , rowId );
287302 mDb .setTransactionSuccessful ();
288303 return true ;
0 commit comments