9
9
import android .content .Context ;
10
10
import android .database .Cursor ;
11
11
import android .database .sqlite .SQLiteDatabase ;
12
- import android .database .sqlite .SQLiteOpenHelper ;
13
12
import android .text .TextUtils ;
14
13
import android .util .Log ;
15
14
16
15
import java .util .concurrent .atomic .AtomicBoolean ;
17
16
18
17
import javax .annotation .ParametersAreNonnullByDefault ;
19
18
19
+ import androidx .sqlite .db .SupportSQLiteDatabase ;
20
+ import androidx .sqlite .db .SupportSQLiteOpenHelper ;
21
+ import androidx .sqlite .db .framework .FrameworkSQLiteOpenHelperFactory ;
22
+
20
23
@ ParametersAreNonnullByDefault
21
- public class PureeSQLiteStorage extends SQLiteOpenHelper implements PureeStorage {
24
+ public class PureeSQLiteStorage extends SupportSQLiteOpenHelper . Callback implements PureeStorage {
22
25
23
26
private static final String DATABASE_NAME = "puree.db" ;
24
27
@@ -32,7 +35,7 @@ public class PureeSQLiteStorage extends SQLiteOpenHelper implements PureeStorage
32
35
33
36
private final JsonParser jsonParser = new JsonParser ();
34
37
35
- private final SQLiteDatabase db ;
38
+ private final SupportSQLiteOpenHelper openHelper ;
36
39
37
40
private final AtomicBoolean lock = new AtomicBoolean (false );
38
41
@@ -47,23 +50,30 @@ static String databaseName(Context context) {
47
50
}
48
51
49
52
public PureeSQLiteStorage (Context context ) {
50
- super (context , databaseName (context ), null , DATABASE_VERSION );
51
- db = getWritableDatabase ();
53
+ super (DATABASE_VERSION );
54
+ openHelper = new FrameworkSQLiteOpenHelperFactory ()
55
+ .create (
56
+ SupportSQLiteOpenHelper .Configuration
57
+ .builder (context )
58
+ .name (databaseName (context ))
59
+ .callback (this )
60
+ .build ()
61
+ );
52
62
}
53
63
54
64
public void insert (String type , JsonObject jsonLog ) {
55
65
ContentValues contentValues = new ContentValues ();
56
66
contentValues .put (COLUMN_NAME_TYPE , type );
57
67
contentValues .put (COLUMN_NAME_LOG , jsonLog .toString ());
58
- db . insert (TABLE_NAME , null , contentValues );
68
+ openHelper . getWritableDatabase (). insert (TABLE_NAME , SQLiteDatabase . CONFLICT_NONE , contentValues );
59
69
}
60
70
61
71
public Records select (String type , int logsPerRequest ) {
62
72
String query = "SELECT * FROM " + TABLE_NAME +
63
73
" WHERE " + COLUMN_NAME_TYPE + " = ?" +
64
74
" ORDER BY id ASC" +
65
75
" LIMIT " + logsPerRequest ;
66
- Cursor cursor = db . rawQuery (query , new String []{type });
76
+ Cursor cursor = openHelper . getReadableDatabase (). query (query , new String []{type });
67
77
68
78
try {
69
79
return recordsFromCursor (cursor );
@@ -75,7 +85,7 @@ public Records select(String type, int logsPerRequest) {
75
85
@ Override
76
86
public Records selectAll () {
77
87
String query = "SELECT * FROM " + TABLE_NAME + " ORDER BY id ASC" ;
78
- Cursor cursor = db . rawQuery ( query , null );
88
+ Cursor cursor = openHelper . getReadableDatabase (). query ( query );
79
89
80
90
try {
81
91
return recordsFromCursor (cursor );
@@ -103,7 +113,7 @@ private Record buildRecord(Cursor cursor) {
103
113
104
114
private int getRecordCount () {
105
115
String query = "SELECT COUNT(*) FROM " + TABLE_NAME ;
106
- Cursor cursor = db . rawQuery ( query , null );
116
+ Cursor cursor = openHelper . getReadableDatabase (). query ( query );
107
117
int count = 0 ;
108
118
if (cursor .moveToNext ()) {
109
119
count = cursor .getInt (0 );
@@ -120,31 +130,27 @@ private JsonObject parseJsonString(String jsonString) {
120
130
121
131
@ Override
122
132
public void delete (Records records ) {
123
- String query = "DELETE FROM " + TABLE_NAME +
124
- " WHERE id IN (" + records .getIdsAsString () + ")" ;
125
- db .execSQL (query );
133
+ String where = "id IN (" + records .getIdsAsString () + ")" ;
134
+ openHelper .getWritableDatabase ().delete (TABLE_NAME , where , null );
126
135
}
127
136
128
137
@ Override
129
138
public void truncateBufferedLogs (int maxRecords ) {
130
139
int recordSize = getRecordCount ();
131
140
if (recordSize > maxRecords ) {
132
- String query = "DELETE FROM " + TABLE_NAME +
133
- " WHERE id IN ( SELECT id FROM " + TABLE_NAME +
134
- " ORDER BY id ASC LIMIT " + String .valueOf (recordSize - maxRecords ) +
135
- ")" ;
136
- db .execSQL (query );
141
+ String where = "id IN ( SELECT id FROM " + TABLE_NAME +
142
+ " ORDER BY id ASC LIMIT " + (recordSize - maxRecords ) + ")" ;
143
+ openHelper .getWritableDatabase ().delete (TABLE_NAME , where , null );
137
144
}
138
145
}
139
146
140
147
@ Override
141
148
public void clear () {
142
- String query = "DELETE FROM " + TABLE_NAME ;
143
- db .execSQL (query );
149
+ openHelper .getWritableDatabase ().delete (TABLE_NAME , null , null );
144
150
}
145
151
146
152
@ Override
147
- public void onCreate (SQLiteDatabase db ) {
153
+ public void onCreate (SupportSQLiteDatabase db ) {
148
154
String query = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
149
155
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
150
156
COLUMN_NAME_TYPE + " TEXT," +
@@ -154,13 +160,13 @@ public void onCreate(SQLiteDatabase db) {
154
160
}
155
161
156
162
@ Override
157
- public void onUpgrade (SQLiteDatabase db , int oldVersion , int newVersion ) {
163
+ public void onUpgrade (SupportSQLiteDatabase db , int oldVersion , int newVersion ) {
158
164
Log .e ("PureeDbHelper" , "unexpected onUpgrade(db, " + oldVersion + ", " + newVersion + ")" );
159
165
}
160
166
161
167
@ Override
162
168
protected void finalize () throws Throwable {
163
- db .close ();
169
+ openHelper .close ();
164
170
super .finalize ();
165
171
}
166
172
0 commit comments