]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/device/sqlite/Sencha.js
4 Ext
.define('Ext.device.sqlite.Sencha', {
6 * Returns a {@link Ext.device.sqlite.Database} instance.
7 * If the database with specified name does not exist, it will be created.
8 * If the creationCallback is provided,
9 * the database is created with the empty string as its version regardless of the specified version.
11 * @param {Object} config
12 * The object which contains the following config options:
14 * @param {String} config.name This is required.
15 * The name of the database to open.
17 * @param {String} config.version This is required.
18 * The version of the database to open.
20 * @param {String} config.displayName This is required.
21 * The display name of the database to open.
23 * @param {Number} config.estimatedSize This is required.
24 * The estimated size of the database to open.
26 * @param {Function} config.creationCallback This is optional.
27 * The callback to be called when the database has been created.
29 * @param {Ext.device.sqlite.Database} config.creationCallback.database
30 * The created database with the empty string as its version regardless of the specified version.
32 * @param {Object} config.scope This is optional.
35 * @return {Ext.device.sqlite.Database}
36 * The opened database, null if an error occured.
38 openDatabase: function(config
) {
39 if (config
.name
== null) {
40 Ext
.Logger
.error('Ext.device.SQLite#openDatabase: You must specify a `name` of the database.');
44 if (config
.version
== null) {
45 Ext
.Logger
.error('Ext.device.SQLite#openDatabase: You must specify a `version` of the database.');
49 if (config
.displayName
== null) {
50 Ext
.Logger
.error('Ext.device.SQLite#openDatabase: You must specify a `displayName` of the database.');
54 if (config
.estimatedSize
== null) {
55 Ext
.Logger
.error('Ext.device.SQLite#openDatabase: You must specify a `estimatedSize` of the database.');
61 var result
= Ext
.device
.Communicator
.send({
62 command
: 'SQLite#openDatabase',
65 version
: config
.version
,
66 displayName
: config
.displayName
,
67 estimatedSize
: config
.estimatedSize
,
69 // `creationCallback != null` is checked for internal logic in native plugin code
70 creationCallback
: !config
.creationCallback
? null : function() {
71 config
.creationCallback
.call(config
.scope
|| this, database
);
74 scope
: config
.scope
|| this
79 Ext
.Logger
.error(result
.error
);
83 database
= Ext
.create('Ext.device.sqlite.Database', result
.id
, result
.version
);
92 * The Database class which is used to perform transactions.
94 Ext
.define('Ext.device.sqlite.Database', {
98 constructor: function(id
, version
) {
100 this.version
= version
;
104 * Returns the current version of the database.
107 * The database current version.
109 getVersion: function() {
110 return Ext
.device
.Communicator
.send({
111 command
: 'SQLite#getVersion',
118 * Performs a {@link Ext.device.sqlite.SQLTransaction} instance in a read/write mode.
120 * @param {Object} config
121 * The object which contains the following config options:
123 * @param {Function} config.callback This is required.
124 * The callback to be called when the transaction has been created.
126 * @param {Ext.device.sqlite.SQLTransaction} config.callback.transaction
127 * The created transaction.
129 * @param {Function} config.success This is optional.
130 * The callback to be called when the transaction has been successfully commited.
132 * @param {Function} config.failure This is optional.
133 * The callback to be called when an error occurred and the transaction has been rolled back.
135 * @param {Object} config.failure.error
136 * The occurred error.
138 * @param {Object} config.scope
141 transaction: function(config
) {
142 if (!config
.callback
) {
143 Ext
.Logger
.error('Ext.device.sqlite.Database#transaction: You must specify a `callback` callback.');
148 Ext
.device
.Communicator
.send({
149 command
: 'SQLite#createTransaction',
151 readOnly
: config
.readOnly
,
153 success: function(id
) {
154 var exception
= null;
156 var transaction
= Ext
.create('Ext.device.sqlite.SQLTransaction', id
);
158 error
= Ext
.device
.Communicator
.send({
159 command
: 'SQLite#beginTransaction',
161 transactionId
: transaction
.id
164 if (!error
&& config
.preflight
) {
165 error
= config
.preflight
.call(config
.scope
|| this);
170 transaction
.active
= true;
171 config
.callback
.call(config
.scope
|| this, transaction
); // may throw exception
175 transaction
.active
= false;
179 var statements
= transaction
.statements
;
181 while (!(exception
|| error
) && statements
.length
> 0) {
182 var statement
= statements
.shift();
183 var result
= Ext
.device
.Communicator
.send({
184 command
: 'SQLite#executeStatement',
186 transactionId
: transaction
.id
,
189 sqlStatement
: statement
.sqlStatement
,
190 arguments
: JSON
.stringify(statement
.arguments
)
195 error
= result
.error
;
196 } else if (statement
.callback
) {
197 var resultSet
= Ext
.create('Ext.device.sqlite.SQLResultSet', result
);
200 transaction
.active
= true;
201 statement
.callback
.call(statement
.scope
|| this, transaction
, resultSet
); // may throw exception
205 transaction
.active
= false;
210 if (error
&& statement
.failure
) {
212 transaction
.active
= true;
213 if (!statement
.failure
.call(statement
.scope
|| this, transaction
, error
)) { // may throw exception
219 transaction
.active
= false;
224 if (!(exception
|| error
)) {
225 error
= Ext
.device
.Communicator
.send({
226 command
: 'SQLite#commitTransaction',
228 transactionId
: transaction
.id
232 if (config
.postflight
) {
233 config
.postflight
.call(config
.scope
|| this);
236 if (config
.success
) {
237 config
.success
.call(config
.scope
|| this);
242 if (exception
|| error
) {
243 statements
.splice(0, statements
.length
);
245 Ext
.device
.Communicator
.send({
246 command
: 'SQLite#rollbackTransaction',
248 transactionId
: transaction
.id
253 } else if (config
.failure
) {
254 config
.failure
.call(config
.scope
|| this, error
);
258 failure: function(error
) {
259 if (config
.failure
) {
260 config
.failure
.call(config
.scope
|| this, error
);
264 scope
: config
.scope
|| this
269 * Works the same way as {@link Ext.device.sqlite.Database#transaction},
270 * but performs a {@link Ext.device.sqlite.SQLTransaction} instance in a read-only mode.
272 readTransaction: function(config
) {
273 this.transaction(Ext
.apply(config
, {
279 * Verifies and changes the version of the database at the same time
280 * as doing a schema update with a {@link Ext.device.sqlite.SQLTransaction} instance.
282 * @param {Object} config
283 * The object which contains the following config options:
285 * @param {String} config.oldVersion This is required.
286 * The current version of the database.
288 * @param {String} config.newVersion This is required.
289 * The new version of the database.
291 * @param {Function} config.callback This is optional.
292 * The callback to be called when the transaction has been created.
294 * @param {Ext.device.sqlite.SQLTransaction} config.callback.transaction
295 * The created transaction.
297 * @param {Function} config.success This is optional.
298 * The callback to be called when the transaction has been successfully commited.
300 * @param {Function} config.failure This is optional.
301 * The callback to be called when an error occurred and the transaction has been rolled back.
303 * @param {Object} config.failure.error
304 * The occurred error.
306 * @param {Object} config.scope
309 changeVersion: function(config
) {
310 if (config
.oldVersion
== null) {
311 Ext
.Logger
.error('Ext.device.SQLite#changeVersion: You must specify an `oldVersion` of the database.');
315 if (config
.newVersion
== null) {
316 Ext
.Logger
.error('Ext.device.SQLite#changeVersion: You must specify a `newVersion` of the database.');
320 this.transaction(Ext
.apply(config
, {
321 preflight: function() {
322 return config
.oldVersion
== this.getVersion() ? null : 'Unable to change version: version mismatch';
324 postflight: function() {
325 var result
= Ext
.device
.Communicator
.send({
326 command
: 'SQLite#setVersion',
329 version
: config
.newVersion
333 this.version
= config
.newVersion
;
340 * The SQLTransaction class which is used to execute SQL statements.
342 Ext
.define('Ext.device.sqlite.SQLTransaction', {
347 constructor: function(id
) {
349 this.statements
= new Array();
353 * Executes an SQL statement.
355 * @param {Object} config
356 * The object which contains the following config options:
358 * @param {String} config.sqlStatement This is required.
359 * The SQL statement to execute.
361 * @param {Array} config.arguments This is optional.
362 * The arguments array to bind each '?' placeholder in the SQL statement.
364 * @param {Function} config.callback This is optional.
365 * The callback to be called when the SQL statement succeeded.
367 * @param {Ext.device.sqlite.SQLTransaction} config.callback.transaction
368 * The transaction of the SQL statement.
370 * @param {Ext.device.sqlite.SQLTransaction} config.callback.resultSet
371 * The result of the SQL statement.
373 * @param {Function} config.failure This is optional.
374 * The callback to be called when an error occurred.
375 * If the callback returns false, next SQL statement will be executed.
377 * @param {Ext.device.sqlite.SQLTransaction} config.failure.transaction
378 * The transaction of the SQL statement.
380 * @param {Object} config.failure.error
381 * The occurred error.
383 * @param {Object} config.scope
386 executeSql: function(config
) {
388 Ext
.Logger
.error('Ext.device.sqlite.SQLTransaction#executeSql: An attempt was made to use a SQLTransaction that is no longer usable.');
392 if (config
.sqlStatement
== null) {
393 Ext
.Logger
.error('Ext.device.sqlite.SQLTransaction#executeSql: You must specify a `sqlStatement` for the transaction.');
397 this.statements
.push({
398 sqlStatement
: config
.sqlStatement
,
399 arguments
: config
.arguments
,
400 callback
: config
.callback
,
401 failure
: config
.failure
,
407 * The SQLResultSet class which is used to represent SQL statements results.
409 Ext
.define('Ext.device.sqlite.SQLResultSet', {
414 constructor: function(data
) {
415 this.insertId
= data
.insertId
;
416 this.rowsAffected
= data
.rowsAffected
;
417 this.rows
= Ext
.create('Ext.device.sqlite.SQLResultSetRowList', data
);
421 * Returns the row ID of the last row that the SQL statement inserted into the database,
422 * if the statement inserted any rows.
423 * If the statement did not insert a row, throws an exception.
426 * The inserted row ID.
428 getInsertId: function() {
429 if (this.insertId
!= 0) {
430 return this.insertId
;
432 Ext
.Logger
.error('Ext.device.sqlite.SQLResultSet#getInsertId: An SQLTransaction did not insert a row.');
438 * Returns the number of rows that were changed by the SQL statement.
439 * If the statement did not change any rows, returns zero.
442 * The number of rows affected.
444 getRowsAffected: function() {
445 return this.rowsAffected
;
449 * Returns a {@link Ext.device.sqlite.SQLResultSetRowList} instance representing rows returned by the SQL statement.
451 * @return {Ext.device.sqlite.SQLResultSetRowList}
454 getRows: function() {
459 * The SQLResultSetRowList class which is used to represent rows returned by SQL statements.
461 Ext
.define('Ext.device.sqlite.SQLResultSetRowList', {
465 constructor: function(data
) {
466 this.names
= data
.names
;
467 this.rows
= data
.rows
;
471 * Returns the number of rows returned by the SQL statement.
474 * The number of rows.
476 getLength: function() {
477 return this.rows
.length
;
481 * Returns a row at specified index returned by the SQL statement.
482 * If there is no such row, returns null.
484 * @param {Number} index This is required.
485 * The index of a row.
490 item: function(index
) {
491 if (index
< this.getLength()) {
493 var row
= this.rows
[index
];
494 this.names
.forEach(function(name
, index
) {
495 item
[name
] = row
[index
];