Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.run.store;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- public class RunSource {
- private SQLiteDatabase database;
- private final RunHelper helper;
- static private RunSource instance;
- private RunSource(final Context context) {
- helper = new RunHelper(context);
- }
- public static RunSource getInstance(final Context context) {
- if (instance == null) {
- instance = new RunSource(context);
- }
- return instance;
- }
- public void open() throws SQLException {
- database = helper.getWritableDatabase();
- }
- public void close() {
- helper.close();
- }
- public Storable getStorableByPrimaryKey(final Storable storable) {
- final String restrict = RunHelper.PRIMARY_KEY + "="
- + storable.getPrimaryKeyValue();
- final Cursor cursor = database.query(true, storable.getTableName(),
- storable.getColNames(), restrict, null, null, null, null, null);
- Storable run = null;
- if ((cursor != null) && (cursor.getCount() > 0)) {
- cursor.moveToFirst();
- run = storable.get(cursor);
- }
- cursor.close();
- return run;
- }
- public List<Storable> getStorableList(final Storable storable) {
- final List<Storable> list = new ArrayList<Storable>();
- final Cursor cursor = database.query(storable.getTableName(),
- storable.getColNames(), null, null, null, null, null);
- cursor.moveToFirst();
- while (!cursor.isAfterLast()) {
- final Storable run = storable.get(cursor);
- list.add(run);
- cursor.moveToNext();
- }
- cursor.close();
- return list;
- }
- public boolean updateStorable(final Storable item) {
- final ContentValues args = item.getContentValues();
- final String restrict = RunHelper.PRIMARY_KEY + "="
- + item.getPrimaryKeyValue();
- return database.update(item.getTableName(), args, restrict, null) > 0;
- }
- public void deleteStorable(final Storable storable) {
- final long primaryKeyValue = storable.getPrimaryKeyValue();
- Log.d("RunSource", "Storable deleted with id: " + primaryKeyValue);
- database.delete(storable.getTableName(), RunHelper.PRIMARY_KEY + " = "
- + primaryKeyValue, null);
- }
- public Storable addStorable(final Storable storing) {
- final ContentValues values = storing.getContentValues();
- final long insertId = database.insert(storing.getTableName(), null,
- values);
- final Cursor cursor = database.query(storing.getTableName(),
- storing.getColNames(),
- RunHelper.PRIMARY_KEY + " = " + insertId, null, null, null,
- null);
- cursor.moveToFirst();
- final Storable run = storing.get(cursor);
- cursor.close();
- return run;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement