|
1 | | - |
2 | | -/** |
3 | | - UMD envelope |
4 | | - */ |
5 | | - |
6 | | -(function (root, factory) { |
7 | | - if (typeof define === 'function' && define.amd) { |
8 | | - define([], factory); |
9 | | - } else if (typeof exports === 'object') { |
10 | | - module.exports = factory(); |
11 | | - } else { |
12 | | - root.alasql = factory(); |
13 | | - } |
14 | | -}(this, function () { |
15 | | - |
16 | | -/** |
17 | | - alasql - Main Alasql class |
18 | | - @param {string | Object} sql SQL-statement or data object for fuent interface |
19 | | - @param {Object} params SQL parameters |
20 | | - @param {Function} cb callback function |
21 | | - @param {Object} scope Scope for nested queries |
22 | | - @return {array} Result data object |
23 | | - |
24 | | - Standard sync call: |
25 | | - alasql('CREATE TABLE one'); |
26 | | - Query: |
27 | | - var res = alasql('SELECT * FROM one'); |
28 | | - Call with parameters: |
29 | | - var res = alasql('SELECT * FROM ?',[data]); |
30 | | - Standard async call with callback function: |
31 | | - alasql('SELECT * FROM ?',[data],function(res){ |
32 | | - console.log(data); |
33 | | - }); |
34 | | - Call with scope for subquery (to pass common values): |
35 | | - var scope = {one:{a:2,b;20}} |
36 | | - alasql('SELECT * FROM ? two WHERE two.a = one.a',[data],null,scope); |
37 | | - Call for fluent interface with data object: |
38 | | - alasql(data).Where(function(x){return x.a == 10}).exec(); |
39 | | - Call for fluent interface without data object: |
40 | | - alasql().From(data).Where(function(x){return x.a == 10}).exec(); |
41 | | - */ |
42 | | - |
43 | | -var alasql = function(sql, params, cb, scope) { |
44 | | - if(typeof importScripts != 'function' && alasql.webworker) { |
45 | | - var id = alasql.lastid++; |
46 | | - alasql.buffer[id] = cb; |
47 | | - alasql.webworker.postMessage({id:id,sql:sql,params:params}); |
48 | | - } else { |
49 | | - if(arguments.length == 0) { |
50 | | - // Without arguments - Fluent interface |
51 | | - return new yy.Select({ |
52 | | - columns:[new yy.Column({columnid:'*'})], |
53 | | - from: [new yy.ParamValue({param:0})] |
54 | | - }); |
55 | | - } else if (arguments.length == 1 && typeof sql == "object" && sql instanceof Array) { |
56 | | - // One argument data object - fluent interface |
57 | | - var select = new yy.Select({ |
58 | | - columns:[new yy.Column({columnid:'*'})], |
59 | | - from: [new yy.ParamValue({param:0})] |
60 | | - }); |
61 | | - select.preparams = [sql]; |
62 | | - return select; |
63 | | - } else { |
64 | | - // Standard interface |
65 | | - // alasql('#sql'); |
66 | | - if(typeof sql == 'string' && sql[0]=='#' && typeof document == "object") { |
67 | | - sql = document.querySelector(sql).textContent; |
68 | | - } else if(typeof sql == 'object' && sql instanceof HTMElement) { |
69 | | - sql = sql.textContent; |
70 | | - } else if(typeof sql == 'function') { |
71 | | - // to run multiline functions |
72 | | - sql = sql.toString().slice(14,-3); |
73 | | - } |
74 | | - // Run SQL |
75 | | - return alasql.exec(sql, params, cb, scope); |
76 | | - } |
77 | | - }; |
78 | | -}; |
79 | | - |
80 | | -/** Current version of alasql */ |
81 | | -alasql.version = "0.0.44"; |
82 | | -
|
| 1 | + |
| 2 | +/** |
| 3 | + UMD envelope |
| 4 | + */ |
| 5 | + |
| 6 | +(function (root, factory) { |
| 7 | + if (typeof define === 'function' && define.amd) { |
| 8 | + define([], factory); |
| 9 | + } else if (typeof exports === 'object') { |
| 10 | + module.exports = factory(); |
| 11 | + } else { |
| 12 | + root.alasql = factory(); |
| 13 | + } |
| 14 | +}(this, function () { |
| 15 | + |
| 16 | +/** |
| 17 | + alasql - Main Alasql class |
| 18 | + @param {string | Object} sql SQL-statement or data object for fuent interface |
| 19 | + @param {Object} params SQL parameters |
| 20 | + @param {Function} cb callback function |
| 21 | + @param {Object} scope Scope for nested queries |
| 22 | + @return {array} Result data object |
| 23 | + |
| 24 | + Standard sync call: |
| 25 | + alasql('CREATE TABLE one'); |
| 26 | + Query: |
| 27 | + var res = alasql('SELECT * FROM one'); |
| 28 | + Call with parameters: |
| 29 | + var res = alasql('SELECT * FROM ?',[data]); |
| 30 | + Standard async call with callback function: |
| 31 | + alasql('SELECT * FROM ?',[data],function(res){ |
| 32 | + console.log(data); |
| 33 | + }); |
| 34 | + Call with scope for subquery (to pass common values): |
| 35 | + var scope = {one:{a:2,b;20}} |
| 36 | + alasql('SELECT * FROM ? two WHERE two.a = one.a',[data],null,scope); |
| 37 | + Call for fluent interface with data object: |
| 38 | + alasql(data).Where(function(x){return x.a == 10}).exec(); |
| 39 | + Call for fluent interface without data object: |
| 40 | + alasql().From(data).Where(function(x){return x.a == 10}).exec(); |
| 41 | + */ |
| 42 | + |
| 43 | +var alasql = function(sql, params, cb, scope) { |
| 44 | + if(typeof importScripts != 'function' && alasql.webworker) { |
| 45 | + var id = alasql.lastid++; |
| 46 | + alasql.buffer[id] = cb; |
| 47 | + alasql.webworker.postMessage({id:id,sql:sql,params:params}); |
| 48 | + } else { |
| 49 | + if(arguments.length == 0) { |
| 50 | + // Without arguments - Fluent interface |
| 51 | + return new yy.Select({ |
| 52 | + columns:[new yy.Column({columnid:'*'})], |
| 53 | + from: [new yy.ParamValue({param:0})] |
| 54 | + }); |
| 55 | + } else if (arguments.length == 1 && typeof sql == "object" && sql instanceof Array) { |
| 56 | + // One argument data object - fluent interface |
| 57 | + var select = new yy.Select({ |
| 58 | + columns:[new yy.Column({columnid:'*'})], |
| 59 | + from: [new yy.ParamValue({param:0})] |
| 60 | + }); |
| 61 | + select.preparams = [sql]; |
| 62 | + return select; |
| 63 | + } else { |
| 64 | + // Standard interface |
| 65 | + // alasql('#sql'); |
| 66 | + if(typeof sql == 'string' && sql[0]=='#' && typeof document == "object") { |
| 67 | + sql = document.querySelector(sql).textContent; |
| 68 | + } else if(typeof sql == 'object' && sql instanceof HTMElement) { |
| 69 | + sql = sql.textContent; |
| 70 | + } else if(typeof sql == 'function') { |
| 71 | + // to run multiline functions |
| 72 | + sql = sql.toString().slice(14,-3); |
| 73 | + } |
| 74 | + // Run SQL |
| 75 | + return alasql.exec(sql, params, cb, scope); |
| 76 | + } |
| 77 | + }; |
| 78 | +}; |
| 79 | + |
| 80 | +/** Current version of alasql */ |
| 81 | +alasql.version = "0.0.45"; |
| 82 | + |
0 commit comments