summaryrefslogtreecommitdiffstats
path: root/javascripts/libs/browser_engines/sqljs_driver.js
diff options
context:
space:
mode:
authorJake Feasel <jfeasel@gmail.com>2012-09-10 20:42:38 -0700
committerJake Feasel <jfeasel@gmail.com>2012-09-10 20:42:38 -0700
commit4509373f3b392623a4eceaff6ed6afffb51a32f5 (patch)
tree0ac9f11eb061393f7f97c9c939a4e38be3eb9e27 /javascripts/libs/browser_engines/sqljs_driver.js
parentb5ede40d9e877fbf30f46312f0f008f4ee8bad46 (diff)
downloadsqlfiddle-4509373f3b392623a4eceaff6ed6afffb51a32f5.zip
sqlfiddle-4509373f3b392623a4eceaff6ed6afffb51a32f5.tar.gz
sqlfiddle-4509373f3b392623a4eceaff6ed6afffb51a32f5.tar.bz2
Initial moves
Diffstat (limited to 'javascripts/libs/browser_engines/sqljs_driver.js')
-rw-r--r--javascripts/libs/browser_engines/sqljs_driver.js160
1 files changed, 160 insertions, 0 deletions
diff --git a/javascripts/libs/browser_engines/sqljs_driver.js b/javascripts/libs/browser_engines/sqljs_driver.js
new file mode 100644
index 0000000..89831da
--- /dev/null
+++ b/javascripts/libs/browser_engines/sqljs_driver.js
@@ -0,0 +1,160 @@
+define(["jQuery", "sqlite_driver"], function ($, SQLite_driver) {
+
+ var SQLjs_driver = function () {
+ this.db = null;
+ return this;
+ }
+
+ $.extend(SQLjs_driver.prototype,SQLite_driver.prototype); // inherit from parent class
+
+
+ SQLjs_driver.prototype.buildSchema = function (args) {
+
+ var _this = this; // preserve reference to current object through local closures
+
+ try {
+
+ /*
+ * Closure used to handle both cases of when the sql.js library
+ * has already been loaded, or when it has not yet been.
+ */
+ var jsBuildSchema = function () {
+
+ _this.db = SQL.open();
+ $.each(SQLite_driver.prototype.splitStatement.call(this,args["ddl"],args["statement_separator"]), function (i, statement) {
+ _this.db.exec(statement);
+ });
+
+ args["success"]();
+ }
+
+ // If the sql.js code isn't yet loaded, do it now.
+ if (window.SQL === undefined)
+ {
+ $.getScript("javascripts_static/sql.js", function (script, textStatus, jqXHR) {
+ jsBuildSchema();
+ }).fail(function(jqxhr, settings, exception){
+ args["error"]("Your browser does not work with SQL.js. Try using a different browser (Chrome, Safari, Firefox, IE 10, etc...), or a newer version of your current one.");
+ });
+ }
+ else
+ {
+ if (_this.db)
+ {
+ _this.db.close();
+ }
+
+ jsBuildSchema();
+ }
+
+ }
+ catch (e)
+ {
+ args["error"](e);
+ }
+
+ }
+
+ SQLjs_driver.prototype.executeQuery = function (args) {
+
+ var _this = this; // preserve reference to current object through local closures
+
+ try {
+ if (! _this.db)
+ {
+ throw ("Database Schema not available!");
+ }
+
+ var returnSets = [];
+
+ _this.db.exec("BEGIN TRANSACTION");
+
+ $.each(SQLite_driver.prototype.splitStatement.call(this,args["sql"],args["statement_separator"]), function (i, statement) {
+ if ($.trim(statement).length) {
+ var startTime = new Date();
+
+ var setArray = [];
+
+ try {
+ setArray = _this.db.exec(statement);
+
+ var thisSet = {
+ "SUCCEEDED": true,
+ "EXECUTIONTIME": (new Date()) - startTime,
+ "RESULTS": {
+ "COLUMNS": [],
+ "DATA": []
+ },
+ "EXECUTIONPLAN": {
+ "COLUMNS": [],
+ "DATA": []
+ }
+
+ };
+
+ if (setArray.length) {
+ $.each(setArray, function(rowNumber, row){
+ var rowVals = [];
+ $.each(row, function(columnNumber, col){
+ if (rowNumber == 0) {
+ thisSet["RESULTS"]["COLUMNS"].push(col.column);
+ }
+ rowVals.push(col.value);
+ });
+ thisSet["RESULTS"]["DATA"].push(rowVals);
+ });
+ }
+
+ try {
+
+ exectionPlanArray = _this.db.exec("EXPLAIN QUERY PLAN " + statement);
+
+ if (exectionPlanArray.length) {
+ $.each(exectionPlanArray, function(rowNumber, row){
+ var rowVals = [];
+ $.each(row, function(columnNumber, col){
+ if (rowNumber == 0) {
+ thisSet["EXECUTIONPLAN"]["COLUMNS"].push(col.column);
+ }
+ rowVals.push(col.value);
+ });
+ thisSet["EXECUTIONPLAN"]["DATA"].push(rowVals);
+ });
+ }
+
+ }
+ catch (e) {
+ // if we get an error with the execution plan, just ignore and move on.
+ }
+
+ returnSets.push(thisSet);
+
+
+ }
+ catch (e) {
+ var thisSet = {
+ "SUCCEEDED": false,
+ "EXECUTIONTIME": (new Date()) - startTime,
+ "ERRORMESSAGE": e
+ };
+ returnSets.push(thisSet);
+ return false; // breaks the each loop
+ }
+
+ }
+ });
+
+ _this.db.exec("ROLLBACK TRANSACTION");
+
+ args["success"](returnSets);
+
+ }
+ catch (e)
+ {
+ args["error"](e);
+ }
+
+ }
+
+ return SQLjs_driver;
+});