-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbhandler.js
236 lines (216 loc) · 11.6 KB
/
dbhandler.js
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
/*
* Wrapper(Middleware) for websql DB transactions.
* Works well with Cordova sqlite plugin 'https://github.com/brodysoft/Cordova-SQLitePlugin'
* ----------------------------------------------------------------------------------------
* Author: Subash S
* Website: http://www.subashselvaraj.com
* Date Created: 28th FEB, 2015
*
* GIT : https://github.com/sesubash/cordova-sqlite-wrapper.git
*
*/
//==============================================
// TODO: Make a way to handle multiple database
//==============================================
var DBHandler = (function() {
var db, dbName = "app.db";
//----------------------
// Initiate the database
//----------------------
function initDatabase() {
//
if (window.sqlitePlugin) {
db = window.sqlitePlugin.openDatabase({name: dbName});
}else if (window.openDatabase) {
db = window.openDatabase("app", "1.0", "MyApp", 200000);
}
}
//---------------------------------
// Do appropriate stuffs on error
//---------------------------------
function onError(tx, error) {
console.log(error.message);
}
//-------------------------------------------------------------------------
// Checks if the table with the name passed is exists in the database and
// returns status(true/false) via callback
//
// Params: @table => name of the table
// @callback => function to be called upon query execution
//-------------------------------------------------------------------------
function isExists(table, callback) {
db.transaction(function(tx) {
tx.executeSql("SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name= '"+table+"'", [], function(tx, result) {
callback(result);
}, onError);
});
}
//---------------------------------------------------------------
// Selects all records in the table and returns via callback
// Params: @table => name of the table
// @callback => function to be called upon query execution
//----------------------------------------------------------------
function getAllRecords(table, callback) {
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM "+table, [], function(tx, result) {
callback(result);
});
});
}
//---------------------------------------------------------------
// Selects records in the table based on condition and columns passed
// and returns via callback
// Params: @table => name of the table
// @columns => colums to be fetched as query formatted string
// eg., "id, student_id, data" or "*"
// @condition => condition to be checked as query formatted string
// eg., "student_id = 2 AND id = 1"
// @callback => function to be called upon query execution
//----------------------------------------------------------------
function getRecord (table, columns, condition, callback) {
console.log("SELECT "+columns+" FROM "+table+" WHERE "+condition);
db.transaction(function(tx) {
tx.executeSql("SELECT "+columns+" FROM "+table+" WHERE "+condition, [], function(tx, result) {
callback(result);
}, onError);
});
}
//---------------------------------------------------------------
// Creates table with the name and columns passed
// Params: @table => name of the table
// @columns => colums to be created as query formatted string
// eg., "id integer primary key, student_id integer, data text"
// @callback => function to be called upon query execution
//----------------------------------------------------------------
function createTable(table, columns, callback) {
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE "+table+" ("+columns+")", [], function(tx, result) {
console.log("=====created table :"+table);
callback(result);
},
onError);
});
}
//---------------------------------------------------------------
// Add new record with passed values
// Params: @table => name of the table
// @columns => Array of colums to be inserted as query formatted string
// eg., ["student_id", "sem_id"];
// @values => Array of values to be inserted as query formatted string
// eg., ["1", "2"];
// @callback => function to be called upon query execution
//----------------------------------------------------------------
function newRecord(table, columns, values, callback) {
console.log(columns+":"+values)
var _columns = columns.join(",");
db.transaction(function(tx) {
tx.executeSql("INSERT INTO "+table+" ("+_columns+") VALUES ("+getValuePlaceHolder(_columns)+")", values,
function(tx, result) {
//console.log("insertID", result.insertId, "rows affected", result.rowsAffected);
callback(result);
},
onError);
});
}
//---------------------------------------------------------------
// Update record with passed condition and values
// Params: @table => name of the table
// @condition => condition to be checked as query formatted string
// eg., "student_id = 2 AND id = 1"
// @columns => Array of colums to be updated as query formatted string
// eg., ["student_id", "sem_id"];
// @values => Array of values to be updated as query formatted string
// eg., ["1", "2"];
// @callback => function to be called upon query execution
//----------------------------------------------------------------
function updateRecord(table, condition, columns, values, callback) {
console.log(condition);
console.log(values);
db.transaction(function(tx) {
tx.executeSql("UPDATE "+table+" SET "+getUpdateColumns(columns)+" WHERE "+condition, values ,
function (tx, result) {
callback(result);
}, onError);
});
}
//---------------------------------------------------------------
// Delete record with passed condition
// Params: @table => name of the table
// @condition => condition to be checked as query formatted string
// eg., "student_id = 2 AND id = 1"
// @callback => function to be called upon query execution
//----------------------------------------------------------------
function deleteRecord(table, condition, callback) {
db.transaction(function(tx) {
tx.executeSql("DELETE FROM "+table+" WHERE "+condition, [],
function(tx, result) {
callback(result)
},
onError);
});
}
//---------------------------------------------------------------
// Delete table with passed name
// Params: @table => name of the table
// @callback => function to be called upon query execution
//----------------------------------------------------------------
function dropTable(table, callback) {
db.transaction(function(tx) {
tx.executeSql("DROP TABLE "+table, [],
function(tx, result) { callback(result) },
onError);
});
}
//---------------------------------------------------------------
// Get table schema
// Params: @table => name of the table
// @callback => function to be called upon query execution
//----------------------------------------------------------------
function getTableSchema(table, callback){
db.transaction(function(tx){
tx.executeSql("PRAGMA table_info("+table+")",[], function(tx, result){
callback(result);
});
});
}
//========================
// Helper functions
//========================
//---------------------------------------------------------------
// Get place holder as query formatted string for the columns passed
// Params: @columns => Array of colums to be updated as query formatted string
// eg., ["student_id", "sem_id"];
//----------------------------------------------------------------
function getValuePlaceHolder(columns) {
var valuePlaceHolder = [];
columns.split(",").forEach(function () {
valuePlaceHolder.push("?");
});
return (valuePlaceHolder.length > 1) ? valuePlaceHolder.join(",") : valuePlaceHolder[0];;
}
//---------------------------------------------------------------
// Get columns to be updated as query formatted string from the columns passed
// Params: @columns => Array of colums to be updated as query formatted string
// eg., ["student_id", "sem_id"];
//----------------------------------------------------------------
function getUpdateColumns(columns) {
var result = [];
columns.forEach(function (value) {
result.push(value+" = ? ");
});
return result.join(",");
}
return {
initDatabase: initDatabase,
isExists: isExists,
getAllRecords: getAllRecords,
getRecord: getRecord,
getCount: getCount,
getTableSchema: getTableSchema,
newRecord: newRecord,
createTable: createTable,
updateRecord: updateRecord,
deleteRecord: deleteRecord,
dropTable: dropTable
}
})();