forked from AlaSQL/alasql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqljs_driver.js
151 lines (115 loc) · 5.16 KB
/
sqljs_driver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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 = new window.SQL.Database();
$.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)
{
window.define_tmp = window.define;
window.define = undefined;
$.getScript("javascript/sql.js", function (script, textStatus, jqXHR) {
window.define = window.define_tmp;
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(this.splitStatement(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,
"STATEMENT": statement,
"EXECUTIONTIME": (new Date()) - startTime,
"RESULTS": {
"COLUMNS": [],
"DATA": []
},
"EXECUTIONPLAN": {
"COLUMNS": [],
"DATA": []
}
};
if (setArray.length) {
$.each(setArray, function(){
thisSet["RESULTS"]["COLUMNS"] = this.columns;
thisSet["RESULTS"]["DATA"] = this.values;
});
}
try {
exectionPlanArray = _this.db.exec("EXPLAIN QUERY PLAN " + statement);
if (exectionPlanArray.length) {
$.each(exectionPlanArray, function(){
thisSet["EXECUTIONPLAN"]["COLUMNS"] = this.columns;
thisSet["EXECUTIONPLAN"]["DATA"] = this.values;
});
}
}
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;
});