DbManager.java
8.7 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
277
278
279
280
281
282
283
284
285
286
287
package com.xdy.dbmanager;
import android.content.Context;
import android.database.sqlite.SQLiteException;
import com.xdy.dbmanager.entity.CheckInEntity;
import com.xdy.dbmanager.entity.CrashExceptionEntity;
import com.xdy.dbmanager.entity.SearchRecordEntity;
import com.xdy.dbmanager.gen.CheckInEntityDao;
import com.xdy.dbmanager.gen.CrashExceptionEntityDao;
import com.xdy.dbmanager.gen.DaoMaster;
import com.xdy.dbmanager.gen.DaoSession;
import com.xdy.dbmanager.gen.SearchRecordEntityDao;
import org.greenrobot.greendao.async.AsyncOperation;
import org.greenrobot.greendao.async.AsyncOperationListener;
import org.greenrobot.greendao.async.AsyncSession;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class DbManager implements IDBManager, AsyncOperationListener {
/**
* Class tag. Used for debug.
*/
private static final String TAG = DbManager.class.getCanonicalName();
/**
* Instance of DbManager
*/
private static DbManager instance;
/**
* The Android Activity reference for access to DbManager.
*/
private Context context;
private ReleaseOpenHelper mHelper;
private Database database;
private DaoMaster daoMaster;
private DaoSession daoSession;
private AsyncSession asyncSession;
private List<AsyncOperation> completedOperations;
/**
* Constructs a new DbManager with the specified arguments.
* @param context The Android {@link Context}.
*/
public DbManager(final Context context) {
this.context = context;
mHelper = new ReleaseOpenHelper(this.context, "database", null);
completedOperations = new CopyOnWriteArrayList<>();
}
/**
* @param context The Android {@link Context}.
* @return this.instance
*/
public static DbManager getInstance(Context context) {
if (instance == null) {
instance = new DbManager(context);
}
return instance;
}
@Override
public void onAsyncOperationCompleted(AsyncOperation operation) {
completedOperations.add(operation);
}
private void assertWaitForCompletion1Sec() {
asyncSession.waitForCompletion(1000);
asyncSession.isCompleted();
}
/**
* Query for readable DB
*/
public void openReadableDb() throws SQLiteException {
database = mHelper.getWritableDb();
daoMaster = new DaoMaster(database);
daoSession = daoMaster.newSession();
asyncSession = daoSession.startAsyncSession();
asyncSession.setListener(this);
}
/**
* Query for writable DB
*/
public void openWritableDb() throws SQLiteException {
database = mHelper.getWritableDb();
daoMaster = new DaoMaster(database);
daoSession = daoMaster.newSession();
asyncSession = daoSession.startAsyncSession();
asyncSession.setListener(this);
}
@Override
public void closeDbConnections() {
if (daoSession != null) {
daoSession.clear();
daoSession = null;
}
if (database != null && database.isDbLockedByCurrentThread()) {
database.close();
}
if (mHelper != null) {
mHelper.close();
mHelper = null;
}
if (instance != null) {
instance = null;
}
}
@Override
public synchronized void dropDatabase() {
try {
openWritableDb();
DaoMaster.dropAllTables(database, true); // drops all tables
mHelper.onCreate(database); // creates the tables
asyncSession.deleteAll(CrashExceptionEntity.class);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean deleteExceptionByDigest(String md5digest) {
try {
openReadableDb();
CrashExceptionEntityDao dao = daoSession.getCrashExceptionEntityDao();
dao.deleteByKey(md5digest);
daoSession.clear();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public synchronized CrashExceptionEntity insertOrUpdateData(CrashExceptionEntity info) {
try {
if (info != null) {
openWritableDb();
daoSession.insertOrReplace(info);
daoSession.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
@Override
public List<CrashExceptionEntity> insertOrUpdateCrashExceptionEntity(List<CrashExceptionEntity> info) {
try {
if (info != null) {
openWritableDb();
CrashExceptionEntityDao exceptionEntityDao = daoSession.getCrashExceptionEntityDao();
exceptionEntityDao.insertOrReplaceInTx(info);
daoSession.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
@Override
public List<CrashExceptionEntity> getUnuploadCrashExceptionEntity() {
List<CrashExceptionEntity> info = null;
try {
openReadableDb();
CrashExceptionEntityDao dao = daoSession.getCrashExceptionEntityDao();
QueryBuilder<CrashExceptionEntity> builder = dao.queryBuilder().where(CrashExceptionEntityDao.Properties.UploadFlag.eq(new Boolean(false)));
info = builder.list();
daoSession.clear();
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
@Override
public SearchRecordEntity insertOrUpdateData(SearchRecordEntity info) {
try {
if (info != null) {
openWritableDb();
daoSession.insertOrReplace(info);
daoSession.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
/**
* 只获取最近10条
* @return
*/
@Override
public List<SearchRecordEntity> getSearchRecordEntity() {
List<SearchRecordEntity> info = null;
try {
openReadableDb();
SearchRecordEntityDao dao = daoSession.getSearchRecordEntityDao();
QueryBuilder<SearchRecordEntity> builder = dao.queryBuilder().orderDesc(SearchRecordEntityDao.Properties.OccurDate).limit(10);
info = builder.list();
daoSession.clear();
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
/**
* 只获取最近10条
* @return
*/
@Override
public List<SearchRecordEntity> getSearchRecordEntity(String keyword) {
List<SearchRecordEntity> info = null;
try {
openReadableDb();
SearchRecordEntityDao dao = daoSession.getSearchRecordEntityDao();
QueryBuilder<SearchRecordEntity> builder = dao.queryBuilder()
.where(SearchRecordEntityDao.Properties.Keyword.like("%" + keyword + "%"))
.orderDesc(SearchRecordEntityDao.Properties.OccurDate).limit(10);
info = builder.list();
daoSession.clear();
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
@Override
public void deleteAllRecordEntity() {
try {
openReadableDb();
SearchRecordEntityDao dao = daoSession.getSearchRecordEntityDao();
dao.deleteAll();
daoSession.clear();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public CheckInEntity insertCheckIn(CheckInEntity entity) {
try {
if (entity != null) {
openWritableDb();
daoSession.insertOrReplace(entity);
daoSession.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
return entity;
}
@Override
public List<CheckInEntity> getCheckInThisMonth(String memberSid, String year, String month) {
List<CheckInEntity> info = null;
try {
openReadableDb();
CheckInEntityDao dao = daoSession.getCheckInEntityDao();
QueryBuilder<CheckInEntity> builder = dao.queryBuilder()
.where(CheckInEntityDao.Properties.MemberSid.eq(memberSid),
CheckInEntityDao.Properties.Year.eq(year),
CheckInEntityDao.Properties.Month.eq(month));
info = builder.list();
daoSession.clear();
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
}