Skip to content

Commit 19a346b

Browse files
committed
close cursors
1 parent b469900 commit 19a346b

2 files changed

Lines changed: 79 additions & 41 deletions

File tree

app/src/org/gnucash/android/db/AccountsDbAdapter.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,12 @@ public long getId(String accountUID){
699699
new String[]{DatabaseHelper.KEY_ROW_ID, DatabaseHelper.KEY_UID},
700700
DatabaseHelper.KEY_UID + "='" + accountUID + "'",
701701
null, null, null, null);
702-
if (c != null && c.moveToFirst()){
703-
id = c.getLong(DatabaseAdapter.COLUMN_ROW_ID);
704-
c.close();
705-
}
702+
if (c != null) {
703+
if (c.moveToFirst()) {
704+
id = c.getLong(DatabaseAdapter.COLUMN_ROW_ID);
705+
}
706+
c.close();
707+
}
706708
return id;
707709
}
708710

@@ -740,7 +742,10 @@ public String getAccountName(String accountUID){
740742
DatabaseHelper.KEY_UID + " = ?",
741743
new String[]{accountUID}, null, null, null);
742744

743-
if (cursor == null || cursor.getCount() < 1){
745+
if (cursor == null) {
746+
return null;
747+
} else if ( cursor.getCount() < 1) {
748+
cursor.close();
744749
return null;
745750
} else { //account UIDs should be unique
746751
cursor.moveToFirst();
@@ -763,7 +768,10 @@ public long getDefaultTransferAccountID(long accountID){
763768
DatabaseHelper.KEY_ROW_ID + " = " + accountID,
764769
null, null, null, null);
765770

766-
if (cursor == null || cursor.getCount() < 1){
771+
if (cursor == null) {
772+
return 0;
773+
} else if (cursor.getCount() < 1) {
774+
cursor.close();
767775
return 0;
768776
} else {
769777
cursor.moveToFirst();
@@ -818,7 +826,10 @@ public boolean isPlaceholderAccount(String accountUID){
818826
DatabaseHelper.KEY_UID + " = ?",
819827
new String[]{accountUID}, null, null, null);
820828

821-
if (cursor == null || !cursor.moveToFirst()){
829+
if (cursor == null)
830+
return false;
831+
if (!cursor.moveToFirst()) {
832+
cursor.close();
822833
return false;
823834
}
824835
boolean isPlaceholder = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.KEY_PLACEHOLDER)) == 1;
@@ -847,7 +858,10 @@ public boolean isFavoriteAccount(long accountId){
847858
DatabaseHelper.KEY_ROW_ID + " = " + accountId, null,
848859
null, null, null);
849860

850-
if (cursor == null || !cursor.moveToFirst()){
861+
if (cursor == null)
862+
return false;
863+
if (!cursor.moveToFirst()){
864+
cursor.close();
851865
return false;
852866
}
853867
boolean isFavorite = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.KEY_FAVORITE)) == 1;

app/src/org/gnucash/android/db/TransactionsDbAdapter.java

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ public long fetchTransactionWithUID(String uid){
9292
DatabaseHelper.KEY_UID + " = '" + uid + "'",
9393
null, null, null, null);
9494
long result = -1;
95-
if (cursor != null && cursor.moveToFirst()){
96-
Log.d(TAG, "Transaction already exists. Returning existing id");
97-
result = cursor.getLong(0); //0 because only one row was requested
98-
99-
cursor.close();
100-
}
95+
if (cursor != null) {
96+
if (cursor.moveToFirst()) {
97+
Log.d(TAG, "Transaction already exists. Returning existing id");
98+
result = cursor.getLong(0); //0 because only one row was requested
99+
}
100+
cursor.close();
101+
}
101102
return result;
102103
}
103104

@@ -113,10 +114,12 @@ public Transaction getTransaction(long rowId){
113114
Log.v(TAG, "Fetching transaction with id " + rowId);
114115
Transaction transaction = null;
115116
Cursor c = fetchRecord(DatabaseHelper.TRANSACTIONS_TABLE_NAME, rowId);
116-
if (c != null && c.moveToFirst()){
117-
transaction = buildTransactionInstance(c);
118-
c.close();
119-
}
117+
if (c != null) {
118+
if (c.moveToFirst()) {
119+
transaction = buildTransactionInstance(c);
120+
}
121+
c.close();
122+
}
120123
return transaction;
121124
}
122125

@@ -173,9 +176,12 @@ public Cursor fetchAllTransactionsForAccount(long accountID){
173176
public List<Transaction> getAllTransactionsForAccount(String accountUID){
174177
Cursor c = fetchAllTransactionsForAccount(accountUID);
175178
ArrayList<Transaction> transactionsList = new ArrayList<Transaction>();
176-
177-
if (c == null || (c.getCount() <= 0))
179+
if (c == null)
178180
return transactionsList;
181+
if (c.getCount() <= 0) {
182+
c.close();
183+
return transactionsList;
184+
}
179185

180186
while (c.moveToNext()) {
181187
Transaction transaction = buildTransactionInstance(c);
@@ -235,8 +241,12 @@ public String getCurrencyCode(String accountUID) {
235241
DatabaseHelper.KEY_UID + "= '" + accountUID + "'",
236242
null, null, null, null);
237243

238-
if (cursor == null || cursor.getCount() <= 0)
244+
if (cursor == null)
239245
return null;
246+
if (cursor.getCount() <= 0) {
247+
cursor.close();
248+
return null;
249+
}
240250

241251
cursor.moveToFirst();
242252
String currencyCode = cursor.getString(0);
@@ -338,7 +348,7 @@ public long getAllTransactionsCount(){
338348
* @param accountId Record ID of the account
339349
* @return Sum of transactions belonging to the account
340350
*/
341-
public Money getTransactionsSum(long accountId){
351+
public Money getTransactionsSum(long accountId) {
342352
//FIXME: Properly compute the balance while considering normal account balance
343353
String accountUID = getAccountUID(accountId);
344354

@@ -350,8 +360,10 @@ public Money getTransactionsSum(long accountId){
350360
Cursor sumCursor = mDb.rawQuery(querySum, new String[]{accountUID});
351361
double sum = 0d;
352362

353-
if (sumCursor != null && sumCursor.moveToFirst()){
354-
sum += sumCursor.getFloat(0);
363+
if (sumCursor != null) {
364+
if (sumCursor.moveToFirst()) {
365+
sum += sumCursor.getFloat(0);
366+
}
355367
sumCursor.close();
356368
}
357369

@@ -362,8 +374,10 @@ public Money getTransactionsSum(long accountId){
362374

363375
sumCursor = mDb.rawQuery(querySum, new String[]{accountUID});
364376

365-
if (sumCursor != null && sumCursor.moveToFirst()){
366-
sum -= sumCursor.getFloat(0);
377+
if (sumCursor != null) {
378+
if (sumCursor.moveToFirst()) {
379+
sum -= sumCursor.getFloat(0);
380+
}
367381
sumCursor.close();
368382
}
369383

@@ -393,8 +407,10 @@ public Account.AccountType getAccountType(String accountUID){
393407
new String[]{DatabaseHelper.KEY_TYPE},
394408
DatabaseHelper.KEY_UID + "='" + accountUID + "'",
395409
null, null, null, null);
396-
if (c != null && c.moveToFirst()){
397-
type = c.getString(c.getColumnIndexOrThrow(DatabaseHelper.KEY_TYPE));
410+
if (c != null) {
411+
if (c.moveToFirst()) {
412+
type = c.getString(c.getColumnIndexOrThrow(DatabaseHelper.KEY_TYPE));
413+
}
398414
c.close();
399415
}
400416
return Account.AccountType.valueOf(type);
@@ -432,6 +448,7 @@ public List<Transaction> getNonExportedTransactionsForAccount(String accountUID)
432448
while (c.moveToNext()){
433449
transactionsList.add(buildTransactionInstance(c));
434450
}
451+
c.close();
435452
return transactionsList;
436453
}
437454

@@ -446,10 +463,12 @@ public String getAccountUID(long accountRowID){
446463
new String[]{DatabaseHelper.KEY_UID},
447464
DatabaseHelper.KEY_ROW_ID + "=" + accountRowID,
448465
null, null, null, null);
449-
if (c != null && c.moveToFirst()){
450-
uid = c.getString(0);
451-
c.close();
452-
}
466+
if (c != null) {
467+
if (c.moveToFirst()) {
468+
uid = c.getString(0);
469+
}
470+
c.close();
471+
}
453472
return uid;
454473
}
455474

@@ -464,11 +483,12 @@ public String getAccountUidFromTransaction(long transactionID){
464483
DatabaseHelper.KEY_ROW_ID + "=" + transactionID,
465484
null, null, null, null);
466485
String accountUID = null;
467-
if (c != null && c.moveToFirst()){
468-
accountUID = c.getString(0);
486+
if (c != null) {
487+
if (c.moveToFirst()) {
488+
accountUID = c.getString(0);
489+
}
469490
c.close();
470491
}
471-
472492
return accountUID;
473493
}
474494

@@ -483,10 +503,12 @@ public long getAccountID(String accountUID){
483503
new String[]{DatabaseHelper.KEY_ROW_ID},
484504
DatabaseHelper.KEY_UID + "='" + accountUID + "'",
485505
null, null, null, null);
486-
if (c != null && c.moveToFirst()){
487-
id = c.getLong(0);
488-
c.close();
489-
}
506+
if (c != null) {
507+
if (c.moveToFirst()) {
508+
id = c.getLong(0);
509+
}
510+
c.close();
511+
}
490512
return id;
491513
}
492514

@@ -501,8 +523,10 @@ public long getID(String transactionUID){
501523
new String[]{DatabaseHelper.KEY_ROW_ID},
502524
DatabaseHelper.KEY_UID + "='" + transactionUID + "'",
503525
null, null, null, null);
504-
if (c != null && c.moveToFirst()){
505-
id = c.getLong(0);
526+
if (c != null) {
527+
if (c.moveToFirst()) {
528+
id = c.getLong(0);
529+
}
506530
c.close();
507531
}
508532
return id;

0 commit comments

Comments
 (0)