Advertisement
Guest User

RunSource

a guest
Nov 1st, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package com.run.store;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.SQLException;
  10. import android.database.sqlite.SQLiteDatabase;
  11. import android.util.Log;
  12.  
  13. public class RunSource {
  14.     private SQLiteDatabase database;
  15.     private final RunHelper helper;
  16.  
  17.     static private RunSource instance;
  18.  
  19.     private RunSource(final Context context) {
  20.         helper = new RunHelper(context);
  21.     }
  22.  
  23.     public static RunSource getInstance(final Context context) {
  24.         if (instance == null) {
  25.             instance = new RunSource(context);
  26.         }
  27.         return instance;
  28.     }
  29.  
  30.     public void open() throws SQLException {
  31.         database = helper.getWritableDatabase();
  32.     }
  33.  
  34.     public void close() {
  35.         helper.close();
  36.     }
  37.  
  38.     public Storable getStorableByPrimaryKey(final Storable storable) {
  39.         final String restrict = RunHelper.PRIMARY_KEY + "="
  40.                 + storable.getPrimaryKeyValue();
  41.         final Cursor cursor = database.query(true, storable.getTableName(),
  42.                 storable.getColNames(), restrict, null, null, null, null, null);
  43.         Storable run = null;
  44.         if ((cursor != null) && (cursor.getCount() > 0)) {
  45.             cursor.moveToFirst();
  46.             run = storable.get(cursor);
  47.         }
  48.         cursor.close();
  49.         return run;
  50.     }
  51.  
  52.     public List<Storable> getStorableList(final Storable storable) {
  53.         final List<Storable> list = new ArrayList<Storable>();
  54.         final Cursor cursor = database.query(storable.getTableName(),
  55.                 storable.getColNames(), null, null, null, null, null);
  56.         cursor.moveToFirst();
  57.         while (!cursor.isAfterLast()) {
  58.             final Storable run = storable.get(cursor);
  59.             list.add(run);
  60.             cursor.moveToNext();
  61.         }
  62.         cursor.close();
  63.         return list;
  64.     }
  65.  
  66.     public boolean updateStorable(final Storable item) {
  67.         final ContentValues args = item.getContentValues();
  68.         final String restrict = RunHelper.PRIMARY_KEY + "="
  69.                 + item.getPrimaryKeyValue();
  70.         return database.update(item.getTableName(), args, restrict, null) > 0;
  71.     }
  72.  
  73.     public void deleteStorable(final Storable storable) {
  74.         final long primaryKeyValue = storable.getPrimaryKeyValue();
  75.         Log.d("RunSource", "Storable deleted with id: " + primaryKeyValue);
  76.         database.delete(storable.getTableName(), RunHelper.PRIMARY_KEY + " = "
  77.                 + primaryKeyValue, null);
  78.     }
  79.  
  80.     public Storable addStorable(final Storable storing) {
  81.         final ContentValues values = storing.getContentValues();
  82.  
  83.         final long insertId = database.insert(storing.getTableName(), null,
  84.                 values);
  85.         final Cursor cursor = database.query(storing.getTableName(),
  86.                 storing.getColNames(),
  87.                 RunHelper.PRIMARY_KEY + " = " + insertId, null, null, null,
  88.                 null);
  89.         cursor.moveToFirst();
  90.         final Storable run = storing.get(cursor);
  91.         cursor.close();
  92.         return run;
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement