-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathDbConnection.java
More file actions
57 lines (45 loc) · 1.84 KB
/
DbConnection.java
File metadata and controls
57 lines (45 loc) · 1.84 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
package fr.gaulupeau.apps.Poche.data;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.util.Log;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.query.QueryBuilder;
import fr.gaulupeau.apps.InThePoche.BuildConfig;
import fr.gaulupeau.apps.Poche.App;
import fr.gaulupeau.apps.Poche.data.dao.DaoMaster;
import fr.gaulupeau.apps.Poche.data.dao.DaoSession;
public class DbConnection {
private static final String TAG = DbConnection.class.getSimpleName();
public static DaoSession getSession() {
if(Holder.session == null) {
// enable some debugging
if(BuildConfig.DEBUG) {
QueryBuilder.LOG_SQL = true;
QueryBuilder.LOG_VALUES = true;
}
String dbPath = App.getSettings().getDbPathForDbHelper();
Log.d(TAG, "creating new db session");
WallabagDbOpenHelper dbHelper = new WallabagDbOpenHelper(App.getInstance(), dbPath, null);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
dbHelper.setWriteAheadLoggingEnabled(true);
}
Database db = dbHelper.getWritableDb();
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
if(!((SQLiteDatabase)db.getRawDatabase()).enableWriteAheadLogging()) {
Log.w(TAG, "write ahead logging was not enabled");
}
}
DaoMaster daoMaster = new DaoMaster(db);
Holder.session = daoMaster.newSession();
} else {
Log.d(TAG, "using existing db session");
}
return Holder.session;
}
public static void resetSession() {
Holder.session = null;
}
private static class Holder {
private static DaoSession session = getSession();
}
}