package org.edx.mobile.module.db.impl;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

class DbOperationSingleValueByRawQuery<T> extends DbOperationBase<T> {
    
    private String sqlQuery;
    private String[] selectionArgs;
    private Class<T> columnType;
    
    DbOperationSingleValueByRawQuery(String sqlQuery, String[] selectionArgs, Class<T> valueType) {
        this.sqlQuery = sqlQuery;
        this.selectionArgs = selectionArgs;
        this.columnType = valueType;
    }
    
    @Override
    public T execute(SQLiteDatabase db) {
        Cursor c = db.rawQuery(sqlQuery, selectionArgs);
        
        if (c.moveToFirst()) {
            if (columnType == Long.class) { 
                Long column = c.getLong(0);
                return (T) column;
            } else if (columnType == String.class) {
                String column = c.getString(0);
                return (T) column;
            } else if (columnType == Integer.class) {
                Integer column = c.getInt(0);
                return (T) column;
            } else {
                logger.warn("Class types does NOT match for: " + columnType);
            }
        }
        
        c.close();
        
        return null;
    }
    
}

@@ -657,32 +657,57 @@
 Args);%0A        %0A
+        T result = null;%0A
         if (c.mo
@@ -717,24 +717,24 @@
 oFirst()) %7B%0A
-
             
@@ -820,36 +820,38 @@
               re
-turn
+sult =
  (T) column;%0A   
@@ -958,36 +958,38 @@
               re
-turn
+sult =
  (T) column;%0A   
@@ -1103,20 +1103,22 @@
       re
-turn
+sult =
  (T) col
@@ -1297,20 +1297,22 @@
  return 
-null
+result
 ;%0A    %7D%0A

Fix cursor not closed in database single value query