alasql 3.0.2 → 3.0.3

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  //
4
4
  // Command line interface for Alasql
5
- // Version: 3.0.2
5
+ // Version: 3.0.3
6
6
  // Date: 28.07.2015
7
7
  // (c) 2014-2022, Andrey Gershun & Mathias Wulff
8
8
  //
@@ -1,7 +1,7 @@
1
- //! AlaSQL v3.0.2 build: develop-26e0bfa8 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v3.0.3 build: develop-26e0bfa8 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 3.0.2
4
+ @version 3.0.3
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2023 Andrey Gershun & Mathias Wulff
@@ -1,2 +1,2 @@
1
- //! AlaSQL v3.0.2 build: develop-26e0bfa8 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v3.0.3 build: develop-26e0bfa8 | © 2014-2023 Andrey Gershun & Mathias 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,o){"function"==typeof(e=e||[])&&(scope=o,o=e,e=[]),"object"!=typeof e&&(e=[e]);var t=i.lastid++;i.buffer[t]=o,i.webworker.postMessage({id:t,sql:r,params:e})}var a;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&&(a=function(r,e,n,s){return new Promise(function(o,t){i(r,e,function(r,e){e?t(e):(n&&s&&!1!==i.options.progress&&i.options.progress(n,s),o(r))})})},i.promise=function(r,e){if("undefined"==typeof Promise)throw new Error("Please include a Promise/A+ library");if("string"==typeof r)return a(r,e);if(!isArray(r)||r.length<1||void 0!==e)throw new Error("Error in .promise parameters");var o=r;if(!(o.length<1)){for(var t,n,s=[],i=0;i<o.length;i++){if("string"==typeof(n=o[i])&&(n=[n]),!isArray(n)||n.length<1||2<n.length)throw new Error("Error in .promise parameter");t=n[0],n=n[1]||void 0,s.push(a(t,n,i,o.length))}return Promise.all(s)}}),i=i||!1)return i.worker=function(){throw new Error("Can find webworker in this enviroment")},"undefined"!=typeof Worker&&(i.worker=function(r,e,o){if(void 0===(r=!0===r?void 0:r))for(var t=document.getElementsByTagName("script"),n=0;n<t.length;n++){if("alasql-worker.js"===t[n].src.substr(-16).toLowerCase()){r=t[n].src.substr(0,t[n].src.length-16)+"alasql.js";break}if("alasql-worker.min.js"===t[n].src.substr(-20).toLowerCase()){r=t[n].src.substr(0,t[n].src.length-20)+"alasql.min.js";break}if("alasql.js"===t[n].src.substr(-9).toLowerCase()){r=t[n].src;break}if("alasql.min.js"===t[n].src.substr(-13).toLowerCase()){r=t[n].src.substr(0,t[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=(s="importScripts('")+r+"');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(","),[],o)):!1===r&&delete i.webworker}),i.lastid=0,i.buffer={},i.worker(),i;throw new Error("alasql was not found")});
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v3.0.2 build: develop-26e0bfa8 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v3.0.3 build: develop-26e0bfa8 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 3.0.2
4
+ @version 3.0.3
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2023 Andrey Gershun & Mathias 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 = '3.0.2';
145
+ alasql.version = '3.0.3';
146
146
  alasql.build = 'develop-26e0bfa8';
147
147
 
148
148
  /**
@@ -4455,9 +4455,201 @@ alasql.parser.parseError = function (str, hash) {
4455
4455
  }
4456
4456
  */
4457
4457
  alasql.parse = function (sql) {
4458
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4458
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4459
+ checkSqlNames(ast);
4460
+ return ast;
4459
4461
  };
4460
4462
 
4463
+ /*
4464
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4465
+ holds a quote, a backslash or a line break would end the string and make the rest code
4466
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4467
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4468
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4469
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4470
+ of a JSON value are data, not names.
4471
+ */
4472
+ var checkSqlNames = (function () {
4473
+ var hasOwn = Object.prototype.hasOwnProperty;
4474
+ var badName = /['\\\n\r\u2028\u2029]/;
4475
+ var nameKeys = {
4476
+ as: 1,
4477
+ columnid: 1,
4478
+ tableid: 1,
4479
+ databaseid: 1,
4480
+ viewid: 1,
4481
+ indexid: 1,
4482
+ trigger: 1,
4483
+ variable: 1,
4484
+ dropcolumn: 1,
4485
+ renamecolumn: 1,
4486
+ renameto: 1,
4487
+ to: 1,
4488
+ tocolumnid: 1,
4489
+ forcolumnid: 1,
4490
+ };
4491
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4492
+
4493
+ function checkName(name) {
4494
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4495
+ }
4496
+
4497
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4498
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4499
+ function checkOrder(columns, order) {
4500
+ var aliases = {};
4501
+ (columns || []).forEach(function (col) {
4502
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4503
+ });
4504
+ order.forEach(function (ord) {
4505
+ var e = ord && ord.expression;
4506
+ var name;
4507
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4508
+ if (typeof name === 'string') {
4509
+ if (!hasOwn.call(aliases, name)) checkName(name);
4510
+ return;
4511
+ }
4512
+ checkNames(ord, false, false);
4513
+ });
4514
+ }
4515
+
4516
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4517
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4518
+ // graph item, name is data.
4519
+ function entryOf(node, key) {
4520
+ var yy = alasql.yy;
4521
+ if (
4522
+ (node instanceof yy.Select && key === 'columns') ||
4523
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4524
+ (node.srchid === 'RETURN' && key === 'args')
4525
+ )
4526
+ return 'alias';
4527
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4528
+ return '';
4529
+ }
4530
+
4531
+ // Names that never reach generated code: they are used only at run time, as the key of a
4532
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4533
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4534
+ // names are free (what they hold is still walked), and skipChild are children that are not
4535
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4536
+ var varValueFree = false;
4537
+ var intoVarFree = false;
4538
+ var freeKeys = [
4539
+ ['CreateIndex', {indexid: 1}],
4540
+ ['DropIndex', {indexid: 1}],
4541
+ ['Reindex', {indexid: 1}],
4542
+ ['CreateTrigger', {trigger: 1}],
4543
+ ['DropTrigger', {trigger: 1}],
4544
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4545
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4546
+ ['DetachDatabase', {databaseid: 1}],
4547
+ ['DropDatabase', {databaseid: 1}],
4548
+ ['ShowTables', {databaseid: 1}],
4549
+ ['ShowColumns', {databaseid: 1}],
4550
+ ['ShowIndex', {databaseid: 1}],
4551
+ ['ShowCreateTable', {databaseid: 1}],
4552
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4553
+ ];
4554
+ var freeChild = [
4555
+ ['CreateTable', {table: 1}],
4556
+ ['DropTable', {tables: 1}],
4557
+ ['TruncateTable', {table: 1}],
4558
+ ['AlterTable', {table: 1}],
4559
+ ['ShowColumns', {table: 1}],
4560
+ ['ShowIndex', {table: 1}],
4561
+ ['ShowCreateTable', {table: 1}],
4562
+ ['CreateIndex', {table: 1}],
4563
+ ['CreateTrigger', {table: 1}],
4564
+ ['Declare', {declares: 1}],
4565
+ ];
4566
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4567
+
4568
+ function freeOf(list, node) {
4569
+ var yy = alasql.yy;
4570
+ for (var i = 0; i < list.length; i++) {
4571
+ var cls = yy[list[i][0]];
4572
+ if (cls && node instanceof cls) return list[i][1];
4573
+ }
4574
+ return null;
4575
+ }
4576
+
4577
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4578
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4579
+ function freeVar(node, key, entry) {
4580
+ var yy = alasql.yy;
4581
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4582
+ if (entry === 'source') return false;
4583
+ if (entry === 'into') return intoVarFree;
4584
+ return varValueFree;
4585
+ }
4586
+
4587
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4588
+ function freeSet(node, key) {
4589
+ var yy = alasql.yy;
4590
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4591
+ }
4592
+
4593
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4594
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4595
+ // names never reach generated code.
4596
+ function checkNames(node, data, entry) {
4597
+ if (node === null || typeof node !== 'object') return;
4598
+ var i;
4599
+ if (Array.isArray(node)) {
4600
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4601
+ return;
4602
+ }
4603
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4604
+ if (data && !isNode) {
4605
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4606
+ return;
4607
+ }
4608
+ var yy = alasql.yy;
4609
+ if (node instanceof yy.Json) {
4610
+ checkNames(node.value, true, false);
4611
+ return;
4612
+ }
4613
+ var fk = freeOf(freeKeys, node);
4614
+ var fc = freeOf(freeChild, node);
4615
+ var sc = freeOf(skipChild, node);
4616
+ for (var key in node) {
4617
+ if (!hasOwn.call(node, key)) continue;
4618
+ var value = node[key];
4619
+ if (sc && sc[key] === 1) continue;
4620
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4621
+ checkOrder(node.columns, value);
4622
+ continue;
4623
+ }
4624
+ // the column list of a WITH entry renames the result at run time
4625
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4626
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4627
+ if (typeof value === 'string') {
4628
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4629
+ continue;
4630
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4631
+ checkName(value);
4632
+ } else if (value !== null && typeof value === 'object') {
4633
+ if (Array.isArray(value)) {
4634
+ for (i = 0; i < value.length; i++) {
4635
+ if (typeof value[i] === 'string') {
4636
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4637
+ } else {
4638
+ checkNames(value[i], false, child || entryOf(node, key));
4639
+ }
4640
+ }
4641
+ } else {
4642
+ checkNames(value, false, child);
4643
+ }
4644
+ }
4645
+ }
4646
+ }
4647
+
4648
+ return function (ast) {
4649
+ checkNames(ast, false, false);
4650
+ };
4651
+ })();
4652
+
4461
4653
  /**
4462
4654
  List of engines of external databases
4463
4655
  @type {object}
@@ -7583,7 +7775,7 @@ yy.Select = class Select {
7583
7775
  // Save data into parameters array
7584
7776
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7585
7777
  //
7586
- query.intofns = "params['" + this.into.param + "'].push(r)";
7778
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
7587
7779
  }
7588
7780
 
7589
7781
  if (query.intofns) {
@@ -7995,7 +8187,7 @@ yy.Select.prototype.compileFrom = function (query) {
7995
8187
  return res;
7996
8188
  };
7997
8189
  } else if (tq instanceof yy.ParamValue) {
7998
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8190
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
7999
8191
 
8000
8192
  if (tq.array) ps += ',true';
8001
8193
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8244,7 +8436,7 @@ yy.Select.prototype.compileJoins = function (query) {
8244
8436
  // source.data = ;
8245
8437
  var jnparam = jn.param.param;
8246
8438
 
8247
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8439
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8248
8440
  if (jn.array) ps += ',true';
8249
8441
  ps += ');if(cb)res=cb(res, idx, query);return res';
8250
8442
 
@@ -9062,9 +9254,10 @@ function compileSelectStar(query, aliases, joinstar) {
9062
9254
 
9063
9255
  if (columns && columns.length > 0) {
9064
9256
  columns.forEach(function (tcol) {
9257
+ var escapedColumnId = escapeq(tcol.columnid);
9065
9258
  if (joinstar && alasql.options.joinstar == 'underscore') {
9066
9259
  ss.push(
9067
- "'" + alias + '_' + tcol.columnid + "':p['" + alias + "']['" + tcol.columnid + "']"
9260
+ "'" + alias + '_' + escapedColumnId + "':p['" + alias + "']['" + escapedColumnId + "']"
9068
9261
  );
9069
9262
  } else if (joinstar && alasql.options.joinstar == 'json') {
9070
9263
  // ss.push('\''+alias+'_'+tcol.columnid+'\':p[\''+alias+'\'][\''+tcol.columnid+'\']');
@@ -9072,16 +9265,16 @@ function compileSelectStar(query, aliases, joinstar) {
9072
9265
  "r['" +
9073
9266
  alias +
9074
9267
  "']['" +
9075
- tcol.columnid +
9268
+ escapedColumnId +
9076
9269
  "']=p['" +
9077
9270
  alias +
9078
9271
  "']['" +
9079
- tcol.columnid +
9272
+ escapedColumnId +
9080
9273
  "'];";
9081
9274
  } else {
9082
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
9275
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
9083
9276
  if (!columnIds[tcol.columnid]) {
9084
- var key = "'" + tcol.columnid + "':";
9277
+ var key = "'" + escapedColumnId + "':";
9085
9278
  ss.push(key + value);
9086
9279
  columnIds[tcol.columnid] = {
9087
9280
  id: ss.length - 1,
@@ -9112,7 +9305,7 @@ function compileSelectStar(query, aliases, joinstar) {
9112
9305
  } else {
9113
9306
 
9114
9307
  // if column not exist, then copy all
9115
- 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]};';
9308
+ 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]};';
9116
9309
 
9117
9310
  query.dirtyColumns = true;
9118
9311
  }
@@ -9138,7 +9331,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9138
9331
  if (col.columnid === '*') {
9139
9332
  if (col.func) {
9140
9333
  sp +=
9141
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9334
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9142
9335
  } else if (col.tableid) {
9143
9336
  //Copy all
9144
9337
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9190,7 +9383,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9190
9383
  if (isMultisheetParam) {
9191
9384
  sp =
9192
9385
  'var r={};var w=p["' +
9193
- tbid +
9386
+ escapeq(tbid) +
9194
9387
  '"];' +
9195
9388
  'var cols=[' +
9196
9389
  self.columns
@@ -9201,7 +9394,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9201
9394
  '];var colas=[' +
9202
9395
  self.columns
9203
9396
  .map(function (col) {
9204
- return "'" + (col.as || col.columnid) + "'";
9397
+ return "'" + escapeq(col.as || col.columnid) + "'";
9205
9398
  })
9206
9399
  .join(',') +
9207
9400
  '];' +
@@ -9365,9 +9558,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9365
9558
  this.orderColumns.forEach(function (v, idx) {
9366
9559
  var key = '$$$' + idx;
9367
9560
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9368
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9561
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9369
9562
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9370
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9563
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9371
9564
  } else {
9372
9565
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9373
9566
  }
@@ -9459,7 +9652,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9459
9652
  query.groupColumns[colas] = col.nick;
9460
9653
 
9461
9654
  // if(col.as) {
9462
- s += "r['" + colas + "']=";
9655
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9463
9656
  // // } else {
9464
9657
  // // s += 'r[\''+escapeq()+'\']=';
9465
9658
  // // };
@@ -9486,7 +9679,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9486
9679
  self.columns.forEach(function (col) {
9487
9680
 
9488
9681
  if (query.ingroup.indexOf(col.nick) > -1) {
9489
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9682
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9490
9683
  }
9491
9684
  });
9492
9685
 
@@ -9496,7 +9689,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9496
9689
  var key = '$$$' + idx;
9497
9690
 
9498
9691
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9499
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9692
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9500
9693
  } else {
9501
9694
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9502
9695
  }
@@ -10574,7 +10767,7 @@ yy.Over = class Over {
10574
10767
  var ljs = '(' + leftJS() + '||{})';
10575
10768
 
10576
10769
  if (typeof this.right === 'string') {
10577
- s = ljs + '["' + this.right + '"]';
10770
+ s = ljs + '["' + escapeq(this.right) + '"]';
10578
10771
  } else if (typeof this.right === 'number') {
10579
10772
  s = ljs + '[' + this.right + ']';
10580
10773
  } else if (this.right instanceof yy.FuncValue) {
@@ -10590,7 +10783,7 @@ yy.Over = class Over {
10590
10783
 
10591
10784
  if (this.op === '!') {
10592
10785
  if (typeof this.right === 'string') {
10593
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
10786
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10594
10787
  }
10595
10788
  // TODO - add other cases
10596
10789
  }
@@ -10942,7 +11135,7 @@ yy.Over = class Over {
10942
11135
 
10943
11136
  toJS() {
10944
11137
  if (typeof this.param === 'string') {
10945
- return "params['" + this.param + "']";
11138
+ return "params['" + escapeq(this.param) + "']";
10946
11139
  }
10947
11140
 
10948
11141
  return 'params[' + this.param + ']';
@@ -12014,9 +12207,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12014
12207
  return s;
12015
12208
  });
12016
12209
 
12210
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12211
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12212
+ // are escaped, so the string cannot end early.
12213
+ function escapeJSONString(s) {
12214
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12215
+ if (c.length === 2) return c;
12216
+ if (c === '\\') return '\\\\';
12217
+ if (c === '"') return '\\"';
12218
+ if (c === '\n') return '\\n';
12219
+ if (c === '\r') return '\\r';
12220
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12221
+ });
12222
+ }
12223
+
12017
12224
  function JSONtoJS(obj, context, tableid, defcols) {
12018
12225
  var s = '';
12019
- if (typeof obj == 'string') s = '"' + obj + '"';
12226
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12020
12227
  else if (typeof obj == 'number') s = '(' + obj + ')';
12021
12228
  else if (typeof obj == 'boolean') s = obj;
12022
12229
  else if (typeof obj == 'object') {
@@ -12035,7 +12242,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12035
12242
  var ss = [];
12036
12243
  for (var k in obj) {
12037
12244
  var s1 = '';
12038
- if (typeof k == 'string') s1 += '"' + k + '"';
12245
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12039
12246
  else if (typeof k == 'number') s1 += k;
12040
12247
  else if (typeof k == 'boolean') s1 += k;
12041
12248
  else {