@@ -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