|
1 |
| -//! AlaSQL v0.5.3-develop-8b2fda7fundefined | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT |
| 1 | +//! AlaSQL v0.5.3-develop-68939748undefined | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT |
2 | 2 | /*
|
3 | 3 | @module alasql
|
4 |
| -@version 0.5.3-develop-8b2fda7fundefined |
| 4 | +@version 0.5.3-develop-68939748undefined |
5 | 5 |
|
6 | 6 | AlaSQL - JavaScript SQL database
|
7 | 7 | © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
|
@@ -37,203 +37,209 @@ SOFTWARE.
|
37 | 37 | // (c) 2014-2015, Andrey Gershun
|
38 | 38 | //
|
39 | 39 | */
|
40 |
| -(function(root, factory) { |
41 |
| - if (typeof define === 'function' && define.amd) { |
42 |
| - define([], factory); |
43 |
| - } else if (typeof exports === 'object') { |
44 |
| - module.exports = factory(); |
45 |
| - } else { |
46 |
| - root.alasql = factory(); |
47 |
| - } |
48 |
| -})(this, function() { |
49 |
| - /** |
| 40 | +(function (root, factory) { |
| 41 | + if (typeof define === 'function' && define.amd) { |
| 42 | + define([], factory); |
| 43 | + } else if (typeof exports === 'object') { |
| 44 | + module.exports = factory(); |
| 45 | + } else { |
| 46 | + root.alasql = factory(); |
| 47 | + } |
| 48 | +}(this, function () { |
| 49 | + |
| 50 | +/** |
50 | 51 | Main procedure for worker
|
51 | 52 | @function
|
52 | 53 | @param {string} sql SQL statement
|
53 | 54 | @param {object} params List of parameters (can be omitted)
|
54 | 55 | @param {callback} cb Callback function
|
55 | 56 | @return {object} Query result
|
56 | 57 | */
|
57 |
| - function alasql(sql, params, cb) { |
58 |
| - params = params || []; |
59 |
| - |
60 |
| - // Avoid setting params if not needed even with callback |
61 |
| - if (typeof params === 'function') { |
62 |
| - scope = cb; |
63 |
| - cb = params; |
64 |
| - params = []; |
65 |
| - } |
| 58 | +function alasql(sql,params,cb){ |
66 | 59 |
|
67 |
| - if (typeof params !== 'object') { |
68 |
| - params = [params]; |
69 |
| - } |
| 60 | + params = params||[]; |
70 | 61 |
|
71 |
| - // Increase last request id |
72 |
| - var id = alasql.lastid++; |
73 |
| - // Save callback |
74 |
| - alasql.buffer[id] = cb; |
75 |
| - // Send a message to worker |
76 |
| - alasql.webworker.postMessage({id: id, sql: sql, params: params}); |
| 62 | + // Avoid setting params if not needed even with callback |
| 63 | + if(typeof params === 'function'){ |
| 64 | + scope = cb; |
| 65 | + cb = params; |
| 66 | + params = []; |
77 | 67 | }
|
78 | 68 |
|
79 |
| - alasql.options = {}; |
80 |
| - alasql.options.progress = function() {}; |
| 69 | + if(typeof params !== 'object'){ |
| 70 | + params = [params]; |
| 71 | + } |
81 | 72 |
|
82 |
| - isArray = function(obj) { |
83 |
| - return '[object Array]' === Object.prototype.toString.call(obj); |
84 |
| - }; |
| 73 | + // Increase last request id |
| 74 | + var id = alasql.lastid++; |
| 75 | + // Save callback |
| 76 | + alasql.buffer[id] = cb; |
| 77 | + // Send a message to worker |
| 78 | + alasql.webworker.postMessage({id:id,sql:sql,params:params}); |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +alasql.options = {}; |
| 83 | +alasql.options.progress = function(){}; |
| 84 | + |
| 85 | +isArray = function(obj){ |
| 86 | + return "[object Array]"===Object.prototype.toString.call(obj); |
| 87 | +} |
| 88 | + |
| 89 | +alasql.promise = function() { |
| 90 | + throw new Error('Please include a Promise/A+ library'); |
| 91 | +} |
| 92 | + |
| 93 | +// From src/18promise.js |
| 94 | +if(typeof Promise !== "undefined"){ |
| 95 | + var promiseExec = function(sql, params, counterStep, counterTotal){ |
| 96 | + return new Promise(function(resolve, reject){ |
| 97 | + alasql(sql, params, function(data,err) { |
| 98 | + if(err) { |
| 99 | + reject(err); |
| 100 | + } else { |
| 101 | + if (counterStep && counterTotal && alasql.options.progress !== false) { |
| 102 | + alasql.options.progress(counterStep, counterTotal); |
| 103 | + } |
| 104 | + resolve(data); |
| 105 | + } |
| 106 | + }); |
| 107 | + }); |
| 108 | + } |
85 | 109 |
|
86 |
| - alasql.promise = function() { |
87 |
| - throw new Error('Please include a Promise/A+ library'); |
88 |
| - }; |
| 110 | + var promiseAll = function(sqlParamsArray){ |
| 111 | + if(sqlParamsArray.length<1){ |
| 112 | + return ; |
| 113 | + } |
89 | 114 |
|
90 |
| - // From src/18promise.js |
91 |
| - if (typeof Promise !== 'undefined') { |
92 |
| - var promiseExec = function(sql, params, counterStep, counterTotal) { |
93 |
| - return new Promise(function(resolve, reject) { |
94 |
| - alasql(sql, params, function(data, err) { |
95 |
| - if (err) { |
96 |
| - reject(err); |
97 |
| - } else { |
98 |
| - if (counterStep && counterTotal && alasql.options.progress !== false) { |
99 |
| - alasql.options.progress(counterStep, counterTotal); |
100 |
| - } |
101 |
| - resolve(data); |
102 |
| - } |
103 |
| - }); |
104 |
| - }); |
105 |
| - }; |
| 115 | + var active, sql, params; |
106 | 116 |
|
107 |
| - var promiseAll = function(sqlParamsArray) { |
108 |
| - if (sqlParamsArray.length < 1) { |
109 |
| - return; |
110 |
| - } |
| 117 | + var execArray = []; |
111 | 118 |
|
112 |
| - var active, sql, params; |
| 119 | + for (var i = 0; i < sqlParamsArray.length; i++) { |
| 120 | + active = sqlParamsArray[i]; |
113 | 121 |
|
114 |
| - var execArray = []; |
| 122 | + if(typeof active === 'string'){ |
| 123 | + active = [active]; |
| 124 | + } |
115 | 125 |
|
116 |
| - for (var i = 0; i < sqlParamsArray.length; i++) { |
117 |
| - active = sqlParamsArray[i]; |
| 126 | + if(!isArray(active) || active.length<1 || 2<active.length){ |
| 127 | + throw new Error('Error in .promise parameter'); |
| 128 | + } |
118 | 129 |
|
119 |
| - if (typeof active === 'string') { |
120 |
| - active = [active]; |
121 |
| - } |
| 130 | + sql = active[0]; |
| 131 | + params = active[1]||undefined; |
122 | 132 |
|
123 |
| - if (!isArray(active) || active.length < 1 || 2 < active.length) { |
124 |
| - throw new Error('Error in .promise parameter'); |
125 |
| - } |
| 133 | + execArray.push(promiseExec(sql, params, i, sqlParamsArray.length)); |
| 134 | + } |
126 | 135 |
|
127 |
| - sql = active[0]; |
128 |
| - params = active[1] || undefined; |
| 136 | + return Promise.all(execArray); |
| 137 | + } |
129 | 138 |
|
130 |
| - execArray.push(promiseExec(sql, params, i, sqlParamsArray.length)); |
131 |
| - } |
| 139 | + alasql.promise = function(sql, params) { |
| 140 | + if(typeof Promise === "undefined"){ |
| 141 | + throw new Error('Please include a Promise/A+ library'); |
| 142 | + } |
132 | 143 |
|
133 |
| - return Promise.all(execArray); |
134 |
| - }; |
| 144 | + if(typeof sql === 'string'){ |
| 145 | + return promiseExec(sql, params); |
| 146 | + } |
135 | 147 |
|
136 |
| - alasql.promise = function(sql, params) { |
137 |
| - if (typeof Promise === 'undefined') { |
138 |
| - throw new Error('Please include a Promise/A+ library'); |
139 |
| - } |
| 148 | + if(!isArray(sql) || sql.length<1 || typeof params !== "undefined"){ |
| 149 | + throw new Error('Error in .promise parameters'); |
| 150 | + } |
| 151 | + return promiseAll(sql); |
| 152 | + }; |
140 | 153 |
|
141 |
| - if (typeof sql === 'string') { |
142 |
| - return promiseExec(sql, params); |
143 |
| - } |
| 154 | +} |
144 | 155 |
|
145 |
| - if (!isArray(sql) || sql.length < 1 || typeof params !== 'undefined') { |
146 |
| - throw new Error('Error in .promise parameters'); |
147 |
| - } |
148 |
| - return promiseAll(sql); |
149 |
| - }; |
150 |
| - } |
151 | 156 |
|
152 |
| - alasql = alasql || false; |
| 157 | +alasql = alasql || false; |
153 | 158 |
|
154 |
| - if (!alasql) { |
155 |
| - throw new Error('alasql was not found'); |
156 |
| - } |
| 159 | +if (!alasql) { |
| 160 | + throw new Error('alasql was not found'); |
| 161 | +} |
157 | 162 |
|
158 |
| - alasql.worker = function() { |
159 |
| - throw new Error('Can find webworker in this enviroment'); |
160 |
| - }; |
| 163 | +alasql.worker = function() { |
| 164 | + throw new Error('Can find webworker in this enviroment'); |
| 165 | +}; |
161 | 166 |
|
162 |
| - if (typeof Worker !== 'undefined') { |
163 |
| - alasql.worker = function(path, paths, cb) { |
164 |
| - // var path; |
165 |
| - if (path === true) { |
166 |
| - path = undefined; |
167 |
| - } |
| 167 | +if (typeof Worker !== 'undefined') { |
| 168 | + alasql.worker = function(path, paths, cb) { |
| 169 | + // var path; |
| 170 | + if (path === true) { |
| 171 | + path = undefined; |
| 172 | + } |
168 | 173 |
|
169 |
| - if (typeof path === 'undefined') { |
170 |
| - var sc = document.getElementsByTagName('script'); |
171 |
| - for (var i = 0; i < sc.length; i++) { |
172 |
| - if (sc[i].src.substr(-16).toLowerCase() === 'alasql-worker.js') { |
173 |
| - path = sc[i].src.substr(0, sc[i].src.length - 16) + 'alasql.js'; |
174 |
| - break; |
175 |
| - } else if (sc[i].src.substr(-20).toLowerCase() === 'alasql-worker.min.js') { |
176 |
| - path = sc[i].src.substr(0, sc[i].src.length - 20) + 'alasql.min.js'; |
177 |
| - break; |
178 |
| - } else if (sc[i].src.substr(-9).toLowerCase() === 'alasql.js') { |
179 |
| - path = sc[i].src; |
180 |
| - break; |
181 |
| - } else if (sc[i].src.substr(-13).toLowerCase() === 'alasql.min.js') { |
182 |
| - path = sc[i].src.substr(0, sc[i].src.length - 13) + 'alasql.min.js'; |
183 |
| - break; |
184 |
| - } |
| 174 | + if (typeof path === 'undefined') { |
| 175 | + var sc = document.getElementsByTagName('script'); |
| 176 | + for (var i = 0; i < sc.length; i++) { |
| 177 | + if (sc[i].src.substr(-16).toLowerCase() === 'alasql-worker.js') { |
| 178 | + path = sc[i].src.substr(0, sc[i].src.length - 16) + 'alasql.js'; |
| 179 | + break; |
| 180 | + } else if (sc[i].src.substr(-20).toLowerCase() === 'alasql-worker.min.js') { |
| 181 | + path = sc[i].src.substr(0, sc[i].src.length - 20) + 'alasql.min.js'; |
| 182 | + break; |
| 183 | + } else if (sc[i].src.substr(-9).toLowerCase() === 'alasql.js') { |
| 184 | + path = sc[i].src; |
| 185 | + break; |
| 186 | + } else if (sc[i].src.substr(-13).toLowerCase() === 'alasql.min.js') { |
| 187 | + path = sc[i].src.substr(0, sc[i].src.length - 13) + 'alasql.min.js'; |
| 188 | + break; |
185 | 189 | }
|
186 | 190 | }
|
| 191 | + } |
187 | 192 |
|
188 |
| - if (typeof path === 'undefined') { |
189 |
| - throw new Error('Path to alasql.js is not specified'); |
190 |
| - } else if (path !== false) { |
191 |
| - var js = "importScripts('"; |
192 |
| - js += path; |
193 |
| - js += |
194 |
| - "');self.onmessage = function(event) {" + |
195 |
| - 'alasql(event.data.sql,event.data.params, function(data){' + |
196 |
| - 'postMessage({id:event.data.id, data:data});});}'; |
197 |
| - |
198 |
| - var blob = new Blob([js], {type: 'text/plain'}); |
199 |
| - alasql.webworker = new Worker(URL.createObjectURL(blob)); |
200 |
| - |
201 |
| - alasql.webworker.onmessage = function(event) { |
202 |
| - var id = event.data.id; |
203 |
| - |
204 |
| - alasql.buffer[id](event.data.data); |
205 |
| - delete alasql.buffer[id]; |
206 |
| - }; |
207 |
| - |
208 |
| - alasql.webworker.onerror = function(e) { |
209 |
| - throw e; |
210 |
| - }; |
211 |
| - |
212 |
| - if (arguments.length > 1) { |
213 |
| - var sql = |
214 |
| - 'REQUIRE ' + |
215 |
| - paths |
216 |
| - .map(function(p) { |
217 |
| - return '"' + p + '"'; |
218 |
| - }) |
219 |
| - .join(','); |
220 |
| - alasql(sql, [], cb); |
221 |
| - } |
222 |
| - } else if (path === false) { |
223 |
| - delete alasql.webworker; |
224 |
| - return; |
| 193 | + if (typeof path === 'undefined') { |
| 194 | + throw new Error('Path to alasql.js is not specified'); |
| 195 | + } else if (path !== false) { |
| 196 | + var js = "importScripts('"; |
| 197 | + js += path; |
| 198 | + js += |
| 199 | + "');self.onmessage = function(event) {" + |
| 200 | + 'alasql(event.data.sql,event.data.params, function(data){' + |
| 201 | + 'postMessage({id:event.data.id, data:data});});}'; |
| 202 | + |
| 203 | + var blob = new Blob([js], {type: 'text/plain'}); |
| 204 | + alasql.webworker = new Worker(URL.createObjectURL(blob)); |
| 205 | + |
| 206 | + alasql.webworker.onmessage = function(event) { |
| 207 | + var id = event.data.id; |
| 208 | + |
| 209 | + alasql.buffer[id](event.data.data); |
| 210 | + delete alasql.buffer[id]; |
| 211 | + }; |
| 212 | + |
| 213 | + alasql.webworker.onerror = function(e) { |
| 214 | + throw e; |
| 215 | + }; |
| 216 | + |
| 217 | + if (arguments.length > 1) { |
| 218 | + var sql = |
| 219 | + 'REQUIRE ' + |
| 220 | + paths |
| 221 | + .map(function(p) { |
| 222 | + return '"' + p + '"'; |
| 223 | + }) |
| 224 | + .join(','); |
| 225 | + alasql(sql, [], cb); |
225 | 226 | }
|
226 |
| - }; |
227 |
| - } |
| 227 | + } else if (path === false) { |
| 228 | + delete alasql.webworker; |
| 229 | + return; |
| 230 | + } |
| 231 | + }; |
| 232 | +} |
| 233 | + |
228 | 234 |
|
229 |
| - /* WebWorker */ |
230 |
| - /** @type {number} */ |
231 |
| - alasql.lastid = 0; |
| 235 | +/* WebWorker */ |
| 236 | +/** @type {number} */ |
| 237 | +alasql.lastid = 0; |
232 | 238 |
|
233 |
| - /** @type {object} */ |
234 |
| - alasql.buffer = {}; |
| 239 | +/** @type {object} */ |
| 240 | +alasql.buffer = {}; |
235 | 241 |
|
236 |
| - alasql.worker(); |
| 242 | +alasql.worker(); |
237 | 243 |
|
238 |
| - return alasql; |
239 |
| -}); |
| 244 | +return alasql; |
| 245 | +})); |
0 commit comments