forked from guanxiong/Ext.data.WebSqlProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExt.data.WebSqlProxy.js
575 lines (481 loc) · 16.4 KB
/
Ext.data.WebSqlProxy.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/**
* @author Grgur Grisogono. Modifications for Sencha Touch by Lisimba Gilkes ([email protected])
*
* WebSQL proxy connects models and stores to local WebSQL database.
*
* WebSQL is only available in Chrome and Safari at the moment.
*
* Version: 0.2
*
* TODO: respect sorters, filters, start and limit options on the Operation; failover option for remote proxies, ..
*/
// Ext.data.WebSqlProxy = Ext.extend(Ext.data.Proxy, {
Ext.define('Ext.data.proxy.WebSql', {
extend: 'Ext.data.proxy.Proxy',
alias: 'proxy.websql',
config: {
/**
* @cfg {String} version
* database version. If different than current, use updatedb event to update database
*/
dbVersion: '1.0',
/**
* @cfg {String} dbName
* Name of database
*/
dbName: undefined,
/**
* @cfg {String} dbDescription
* Description of the database
*/
dbDescription: '',
/**
* @cfg {String} dbSize
* Max storage size in bytes
*/
dbSize: 10 * 1024 * 1024,
/**
* @cfg {String} dbTable
* Name for table where all the data will be stored
*/
dbTable: undefined,
/**
* @cfg {String} pkField
* Primary key name. Defaults to idProperty
*/
pkField: undefined,
/**
* @cfg {String} pkType
* Type of primary key. By default it an autoincrementing integer
*/
pkType: 'TEXT PRIMARY KEY ASC',
/**
* @cfg {Array} initialData
* Initial data that will be inserted in object store on store creation
*/
initialData: []
},
/**
* @private
* db object
*/
db: undefined,
/**
* @private
* used to monitor initial data insertion. A helper to know when all data is in. Helps fight asynchronous nature of idb.
*/
initialDataCount: 0,
/**
* @private
* Trigger that tells that proxy is currently inserting initial data
*/
insertingInitialData: false,
/**
* Creates the proxy, throws an error if local storage is not supported in the current browser.
* @param {Object} config (optional) Config object.
*/
constructor: function (config) {
this.registerExtJsFx();
// Ext.apply(this, config || {
// });
// Ext.data.proxy.WebSql.superclass.constructor.call(this, config);
this.initConfig(config);
this.checkDependencies();
this.addEvents('dbopen', 'updatedb', 'exception', 'cleardb', 'initialDataInserted', 'noWebDb');
this.callParent(arguments);
// this.initialize();
},
/**
* @private
* Sets up the Proxy by opening database and creating table if necessary
*/
initialize: function () {
var me = this,
pk = 'id',
db;
me.db = db = openDatabase(me.getDbName(), me.getDbVersion(), me.getDbDescription(), me.getDbSize());
//take care of the table
db.transaction(function (tx) {
pk = me.getPkField() || me.getReader().idProperty || pk;
me.setPkField(pk);
var createTable = function () {
tx.executeSql('CREATE TABLE IF NOT EXISTS ' + me.getDbTable() + '(' + pk + ' ' + me.getPkType() + ', ' + me.constructFields() + ')', [], Ext.bind(me.addInitialData, me), //on success
Ext.bind(me.onError, me)); // on error
}
tx.executeSql('SELECT * FROM ' + me.getDbTable() + ' LIMIT 1', [], Ext.emptyFn, createTable);
});
},
/**
* @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) {
var name = f.getName();
if (name == me.getPkField()) {
return;
}
var type = f.getType().type;
type = type.replace(/int/i, 'INTEGER').replace(/string/i, 'TEXT').replace(/date/i, 'DATETIME');
flatFields.push('"' + name + '" ' + type);
});
return flatFields.join(',');
},
/**
* Universal error reporter for debugging purposes
* @param {Object} err Error object.
*/
onError: function (err, e) {
var error = (e && e.message) || err;
console.log(error, arguments);
},
/**
* Check if all needed config options are set
*/
checkDependencies: function () {
var me = this;
if (!window.openDatabase) {
me.fireEvent('noWebDb');
Ext.Error.raise("WebDB is not supported in your browser.");
}
if (!Ext.isString(me.getDbName())) console.log("The dbName string has not been defined in your Ext.data.proxy.WebDB");
if (!Ext.isString(me.getDbTable())) console.log("The dbTable string has not been defined in your Ext.data.proxy.WebDB");
return true;
},
/**
* Add initial data if set at {@link #initialData}
*/
addInitialData: function () {
this.addData();
},
/**
* Add data when needed
* Also add initial data if set at {@link #initialData}
* @param {Array/Ext.data.Store} newData Data to add as array of objects or a store instance. Optional
* @param {Boolean} clearFirst Clear existing data first
*/
addData: function (newData, clearFirst) {
var me = this,
model = me.getModel().modelName,
data = newData || me.getInitialData();
//clear objectStore first
if (clearFirst === true) {
me.clear();
me.addData(data);
return;
}
if (Ext.isObject(data) && data.isStore === true) {
data = me.getDataFromStore(data);
}
// console.log(data);
me.initialDataCount = data.length;
me.insertingInitialData = true;
Ext.each(data, function (entry) {
Ext.create(model,entry).save();
})
},
/**
* Get data from store. Usually from Server proxy.
* Useful if caching data data that don't change much (e.g. for comboboxes)
* Used at {@link #addData}
* @private
* @param {Ext.data.Store} store Store instance
* @return {Array} Array of raw data
*/
getDataFromStore: function (store) {
var data = [];
store.each(function (item) {
data.push(item.data)
});
return data;
},
//inherit docs
create: function (operation, callback, scope) {
var records = operation.getRecords(),
length = records.length,
id, record, i;
operation.setStarted();
for (i = 0; i < length; i++) {
record = records[i];
this.setRecord(record);
}
operation.setCompleted();
operation.setSuccessful();
if (typeof callback == 'function') {
callback.call(scope || this, operation);
}
},
//inherit docs
read: function (operation, callback, scope) {
var records = [],
me = this;
var finishReading = function (record) {
me.readCallback(operation, record);
if (typeof callback == 'function') {
callback.call(scope || this, operation);
}
}
console.log(operation);
//read a single record
if (operation.id) {
this.getRecord(operation.id, finishReading, me);
} else {
this.getAllRecords(finishReading, me);
operation.setSuccessful();
}
},
/**
* Injects data in operation instance
*/
readCallback: function (operation, records) {
console.log(operation);
console.log(records);
var rec = Ext.isArray(records) ? records : [records];
operation.setSuccessful();
operation.setCompleted();
/*
operation.resultSet = Ext.create('Ext.data.ResultSet', {
records: rec,
total : rec.length,
loaded : true
});*/
operation.resultSet = new Ext.data.ResultSet({
records: rec,
total: rec.length,
loaded: true
});
},
//inherit docs
update: function (operation, callback, scope) {
var records = operation.getRecords(),
length = records.length,
record, id, i;
operation.setStarted();
for (i = 0; i < length; i++) {
record = records[i];
this.updateRecord(record);
}
operation.setCompleted();
operation.setSuccessful();
if (typeof callback == 'function') {
callback.call(scope || this, operation);
}
},
//inherit
destroy: function (operation, callback, scope) {
var records = operation.getRecords(),
length = records.length,
i;
for (i = 0; i < length; i++) {
Ext.Array.remove(newIds, records[i].getId());
this.removeRecord(records[i], false);
}
//this.setIds(newIds);
operation.setCompleted();
operation.setSuccessful();
if (typeof callback == 'function') {
callback.call(scope || this, operation);
}
},
/**
* @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;
},
/**
* @private
* Fetches a single record by id.
* @param {Mixed} id Record id
* @param {Function} callback Callback function
* @param {Object} scope Callback fn scope
*/
getRecord: function (id, callback, scope) {
var me = this,
Model = this.getModel(),
record, onSuccess = function (tx, rs) {
var result = me.parseData(tx, rs);
record = new Model(result, id);
if (typeof callback == 'function') {
callback.call(scope || me, result, me);
}
}
me.db.transaction(function (tx) {
tx.executeSql('SELECT * FROM ' + me.getDbTable() + ' where ' + me.getPkField() + ' = ?', [id], onSuccess, //on success
Ext.bind(me.onError, me)); // on error
});
return true;
},
/**
* @private
* Fetches all records
* @param {Function} callback Callback function
* @param {Object} scope Callback fn scope
*/
getAllRecords: function (callback, scope) {
var me = this,
Model = me.getModel(),
record, onSuccess = function (tx, rs) {
var records = me.parseData(tx, rs),
results = [],
i = 0,
id;
console.log(rs);
for (; i < records.length; i++) {
id = records[i][me.getPkField()];
results.push(new Model(records[i], id));
}
if (typeof callback == 'function') {
callback.call(scope || me, results, me);
}
}
me.db.transaction(function (tx) {
tx.executeSql('SELECT * FROM ' + me.getDbTable(), [], onSuccess, //on success
Ext.bind(me.onError, me)); // on error
});
return true;
},
/**
* Saves the given record in the Proxy.
* @param {Ext.data.Model} record The model instance
*/
setRecord: function (record) {
console.log(record);
var me = this,
rawData = record.data,
fields = [],
values = [],
placeholders = [],
onSuccess = function (tx, rs) {
if (me.insertingInitialData) {
me.initialDataCount--;
if (me.initialDataCount === 0) {
me.insertingInitialData = false;
me.fireEvent('initialDataInserted');
}
}
};
//extract data to be inserted
for (var i in rawData) {
fields.push('"' + i + '"');
var value = rawData[i];
if(value == null){
value = '';
}
if(Ext.typeOf(value) == 'date'){
value = Ext.Date.format(value,'Y-m-d H:i:s');
}
console.log(value);
values.push(value);
placeholders.push('?');
}
me.db.transaction(function (tx) {
tx.executeSql('INSERT INTO ' + me.getDbTable() + '(' + fields.join(',') + ') VALUES (' + placeholders.join(',') + ')', values, onSuccess, //on success
Ext.bind(me.onError, me)); // on error
});
return true;
},
/**
* Updates the given record.
* @param {Ext.data.Model} record The model instance
*/
updateRecord: function (record) {
var me = this,
id = record.internalId || record[me.getPkField()],
key = me.getReader().idProperty,
modifiedData = record.modified,
newData = record.data,
pairs = [],
values = [],
onSuccess = function (tx, rs) {
//add new record if id doesn't exist
if (!rs.rowsAffected) me.setRecord(record);
};
/*for (var i in modifiedData) {
pairs.push('"'+i+'" = ?');
values.push(newData[i]);
}*/
var fields = Object.keys(record.data);
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
if (key == field) {
// Don't want to update primary key
continue;
}
pairs.push(field + ' = ?');
values.push(record.data[field]);
}
values.push(id);
me.db.transaction(function (tx) {
tx.executeSql('UPDATE ' + me.getDbTable() + ' SET ' + pairs.join(',') + ' WHERE ' + key + ' = ?', values, onSuccess, //on success
Ext.bind(me.setRecord, me, [record])); // on error
});
return true;
},
/**
* @private
* Physically removes a given record from the object store.
* @param {Mixed} id The id of the record to remove
*/
removeRecord: function (record) {
var me = this;
var id = record.internalId || record[me.getPkField()] || record.data[me.getPkField()];
me.db.transaction(function (tx) {
tx.executeSql('DELETE FROM ' + me.getDbTable() + ' WHERE ' + me.getPkField() + ' = ?', [id], Ext.emptyFn, //on success
Ext.bind(me.onError, me)); // on error
});
return true;
},
/**
* Destroys all records stored in the proxy
*/
clear: function (callback, scope) {
var me = this;
me.db.transaction(function (tx) {
tx.executeSql('DELETE FROM ' + me.getDbTable(), [], Ext.emptyFn, //on success
Ext.bind(me.onError, me)); // on error
});
return true;
},
registerExtJsFx: function () {
Ext.apply(Ext, {
bind: function (fn, scope, args, appendArgs) {
var method = fn,
slice = Array.prototype.slice;
return function () {
var callArgs = args || arguments;
if (appendArgs === true) {
callArgs = slice.call(arguments, 0);
callArgs = callArgs.concat(args);
} else if (Ext.isNumber(appendArgs)) {
callArgs = slice.call(arguments, 0); // copy arguments first
Ext.Array.insert(callArgs, appendArgs, args);
}
return method.apply(scope || window, callArgs);
};
}
});
Ext.apply(Ext, {
isString: function (value) {
return typeof value === 'string';
}
});
}
});
// Ext.data.ProxyMgr.registerType('websql', Ext.data.WebSqlProxy);