-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLITE.java
More file actions
276 lines (247 loc) · 8.83 KB
/
Copy pathSQLITE.java
File metadata and controls
276 lines (247 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// ====================================================
// EasySQLITE Copyright(C) 2017 EasySQLITE
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package com.dentrax.easysqlite.source;
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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by Furkan on 11/09/2013.
*/
public final class SQLITE extends SQLITEBase {
public static final Boolean DEBUG_MODE = false;
public static final Boolean CAN_RESETTABLE = false;
public SQLITE(Context context, SQLITESetting setting) {
super(context, setting);
}
public synchronized void onCreate(SQLiteDatabase db) {
try {
List<String> createQueries = super.getCreateQuaries();
if(!createQueries.isEmpty()){
this.execQueryList(db, createQueries);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public synchronized void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
List<String> upgradeQueries = super.getUpgradeQueries(oldVersion, newVersion);
if(!upgradeQueries.isEmpty()){
this.execQueryList(db, upgradeQueries);
}
}
protected void execQueryList(SQLiteDatabase db, List<String> queries){
for (int i = 0; i < queries.size(); i++) {
String query = queries.get(i).trim();
if(!query.isEmpty()){
db.execSQL(query);
}
}
}
public synchronized List<String> listTables(){
List<String> tables = new ArrayList<String>();
SQLiteDatabase db = this.getDb();
Cursor c = null;
try {
if(this.isOpen()){
c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
if (c.moveToFirst()){
while ( !c.isAfterLast() ){
tables.add( c.getString( c.getColumnIndex("name")) );
c.moveToNext();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null){
c.close();
}
if(db != null){
db.close();
}
}
return tables;
}
public synchronized Cursor queryCursor(String query){
Cursor c = null;
SQLiteDatabase db = this.m_sqLiteOpenHelper.getReadableDatabase();
try {
if(db.isOpen()){
c = db.rawQuery(query, null);
c.moveToFirst();
}
} catch (Exception e) {
Log.v(query, e.getMessage(), e);
e.printStackTrace();
} finally {
if (c != null){
c.close();
}
if(db != null){
db.close();
}
}
return c;
}
public synchronized Cursor query(String tableName, String[] columns){
return this.query(tableName, columns, null, null, null, null, null, null);
}
public synchronized Cursor query(String tableName, String[] columns, String selection){
return this.query(tableName, columns, selection, null, null, null, null, null);
}
public synchronized Cursor query(String tableName, String[] columns, String selection, String[] selectArgs){
return this.query(tableName, columns, selection, selectArgs, null, null, null, null);
}
public synchronized Cursor query(String tableName, String[] columns, String selection, String[] selectArgs, String groupBy){
return this.query(tableName, columns, selection, selectArgs, groupBy, null, null, null);
}
public synchronized Cursor query(String tableName, String[] columns, String selection, String[] selectArgs, String groupBy, String having){
return this.query(tableName, columns, selection, selectArgs, groupBy, having, null, null);
}
public synchronized Cursor query(String tableName, String[] columns, String selection, String[] selectArgs, String groupBy, String having, String orderBy){
return this.query(tableName, columns, selection, selectArgs, groupBy, having, orderBy, null);
}
public synchronized Cursor query(String tableName, String[] columns, String selection, String[] selectArgs, String groupBy, String having, String orderBy, String limit ){
SQLiteDatabase db = this.getDb();
Cursor c = null;
try {
if(this.isOpen()){
c = db.query(tableName, columns, selection, selectArgs, groupBy, having, orderBy, limit);
return c;
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
}
return c;
}
public synchronized void queryDelete(String tableName, String where){
this.queryDelete(tableName, where, null);
}
public synchronized void queryDelete(String tableName, String where, String[] whereArgs){
SQLiteDatabase db = this.getDb();
try {
if(this.isOpen()){
db.delete(tableName, where, whereArgs);
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if(db != null){
db.close();
}
}
}
public synchronized void queryInsert(String tableName, ContentValues values){
SQLiteDatabase db = this.getDb();
try {
if(this.isOpen()){
db.insert(tableName, null, values);
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if(db != null){
db.close();
}
}
}
public synchronized void queryUpdate(String tableName, ContentValues values, String where){
this.queryUpdate(tableName, values, null);
}
public synchronized void queryUpdate(String tableName, ContentValues values, String where, String[] whereArgs){
SQLiteDatabase db = this.getDb();
try {
if(this.isOpen()){
db.update(tableName, values, where, whereArgs);
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if(db != null){
db.close();
}
}
}
public synchronized int getRowCount(String tableName) {
String countQuery = "SELECT * FROM " + tableName;
SQLiteDatabase db = this.getDb();
Cursor c = null;
try {
if(db.isOpen()){
c = db.rawQuery(countQuery, null);
int rowCount = c.getCount();
return rowCount;
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if (c != null){
c.close();
}
if(db != null){
db.close();
}
}
return -1;
}
public synchronized List<String> getColumns(String tableName) {
List<String> ar = null;
Cursor c = null;
SQLiteDatabase db = this.getDb();
try {
if(db.isOpen()){
c = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 1", null);
if (c != null) {
ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
}
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if (c != null){
c.close();
}
if(db != null){
db.close();
}
}
return ar;
}
public synchronized void resetTable(String tableName){
SQLiteDatabase db = this.getDb();
try {
if(db.isOpen()){
if(!SQLITE.CAN_RESETTABLE){
Log.e("[SQLITE::resetTables()]", "You don't have permission to reset the database!");
db.close();
return;
}
db.delete(tableName, null, null);
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if(db != null){
db.close();
}
}
}
}