
package be.ugent.zeus.resto.client.data.caches;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 *
 * @author Thomas Meire
 */
public class VersionCache {

  private static final String DATABASE_NAME = "hydradb";
  private static final int DATABASE_VERSION = 1;
  
  private DatabaseHelper dbhelper;
  
  public VersionCache (Context context) {
    dbhelper = new DatabaseHelper(context);
  }
  
  public void close () {
    dbhelper.close();
  }
  
  public void put (String key, int version) {
    SQLiteDatabase db = dbhelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("key", key);
    values.put("version", version);
    db.insertWithOnConflict("version_cache", null, values, SQLiteDatabase.CONFLICT_REPLACE);    
  }

  public int version (String key) {
    try {
      SQLiteDatabase db = dbhelper.getReadableDatabase();

      Cursor cursor = db.query("version_cache", new String[]{"version"}, "key = ?", new String[]{key}, "", "", "");

      if (cursor.getCount() == 0) {
        return -1;
      } else {
        cursor.moveToFirst();

        return cursor.getInt(cursor.getColumnIndex("version"));
      }
    } catch (Exception e) {
      Log.e("[VersionCache]", "Database exception " + e.getMessage());
      return -1;
    }
  }

  private class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper (Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL("CREATE TABLE version_cache (key TEXT PRIMARY KEY, version INT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    }
  }
}

@@ -1196,24 +1196,48 @@
 t() == 0) %7B%0A
+        cursor.close();%0A
         retu
@@ -1239,24 +1239,24 @@
  return -1;%0A
-
       %7D else
@@ -1297,22 +1297,29 @@
         
-return
+int version =
  cursor.
@@ -1356,24 +1356,72 @@
 version%22));%0A
+        cursor.close();%0A        return version;%0A
       %7D%0A    

Make sure we close the db cursor