alasql 1.7.7 → 1.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/alasql-cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  //
3
3
  // Command line interface for Alasql
4
- // Version: 1.7.7
4
+ // Version: 1.7.8
5
5
  // Date: 28.07.2015
6
6
  // (c) 2014-2015, Andrey Gershun & M. Rangel Wulff
7
7
  //
@@ -1,7 +1,7 @@
1
- //! AlaSQL v1.7.7 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v1.7.8 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 1.7.7
4
+ @version 1.7.8
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
@@ -1,2 +1,2 @@
1
- //! AlaSQL v1.7.7 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v1.7.8 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  !function(r,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():r.alasql=e()}(this,function(){function i(r,e,t){"function"==typeof(e=e||[])&&(scope=t,t=e,e=[]),"object"!=typeof e&&(e=[e]);var o=i.lastid++;i.buffer[o]=t,i.webworker.postMessage({id:o,sql:r,params:e})}var s;if(i.options={},i.options.progress=function(){},isArray=function(r){return"[object Array]"===Object.prototype.toString.call(r)},i.promise=function(){throw new Error("Please include a Promise/A+ library")},"undefined"!=typeof Promise&&(s=function(r,e,n,s){return new Promise(function(t,o){i(r,e,function(r,e){e?o(e):(n&&s&&!1!==i.options.progress&&i.options.progress(n,s),t(r))})})},i.promise=function(r,e){if("undefined"==typeof Promise)throw new Error("Please include a Promise/A+ library");if("string"==typeof r)return s(r,e);if(!isArray(r)||r.length<1||void 0!==e)throw new Error("Error in .promise parameters");return function(r){if(!(r.length<1)){for(var e,t,o=[],n=0;n<r.length;n++){if("string"==typeof(t=r[n])&&(t=[t]),!isArray(t)||t.length<1||2<t.length)throw new Error("Error in .promise parameter");e=t[0],t=t[1]||void 0,o.push(s(e,t,n,r.length))}return Promise.all(o)}}(r)}),!(i=i||!1))throw new Error("alasql was not found");return i.worker=function(){throw new Error("Can find webworker in this enviroment")},"undefined"!=typeof Worker&&(i.worker=function(r,e,t){if(void 0===(r=!0===r?void 0:r))for(var o=document.getElementsByTagName("script"),n=0;n<o.length;n++){if("alasql-worker.js"===o[n].src.substr(-16).toLowerCase()){r=o[n].src.substr(0,o[n].src.length-16)+"alasql.js";break}if("alasql-worker.min.js"===o[n].src.substr(-20).toLowerCase()){r=o[n].src.substr(0,o[n].src.length-20)+"alasql.min.js";break}if("alasql.js"===o[n].src.substr(-9).toLowerCase()){r=o[n].src;break}if("alasql.min.js"===o[n].src.substr(-13).toLowerCase()){r=o[n].src.substr(0,o[n].src.length-13)+"alasql.min.js";break}}if(void 0===r)throw new Error("Path to alasql.js is not specified");var s;!1!==r?(s="importScripts('",s+=r,s+="');self.onmessage = function(event) {alasql(event.data.sql,event.data.params, function(data){postMessage({id:event.data.id, data:data});});}",s=new Blob([s],{type:"text/plain"}),i.webworker=new Worker(URL.createObjectURL(s)),i.webworker.onmessage=function(r){var e=r.data.id;i.buffer[e](r.data.data),delete i.buffer[e]},i.webworker.onerror=function(r){throw r},1<arguments.length&&i("REQUIRE "+e.map(function(r){return'"'+r+'"'}).join(","),[],t)):!1===r&&delete i.webworker}),i.lastid=0,i.buffer={},i.worker(),i});
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v1.7.7 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v1.7.8 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 1.7.7
4
+ @version 1.7.8
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
@@ -142,7 +142,7 @@ var alasql = function(sql, params, cb, scope) {
142
142
  Current version of alasql
143
143
  @constant {string}
144
144
  */
145
- alasql.version = '1.7.7';
145
+ alasql.version = '1.7.8';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -4451,9 +4451,201 @@ alasql.parser.parseError = function (str, hash) {
4451
4451
  }
4452
4452
  */
4453
4453
  alasql.parse = function (sql) {
4454
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4454
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4455
+ checkSqlNames(ast);
4456
+ return ast;
4455
4457
  };
4456
4458
 
4459
+ /*
4460
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4461
+ holds a quote, a backslash or a line break would end the string and make the rest code
4462
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4463
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4464
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4465
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4466
+ of a JSON value are data, not names.
4467
+ */
4468
+ var checkSqlNames = (function () {
4469
+ var hasOwn = Object.prototype.hasOwnProperty;
4470
+ var badName = /['\\\n\r\u2028\u2029]/;
4471
+ var nameKeys = {
4472
+ as: 1,
4473
+ columnid: 1,
4474
+ tableid: 1,
4475
+ databaseid: 1,
4476
+ viewid: 1,
4477
+ indexid: 1,
4478
+ trigger: 1,
4479
+ variable: 1,
4480
+ dropcolumn: 1,
4481
+ renamecolumn: 1,
4482
+ renameto: 1,
4483
+ to: 1,
4484
+ tocolumnid: 1,
4485
+ forcolumnid: 1,
4486
+ };
4487
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4488
+
4489
+ function checkName(name) {
4490
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4491
+ }
4492
+
4493
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4494
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4495
+ function checkOrder(columns, order) {
4496
+ var aliases = {};
4497
+ (columns || []).forEach(function (col) {
4498
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4499
+ });
4500
+ order.forEach(function (ord) {
4501
+ var e = ord && ord.expression;
4502
+ var name;
4503
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4504
+ if (typeof name === 'string') {
4505
+ if (!hasOwn.call(aliases, name)) checkName(name);
4506
+ return;
4507
+ }
4508
+ checkNames(ord, false, false);
4509
+ });
4510
+ }
4511
+
4512
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4513
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4514
+ // graph item, name is data.
4515
+ function entryOf(node, key) {
4516
+ var yy = alasql.yy;
4517
+ if (
4518
+ (node instanceof yy.Select && key === 'columns') ||
4519
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4520
+ (node.srchid === 'RETURN' && key === 'args')
4521
+ )
4522
+ return 'alias';
4523
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4524
+ return '';
4525
+ }
4526
+
4527
+ // Names that never reach generated code: they are used only at run time, as the key of a
4528
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4529
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4530
+ // names are free (what they hold is still walked), and skipChild are children that are not
4531
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4532
+ var varValueFree = true;
4533
+ var intoVarFree = false;
4534
+ var freeKeys = [
4535
+ ['CreateIndex', {indexid: 1}],
4536
+ ['DropIndex', {indexid: 1}],
4537
+ ['Reindex', {indexid: 1}],
4538
+ ['CreateTrigger', {trigger: 1}],
4539
+ ['DropTrigger', {trigger: 1}],
4540
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4541
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4542
+ ['DetachDatabase', {databaseid: 1}],
4543
+ ['DropDatabase', {databaseid: 1}],
4544
+ ['ShowTables', {databaseid: 1}],
4545
+ ['ShowColumns', {databaseid: 1}],
4546
+ ['ShowIndex', {databaseid: 1}],
4547
+ ['ShowCreateTable', {databaseid: 1}],
4548
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4549
+ ];
4550
+ var freeChild = [
4551
+ ['CreateTable', {table: 1}],
4552
+ ['DropTable', {tables: 1}],
4553
+ ['TruncateTable', {table: 1}],
4554
+ ['AlterTable', {table: 1}],
4555
+ ['ShowColumns', {table: 1}],
4556
+ ['ShowIndex', {table: 1}],
4557
+ ['ShowCreateTable', {table: 1}],
4558
+ ['CreateIndex', {table: 1}],
4559
+ ['CreateTrigger', {table: 1}],
4560
+ ['Declare', {declares: 1}],
4561
+ ];
4562
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4563
+
4564
+ function freeOf(list, node) {
4565
+ var yy = alasql.yy;
4566
+ for (var i = 0; i < list.length; i++) {
4567
+ var cls = yy[list[i][0]];
4568
+ if (cls && node instanceof cls) return list[i][1];
4569
+ }
4570
+ return null;
4571
+ }
4572
+
4573
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4574
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4575
+ function freeVar(node, key, entry) {
4576
+ var yy = alasql.yy;
4577
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4578
+ if (entry === 'source') return false;
4579
+ if (entry === 'into') return intoVarFree;
4580
+ return varValueFree;
4581
+ }
4582
+
4583
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4584
+ function freeSet(node, key) {
4585
+ var yy = alasql.yy;
4586
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4587
+ }
4588
+
4589
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4590
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4591
+ // names never reach generated code.
4592
+ function checkNames(node, data, entry) {
4593
+ if (node === null || typeof node !== 'object') return;
4594
+ var i;
4595
+ if (Array.isArray(node)) {
4596
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4597
+ return;
4598
+ }
4599
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4600
+ if (data && !isNode) {
4601
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4602
+ return;
4603
+ }
4604
+ var yy = alasql.yy;
4605
+ if (node instanceof yy.Json) {
4606
+ checkNames(node.value, true, false);
4607
+ return;
4608
+ }
4609
+ var fk = freeOf(freeKeys, node);
4610
+ var fc = freeOf(freeChild, node);
4611
+ var sc = freeOf(skipChild, node);
4612
+ for (var key in node) {
4613
+ if (!hasOwn.call(node, key)) continue;
4614
+ var value = node[key];
4615
+ if (sc && sc[key] === 1) continue;
4616
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4617
+ checkOrder(node.columns, value);
4618
+ continue;
4619
+ }
4620
+ // the column list of a WITH entry renames the result at run time
4621
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4622
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4623
+ if (typeof value === 'string') {
4624
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4625
+ continue;
4626
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4627
+ checkName(value);
4628
+ } else if (value !== null && typeof value === 'object') {
4629
+ if (Array.isArray(value)) {
4630
+ for (i = 0; i < value.length; i++) {
4631
+ if (typeof value[i] === 'string') {
4632
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4633
+ } else {
4634
+ checkNames(value[i], false, child || entryOf(node, key));
4635
+ }
4636
+ }
4637
+ } else {
4638
+ checkNames(value, false, child);
4639
+ }
4640
+ }
4641
+ }
4642
+ }
4643
+
4644
+ return function (ast) {
4645
+ checkNames(ast, false, false);
4646
+ };
4647
+ })();
4648
+
4457
4649
  /**
4458
4650
  List of engines of external databases
4459
4651
  @type {object}
@@ -7996,7 +8188,7 @@ yy.Select.prototype.compile = function (databaseid, params) {
7996
8188
  // Save data into parameters array
7997
8189
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7998
8190
  //
7999
- query.intofns = "params['" + this.into.param + "'].push(r)";
8191
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
8000
8192
  }
8001
8193
 
8002
8194
  if (query.intofns) {
@@ -8401,7 +8593,7 @@ yy.Select.prototype.compileFrom = function (query) {
8401
8593
  return res;
8402
8594
  };
8403
8595
  } else if (tq instanceof yy.ParamValue) {
8404
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8596
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8405
8597
 
8406
8598
  if (tq.array) ps += ',true';
8407
8599
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8650,7 +8842,7 @@ yy.Select.prototype.compileJoins = function (query) {
8650
8842
  // source.data = ;
8651
8843
  var jnparam = jn.param.param;
8652
8844
 
8653
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8845
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8654
8846
  if (jn.array) ps += ',true';
8655
8847
  ps += ');if(cb)res=cb(res, idx, query);return res';
8656
8848
 
@@ -9400,7 +9592,7 @@ function compileSelectStar(query, aliases, joinstar) {
9400
9592
  } else {
9401
9593
 
9402
9594
  // if column not exist, then copy all
9403
- sp += 'var w=p["' + alias + '"];for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
9595
+ sp += 'var w=p["' + escapeq(alias) + '"];for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
9404
9596
 
9405
9597
  query.dirtyColumns = true;
9406
9598
  }
@@ -9427,7 +9619,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9427
9619
  if (col.columnid === '*') {
9428
9620
  if (col.func) {
9429
9621
  sp +=
9430
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9622
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9431
9623
  } else if (col.tableid) {
9432
9624
  //Copy all
9433
9625
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9479,7 +9671,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9479
9671
  if (isMultisheetParam) {
9480
9672
  sp =
9481
9673
  'var r={};var w=p["' +
9482
- tbid +
9674
+ escapeq(tbid) +
9483
9675
  '"];' +
9484
9676
  'var cols=[' +
9485
9677
  self.columns
@@ -9490,7 +9682,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9490
9682
  '];var colas=[' +
9491
9683
  self.columns
9492
9684
  .map(function (col) {
9493
- return "'" + (col.as || col.columnid) + "'";
9685
+ return "'" + escapeq(col.as || col.columnid) + "'";
9494
9686
  })
9495
9687
  .join(',') +
9496
9688
  '];' +
@@ -9653,9 +9845,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9653
9845
  this.orderColumns.forEach(function (v, idx) {
9654
9846
  var key = '$$$' + idx;
9655
9847
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9656
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9848
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9657
9849
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9658
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9850
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9659
9851
  } else {
9660
9852
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9661
9853
  }
@@ -9748,7 +9940,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9748
9940
  query.groupColumns[colas] = col.nick;
9749
9941
 
9750
9942
  // if(col.as) {
9751
- s += "r['" + colas + "']=";
9943
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9752
9944
  // // } else {
9753
9945
  // // s += 'r[\''+escapeq()+'\']=';
9754
9946
  // // };
@@ -9775,7 +9967,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9775
9967
  self.columns.forEach(function (col) {
9776
9968
 
9777
9969
  if (query.ingroup.indexOf(col.nick) > -1) {
9778
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9970
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9779
9971
  }
9780
9972
  });
9781
9973
 
@@ -9785,7 +9977,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9785
9977
  var key = '$$$' + idx;
9786
9978
 
9787
9979
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9788
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9980
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9789
9981
  } else {
9790
9982
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9791
9983
  }
@@ -10870,7 +11062,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10870
11062
 
10871
11063
  if (this.op === '!') {
10872
11064
  if (typeof this.right === 'string') {
10873
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
11065
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10874
11066
  }
10875
11067
  // TODO - add other cases
10876
11068
  }
@@ -11196,7 +11388,7 @@ yy.ParamValue.prototype.toString = function () {
11196
11388
  };
11197
11389
  yy.ParamValue.prototype.toJS = function () {
11198
11390
  if (typeof this.param === 'string') {
11199
- return "params['" + this.param + "']";
11391
+ return "params['" + escapeq(this.param) + "']";
11200
11392
  }
11201
11393
 
11202
11394
  return 'params[' + this.param + ']';
@@ -12265,9 +12457,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12265
12457
  return s;
12266
12458
  });
12267
12459
 
12460
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12461
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12462
+ // are escaped, so the string cannot end early.
12463
+ function escapeJSONString(s) {
12464
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12465
+ if (c.length === 2) return c;
12466
+ if (c === '\\') return '\\\\';
12467
+ if (c === '"') return '\\"';
12468
+ if (c === '\n') return '\\n';
12469
+ if (c === '\r') return '\\r';
12470
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12471
+ });
12472
+ }
12473
+
12268
12474
  function JSONtoJS(obj, context, tableid, defcols) {
12269
12475
  var s = '';
12270
- if (typeof obj == 'string') s = '"' + obj + '"';
12476
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12271
12477
  else if (typeof obj == 'number') s = '(' + obj + ')';
12272
12478
  else if (typeof obj == 'boolean') s = obj;
12273
12479
  else if (typeof obj == 'object') {
@@ -12286,7 +12492,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12286
12492
  var ss = [];
12287
12493
  for (var k in obj) {
12288
12494
  var s1 = '';
12289
- if (typeof k == 'string') s1 += '"' + k + '"';
12495
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12290
12496
  else if (typeof k == 'number') s1 += k;
12291
12497
  else if (typeof k == 'boolean') s1 += k;
12292
12498
  else {