Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / model / sqlite / DbAdapter.java
1 package com.folioreader.model.sqlite;
2
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;
8
9 public class DbAdapter {
10     private static final String TAG = "DBAdapter";
11
12     private Context mContext;
13     public static SQLiteDatabase mDatabase;
14
15     public DbAdapter(Context ctx) {
16         this.mContext = ctx;
17         mDatabase = FolioDatabaseHelper.getInstance(mContext).getMyWritableDatabase();
18     }
19
20     public static boolean insert(String table, ContentValues contentValues) {
21
22         return mDatabase.insert(table, null, contentValues) > 0;
23     }
24
25     public static boolean update(String table, String key, String value, ContentValues contentValues) {
26
27         return mDatabase.update(table, contentValues, key + "=?", new String[]{value}) > 0;
28     }
29
30     public static Cursor getHighLightsForBookId(String bookId) {
31         return mDatabase.rawQuery("SELECT * FROM " + HighLightTable.TABLE_NAME + " WHERE " + HighLightTable.COL_BOOK_ID + " = '" + bookId + "'", null);
32     }
33
34     public boolean deleteAll(String table) {
35         return mDatabase.delete(table, null, null) > 0;
36     }
37
38     public boolean deleteAll(String table, String whereClause, String[] whereArgs) {
39         return mDatabase.delete(table, whereClause + "=?", whereArgs) > 0;
40     }
41
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);
45     }
46
47     public Cursor getAll(String table) {
48         return getAll(table, null, null, null, null);
49     }
50
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);
54     }
55
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);
59     }
60
61     public Cursor get(String table, long id) throws SQLException {
62         return get(table, id, null, FolioDatabaseHelper.KEY_ID);
63     }
64
65     public static boolean deleteById(String table, String key, String value) {
66         return mDatabase.delete(table, key + "=?", new String[]{value}) > 0;
67     }
68
69     public Cursor getMaxId(String tableName, String key) {
70         return mDatabase.rawQuery("SELECT MAX(" + key + ") FROM " + tableName, null);
71     }
72
73     public static long saveHighLight(ContentValues highlightContentValues) {
74         return mDatabase.insert(HighLightTable.TABLE_NAME, null, highlightContentValues);
75     }
76
77     public static boolean updateHighLight(ContentValues highlightContentValues, String id) {
78         return mDatabase.update(HighLightTable.TABLE_NAME, highlightContentValues, HighLightTable.ID + " = " + id, null) > 0;
79     }
80
81     public static Cursor getHighlightsForPageId(String query, String pageId) {
82         return mDatabase.rawQuery(query, null);
83     }
84
85     public static int getIdForQuery(String query) {
86         Cursor c = mDatabase.rawQuery(query, null);
87
88         int id = -1;
89         while (c.moveToNext()) {
90             id = c.getInt(c.getColumnIndex(HighLightTable.ID));
91         }
92         c.close();
93         return id;
94     }
95
96     public static Cursor getHighlightsForId(int id) {
97         return mDatabase.rawQuery("SELECT * FROM " + HighLightTable.TABLE_NAME + " WHERE " + HighLightTable.ID + " = '" + id + "'", null);
98     }
99 }