-
Notifications
You must be signed in to change notification settings - Fork 9
/
SqliteProxy.js
447 lines (401 loc) · 13.6 KB
/
SqliteProxy.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* @author Thomas Alexander
*
* SqliteStorage is simply a superclass for the {@link Ext.data.proxy.LocalStorage localStorage} and {@link
* Ext.data.proxy.SqliteStorage sqlitestorage} proxies. It uses the HTML5 Sqlite Database storage
* save {@link Ext.data.Model model instances} for offline use.
*/
Ext.define('Ext.data.proxy.SqliteStorage', {
extend: 'Ext.data.proxy.Client',
alias: 'proxy.sqlitestorage',
alternateClassName: 'Ext.data.SqliteStorageProxy',
constructor: function(config) {
this.callParent([config]);
//ensures that the reader has been instantiated properly
this.setReader(this.reader);
var me = this;
//console.log(me);
me.createTable();
},
//inherit docs
create: function(operation, callback, scope) {
//console.log("create");
var me = this;
//console.log(operation);
var records = me.getTableFields(operation.getRecords()),
length = records.length,
id, record, i,
model = this.getModel(),
idProperty = model.getIdProperty();
//console.log(idProperty,"model")
operation.setStarted();
for (i = 0; i < length; i++) {
record = records[i];
//console.log(record,"record");
this.setRecord(record, me.config.dbConfig.tablename, idProperty)
}
operation.setCompleted();
operation.setSuccessful();
if (typeof callback == 'function') {
callback.call(scope || this, operation);
}
},
//inherit docs
update: function(operation, callback, scope) {
//console.log("update");
var me = this;
var records = this.getTableFields(operation.getRecords()),
length = records.length,
record, id, i, tbl_Id = me.getModel().getIdProperty();
//console.log(me.getModel().getClientIdProperty(),"primaryKey");
operation.setStarted();
for (i = 0; i < length; i++) {
record = records[i];
this.updateRecord(record, me.config.dbConfig.tablename,tbl_Id);
}
operation.setCompleted();
operation.setSuccessful();
if (typeof callback == 'function') {
callback.call(scope || this, operation);
}
},
//inherit docs
read: function(operation, callback, scope) {
var me = this,
fields = [],
values = [];
Ext.iterate(operation.getParams(),function(k,v){
fields.push(k + ' = ?');
values.push(v);
});
var sql = operation.config.query || me.config.dbConfig.dbQuery || 'SELECT * FROM '+me.config.dbConfig.tablename+'';
if(fields.length)
{
sql = sql + ' WHERE ' + fields.join(' AND ');
}
var onSucess, onError;
onSucess = function(tx, results) {
//console.log(results);
me.applyDataToModel(tx, results, operation, callback, scope);
};
onError = function(tx, err) {
me.throwDbError(tx, err);
};
me.queryDB(me.getDb(), sql, onSucess, onError,values);
},
//inherit docs
destroy: function(operation, callback, scope) {
//console.log("destroy");
var me = this;
var records = operation.records,
length = records.length,
i, tbl_Id = me.getModel().getIdProperty();
for (i = 0; i < length; i++) {
this.removeRecord(records[i].data[tbl_Id], me.config.dbConfig.tablename, tbl_Id, false);
}
operation.setCompleted();
operation.setSuccessful();
if (typeof callback == 'function') {
callback.call(scope || this, operation);
}
},
/**
*@private
* Get Database instance
*/
getDb : function(){
//return Ext.DbConnection.dbConn || this.config.dbConfig.dbConn;
return this.config.dbConfig.dbConn.dbConn;
},
/**
*@private
*Creates table if not exists
*/
createTable : function(){
var me = this;
me.getDb().transaction(function(tx) {
var onError = function(tx, err) {
me.throwDbError(tx, err);
};
var onSucess = function(tx, results) {
//console.log("success");
}
var createTableSchema = function() {
//console.log(me.constructFields());
var createsql = 'CREATE TABLE IF NOT EXISTS ' + me.config.dbConfig.tablename + '('+me.constructFields()+')';
console.log(createsql);
tx.executeSql(createsql,[],onSucess,onError);
}
var tablesql = 'SELECT * FROM '+ me.config.dbConfig.tablename+' LIMIT 1';
console.log(tablesql);
tx.executeSql(tablesql,[], Ext.emptyFn, createTableSchema);
});
},
/**
* @private
* Get reader data and set up fields accordingly
* Used for table creation only
* @return {String} fields separated by a comma
*/
constructFields: function() {
var me = this,
m = me.getModel(),
fields = m.prototype.fields.items,
flatFields = [];
Ext.each(fields, function(f) {
if((f.config.isTableField || !Ext.isDefined(f.config.isTableField))){
var name = f.getName();
var type = f.config.type;
var fieldoption = (f.config.fieldOption) ? f.config.fieldOption : '';
type = type.replace(/int/i, 'INTEGER')
.replace(/string/i,'TEXT')
.replace(/date/i, 'DATETIME');
if((f.getName() != m.getIdProperty()))
{
flatFields.push(name + ' ' + type+' '+fieldoption);
}
else
{
flatFields.push(name + ' ' + type+' PRIMARY KEY ');
}
}
});
return flatFields.join(',');
},
/**
* function to return records w.r.t to table fields. If model has fields
* which is not table field name, it will break create and update functionalitites.This looks for field property "isTableField"
*/
getTableFields : function(records){
//console.log(records);
var newrecords = [],removedField = [], length = records.length,
m = this.getModel(), modelFields = m.prototype.fields.items;
//console.log(modelFields);
Ext.each(modelFields,function(item,index){
if(item.config.isTableField == false || (item.getName() == m.getIdProperty())){
//console.log(item);
removedField.push(item.getName());
}
});
for (i = 0; i < length; i++) {
record = records[i];
//console.log(removedField,"remove");
Ext.each(removedField,function(item,index){
//console.log(record.getData(),"record");
//console.log(item,"item");
delete record.getData()[item];
});
//console.log(record);
newrecords.push(record);
}
//console.log(newrecords);
return newrecords;
},
/**
* execute sql statements
* @param {Object} dbConn Database connection Value
* @param {String} sql Sql Statement
* @param {Function} successcallback success callback for sql execution
* @param {Function} errorcallback error callback for sql execution
* @param {Array} params sql statement parameters
* @param {Function} callback additional callback
*/
queryDB: function(dbConn, sql, successcallback, errorcallback, params, callback) {
var me = this;
dbConn.transaction(function(tx) {
if (typeof callback == 'function') {
callback.call(scope || me, results, me);
}
if(!params) params = [];
console.log(sql+" " +JSON.stringify(params))
tx.executeSql(
sql, (params ? params : []), successcallback, errorcallback);
});
},
/**
* @private
* Created array of objects, each representing field=>value pair.
* @param {Object} tx Transaction
* @param {Object} rs Response
* @return {Array} Returns parsed data
*/
parseData: function(tx, rs) {
var rows = rs.rows,
data = [],
i = 0;
for (; i < rows.length; i++) {
data.push(rows.item(i));
}
return data;
},
applyData: function(data, operation, callback, scope) {
//console.log(data);
var me = this;
/*operation.resultSet = new Ext.data.ResultSet({
records: data,
total: data.length,
loaded: true
});*/
operation.setSuccessful();
operation.setCompleted();
operation.setResultSet(Ext.create('Ext.data.ResultSet', {
records: data,
total : data.length,
loaded : true
}));
//console.log(operation);
//finish with callback
operation.setRecords(data);
if (typeof callback == "function") {
callback.call(scope || me, operation);
}
},
applyDataToModel: function(tx, results, operation, callback, scope) {
var me = this,
Model = me.getModel(),
fields = Model.getFields().items,
primarykey = Model.getIdProperty();
//console.log(fields);
var records = me.parseData(tx, results);
//console.log(records.length);
var storedatas = [];
if (results.rows && records.length) {
for (i = 0; i < results.rows.length; i++) {
//console.log(records[i]);
storedatas.push(new Model(records[i],records[i][primarykey]));
}
operation.setSuccessful();
}
me.applyData(storedatas, operation, callback, scope);
},
/**
* Output Query Error
* @param {Object} tx Transaction
* @param {Object} rs Response
*/
throwDbError: function(tx, err) {
//console.log(this.type + "----" + err.message);
},
/**
* Saves the given record in the Proxy.
* @param {Ext.data.Model} record The model instance
*/
setRecord: function(record, tablename, primarykey) {
//console.log(primarykey);
//console.log(record.internalId,"recprd1");
//console.log(record.getData().id,"recprd2");
var me = this,
rawData = record.getData(),
fields = [],
values = [],
placeholders = [],
onSuccess = function(tx, rs) {
//console.log(rs,"balh");
var returnrecord = record,
insertId = rs.insertId;
returnrecord.data[primarykey] = insertId;
returnrecord.internalId = insertId;
returnrecord.commit(true);
},
onError = function(tx, err) {
me.throwDbError(tx, err);
};
//console.log(rawData,"rawdata");
//console.log(rawData.id,"id val");
//extract data to be inserted
for (var i in rawData) {
//console.log(rawData[i],i);
if (rawData[i]) {
//console.log(rawData[i],i);
//if(i != primarykey){
fields.push(i);
values.push(rawData[i]);
placeholders.push('?');
//}
}
}
Ext.iterate(rawData,function(a,b){
//console.log(a,b)
});
//console.log(fields,"fields");
//console.log(values,"values");
var sql = 'INSERT INTO ' + tablename + '(' + fields.join(',') + ') VALUES (' + placeholders.join(',') + ')';
//console.log(sql,"sql");
me.queryDB(me.getDb(), sql, onSuccess, onError, values);
return true;
},
/**
* Updates the given record.
* @param {Ext.data.Model} record The model instance
*/
updateRecord: function(record, tablename, primarykey) {
var me = this,
id = record.get(primarykey),
key = primarykey,
modifiedData = record.modified,
newData = record.data,
pairs = [],
values = [],
onSuccess = function(tx, rs) {
//add new record if id doesn't exist
if (rs.rowsAffected == 0) {
me.setRecord(record, tablename, primarykey);
}
else
{
record.commit(true);
}
},
onError = function(tx, err) {
me.throwDbError(tx, err);
};
// sick hack got work around something clobbering the primary key
if(!id)
{
id = record.getId();
if(!id)
{
record.setId(record.internalId);
id = record.internalId;
}
}
for (var i in newData) {
if (i != primarykey) {
pairs.push(i + ' = ?');
values.push(newData[i]);
}
}
values.push(id);
var sql = 'UPDATE ' + tablename + ' SET ' + pairs.join(',') + ' WHERE ' + key + ' = ?';
//console.log(sql);
me.queryDB(me.getDb(), sql, onSuccess, onError, values);
return true;
},
/**
* @private
* Physically removes a given record from the object store.
* @param {Mixed} id The id of the record to remove
*/
removeRecord: function(id, tablename, primarykey) {
var me = this,
values = [],
onSuccess = function(tx, rs) {
},
onError = function(tx, err) {
me.throwDbError(tx, err);
};
var sql = 'DELETE FROM ' + tablename + ' WHERE ' + primarykey + ' = ?';
values.push(id);
me.queryDB(me.getDb(), sql, onSuccess, onError, values);
return true;
},
/**
* Destroys all records stored in the proxy
*/
truncate: function(tablename) {
var me = this;
var sql = 'DELETE FROM ' + me.config.dbConfig.tablename;
me.queryDB(me.getDb(), sql, function(){}, function(){});
return true;
}
});