|
11 | 11 |
|
12 | 12 | /** |
13 | 13 | * Database handler, SQLite wrapper and ORM layer. |
14 | | - * |
| 14 | + * |
15 | 15 | */ |
16 | 16 | public class DatabaseHandler extends SQLiteOpenHelper { |
17 | 17 |
|
18 | | - // All Static variables |
19 | | - // Database Version |
20 | | - private static final int DATABASE_VERSION = 1; |
21 | | - |
22 | | - // Database Name |
23 | | - private static final String DATABASE_NAME = "SampleDB"; |
24 | | - private final Context context; |
25 | | - |
26 | | - private static DatabaseHandler instance = null; |
27 | | - |
28 | | - public synchronized static DatabaseHandler getInstance(Context context) { |
29 | | - if (instance == null) |
30 | | - instance = new DatabaseHandler(context.getApplicationContext()); |
31 | | - return instance; |
32 | | - } |
33 | | - |
34 | | - public DatabaseHandler(Context context) { |
35 | | - super(context.getApplicationContext(), DATABASE_NAME, null, |
36 | | - DATABASE_VERSION); |
37 | | - this.context = context.getApplicationContext(); |
38 | | - } |
39 | | - |
40 | | - @Override |
41 | | - public void onOpen(SQLiteDatabase db) { |
42 | | - super.onOpen(db); |
43 | | - if (!db.isReadOnly()) { |
44 | | - // Enable foreign key constraints |
45 | | - // This line requires android16 |
46 | | - // db.setForeignKeyConstraintsEnabled(true); |
47 | | - // This line works everywhere though |
48 | | - db.execSQL("PRAGMA foreign_keys=ON;"); |
49 | | - |
50 | | - // Create temporary triggers |
51 | | - DatabaseTriggers.createTemp(db); |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public synchronized void onCreate(SQLiteDatabase db) { |
57 | | - |
58 | | - db.execSQL("DROP TABLE IF EXISTS " + LinkItem.TABLE_NAME); |
59 | | - db.execSQL(LinkItem.CREATE_TABLE); |
60 | | - |
61 | | - |
62 | | - // Create Triggers |
63 | | - DatabaseTriggers.create(db); |
64 | | - } |
65 | | - |
66 | | - // Upgrading database |
67 | | - @Override |
68 | | - public synchronized void onUpgrade(SQLiteDatabase db, int oldVersion, |
69 | | - int newVersion) { |
70 | | - // Try to drop and recreate. You should do something clever here |
71 | | - onCreate(db); |
72 | | - } |
73 | | - |
74 | | - // Convenience methods |
75 | | - public synchronized boolean putItem(final DBItem item) { |
76 | | - boolean success = false; |
77 | | - int result = 0; |
78 | | - final SQLiteDatabase db = this.getWritableDatabase(); |
79 | | - final ContentValues values = item.getContent(); |
80 | | - |
81 | | - if (item.getId() > -1) { |
82 | | - result += db.update(item.getTableName(), values, |
83 | | - DBItem.COL_ID + " IS ?", |
84 | | - new String[] { String.valueOf(item.getId()) }); |
85 | | - } |
86 | | - |
87 | | - // Update failed or wasn't possible, insert instead |
88 | | - if (result < 1) { |
89 | | - final long id = db.insert(item.getTableName(), null, values); |
90 | | - |
91 | | - if (id > 0) { |
92 | | - item.setId(id); |
93 | | - success = true; |
94 | | - } |
95 | | - } |
96 | | - else { |
97 | | - success = true; |
98 | | - } |
99 | | - |
100 | | - if (success) { |
101 | | - item.notifyProvider(context); |
102 | | - } |
103 | | - return success; |
104 | | - } |
105 | | - |
106 | | - public synchronized int deleteItem(LinkItem item) { |
107 | | - final SQLiteDatabase db = this.getWritableDatabase(); |
108 | | - final int result = db.delete(item.getTableName(), LinkItem.COL_SHA |
109 | | - + " IS ?", new String[] { item.sha }); |
110 | | - |
111 | | - if (result > 0) { |
112 | | - item.notifyProvider(context); |
113 | | - } |
114 | | - |
115 | | - return result; |
116 | | - } |
117 | | - |
118 | | - |
119 | | - public synchronized Cursor getLinkItemCursor(final long id) { |
120 | | - final SQLiteDatabase db = this.getReadableDatabase(); |
121 | | - final Cursor cursor = db.query(LinkItem.TABLE_NAME, |
122 | | - LinkItem.FIELDS, LinkItem.COL_ID + " IS ?", |
123 | | - new String[] { String.valueOf(id) }, null, null, null, null); |
124 | | - return cursor; |
125 | | - } |
126 | | - |
127 | | - public synchronized LinkItem getLinkItem(final long id) { |
128 | | - final Cursor cursor = getLinkItemCursor(id); |
129 | | - final LinkItem result; |
130 | | - if (cursor.moveToFirst()) { |
131 | | - result = new LinkItem(cursor); |
132 | | - } |
133 | | - else { |
134 | | - result = null; |
135 | | - } |
136 | | - |
137 | | - cursor.close(); |
138 | | - return result; |
139 | | - } |
140 | | - |
141 | | - public synchronized Cursor getAllLinkItemsCursor(final String selection, |
142 | | - final String[] args, |
143 | | - final String sortOrder) { |
144 | | - final SQLiteDatabase db = this.getReadableDatabase(); |
145 | | - |
146 | | - final Cursor cursor = db.query(LinkItem.TABLE_NAME, |
147 | | - LinkItem.FIELDS, selection, args, null, null, sortOrder, null); |
148 | | - |
149 | | - return cursor; |
150 | | - } |
151 | | - |
152 | | - public synchronized List<LinkItem> getAllLinkItems(final String selection, |
153 | | - final String[] args, |
154 | | - final String sortOrder) { |
155 | | - final List<LinkItem> result = new ArrayList<LinkItem>(); |
156 | | - |
157 | | - final Cursor cursor = getAllLinkItemsCursor(selection, args, sortOrder); |
158 | | - |
159 | | - while (cursor.moveToNext()) { |
160 | | - LinkItem q = new LinkItem(cursor); |
161 | | - result.add(q); |
162 | | - } |
163 | | - |
164 | | - cursor.close(); |
165 | | - return result; |
166 | | - } |
| 18 | + // All Static variables |
| 19 | + // Database Version |
| 20 | + private static final int DATABASE_VERSION = 1; |
| 21 | + |
| 22 | + // Database Name |
| 23 | + private static final String DATABASE_NAME = "SampleDB"; |
| 24 | + private final Context context; |
| 25 | + |
| 26 | + private static DatabaseHandler instance = null; |
| 27 | + |
| 28 | + public synchronized static DatabaseHandler getInstance(Context context) { |
| 29 | + if (instance == null) |
| 30 | + instance = new DatabaseHandler(context.getApplicationContext()); |
| 31 | + return instance; |
| 32 | + } |
| 33 | + |
| 34 | + public DatabaseHandler(Context context) { |
| 35 | + super(context.getApplicationContext(), DATABASE_NAME, null, |
| 36 | + DATABASE_VERSION); |
| 37 | + this.context = context.getApplicationContext(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onOpen(SQLiteDatabase db) { |
| 42 | + super.onOpen(db); |
| 43 | + if (!db.isReadOnly()) { |
| 44 | + // Enable foreign key constraints |
| 45 | + // This line requires android16 |
| 46 | + // db.setForeignKeyConstraintsEnabled(true); |
| 47 | + // This line works everywhere though |
| 48 | + db.execSQL("PRAGMA foreign_keys=ON;"); |
| 49 | + |
| 50 | + // Create temporary triggers |
| 51 | + DatabaseTriggers.createTemp(db); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public synchronized void onCreate(SQLiteDatabase db) { |
| 57 | + |
| 58 | + db.execSQL("DROP TABLE IF EXISTS " + LinkItem.TABLE_NAME); |
| 59 | + db.execSQL(LinkItem.CREATE_TABLE); |
| 60 | + |
| 61 | + // Create Triggers |
| 62 | + DatabaseTriggers.create(db); |
| 63 | + } |
| 64 | + |
| 65 | + // Upgrading database |
| 66 | + @Override |
| 67 | + public synchronized void onUpgrade(SQLiteDatabase db, int oldVersion, |
| 68 | + int newVersion) { |
| 69 | + // Try to drop and recreate. You should do something clever here |
| 70 | + onCreate(db); |
| 71 | + } |
| 72 | + |
| 73 | + // Convenience methods |
| 74 | + public synchronized boolean putItem(final LinkItem item) { |
| 75 | + boolean success = false; |
| 76 | + int result = 0; |
| 77 | + final SQLiteDatabase db = this.getWritableDatabase(); |
| 78 | + final ContentValues values = item.getContent(); |
| 79 | + |
| 80 | + if (item.getId() > -1) { |
| 81 | + result += db.update(item.getTableName(), values, DBItem.COL_ID |
| 82 | + + " IS ?", new String[] { String.valueOf(item.getId()) }); |
| 83 | + } |
| 84 | + // Update failed or wasn't possible, insert instead |
| 85 | + else { |
| 86 | + final long id = db.insert(item.getTableName(), null, values); |
| 87 | + if (id > 0) { |
| 88 | + item.setId(id); |
| 89 | + result++; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (result > 0) { |
| 94 | + success = true; |
| 95 | + } |
| 96 | + if (success) { |
| 97 | + item.notifyProvider(context); |
| 98 | + } |
| 99 | + return success; |
| 100 | + } |
| 101 | + |
| 102 | + public synchronized int deleteItem(LinkItem item) { |
| 103 | + final SQLiteDatabase db = this.getWritableDatabase(); |
| 104 | + final int result = db.delete(item.getTableName(), LinkItem.COL_ID |
| 105 | + + " IS ? OR " + LinkItem.COL_SHA + " IS ?", |
| 106 | + new String[] { Long.toString(item._id), item.sha }); |
| 107 | + |
| 108 | + if (result > 0) { |
| 109 | + item.notifyProvider(context); |
| 110 | + } |
| 111 | + |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + public synchronized Cursor getLinkItemCursor(final long id) { |
| 116 | + final SQLiteDatabase db = this.getReadableDatabase(); |
| 117 | + final Cursor cursor = db.query(LinkItem.TABLE_NAME, LinkItem.FIELDS, |
| 118 | + LinkItem.COL_ID + " IS ?", new String[] { String.valueOf(id) }, |
| 119 | + null, null, null, null); |
| 120 | + return cursor; |
| 121 | + } |
| 122 | + |
| 123 | + public synchronized LinkItem getLinkItem(final long id) { |
| 124 | + final Cursor cursor = getLinkItemCursor(id); |
| 125 | + final LinkItem result; |
| 126 | + if (cursor.moveToFirst()) { |
| 127 | + result = new LinkItem(cursor); |
| 128 | + } |
| 129 | + else { |
| 130 | + result = null; |
| 131 | + } |
| 132 | + |
| 133 | + cursor.close(); |
| 134 | + return result; |
| 135 | + } |
| 136 | + |
| 137 | + public synchronized Cursor getAllLinkItemsCursor(final String selection, |
| 138 | + final String[] args, final String sortOrder) { |
| 139 | + final SQLiteDatabase db = this.getReadableDatabase(); |
| 140 | + |
| 141 | + final Cursor cursor = db.query(LinkItem.TABLE_NAME, LinkItem.FIELDS, |
| 142 | + selection, args, null, null, sortOrder, null); |
| 143 | + |
| 144 | + return cursor; |
| 145 | + } |
| 146 | + |
| 147 | + public synchronized List<LinkItem> getAllLinkItems(final String selection, |
| 148 | + final String[] args, final String sortOrder) { |
| 149 | + final List<LinkItem> result = new ArrayList<LinkItem>(); |
| 150 | + |
| 151 | + final Cursor cursor = getAllLinkItemsCursor(selection, args, sortOrder); |
| 152 | + |
| 153 | + while (cursor.moveToNext()) { |
| 154 | + LinkItem q = new LinkItem(cursor); |
| 155 | + result.add(q); |
| 156 | + } |
| 157 | + |
| 158 | + cursor.close(); |
| 159 | + return result; |
| 160 | + } |
167 | 161 |
|
168 | 162 | } |
0 commit comments