-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLITEBase.java
More file actions
144 lines (113 loc) · 4.7 KB
/
Copy pathSQLITEBase.java
File metadata and controls
144 lines (113 loc) · 4.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
// ====================================================
// 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.Context;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by Furkan on 11/09/2013.
*/
public abstract class SQLITEBase {
protected Context m_context;
private static final Object m_lockObject = new Object();
protected SQLITEOpenHelper m_sqLiteOpenHelper;
private SQLiteDatabase m_db;
protected SQLITESetting m_sqlSetting;
private static final ConcurrentHashMap<String, SQLITEOpenHelper> m_dbMap = new ConcurrentHashMap<String, SQLITEOpenHelper>();
protected String m_db_name;
protected int m_db_version;
protected boolean m_isActive;
public SQLITEBase(Context context, SQLITESetting setting) {
this.m_sqlSetting = setting;
this.m_db_name = setting.DatabaseName;
this.m_db_version = setting.DatabaseVersion;
String dbPath = context.getApplicationContext().getDatabasePath(m_db_name).getAbsolutePath();
synchronized (m_lockObject) {
this.m_sqLiteOpenHelper = m_dbMap.get(dbPath);
if (this.m_sqLiteOpenHelper == null) {
this.m_sqLiteOpenHelper = new SQLITEOpenHelper(context, m_db_name, m_db_version, this);
m_dbMap.put(dbPath,this.m_sqLiteOpenHelper);
}
//For Instantly onCreate() method, add-> this.m_db = this.m_sqLiteOpenHelper.getWritableDatabase();
}
this.m_context = context.getApplicationContext();
this.m_isActive = true;
}
public abstract void onCreate(SQLiteDatabase db);
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
public void onOpen(SQLiteDatabase db){}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
public void onConfigure(SQLiteDatabase db){}
public synchronized SQLiteDatabase getDb(){
return this.open();
}
public boolean isOpen(){
return (this.m_db != null && this.m_db.isOpen());
}
public List<SQLITECreateInfo> getCreateInfos(){
return this.m_sqlSetting.CreateInfos;
}
public List<SQLITEUpgradeInfo> getUpgradeInfos(){
return this.m_sqlSetting.UpgradeInfos;
}
public List<String> getCreateQuaries(){
List<SQLITECreateInfo> creates = this.getCreateInfos();
List<String> totalQueries = new ArrayList<String>();
for(int i = 0; i < creates.size(); i++){
List<String> subQueries = creates.get(i).Queries;
for(int j = 0; j < subQueries.size(); j++){
String query = subQueries.get(j);
totalQueries.add(query);
}
}
return totalQueries;
}
public List<String> getUpgradeQueries(int oldVersion, int newVersion){
List<SQLITEUpgradeInfo> upgrades = this.getUpgradeInfos();
List<String> totalQueries = new ArrayList<String>();
for(int i = 0; i < upgrades.size(); i++){
SQLITEUpgradeInfo subUpgrade = upgrades.get(i);
if(subUpgrade.OldVersion == oldVersion && subUpgrade.NewVersion == newVersion){
List<String> subQueries = subUpgrade.Queries;
for(int j = 0; j < subQueries.size(); j++){
String query = subQueries.get(j);
totalQueries.add(query);
}
}
}
return totalQueries;
}
public synchronized boolean close(){
if (this.m_isActive){
this.m_sqLiteOpenHelper.removeConnection();
}
int count = this.m_sqLiteOpenHelper.getCounter();
if (count==0){
synchronized (this.m_lockObject){
if (this.m_db.inTransaction()) this.m_db.endTransaction();
if (this.m_db.isOpen()) this.m_db.close();
this.m_db = null;
}
}
this.m_isActive = false;
return (count==0);
}
public synchronized SQLiteDatabase open(){
if (!this.m_isActive){
this.m_sqLiteOpenHelper.addConnection();
}
if (this.m_db==null || !this.m_db.isOpen()){
synchronized (this.m_lockObject){
this.m_db = this.m_sqLiteOpenHelper.getWritableDatabase();
}
}
this.m_isActive = true;
return this.m_db;
}
}