Best JavaScript code snippet using wpt
wpOpenDatabase.js
Source:wpOpenDatabase.js
1var cordova = require('cordova');2var utils = {};3function UUIDcreatePart(length) {4 var uuidpart = "";5 for (var i=0; i<length; i++) {6 var uuidchar = parseInt((Math.random() * 256), 10).toString(16);7 if (uuidchar.length == 1) {8 uuidchar = "0" + uuidchar;9 }10 uuidpart += uuidchar;11 }12 return uuidpart;13}14/**15 * Create a UUID16 */17utils.createUUID = function() {18 return UUIDcreatePart(4) + '-' +19 UUIDcreatePart(2) + '-' +20 UUIDcreatePart(2) + '-' +21 UUIDcreatePart(2) + '-' +22 UUIDcreatePart(6);23};24/**25 * Callback from native code when query fails PRIVATE METHOD26 *27 * @param reason28 * Error message29 * @param id30 * Query id31 */32function failQuery(reason, id) {33 var query = openDatabase.queryQueue[id];34 if (query) {35 try {36 delete openDatabase.queryQueue[id];37 // Get transaction38 var tx = query.tx;39 // If transaction hasn't failed40 // Note: We ignore all query results if previous query41 // in the same transaction failed.42 if (tx && tx.queryList[id]) {43 tx.queryList = {};44 try {45 if (typeof query.errorCallback === 'function') {46 query.errorCallback(query.tx, reason);47 }48 } catch (ex) {49 console.log("executeSql error calling user error callback: " + ex);50 }51 tx.queryFailed(id, reason);52 }53 } catch (e) {54 console.log("executeSql error: " + e);55 }56 }57}58/**59 * SQL query object PRIVATE METHOD60 *61 * @constructor62 * @param tx63 * The transaction object that this query belongs to64 */65var wpdb_Query = function(tx) {66 // Set the id of the query67 this.id = utils.createUUID();68 // Add this query to the queue69 openDatabase.queryQueue[this.id] = this;70 // Init result71 this.resultSet = [];72 // Set transaction that this query belongs to73 this.tx = tx;74 // Add this query to transaction list75 this.tx.queryList[this.id] = this;76 // Callbacks77 this.successCallback = null;78 this.errorCallback = null;79};80/**81 * Transaction object PRIVATE METHOD82 *83 * @constructor84 */85var wpdb_Tx = function() {86 // Set the id of the transaction87 this.id = utils.createUUID();88 // Callbacks89 this.successCallback = null;90 this.errorCallback = null;91 // Query list92 this.queryList = {};93};94/**95 * Mark query in transaction as complete. If all queries are complete, call the user's transaction success96 * callback.97 *98 * @param id99 * Query id100 */101wpdb_Tx.prototype.queryComplete = function(id) {102 delete this.queryList[id];103 // If no more outstanding queries, then fire transaction success104 if (this.successCallback) {105 var count = 0;106 var i;107 for (i in this.queryList) {108 if (this.queryList.hasOwnProperty(i)) {109 count++;110 }111 }112 if (count === 0) {113 try {114 this.successCallback();115 } catch (e) {116 console.log("Transaction error calling user success callback: " + e);117 }118 }119 }120};121/**122 * Mark query in transaction as failed.123 *124 * @param id125 * Query id126 * @param reason127 * Error message128 */129wpdb_Tx.prototype.queryFailed = function(id, reason) {130 // The sql queries in this transaction have already been run, since131 // we really don't have a real transaction implemented in native code.132 // However, the user callbacks for the remaining sql queries in transaction133 // will not be called.134 this.queryList = {};135 if (this.errorCallback) {136 try {137 this.errorCallback(reason);138 } catch (e) {139 console.log("Transaction error calling user error callback: " + e);140 }141 }142};143/**144 * Execute SQL statement145 *146 * @param sql147 * SQL statement to execute148 * @param params149 * Statement parameters150 * @param successCallback151 * Success callback152 * @param errorCallback153 * Error callback154 */155wpdb_Tx.prototype.executeSql = function(sql, params, successCallback, errorCallback) {156 // Init params array157 if (typeof params === 'undefined') {158 params = [];159 }160 // Create query and add to queue161 var query = new wpdb_Query(this);162 openDatabase.queryQueue[query.id] = query;163 // Save callbacks164 query.successCallback = successCallback;165 query.errorCallback = errorCallback;166 // Add native code call in the stack167 openDatabase.execPool.push(function(){168 cordova.exec(openDatabase.queryFinished, failQuery, "WebDatabase", "executeSql", [ sql, params, query.id ]);169 });170 if(openDatabase.canExecuteQuery()){171 openDatabase.executeQuery();172 }173};174var DatabaseShell = function() {175};176/**177 * Start a transaction. Does not support rollback in event of failure.178 *179 * @param process180 * {Function} The transaction function181 * @param successCallback182 * {Function}183 * @param errorCallback184 * {Function}185 */186DatabaseShell.prototype.transaction = function(process, errorCallback, successCallback) {187 var tx = new wpdb_Tx();188 tx.successCallback = successCallback;189 tx.errorCallback = errorCallback;190 try {191 process(tx);192 } catch (e) {193 console.log("Transaction error: " + e);194 if (tx.errorCallback) {195 try {196 tx.errorCallback(e);197 } catch (ex) {198 console.log("Transaction error calling user error callback: " + e);199 }200 }201 }202};203/**204 * Open database205 *206 * @param name207 * Database name208 * @param version209 * Database version210 * @param display_name211 * Database display name212 * @param size213 * Database size in bytes214 * @return Database object215 */216var openDatabase = function(name, version, display_name, size) {217 document.addEventListener('deviceready', function(){218 cordova.exec(openDatabase.databaseOpened, failQuery, "WebDatabase", "openDatabase", [ name, version, display_name, size ]);219 }, false);220 var db = new DatabaseShell();221 return db;222};223/**224 * SQL result set object PRIVATE METHOD225 *226 * @constructor227 */228var wpdb_Rows = function() {229 this.resultSet = []; // results array230 this.length = 0; // number of rows231};232/**233 * Get item from SQL result set234 *235 * @param row236 * The row number to return237 * @return The row object238 */239wpdb_Rows.prototype.item = function(row) {240 return this.resultSet[row];241};242/**243 * SQL result set that is returned to user. PRIVATE METHOD244 *245 * @constructor246 */247var wpdb_Result = function() {248 this.rows = new wpdb_Rows();249};250/**251 * Callback from native code when query is complete. PRIVATE METHOD252 *253 * @param id254 * Query id255 */256function completeQuery(id, data) {257 var query = openDatabase.queryQueue[id];258 if (query) {259 try {260 delete openDatabase.queryQueue[id];261 // Get transaction262 var tx = query.tx;263 // If transaction hasn't failed264 // Note: We ignore all query results if previous query265 // in the same transaction failed.266 if (tx && tx.queryList[id]) {267 // Save query results268 var r = new wpdb_Result();269 r.rows.resultSet = data;270 r.rows.length = data.length;271 try {272 if (typeof query.successCallback === 'function') {273 query.successCallback(query.tx, r);274 }275 } catch (ex) {276 console.log("executeSql error calling user success callback: " + ex);277 }278 tx.queryComplete(id);279 }280 } catch (e) {281 console.log("executeSql error: " + e);282 }283 }284}285openDatabase.execPool = new Array();286openDatabase.queryBeingExecuted = true;287openDatabase.canExecuteQuery = function(){288 return !openDatabase.queryBeingExecuted;289};290openDatabase.executeQuery = function(){291 var execFunction = openDatabase.execPool.shift();292 if(execFunction != undefined){293 openDatabase.queryBeingExecuted = true;294 execFunction();295 }296};297openDatabase.databaseOpened = function(id, data){298 openDatabase.queryBeingExecuted = false;299 openDatabase.executeQuery();300};301openDatabase.queryFinished = function(sqlResult){302 openDatabase.queryBeingExecuted = false;303 openDatabase.executeQuery();304 var id = sqlResult.id;305 var data = JSON.parse(sqlResult.data);306 completeQuery(id, data);307};308openDatabase.queryQueue = {};...
Using AI Code Generation
1var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);2db.transaction(function (tx) {3 tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');4 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');5 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');6});7db.transaction(function (tx) {8 tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {9 var len = results.rows.length, i;10 msg = "<p>Found rows: " + len + "</p>";11 document.querySelector('#status').innerHTML += msg;12 for (i = 0; i < len; i++){13 msg = "<p><b>" + results.rows.item(i).log + "</b></p>";14 document.querySelector('#status').innerHTML += msg;15 }16 }, null);17});
Using AI Code Generation
1var db = openDatabase("testDB", "1.0", "Test DB", 200000);2db.transaction(function(tx) {3 tx.executeSql('CREATE TABLE IF NOT EXISTS testTable (id unique, data)');4 tx.executeSql('INSERT INTO testTable (id, data) VALUES (1, "First row")');5 tx.executeSql('INSERT INTO testTable (id, data) VALUES (2, "Second row")');6});7db.transaction(function(tx) {8 tx.executeSql('SELECT * FROM testTable', [], function(tx, results) {9 var len = results.rows.length, i;10 for (i = 0; i < len; i++) {11 console.log(results.rows.item(i));12 }13 }, null);14});
Using AI Code Generation
1var db = openDatabase("testDB", "1.0", "Test Database for wptoolkit", 200000);2db.transaction(function(tx) {3 tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');4 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');5 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');6});7db.transaction(function(tx) {8 tx.executeSql('SELECT * FROM LOGS', [], function(tx, rs) {9 for (var i = 0; i < rs.rows.length; i++) {10 console.log('Item ' + i + ': ' + rs.rows.item(i).log);11 }12 });13});14var db = openDatabase("testDB", "1.0", "Test Database for wptoolkit", 200000);15db.transaction(function(tx) {16 tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');17 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');18 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');19});20db.transaction(function(tx) {21 tx.executeSql('SELECT * FROM LOGS', [], function(tx, rs) {22 for (var i = 0; i < rs.rows.length; i++) {23 console.log('Item ' + i + ': ' + rs.rows.item(i).log);24 }25 });26});27var db = openDatabase("testDB", "1.0", "Test Database for wptoolkit", 200000);28db.transaction(function(tx) {29 tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');30 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');31 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');32});33db.transaction(function(tx) {34 tx.executeSql('SELECT * FROM LOGS', [], function
Using AI Code Generation
1var db = WPTK.openDatabase("testdb", "1.0", "Test DB", 2 * 1024 * 1024);2db.transaction(function (tx) {3 tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');4});5db.transaction(function (tx) {6 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');7 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');8});9db.transaction(function (tx) {10 tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {11 var len = results.rows.length, i;12 msg = "<p>Found rows: " + len + "</p>";13 document.querySelector('#status').innerHTML += msg;14 for (i = 0; i < len; i++){15 console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).log);16 }17 }, null);18});19db.transaction(function (tx) {20 tx.executeSql('DROP TABLE LOGS');21});22db.transaction(function (tx) {23 tx.executeSql('DROP DATABASE testdb');24});25db.transaction(function (tx) {26 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');27 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');28});29db.transaction(function (tx) {30 tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {31 var len = results.rows.length, i;32 msg = "<p>Found rows: " + len + "</p>";33 document.querySelector('#status').innerHTML += msg;34 for (i = 0; i < len; i++){35 console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).log);36 }37 }, null);38});
Using AI Code Generation
1var db = openDatabase("Database", "1.0", "Test Database", 200000);2var msg;3if(!db){4 postMessage("error");5}else{6 postMessage("success");7}8var request = window.indexedDB.open("Database", 1);9var db;10request.onerror = function(event) {11 postMessage("error");12};13request.onsuccess = function(event) {14 db = event.target.result;15 postMessage("success");16};17var db = openDatabase("Database", "1.0", "Test Database", 200000);18var msg;19if(!db){20 postMessage("error");21}else{22 postMessage("success");23}24var db = openDatabase("Database", "1.0", "Test Database", 200000);25var msg;26if(!db){27 postMessage("error");28}else{29 postMessage("success");30}31var db = openDatabase("Database", "1.0", "Test Database", 200000);32var msg;33if(!db){34 postMessage("error");35}else{36 postMessage("success");37}38var db = openDatabase("Database", "1.0", "Test Database", 200000);39var msg;40if(!db){41 postMessage("error");42}else{43 postMessage("success");44}45var db = openDatabase("Database", "1.0", "Test Database", 200000);46var msg;47if(!db){48 postMessage("error");49}else{50 postMessage("success");51}52var db = openDatabase("Database", "1.0", "Test Database", 200000);53var msg;54if(!db){55 postMessage("error");56}else{57 postMessage("success");58}59var db = openDatabase("Database", "1.0", "Test Database", 200000);60var msg;61if(!db){62 postMessage("error");63}else{64 postMessage("success");65}
Using AI Code Generation
1var db = openDatabase("mydb", "1.0", "mydb", 200000);2db.transaction(function(tx) {3tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');4tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');5tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');6});7var db = null;8var shortName = 'mydb';9var version = '1.0';10var displayName = 'mydb';11var maxSize = 200000;12db = openDatabase(shortName, version, displayName, maxSize);13db.transaction(function(tx) {14tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');15tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');16tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');17});18var db = null;19var shortName = 'mydb';20var version = '1.0';21var displayName = 'mydb';22var maxSize = 200000;23db = openDatabase(shortName, version, displayName, maxSize);24db.transaction(function(tx) {25tx.executeSql('SELECT * FROM LOGS', [], function(tx, results) {26var len = results.rows.length, i;27msg = "<p>Found rows: " + len + "</p>";28document.querySelector('#status').innerHTML += msg;29for (i = 0; i < len; i++){30msg = "<p><b>" + results.rows.item(i).log + "</b></p>";31document.querySelector('#status').innerHTML += msg;32}33}, null);34});35var db = null;36var shortName = 'mydb';37var version = '1.0';38var displayName = 'mydb';39var maxSize = 200000;40db = openDatabase(shortName, version, displayName, maxSize);41db.transaction(function(tx) {42tx.executeSql('DELETE FROM LOGS');
Using AI Code Generation
1var db = wptb.openDatabase("testdb", "1.0", "Test DB", 200000);2db.transaction(function (tx) {3 tx.executeSql('CREATE TABLE IF NOT EXISTS testtable (id unique, data)');4 tx.executeSql('INSERT INTO testtable (id, data) VALUES (1, "First row")');5 tx.executeSql('INSERT INTO testtable (id, data) VALUES (2, "Second row")');6 tx.executeSql('INSERT INTO testtable (id, data) VALUES (3, "Third row")');7 tx.executeSql('SELECT * FROM testtable', [], function (tx, results) {8 var len = results.rows.length, i;9 console.log("testtable: " + len + " rows found.");10 for (i = 0; i < len; i++) {11 console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data);12 }13 }, null);14 tx.executeSql('DELETE FROM testtable');15 tx.executeSql('DROP TABLE testtable');16 tx.executeSql('DROP DATABASE testdb');17 db.close();18});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!