]>
git.proxmox.com Git - extjs.git/blob - extjs/build/packages/legacy/classic/legacy-debug.js
2 * SQL proxy lets you store data in a SQL database.
3 * The Sencha Touch SQL proxy outputs model data into an HTML5
4 * local database using WebSQL.
6 * You can create a Store for the proxy, for example:
8 * Ext.require(["Ext.data.proxy.SQL"]);
10 * Ext.define("User", {
11 * extend: "Ext.data.Model",
13 * fields: [ "firstName", "lastName" ]
17 * Ext.create("Ext.data.Store", {
25 * Ext.getStore("Users").add({
30 * Ext.getStore("Users").sync();
32 * To destroy a table use:
34 * Ext.getStore("Users").getProxy().dropTable();
36 * To recreate a table use:
38 * Ext.data.Store.sync() or Ext.data.Model.save()
40 Ext
.define('Ext.data.proxy.Sql', {
42 extend
: 'Ext.data.proxy.Client',
43 alternateClassName
: 'Ext.data.proxy.SQL',
47 * @cfg {Object} reader
52 * @cfg {Object} writer
58 * Optional Table name to use if not provided ModelName will be used
62 * @cfg {String} database
63 * Database name to access tables from
71 updateModel: function(model
) {
73 modelName
, len
, i
, columns
, quoted
;
75 me
.uniqueIdStrategy
= model
.identifier
.isUnique
;
77 modelName
= model
.entityName
;
78 me
.setTable(modelName
.slice(modelName
.lastIndexOf('.') + 1));
80 me
.columns
= columns
= me
.getPersistedModelColumns(model
);
81 me
.quotedColumns
= quoted
= [];
82 for (i
= 0 , len
= columns
.length
; i
< len
; ++i
) {
83 quoted
.push('"' + columns
[i
] + '"');
90 setException: function(operation
, error
) {
91 operation
.setException(error
);
93 create: function(operation
) {
95 records
= operation
.getRecords(),
97 operation
.setStarted();
98 me
.executeTransaction(function(transaction
) {
99 me
.insertRecords(records
, transaction
, function(resultSet
, statementError
) {
101 error
= statementError
;
103 }, function(transactionError
) {
104 operation
.setException(transactionError
);
107 operation
.setException(statementError
);
109 operation
.process(result
);
113 read: function(operation
) {
115 model
= me
.getModel(),
116 records
= operation
.getRecords(),
117 record
= records
? records
[0] : null,
118 result
, error
, id
, params
;
119 if (record
&& !record
.phantom
) {
122 id
= operation
.getId();
124 if (id
!== undefined) {
131 page
: operation
.getPage(),
132 start
: operation
.getStart(),
133 limit
: operation
.getLimit(),
134 sorters
: operation
.getSorters(),
135 filters
: operation
.getFilters()
138 operation
.setStarted();
139 me
.executeTransaction(function(transaction
) {
140 me
.selectRecords(transaction
, params
, function(resultSet
, statementError
) {
142 error
= statementError
;
144 }, function(transactionError
) {
145 operation
.setException(transactionError
);
148 operation
.setException(statementError
);
150 operation
.process(result
);
154 update: function(operation
) {
156 records
= operation
.getRecords(),
158 operation
.setStarted();
159 me
.executeTransaction(function(transaction
) {
160 me
.updateRecords(transaction
, records
, function(resultSet
, statementError
) {
162 error
= statementError
;
164 }, function(transactionError
) {
165 operation
.setException(transactionError
);
168 operation
.setException(statementError
);
170 operation
.process(result
);
174 erase: function(operation
) {
176 records
= operation
.getRecords(),
178 operation
.setStarted();
179 me
.executeTransaction(function(transaction
) {
180 me
.destroyRecords(transaction
, records
, function(resultSet
, statementError
) {
182 error
= statementError
;
184 }, function(transactionError
) {
185 operation
.setException(transactionError
);
188 operation
.setException(error
);
190 operation
.process(result
);
194 createTable: function(transaction
) {
197 me
.executeTransaction(function(transaction
) {
198 me
.createTable(transaction
);
202 me
.executeStatement(transaction
, 'CREATE TABLE IF NOT EXISTS "' + me
.getTable() + '" (' + me
.getSchemaString() + ')', function() {
203 me
.tableExists
= true;
206 insertRecords: function(records
, transaction
, callback
) {
208 columns
= me
.columns
,
209 totalRecords
= records
.length
,
211 uniqueIdStrategy
= me
.uniqueIdStrategy
,
212 setOptions
= me
._createOptions
,
213 len
= records
.length
,
214 i
, record
, placeholders
, sql
, data
, values
, errors
, completeIf
;
215 completeIf = function(transaction
) {
217 if (executed
=== totalRecords
) {
218 callback
.call(me
, new Ext
.data
.ResultSet({
223 placeholders
= Ext
.String
.repeat('?', columns
.length
, ',');
224 sql
= 'INSERT INTO "' + me
.getTable() + '" (' + me
.quotedColumns
.join(',') + ') VALUES (' + placeholders
+ ')';
225 for (i
= 0; i
< len
; ++i
) {
227 data
= me
.getRecordData(record
);
228 values
= me
.getColumnValues(columns
, data
);
229 // Capture the record in closure scope so we can access it later
231 me
.executeStatement(transaction
, sql
, values
, function(transaction
, resultSet
) {
232 if (!uniqueIdStrategy
) {
233 record
.setId(resultSet
.insertId
, setOptions
);
236 }, function(transaction
, error
) {
246 selectRecords: function(transaction
, params
, callback
, scope
) {
248 Model
= me
.getModel(),
249 idProperty
= Model
.idProperty
,
250 sql
= 'SELECT * FROM "' + me
.getTable() + '"',
251 filterStatement
= ' WHERE ',
252 sortStatement
= ' ORDER BY ',
254 sorters
, filters
, placeholder
, i
, len
, result
, filter
, sorter
, property
, operator
, value
;
256 sql
+= filterStatement
+ '"' + idProperty
+ '" = ?';
259 filters
= params
.filters
;
260 len
= filters
&& filters
.length
;
262 for (i
= 0; i
< len
; i
++) {
264 property
= filter
.getProperty();
265 value
= me
.toSqlValue(filter
.getValue(), Model
.getField(property
));
266 operator
= filter
.getOperator();
267 if (property
!== null) {
268 operator
= operator
|| '=';
270 if (operator
=== 'like' || (operator
=== '=' && filter
.getAnyMatch())) {
272 value
= '%' + value
+ '%';
274 if (operator
=== 'in' || operator
=== 'notin') {
275 if (operator
=== 'notin') {
278 placeholder
= '(' + Ext
.String
.repeat('?', value
.length
, ',') + ')';
279 values
= values
.concat(value
);
283 sql
+= filterStatement
+ '"' + property
+ '" ' + operator
+ ' ' + placeholder
;
284 filterStatement
= ' AND ';
288 sorters
= params
.sorters
;
289 len
= sorters
&& sorters
.length
;
291 for (i
= 0; i
< len
; i
++) {
293 property
= sorter
.getProperty();
294 if (property
!== null) {
295 sql
+= sortStatement
+ '"' + property
+ '" ' + sorter
.getDirection();
296 sortStatement
= ', ';
300 // handle start, limit, sort, filter and group params
301 if (params
.page
!== undefined) {
302 sql
+= ' LIMIT ' + parseInt(params
.start
, 10) + ', ' + parseInt(params
.limit
, 10);
305 me
.executeStatement(transaction
, sql
, values
, function(transaction
, resultSet
) {
306 var rows
= resultSet
.rows
,
309 fields
= Model
.fields
,
310 fieldsLen
= fields
.length
,
311 raw
, data
, i
, len
, j
, field
, name
;
312 for (i
= 0 , len
= count
; i
< len
; ++i
) {
315 for (j
= 0; j
< fieldsLen
; ++j
) {
318 data
[name
] = me
.fromSqlValue(raw
[name
], field
);
320 records
.push(new Model(data
));
322 callback
.call(me
, new Ext
.data
.ResultSet({
328 }, function(transaction
, error
) {
329 callback
.call(me
, new Ext
.data
.ResultSet({
336 updateRecords: function(transaction
, records
, callback
) {
338 columns
= me
.columns
,
339 quotedColumns
= me
.quotedColumns
,
340 totalRecords
= records
.length
,
343 setOptions
= me
._createOptions
,
344 len
, i
, record
, placeholders
, sql
, data
, values
, errors
, completeIf
;
345 completeIf = function(transaction
) {
347 if (executed
=== totalRecords
) {
348 callback
.call(me
, new Ext
.data
.ResultSet({
353 for (i
= 0 , len
= quotedColumns
.length
; i
< len
; i
++) {
354 updates
.push(quotedColumns
[i
] + ' = ?');
356 sql
= 'UPDATE "' + me
.getTable() + '" SET ' + updates
.join(', ') + ' WHERE "' + me
.getModel().idProperty
+ '" = ?';
357 for (i
= 0 , len
= records
.length
; i
< len
; ++i
) {
359 data
= me
.getRecordData(record
);
360 values
= me
.getColumnValues(columns
, data
);
361 values
.push(record
.getId());
362 // Capture the record in closure scope so we can access it later
364 me
.executeStatement(transaction
, sql
, values
, function(transaction
, resultSet
) {
366 }, function(transaction
, error
) {
376 destroyRecords: function(transaction
, records
, callback
) {
378 table
= me
.getTable(),
379 idProperty
= me
.getModel().idProperty
,
382 destroyedRecords
= [],
383 len
= records
.length
,
384 idStr
= '"' + idProperty
+ '" = ?',
385 i
, result
, record
, sql
;
386 for (i
= 0; i
< len
; i
++) {
388 values
.push(records
[i
].getId());
390 sql
= 'DELETE FROM "' + me
.getTable() + '" WHERE ' + ids
.join(' OR ');
391 me
.executeStatement(transaction
, sql
, values
, function(transaction
, resultSet
) {
392 callback
.call(me
, new Ext
.data
.ResultSet({
395 }, function(transaction
, error
) {
396 callback
.call(me
, new Ext
.data
.ResultSet({
402 * Formats the data for each record before sending it to the server. This
403 * method should be overridden to format the data in a way that differs from the default.
404 * @param {Object} record The record that we are writing to the server.
405 * @return {Object} An object literal of name/value keys to be written to the server.
406 * By default this method returns the data property on the record.
408 getRecordData: function(record
) {
410 fields
= record
.fields
,
411 idProperty
= record
.idProperty
,
412 uniqueIdStrategy
= me
.uniqueIdStrategy
,
415 recordData
= record
.data
,
416 i
, name
, value
, field
;
417 for (i
= 0; i
< len
; ++i
) {
419 if (field
.persist
!== false) {
421 if (name
=== idProperty
&& !uniqueIdStrategy
) {
425 data
[name
] = me
.toSqlValue(recordData
[name
], field
);
430 getColumnValues: function(columns
, data
) {
431 var len
= columns
.length
,
434 for (i
= 0; i
< len
; i
++) {
436 value
= data
[column
];
437 if (value
!== undefined) {
443 getSchemaString: function() {
446 model
= me
.getModel(),
447 idProperty
= model
.idProperty
,
448 fields
= model
.fields
,
449 uniqueIdStrategy
= me
.uniqueIdStrategy
,
451 i
, field
, type
, name
;
452 for (i
= 0; i
< len
; i
++) {
454 type
= field
.getType();
456 if (name
=== idProperty
) {
457 if (uniqueIdStrategy
) {
458 type
= me
.convertToSqlType(type
);
459 schema
.unshift('"' + idProperty
+ '" ' + type
);
461 schema
.unshift('"' + idProperty
+ '" INTEGER PRIMARY KEY AUTOINCREMENT');
464 type
= me
.convertToSqlType(type
);
465 schema
.push('"' + name
+ '" ' + type
);
468 return schema
.join(', ');
470 convertToSqlType: function(type
) {
471 switch (type
.toLowerCase()) {
484 dropTable: function() {
486 me
.executeTransaction(function(transaction
) {
487 me
.executeStatement(transaction
, 'DROP TABLE "' + me
.getTable() + '"', function() {
488 me
.tableExists
= false;
490 }, null, null, false);
492 getDatabaseObject: function() {
493 return window
.openDatabase(this.getDatabase(), '1.0', 'Sencha Database', 5 * 1024 * 1024);
496 executeStatement: function(transaction
, sql
, values
, success
, failure
) {
498 transaction
.executeSql(sql
, values
, success
? function() {
499 success
.apply(me
, arguments
);
500 } : null, failure
? function() {
501 failure
.apply(me
, arguments
);
504 executeTransaction: function(runner
, failure
, success
, autoCreateTable
) {
506 autoCreateTable
= autoCreateTable
!== false;
507 me
.getDatabaseObject().transaction(runner
? function(transaction
) {
508 if (autoCreateTable
&& !me
.tableExists
) {
509 me
.createTable(transaction
);
511 runner
.apply(me
, arguments
);
512 } : null, failure
? function() {
513 failure
.apply(me
, arguments
);
514 } : null, success
? function() {
515 success
.apply(me
, arguments
);
518 fromSqlValue: function(value
, field
) {
519 if (field
.isDateField
) {
520 value
= value
? new Date(value
) : null;
521 } else if (field
.isBooleanField
) {
526 getPersistedModelColumns: function(model
) {
527 var fields
= model
.fields
,
528 uniqueIdStrategy
= this.uniqueIdStrategy
,
529 idProperty
= model
.idProperty
,
533 for (i
= 0; i
< len
; ++i
) {
536 if (name
=== idProperty
&& !uniqueIdStrategy
) {
540 if (field
.persist
!== false) {
541 columns
.push(field
.name
);
546 toSqlValue: function(value
, field
) {
547 if (field
.isDateField
) {
548 value
= value
? value
.getTime() : null;
549 } else if (field
.isBooleanField
) {
550 value
= value
? 1 : 0;