Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

SQLitePlugin.js 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. cordova.define("cordova-sqlite-storage.SQLitePlugin", function(require, exports, module) {
  2. (function() {
  3. var DB_STATE_INIT, DB_STATE_OPEN, READ_ONLY_REGEX, SQLiteFactory, SQLitePlugin, SQLitePluginTransaction, SelfTest, argsArray, dblocations, iosLocationMap, newSQLError, nextTick, root, txLocks;
  4. root = this;
  5. READ_ONLY_REGEX = /^(\s|;)*(?:alter|create|delete|drop|insert|reindex|replace|update)/i;
  6. DB_STATE_INIT = "INIT";
  7. DB_STATE_OPEN = "OPEN";
  8. txLocks = {};
  9. newSQLError = function(error, code) {
  10. var sqlError;
  11. sqlError = error;
  12. if (!code) {
  13. code = 0;
  14. }
  15. if (!sqlError) {
  16. sqlError = new Error("a plugin had an error but provided no response");
  17. sqlError.code = code;
  18. }
  19. if (typeof sqlError === "string") {
  20. sqlError = new Error(error);
  21. sqlError.code = code;
  22. }
  23. if (!sqlError.code && sqlError.message) {
  24. sqlError.code = code;
  25. }
  26. if (!sqlError.code && !sqlError.message) {
  27. sqlError = new Error("an unknown error was returned: " + JSON.stringify(sqlError));
  28. sqlError.code = code;
  29. }
  30. return sqlError;
  31. };
  32. nextTick = window.setImmediate || function(fun) {
  33. window.setTimeout(fun, 0);
  34. };
  35. /*
  36. Utility that avoids leaking the arguments object. See
  37. https://www.npmjs.org/package/argsarray
  38. */
  39. argsArray = function(fun) {
  40. return function() {
  41. var args, i, len;
  42. len = arguments.length;
  43. if (len) {
  44. args = [];
  45. i = -1;
  46. while (++i < len) {
  47. args[i] = arguments[i];
  48. }
  49. return fun.call(this, args);
  50. } else {
  51. return fun.call(this, []);
  52. }
  53. };
  54. };
  55. SQLitePlugin = function(openargs, openSuccess, openError) {
  56. var dbname;
  57. if (!(openargs && openargs['name'])) {
  58. throw newSQLError("Cannot create a SQLitePlugin db instance without a db name");
  59. }
  60. dbname = openargs.name;
  61. if (typeof dbname !== 'string') {
  62. throw newSQLError('sqlite plugin database name must be a string');
  63. }
  64. this.openargs = openargs;
  65. this.dbname = dbname;
  66. this.openSuccess = openSuccess;
  67. this.openError = openError;
  68. this.openSuccess || (this.openSuccess = function() {
  69. console.log("DB opened: " + dbname);
  70. });
  71. this.openError || (this.openError = function(e) {
  72. console.log(e.message);
  73. });
  74. this.open(this.openSuccess, this.openError);
  75. };
  76. SQLitePlugin.prototype.databaseFeatures = {
  77. isSQLitePluginDatabase: true
  78. };
  79. SQLitePlugin.prototype.openDBs = {};
  80. SQLitePlugin.prototype.addTransaction = function(t) {
  81. if (!txLocks[this.dbname]) {
  82. txLocks[this.dbname] = {
  83. queue: [],
  84. inProgress: false
  85. };
  86. }
  87. txLocks[this.dbname].queue.push(t);
  88. if (this.dbname in this.openDBs && this.openDBs[this.dbname] !== DB_STATE_INIT) {
  89. this.startNextTransaction();
  90. } else {
  91. if (this.dbname in this.openDBs) {
  92. console.log('new transaction is queued, waiting for open operation to finish');
  93. } else {
  94. console.log('database is closed, new transaction is [stuck] waiting until db is opened again!');
  95. }
  96. }
  97. };
  98. SQLitePlugin.prototype.transaction = function(fn, error, success) {
  99. if (!this.openDBs[this.dbname]) {
  100. error(newSQLError('database not open'));
  101. return;
  102. }
  103. this.addTransaction(new SQLitePluginTransaction(this, fn, error, success, true, false));
  104. };
  105. SQLitePlugin.prototype.readTransaction = function(fn, error, success) {
  106. if (!this.openDBs[this.dbname]) {
  107. error(newSQLError('database not open'));
  108. return;
  109. }
  110. this.addTransaction(new SQLitePluginTransaction(this, fn, error, success, false, true));
  111. };
  112. SQLitePlugin.prototype.startNextTransaction = function() {
  113. var self;
  114. self = this;
  115. nextTick((function(_this) {
  116. return function() {
  117. var txLock;
  118. if (!(_this.dbname in _this.openDBs) || _this.openDBs[_this.dbname] !== DB_STATE_OPEN) {
  119. console.log('cannot start next transaction: database not open');
  120. return;
  121. }
  122. txLock = txLocks[self.dbname];
  123. if (!txLock) {
  124. console.log('cannot start next transaction: database connection is lost');
  125. return;
  126. } else if (txLock.queue.length > 0 && !txLock.inProgress) {
  127. txLock.inProgress = true;
  128. txLock.queue.shift().start();
  129. }
  130. };
  131. })(this));
  132. };
  133. SQLitePlugin.prototype.abortAllPendingTransactions = function() {
  134. var j, len1, ref, tx, txLock;
  135. txLock = txLocks[this.dbname];
  136. if (!!txLock && txLock.queue.length > 0) {
  137. ref = txLock.queue;
  138. for (j = 0, len1 = ref.length; j < len1; j++) {
  139. tx = ref[j];
  140. tx.abortFromQ(newSQLError('Invalid database handle'));
  141. }
  142. txLock.queue = [];
  143. txLock.inProgress = false;
  144. }
  145. };
  146. SQLitePlugin.prototype.open = function(success, error) {
  147. var openerrorcb, opensuccesscb, step2;
  148. if (this.dbname in this.openDBs) {
  149. console.log('database already open: ' + this.dbname);
  150. nextTick((function(_this) {
  151. return function() {
  152. success(_this);
  153. };
  154. })(this));
  155. } else {
  156. console.log('OPEN database: ' + this.dbname);
  157. opensuccesscb = (function(_this) {
  158. return function() {
  159. var txLock;
  160. console.log('OPEN database: ' + _this.dbname + ' - OK');
  161. if (!_this.openDBs[_this.dbname]) {
  162. console.log('database was closed during open operation');
  163. }
  164. if (_this.dbname in _this.openDBs) {
  165. _this.openDBs[_this.dbname] = DB_STATE_OPEN;
  166. }
  167. if (!!success) {
  168. success(_this);
  169. }
  170. txLock = txLocks[_this.dbname];
  171. if (!!txLock && txLock.queue.length > 0 && !txLock.inProgress) {
  172. _this.startNextTransaction();
  173. }
  174. };
  175. })(this);
  176. openerrorcb = (function(_this) {
  177. return function() {
  178. console.log('OPEN database: ' + _this.dbname + ' FAILED, aborting any pending transactions');
  179. if (!!error) {
  180. error(newSQLError('Could not open database'));
  181. }
  182. delete _this.openDBs[_this.dbname];
  183. _this.abortAllPendingTransactions();
  184. };
  185. })(this);
  186. this.openDBs[this.dbname] = DB_STATE_INIT;
  187. step2 = (function(_this) {
  188. return function() {
  189. cordova.exec(opensuccesscb, openerrorcb, "SQLitePlugin", "open", [_this.openargs]);
  190. };
  191. })(this);
  192. cordova.exec(step2, step2, 'SQLitePlugin', 'close', [
  193. {
  194. path: this.dbname
  195. }
  196. ]);
  197. }
  198. };
  199. SQLitePlugin.prototype.close = function(success, error) {
  200. if (this.dbname in this.openDBs) {
  201. if (txLocks[this.dbname] && txLocks[this.dbname].inProgress) {
  202. console.log('cannot close: transaction is in progress');
  203. error(newSQLError('database cannot be closed while a transaction is in progress'));
  204. return;
  205. }
  206. console.log('CLOSE database: ' + this.dbname);
  207. delete this.openDBs[this.dbname];
  208. if (txLocks[this.dbname]) {
  209. console.log('closing db with transaction queue length: ' + txLocks[this.dbname].queue.length);
  210. } else {
  211. console.log('closing db with no transaction lock state');
  212. }
  213. cordova.exec(success, error, "SQLitePlugin", "close", [
  214. {
  215. path: this.dbname
  216. }
  217. ]);
  218. } else {
  219. console.log('cannot close: database is not open');
  220. if (error) {
  221. nextTick(function() {
  222. return error();
  223. });
  224. }
  225. }
  226. };
  227. SQLitePlugin.prototype.executeSql = function(statement, params, success, error) {
  228. var myerror, myfn, mysuccess;
  229. mysuccess = function(t, r) {
  230. if (!!success) {
  231. return success(r);
  232. }
  233. };
  234. myerror = function(t, e) {
  235. if (!!error) {
  236. return error(e);
  237. }
  238. };
  239. myfn = function(tx) {
  240. tx.addStatement(statement, params, mysuccess, myerror);
  241. };
  242. this.addTransaction(new SQLitePluginTransaction(this, myfn, null, null, false, false));
  243. };
  244. SQLitePlugin.prototype.sqlBatch = function(sqlStatements, success, error) {
  245. var batchList, j, len1, myfn, st;
  246. if (!sqlStatements || sqlStatements.constructor !== Array) {
  247. throw newSQLError('sqlBatch expects an array');
  248. }
  249. batchList = [];
  250. for (j = 0, len1 = sqlStatements.length; j < len1; j++) {
  251. st = sqlStatements[j];
  252. if (st.constructor === Array) {
  253. if (st.length === 0) {
  254. throw newSQLError('sqlBatch array element of zero (0) length');
  255. }
  256. batchList.push({
  257. sql: st[0],
  258. params: st.length === 0 ? [] : st[1]
  259. });
  260. } else {
  261. batchList.push({
  262. sql: st,
  263. params: []
  264. });
  265. }
  266. }
  267. myfn = function(tx) {
  268. var elem, k, len2, results;
  269. results = [];
  270. for (k = 0, len2 = batchList.length; k < len2; k++) {
  271. elem = batchList[k];
  272. results.push(tx.addStatement(elem.sql, elem.params, null, null));
  273. }
  274. return results;
  275. };
  276. this.addTransaction(new SQLitePluginTransaction(this, myfn, error, success, true, false));
  277. };
  278. SQLitePluginTransaction = function(db, fn, error, success, txlock, readOnly) {
  279. if (typeof fn !== "function") {
  280. /*
  281. This is consistent with the implementation in Chrome -- it
  282. throws if you pass anything other than a function. This also
  283. prevents us from stalling our txQueue if somebody passes a
  284. false value for fn.
  285. */
  286. throw newSQLError("transaction expected a function");
  287. }
  288. this.db = db;
  289. this.fn = fn;
  290. this.error = error;
  291. this.success = success;
  292. this.txlock = txlock;
  293. this.readOnly = readOnly;
  294. this.executes = [];
  295. if (txlock) {
  296. this.addStatement("BEGIN", [], null, function(tx, err) {
  297. throw newSQLError("unable to begin transaction: " + err.message, err.code);
  298. });
  299. } else {
  300. this.addStatement("SELECT 1", [], null, null);
  301. }
  302. };
  303. SQLitePluginTransaction.prototype.start = function() {
  304. var err;
  305. try {
  306. this.fn(this);
  307. this.run();
  308. } catch (error1) {
  309. err = error1;
  310. txLocks[this.db.dbname].inProgress = false;
  311. this.db.startNextTransaction();
  312. if (this.error) {
  313. this.error(newSQLError(err));
  314. }
  315. }
  316. };
  317. SQLitePluginTransaction.prototype.executeSql = function(sql, values, success, error) {
  318. if (this.finalized) {
  319. throw {
  320. message: 'InvalidStateError: DOM Exception 11: This transaction is already finalized. Transactions are committed after its success or failure handlers are called. If you are using a Promise to handle callbacks, be aware that implementations following the A+ standard adhere to run-to-completion semantics and so Promise resolution occurs on a subsequent tick and therefore after the transaction commits.',
  321. code: 11
  322. };
  323. return;
  324. }
  325. if (this.readOnly && READ_ONLY_REGEX.test(sql)) {
  326. this.handleStatementFailure(error, {
  327. message: 'invalid sql for a read-only transaction'
  328. });
  329. return;
  330. }
  331. this.addStatement(sql, values, success, error);
  332. };
  333. SQLitePluginTransaction.prototype.addStatement = function(sql, values, success, error) {
  334. var j, len1, params, sqlStatement, t, v;
  335. sqlStatement = typeof sql === 'string' ? sql : sql.toString();
  336. params = [];
  337. if (!!values && values.constructor === Array) {
  338. for (j = 0, len1 = values.length; j < len1; j++) {
  339. v = values[j];
  340. t = typeof v;
  341. params.push((v === null || v === void 0 ? null : t === 'number' || t === 'string' ? v : v.toString()));
  342. }
  343. }
  344. this.executes.push({
  345. success: success,
  346. error: error,
  347. sql: sqlStatement,
  348. params: params
  349. });
  350. };
  351. SQLitePluginTransaction.prototype.handleStatementSuccess = function(handler, response) {
  352. var payload, rows;
  353. if (!handler) {
  354. return;
  355. }
  356. rows = response.rows || [];
  357. payload = {
  358. rows: {
  359. item: function(i) {
  360. return rows[i];
  361. },
  362. length: rows.length
  363. },
  364. rowsAffected: response.rowsAffected || 0,
  365. insertId: response.insertId || void 0
  366. };
  367. handler(this, payload);
  368. };
  369. SQLitePluginTransaction.prototype.handleStatementFailure = function(handler, response) {
  370. if (!handler) {
  371. throw newSQLError("a statement with no error handler failed: " + response.message, response.code);
  372. }
  373. if (handler(this, response) !== false) {
  374. throw newSQLError("a statement error callback did not return false: " + response.message, response.code);
  375. }
  376. };
  377. SQLitePluginTransaction.prototype.run = function() {
  378. var batchExecutes, handlerFor, i, mycb, mycbmap, request, tropts, tx, txFailure, waiting;
  379. txFailure = null;
  380. tropts = [];
  381. batchExecutes = this.executes;
  382. waiting = batchExecutes.length;
  383. this.executes = [];
  384. tx = this;
  385. handlerFor = function(index, didSucceed) {
  386. return function(response) {
  387. var err;
  388. if (!txFailure) {
  389. try {
  390. if (didSucceed) {
  391. tx.handleStatementSuccess(batchExecutes[index].success, response);
  392. } else {
  393. tx.handleStatementFailure(batchExecutes[index].error, newSQLError(response));
  394. }
  395. } catch (error1) {
  396. err = error1;
  397. txFailure = newSQLError(err);
  398. }
  399. }
  400. if (--waiting === 0) {
  401. if (txFailure) {
  402. tx.executes = [];
  403. tx.abort(txFailure);
  404. } else if (tx.executes.length > 0) {
  405. tx.run();
  406. } else {
  407. tx.finish();
  408. }
  409. }
  410. };
  411. };
  412. mycbmap = {};
  413. i = 0;
  414. while (i < batchExecutes.length) {
  415. request = batchExecutes[i];
  416. mycbmap[i] = {
  417. success: handlerFor(i, true),
  418. error: handlerFor(i, false)
  419. };
  420. tropts.push({
  421. sql: request.sql,
  422. params: request.params
  423. });
  424. i++;
  425. }
  426. mycb = function(result) {
  427. var j, q, r, ref, res, resultIndex, type;
  428. for (resultIndex = j = 0, ref = result.length - 1; 0 <= ref ? j <= ref : j >= ref; resultIndex = 0 <= ref ? ++j : --j) {
  429. r = result[resultIndex];
  430. type = r.type;
  431. res = r.result;
  432. q = mycbmap[resultIndex];
  433. if (q) {
  434. if (q[type]) {
  435. q[type](res);
  436. }
  437. }
  438. }
  439. };
  440. cordova.exec(mycb, null, "SQLitePlugin", "backgroundExecuteSqlBatch", [
  441. {
  442. dbargs: {
  443. dbname: this.db.dbname
  444. },
  445. executes: tropts
  446. }
  447. ]);
  448. };
  449. SQLitePluginTransaction.prototype.abort = function(txFailure) {
  450. var failed, succeeded, tx;
  451. if (this.finalized) {
  452. return;
  453. }
  454. tx = this;
  455. succeeded = function(tx) {
  456. txLocks[tx.db.dbname].inProgress = false;
  457. tx.db.startNextTransaction();
  458. if (tx.error && typeof tx.error === 'function') {
  459. tx.error(txFailure);
  460. }
  461. };
  462. failed = function(tx, err) {
  463. txLocks[tx.db.dbname].inProgress = false;
  464. tx.db.startNextTransaction();
  465. if (tx.error && typeof tx.error === 'function') {
  466. tx.error(newSQLError('error while trying to roll back: ' + err.message, err.code));
  467. }
  468. };
  469. this.finalized = true;
  470. if (this.txlock) {
  471. this.addStatement("ROLLBACK", [], succeeded, failed);
  472. this.run();
  473. } else {
  474. succeeded(tx);
  475. }
  476. };
  477. SQLitePluginTransaction.prototype.finish = function() {
  478. var failed, succeeded, tx;
  479. if (this.finalized) {
  480. return;
  481. }
  482. tx = this;
  483. succeeded = function(tx) {
  484. txLocks[tx.db.dbname].inProgress = false;
  485. tx.db.startNextTransaction();
  486. if (tx.success && typeof tx.success === 'function') {
  487. tx.success();
  488. }
  489. };
  490. failed = function(tx, err) {
  491. txLocks[tx.db.dbname].inProgress = false;
  492. tx.db.startNextTransaction();
  493. if (tx.error && typeof tx.error === 'function') {
  494. tx.error(newSQLError('error while trying to commit: ' + err.message, err.code));
  495. }
  496. };
  497. this.finalized = true;
  498. if (this.txlock) {
  499. this.addStatement("COMMIT", [], succeeded, failed);
  500. this.run();
  501. } else {
  502. succeeded(tx);
  503. }
  504. };
  505. SQLitePluginTransaction.prototype.abortFromQ = function(sqlerror) {
  506. if (this.error) {
  507. this.error(sqlerror);
  508. }
  509. };
  510. dblocations = ["docs", "libs", "nosync"];
  511. iosLocationMap = {
  512. 'default': 'nosync',
  513. 'Documents': 'docs',
  514. 'Library': 'libs'
  515. };
  516. SQLiteFactory = {
  517. /*
  518. NOTE: this function should NOT be translated from Javascript
  519. back to CoffeeScript by js2coffee.
  520. If this function is edited in Javascript then someone will
  521. have to translate it back to CoffeeScript by hand.
  522. */
  523. openDatabase: argsArray(function(args) {
  524. var dblocation, errorcb, okcb, openargs;
  525. if (args.length < 1 || !args[0]) {
  526. throw newSQLError('Sorry missing mandatory open arguments object in openDatabase call');
  527. }
  528. if (args[0].constructor === String) {
  529. throw newSQLError('Sorry first openDatabase argument must be an object');
  530. }
  531. openargs = args[0];
  532. if (!openargs.name) {
  533. throw newSQLError('Database name value is missing in openDatabase call');
  534. }
  535. if (!openargs.iosDatabaseLocation && !openargs.location && openargs.location !== 0) {
  536. throw newSQLError('Database location or iosDatabaseLocation setting is now mandatory in openDatabase call.');
  537. }
  538. if (!!openargs.location && !!openargs.iosDatabaseLocation) {
  539. throw newSQLError('AMBIGUOUS: both location and iosDatabaseLocation settings are present in openDatabase call. Please use either setting, not both.');
  540. }
  541. dblocation = !!openargs.location && openargs.location === 'default' ? iosLocationMap['default'] : !!openargs.iosDatabaseLocation ? iosLocationMap[openargs.iosDatabaseLocation] : dblocations[openargs.location];
  542. if (!dblocation) {
  543. throw newSQLError('Valid iOS database location could not be determined in openDatabase call');
  544. }
  545. openargs.dblocation = dblocation;
  546. if (!!openargs.createFromLocation && openargs.createFromLocation === 1) {
  547. openargs.createFromResource = "1";
  548. }
  549. if (!!openargs.androidDatabaseProvider && !!openargs.androidDatabaseImplementation) {
  550. throw newSQLError('AMBIGUOUS: both androidDatabaseProvider and deprecated androidDatabaseImplementation settings are present in openDatabase call. Please drop androidDatabaseImplementation in favor of androidDatabaseProvider.');
  551. }
  552. if (openargs.androidDatabaseProvider !== void 0 && openargs.androidDatabaseProvider !== 'default' && openargs.androidDatabaseProvider !== 'system') {
  553. throw newSQLError("Incorrect androidDatabaseProvider value. Valid values are: 'default', 'system'");
  554. }
  555. if (!!openargs.androidDatabaseProvider && openargs.androidDatabaseProvider === 'system') {
  556. openargs.androidOldDatabaseImplementation = 1;
  557. }
  558. if (!!openargs.androidDatabaseImplementation && openargs.androidDatabaseImplementation === 2) {
  559. openargs.androidOldDatabaseImplementation = 1;
  560. }
  561. if (!!openargs.androidLockWorkaround && openargs.androidLockWorkaround === 1) {
  562. openargs.androidBugWorkaround = 1;
  563. }
  564. okcb = null;
  565. errorcb = null;
  566. if (args.length >= 2) {
  567. okcb = args[1];
  568. if (args.length > 2) {
  569. errorcb = args[2];
  570. }
  571. }
  572. return new SQLitePlugin(openargs, okcb, errorcb);
  573. }),
  574. deleteDatabase: function(first, success, error) {
  575. var args, dblocation, dbname;
  576. args = {};
  577. if (first.constructor === String) {
  578. throw newSQLError('Sorry first deleteDatabase argument must be an object');
  579. } else {
  580. if (!(first && first['name'])) {
  581. throw new Error("Please specify db name");
  582. }
  583. dbname = first.name;
  584. if (typeof dbname !== 'string') {
  585. throw newSQLError('delete database name must be a string');
  586. }
  587. args.path = dbname;
  588. }
  589. if (!first.iosDatabaseLocation && !first.location && first.location !== 0) {
  590. throw newSQLError('Database location or iosDatabaseLocation setting is now mandatory in deleteDatabase call.');
  591. }
  592. if (!!first.location && !!first.iosDatabaseLocation) {
  593. throw newSQLError('AMBIGUOUS: both location and iosDatabaseLocation settings are present in deleteDatabase call. Please use either setting value, not both.');
  594. }
  595. dblocation = !!first.location && first.location === 'default' ? iosLocationMap['default'] : !!first.iosDatabaseLocation ? iosLocationMap[first.iosDatabaseLocation] : dblocations[first.location];
  596. if (!dblocation) {
  597. throw newSQLError('Valid iOS database location could not be determined in deleteDatabase call');
  598. }
  599. args.dblocation = dblocation;
  600. delete SQLitePlugin.prototype.openDBs[args.path];
  601. return cordova.exec(success, error, "SQLitePlugin", "delete", [args]);
  602. }
  603. };
  604. SelfTest = {
  605. DBNAME: '___$$$___litehelpers___$$$___test___$$$___.db',
  606. start: function(successcb, errorcb) {
  607. SQLiteFactory.deleteDatabase({
  608. name: SelfTest.DBNAME,
  609. location: 'default'
  610. }, (function() {
  611. return SelfTest.step1(successcb, errorcb);
  612. }), (function() {
  613. return SelfTest.step1(successcb, errorcb);
  614. }));
  615. },
  616. step1: function(successcb, errorcb) {
  617. SQLiteFactory.openDatabase({
  618. name: SelfTest.DBNAME,
  619. location: 'default'
  620. }, function(db) {
  621. var check1;
  622. check1 = false;
  623. db.transaction(function(tx) {
  624. tx.executeSql('SELECT UPPER("Test") AS upperText', [], function(ignored, resutSet) {
  625. if (!resutSet.rows) {
  626. return SelfTest.finishWithError(errorcb, 'Missing resutSet.rows');
  627. }
  628. if (!resutSet.rows.length) {
  629. return SelfTest.finishWithError(errorcb, 'Missing resutSet.rows.length');
  630. }
  631. if (resutSet.rows.length !== 1) {
  632. return SelfTest.finishWithError(errorcb, "Incorrect resutSet.rows.length value: " + resutSet.rows.length + " (expected: 1)");
  633. }
  634. if (!resutSet.rows.item(0).upperText) {
  635. return SelfTest.finishWithError(errorcb, 'Missing resutSet.rows.item(0).upperText');
  636. }
  637. if (resutSet.rows.item(0).upperText !== 'TEST') {
  638. return SelfTest.finishWithError(errorcb, "Incorrect resutSet.rows.item(0).upperText value: " + (resutSet.rows.item(0).upperText) + " (expected: 'TEST')");
  639. }
  640. check1 = true;
  641. }, function(ignored, tx_sql_err) {
  642. return SelfTest.finishWithError(errorcb, "TX SQL error: " + tx_sql_err);
  643. });
  644. }, function(tx_err) {
  645. return SelfTest.finishWithError(errorcb, "TRANSACTION error: " + tx_err);
  646. }, function() {
  647. if (!check1) {
  648. return SelfTest.finishWithError(errorcb, 'Did not get expected upperText result data');
  649. }
  650. db.executeSql('BEGIN', null, function(ignored) {
  651. return nextTick(function() {
  652. delete db.openDBs[SelfTest.DBNAME];
  653. delete txLocks[SelfTest.DBNAME];
  654. nextTick(function() {
  655. db.transaction(function(tx2) {
  656. tx2.executeSql('SELECT 1');
  657. }, function(tx_err) {
  658. if (!tx_err) {
  659. return SelfTest.finishWithError(errorcb, 'Missing error object');
  660. }
  661. SelfTest.step2(successcb, errorcb);
  662. }, function() {
  663. return SelfTest.finishWithError(errorcb, 'Missing error object');
  664. });
  665. });
  666. });
  667. });
  668. });
  669. }, function(open_err) {
  670. return SelfTest.finishWithError(errorcb, "Open database error: " + open_err);
  671. });
  672. },
  673. step2: function(successcb, errorcb) {
  674. SQLiteFactory.openDatabase({
  675. name: SelfTest.DBNAME,
  676. location: 'default'
  677. }, function(db) {
  678. db.transaction(function(tx) {
  679. tx.executeSql('SELECT ? AS myResult', [null], function(ignored, resutSet) {
  680. if (!resutSet.rows) {
  681. return SelfTest.finishWithError(errorcb, 'Missing resutSet.rows');
  682. }
  683. if (!resutSet.rows.length) {
  684. return SelfTest.finishWithError(errorcb, 'Missing resutSet.rows.length');
  685. }
  686. if (resutSet.rows.length !== 1) {
  687. return SelfTest.finishWithError(errorcb, "Incorrect resutSet.rows.length value: " + resutSet.rows.length + " (expected: 1)");
  688. }
  689. SelfTest.step3(successcb, errorcb);
  690. });
  691. }, function(txError) {
  692. return SelfTest.finishWithError(errorcb, "UNEXPECTED TRANSACTION ERROR: " + txError);
  693. });
  694. }, function(open_err) {
  695. return SelfTest.finishWithError(errorcb, "Open database error: " + open_err);
  696. });
  697. },
  698. step3: function(successcb, errorcb) {
  699. SQLiteFactory.openDatabase({
  700. name: SelfTest.DBNAME,
  701. location: 'default'
  702. }, function(db) {
  703. return db.sqlBatch(['CREATE TABLE TestTable(id integer primary key autoincrement unique, data);', ['INSERT INTO TestTable (data) VALUES (?);', ['test-value']]], function() {
  704. var firstid;
  705. firstid = -1;
  706. return db.executeSql('SELECT id, data FROM TestTable', [], function(resutSet) {
  707. if (!resutSet.rows) {
  708. SelfTest.finishWithError(errorcb, 'Missing resutSet.rows');
  709. return;
  710. }
  711. if (!resutSet.rows.length) {
  712. SelfTest.finishWithError(errorcb, 'Missing resutSet.rows.length');
  713. return;
  714. }
  715. if (resutSet.rows.length !== 1) {
  716. SelfTest.finishWithError(errorcb, "Incorrect resutSet.rows.length value: " + resutSet.rows.length + " (expected: 1)");
  717. return;
  718. }
  719. if (resutSet.rows.item(0).id === void 0) {
  720. SelfTest.finishWithError(errorcb, 'Missing resutSet.rows.item(0).id');
  721. return;
  722. }
  723. firstid = resutSet.rows.item(0).id;
  724. if (!resutSet.rows.item(0).data) {
  725. SelfTest.finishWithError(errorcb, 'Missing resutSet.rows.item(0).data');
  726. return;
  727. }
  728. if (resutSet.rows.item(0).data !== 'test-value') {
  729. SelfTest.finishWithError(errorcb, "Incorrect resutSet.rows.item(0).data value: " + (resutSet.rows.item(0).data) + " (expected: 'test-value')");
  730. return;
  731. }
  732. return db.transaction(function(tx) {
  733. return tx.executeSql('UPDATE TestTable SET data = ?', ['new-value']);
  734. }, function(tx_err) {
  735. return SelfTest.finishWithError(errorcb, "UPDATE transaction error: " + tx_err);
  736. }, function() {
  737. var readTransactionFinished;
  738. readTransactionFinished = false;
  739. return db.readTransaction(function(tx2) {
  740. return tx2.executeSql('SELECT id, data FROM TestTable', [], function(ignored, resutSet2) {
  741. if (!resutSet2.rows) {
  742. throw newSQLError('Missing resutSet2.rows');
  743. }
  744. if (!resutSet2.rows.length) {
  745. throw newSQLError('Missing resutSet2.rows.length');
  746. }
  747. if (resutSet2.rows.length !== 1) {
  748. throw newSQLError("Incorrect resutSet2.rows.length value: " + resutSet2.rows.length + " (expected: 1)");
  749. }
  750. if (!resutSet2.rows.item(0).id) {
  751. throw newSQLError('Missing resutSet2.rows.item(0).id');
  752. }
  753. if (resutSet2.rows.item(0).id !== firstid) {
  754. throw newSQLError("resutSet2.rows.item(0).id value " + (resutSet2.rows.item(0).id) + " does not match previous primary key id value (" + firstid + ")");
  755. }
  756. if (!resutSet2.rows.item(0).data) {
  757. throw newSQLError('Missing resutSet2.rows.item(0).data');
  758. }
  759. if (resutSet2.rows.item(0).data !== 'new-value') {
  760. throw newSQLError("Incorrect resutSet2.rows.item(0).data value: " + (resutSet2.rows.item(0).data) + " (expected: 'test-value')");
  761. }
  762. return readTransactionFinished = true;
  763. });
  764. }, function(tx2_err) {
  765. return SelfTest.finishWithError(errorcb, "readTransaction error: " + tx2_err);
  766. }, function() {
  767. if (!readTransactionFinished) {
  768. SelfTest.finishWithError(errorcb, 'readTransaction did not finish');
  769. return;
  770. }
  771. return db.transaction(function(tx3) {
  772. tx3.executeSql('DELETE FROM TestTable');
  773. return tx3.executeSql('INSERT INTO TestTable (data) VALUES(?)', [123]);
  774. }, function(tx3_err) {
  775. return SelfTest.finishWithError(errorcb, "DELETE transaction error: " + tx3_err);
  776. }, function() {
  777. var secondReadTransactionFinished;
  778. secondReadTransactionFinished = false;
  779. return db.readTransaction(function(tx4) {
  780. return tx4.executeSql('SELECT id, data FROM TestTable', [], function(ignored, resutSet3) {
  781. if (!resutSet3.rows) {
  782. throw newSQLError('Missing resutSet3.rows');
  783. }
  784. if (!resutSet3.rows.length) {
  785. throw newSQLError('Missing resutSet3.rows.length');
  786. }
  787. if (resutSet3.rows.length !== 1) {
  788. throw newSQLError("Incorrect resutSet3.rows.length value: " + resutSet3.rows.length + " (expected: 1)");
  789. }
  790. if (!resutSet3.rows.item(0).id) {
  791. throw newSQLError('Missing resutSet3.rows.item(0).id');
  792. }
  793. if (resutSet3.rows.item(0).id === firstid) {
  794. throw newSQLError("resutSet3.rows.item(0).id value " + (resutSet3.rows.item(0).id) + " incorrectly matches previous unique key id value value (" + firstid + ")");
  795. }
  796. if (!resutSet3.rows.item(0).data) {
  797. throw newSQLError('Missing resutSet3.rows.item(0).data');
  798. }
  799. if (resutSet3.rows.item(0).data !== 123) {
  800. throw newSQLError("Incorrect resutSet3.rows.item(0).data value: " + (resutSet3.rows.item(0).data) + " (expected 123)");
  801. }
  802. return secondReadTransactionFinished = true;
  803. });
  804. }, function(tx4_err) {
  805. return SelfTest.finishWithError(errorcb, "second readTransaction error: " + tx4_err);
  806. }, function() {
  807. if (!secondReadTransactionFinished) {
  808. SelfTest.finishWithError(errorcb, 'second readTransaction did not finish');
  809. return;
  810. }
  811. db.close(function() {
  812. SelfTest.cleanupAndFinish(successcb, errorcb);
  813. }, function(close_err) {
  814. SelfTest.finishWithError(errorcb, "close error: " + close_err);
  815. });
  816. });
  817. });
  818. });
  819. });
  820. }, function(select_err) {
  821. return SelfTest.finishWithError(errorcb, "SELECT error: " + select_err);
  822. });
  823. }, function(batch_err) {
  824. return SelfTest.finishWithError(errorcb, "sql batch error: " + batch_err);
  825. });
  826. }, function(open_err) {
  827. return SelfTest.finishWithError(errorcb, "Open database error: " + open_err);
  828. });
  829. },
  830. cleanupAndFinish: function(successcb, errorcb) {
  831. SQLiteFactory.deleteDatabase({
  832. name: SelfTest.DBNAME,
  833. location: 'default'
  834. }, successcb, function(cleanup_err) {
  835. SelfTest.finishWithError(errorcb, "CLEANUP DELETE ERROR: " + cleanup_err);
  836. });
  837. },
  838. finishWithError: function(errorcb, message) {
  839. console.log("selfTest ERROR with message: " + message);
  840. SQLiteFactory.deleteDatabase({
  841. name: SelfTest.DBNAME,
  842. location: 'default'
  843. }, function() {
  844. errorcb(newSQLError(message));
  845. }, function(err2) {
  846. console.log("selfTest CLEANUP DELETE ERROR " + err2);
  847. errorcb(newSQLError("CLEANUP DELETE ERROR: " + err2 + " for error: " + message));
  848. });
  849. }
  850. };
  851. root.sqlitePlugin = {
  852. sqliteFeatures: {
  853. isSQLitePlugin: true
  854. },
  855. echoTest: function(okcb, errorcb) {
  856. var error, ok;
  857. ok = function(s) {
  858. if (s === 'test-string') {
  859. return okcb();
  860. } else {
  861. return errorcb("Mismatch: got: '" + s + "' expected 'test-string'");
  862. }
  863. };
  864. error = function(e) {
  865. return errorcb(e);
  866. };
  867. return cordova.exec(ok, error, "SQLitePlugin", "echoStringValue", [
  868. {
  869. value: 'test-string'
  870. }
  871. ]);
  872. },
  873. selfTest: SelfTest.start,
  874. openDatabase: SQLiteFactory.openDatabase,
  875. deleteDatabase: SQLiteFactory.deleteDatabase
  876. };
  877. }).call(this);
  878. });