1 package com.folioreader.model.sqlite;
3 import android.content.ContentValues;
4 import android.content.Context;
5 import android.database.Cursor;
6 import android.database.SQLException;
7 import android.database.sqlite.SQLiteDatabase;
9 public class DbAdapter {
10 private static final String TAG = "DBAdapter";
12 private Context mContext;
13 public static SQLiteDatabase mDatabase;
15 public DbAdapter(Context ctx) {
17 mDatabase = FolioDatabaseHelper.getInstance(mContext).getMyWritableDatabase();
20 public static boolean insert(String table, ContentValues contentValues) {
22 return mDatabase.insert(table, null, contentValues) > 0;
25 public static boolean update(String table, String key, String value, ContentValues contentValues) {
27 return mDatabase.update(table, contentValues, key + "=?", new String[]{value}) > 0;
30 public static Cursor getHighLightsForBookId(String bookId) {
31 return mDatabase.rawQuery("SELECT * FROM " + HighLightTable.TABLE_NAME + " WHERE " + HighLightTable.COL_BOOK_ID + " = '" + bookId + "'", null);
34 public boolean deleteAll(String table) {
35 return mDatabase.delete(table, null, null) > 0;
38 public boolean deleteAll(String table, String whereClause, String[] whereArgs) {
39 return mDatabase.delete(table, whereClause + "=?", whereArgs) > 0;
42 public Cursor getAll(String table, String[] projection, String selection,
43 String[] selectionArgs, String orderBy) {
44 return mDatabase.query(table, projection, selection, selectionArgs, null, null, orderBy);
47 public Cursor getAll(String table) {
48 return getAll(table, null, null, null, null);
51 public Cursor get(String table, long id, String[] projection, String key) throws SQLException {
52 return mDatabase.query(table, projection,
53 key + "=?", new String[]{String.valueOf(id)}, null, null, null, null);
56 public static Cursor getAllByKey(String table, String[] projection, String key, String value) throws SQLException {
57 return mDatabase.query(table, projection,
58 key + "=?", new String[]{value}, null, null, null, null);
61 public Cursor get(String table, long id) throws SQLException {
62 return get(table, id, null, FolioDatabaseHelper.KEY_ID);
65 public static boolean deleteById(String table, String key, String value) {
66 return mDatabase.delete(table, key + "=?", new String[]{value}) > 0;
69 public Cursor getMaxId(String tableName, String key) {
70 return mDatabase.rawQuery("SELECT MAX(" + key + ") FROM " + tableName, null);
73 public static long saveHighLight(ContentValues highlightContentValues) {
74 return mDatabase.insert(HighLightTable.TABLE_NAME, null, highlightContentValues);
77 public static boolean updateHighLight(ContentValues highlightContentValues, String id) {
78 return mDatabase.update(HighLightTable.TABLE_NAME, highlightContentValues, HighLightTable.ID + " = " + id, null) > 0;
81 public static Cursor getHighlightsForPageId(String query, String pageId) {
82 return mDatabase.rawQuery(query, null);
85 public static int getIdForQuery(String query) {
86 Cursor c = mDatabase.rawQuery(query, null);
89 while (c.moveToNext()) {
90 id = c.getInt(c.getColumnIndex(HighLightTable.ID));
96 public static Cursor getHighlightsForId(int id) {
97 return mDatabase.rawQuery("SELECT * FROM " + HighLightTable.TABLE_NAME + " WHERE " + HighLightTable.ID + " = '" + id + "'", null);