alasql 1.7.4 → 1.7.6

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/dist/alasql.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v1.7.4-develop-89075a50undefined | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v1.7.6 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 1.7.4-develop-89075a50undefined
4
+ @version 1.7.6
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.4-develop-89075a50undefined';
145
+ alasql.version = '1.7.6';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -4022,11 +4022,11 @@ var cloneDeep = (utils.cloneDeep = function cloneDeep(obj) {
4022
4022
  return +obj;
4023
4023
  }
4024
4024
 
4025
- var temp = obj.constructor(); // changed
4025
+ var temp = (Object.prototype.hasOwnProperty.call(obj, 'constructor') ? Object : obj.constructor)(); // changed
4026
4026
 
4027
4027
  for (var key in obj) {
4028
4028
  if (obj.hasOwnProperty(key)) {
4029
- temp[key] = cloneDeep(obj[key]);
4029
+ /* A "__proto__" key from parsed data is copied as data, never as the prototype */ if (key === '__proto__') { Object.defineProperty(temp, key, {value: cloneDeep(obj[key]), writable: true, enumerable: true, configurable: true}); } else { temp[key] = cloneDeep(obj[key]); }
4030
4030
  }
4031
4031
  }
4032
4032
  return temp;
@@ -4098,7 +4098,7 @@ var extend = (utils.extend = function extend(a, b) {
4098
4098
  a = a || {};
4099
4099
  for (var key in b) {
4100
4100
  if (b.hasOwnProperty(key)) {
4101
- a[key] = b[key];
4101
+ /* A "__proto__" key from parsed data is copied as data, never as the prototype */ if (key === '__proto__') { Object.defineProperty(a, key, {value: b[key], writable: true, enumerable: true, configurable: true}); } else { a[key] = b[key]; }
4102
4102
  }
4103
4103
  }
4104
4104
  return a;
@@ -4323,6 +4323,12 @@ var getXLSX = function () {
4323
4323
  return XLSX;
4324
4324
  };
4325
4325
 
4326
+ var jss = JSON.stringify;
4327
+ var reName = /[^a-z\d@-_]+/gi;
4328
+ var nss = (e) => e.replace(reName, '');
4329
+ var reTick = /`/g;
4330
+ var tss = (e) => e.replace(reTick, '');
4331
+
4326
4332
  // set AlaSQl path
4327
4333
  alasql.path = alasql.utils.findAlaSQLPath();
4328
4334
 
@@ -6303,7 +6309,7 @@ yy.Search.prototype.toString = function () {
6303
6309
 
6304
6310
  yy.Search.prototype.toJS = function (context) {
6305
6311
 
6306
- var s = 'this.queriesfn[' + (this.queriesidx - 1) + '](this.params,null,' + context + ')';
6312
+ var s = 'this.queriesfn[' + jss(this.queriesidx - 1) + '](this.params,null,' + jss(context) + ')';
6307
6313
  // var s = '';
6308
6314
  return s;
6309
6315
  };
@@ -6664,11 +6670,11 @@ alasql.srch.SET = function (val, args, stope, params) {
6664
6670
  .map(function (st) {
6665
6671
 
6666
6672
  if (st.method === '@') {
6667
- return "alasql.vars['" + st.variable + "']=" + st.expression.toJS('x', '');
6673
+ return 'alasql.vars[' + jss(st.variable) + ']=' + st.expression.toJS('x', '');
6668
6674
  } else if (st.method === '$') {
6669
- return "params['" + st.variable + "']=" + st.expression.toJS('x', '');
6675
+ return 'params[' + jss(st.variable) + ']=' + st.expression.toJS('x', '');
6670
6676
  } else {
6671
- return "x['" + st.column.columnid + "']=" + st.expression.toJS('x', '');
6677
+ return 'x[' + jss(st.column.columnid) + ']=' + st.expression.toJS('x', '');
6672
6678
  }
6673
6679
  })
6674
6680
  .join(';');
@@ -6755,17 +6761,26 @@ var compileSearchOrder = function (order) {
6755
6761
  s += 'if(a' + dg + '==b' + dg + '){';
6756
6762
  } else {
6757
6763
  s +=
6758
- "if((a['" +
6759
- columnid +
6760
- "']||'')" +
6764
+ 'if((a[' +
6765
+ jss(columnid) +
6766
+ "]||'')" +
6761
6767
  dg +
6762
6768
  (ord.direction === 'ASC' ? '>' : '<') +
6763
- "(b['" +
6764
- columnid +
6765
- "']||'')" +
6769
+ '(b[' +
6770
+ jss(columnid) +
6771
+ "]||'')" +
6766
6772
  dg +
6767
6773
  ')return 1;';
6768
- s += "if((a['" + columnid + "']||'')" + dg + "==(b['" + columnid + "']||'')" + dg + '){';
6774
+ s +=
6775
+ 'if((a[' +
6776
+ jss(columnid) +
6777
+ "]||'')" +
6778
+ dg +
6779
+ '==(b[' +
6780
+ jss(columnid) +
6781
+ "]||'')" +
6782
+ dg +
6783
+ '){';
6769
6784
  }
6770
6785
  } else {
6771
6786
  dg = '.valueOf()';
@@ -9056,7 +9071,7 @@ yy.Select.prototype.compileGroup = function (query) {
9056
9071
  aft2 = '';
9057
9072
 
9058
9073
  if (typeof query.groupStar !== 'undefined') {
9059
- aft2 += "for(var f in p['" + query.groupStar + "']) {g[f]=p['" + query.groupStar + "'][f];};";
9074
+ aft2 += "for(var f in p['" + query.groupStar + "']) {if(/^__proto__$/.test(f))Object.defineProperty(g,f,{writable:true,enumerable:true,configurable:true});g[f]=p['" + query.groupStar + "'][f];};";
9060
9075
  }
9061
9076
 
9062
9077
  /*
@@ -9329,9 +9344,10 @@ function compileSelectStar(query, aliases, joinstar) {
9329
9344
 
9330
9345
  if (columns && columns.length > 0) {
9331
9346
  columns.forEach(function (tcol) {
9347
+ var escapedColumnId = escapeq(tcol.columnid);
9332
9348
  if (joinstar && alasql.options.joinstar == 'underscore') {
9333
9349
  ss.push(
9334
- "'" + alias + '_' + tcol.columnid + "':p['" + alias + "']['" + tcol.columnid + "']"
9350
+ "'" + alias + '_' + escapedColumnId + "':p['" + alias + "']['" + escapedColumnId + "']"
9335
9351
  );
9336
9352
  } else if (joinstar && alasql.options.joinstar == 'json') {
9337
9353
  // ss.push('\''+alias+'_'+tcol.columnid+'\':p[\''+alias+'\'][\''+tcol.columnid+'\']');
@@ -9339,16 +9355,16 @@ function compileSelectStar(query, aliases, joinstar) {
9339
9355
  "r['" +
9340
9356
  alias +
9341
9357
  "']['" +
9342
- tcol.columnid +
9358
+ escapedColumnId +
9343
9359
  "']=p['" +
9344
9360
  alias +
9345
9361
  "']['" +
9346
- tcol.columnid +
9362
+ escapedColumnId +
9347
9363
  "'];";
9348
9364
  } else {
9349
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
9365
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
9350
9366
  if (!columnIds[tcol.columnid]) {
9351
- var key = "'" + tcol.columnid + "':";
9367
+ var key = "'" + escapedColumnId + "':";
9352
9368
  ss.push(key + value);
9353
9369
  columnIds[tcol.columnid] = {
9354
9370
  id: ss.length - 1,
@@ -9379,7 +9395,7 @@ function compileSelectStar(query, aliases, joinstar) {
9379
9395
  } else {
9380
9396
 
9381
9397
  // if column not exist, then copy all
9382
- sp += 'var w=p["' + alias + '"];for(var k in w){r[k]=w[k]};';
9398
+ 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]};';
9383
9399
 
9384
9400
  query.dirtyColumns = true;
9385
9401
  }
@@ -9387,7 +9403,7 @@ function compileSelectStar(query, aliases, joinstar) {
9387
9403
 
9388
9404
  });
9389
9405
 
9390
- return {s: ss.join(','), sp: sp};
9406
+ return { s: ss.join(','), sp: sp };
9391
9407
  }
9392
9408
 
9393
9409
  yy.Select.prototype.compileSelect1 = function (query, params) {
@@ -9438,14 +9454,14 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9438
9454
  if (false && tbid && !query.defcols['.'][col.tableid] && !query.defcols[col.columnid]) {
9439
9455
  ss.push(
9440
9456
  "'" +
9441
- escapeq(col.as || col.columnid) +
9442
- "':p['" +
9443
- query.defaultTableid +
9444
- "']['" +
9445
- col.tableid +
9446
- "']['" +
9447
- col.columnid +
9448
- "']"
9457
+ escapeq(col.as || col.columnid) +
9458
+ "':p['" +
9459
+ query.defaultTableid +
9460
+ "']['" +
9461
+ col.tableid +
9462
+ "']['" +
9463
+ col.columnid +
9464
+ "']"
9449
9465
  );
9450
9466
  } else {
9451
9467
  // workaround for multisheet xlsx export with custom COLUMNS
@@ -9480,12 +9496,12 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9480
9496
  } else {
9481
9497
  ss.push(
9482
9498
  "'" +
9483
- escapeq(col.as || col.columnid) +
9484
- "':p['" +
9485
- tbid +
9486
- "']['" +
9487
- col.columnid +
9488
- "']"
9499
+ escapeq(col.as || col.columnid) +
9500
+ "':p['" +
9501
+ tbid +
9502
+ "']['" +
9503
+ col.columnid +
9504
+ "']"
9489
9505
  );
9490
9506
  }
9491
9507
  }
@@ -9570,9 +9586,9 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9570
9586
  ) {
9571
9587
  ss.push(
9572
9588
  "'" +
9573
- escapeq(col.as) +
9574
- "':" +
9575
- n2u(col.expression.toJS('p', query.defaultTableid, query.defcols))
9589
+ escapeq(col.as) +
9590
+ "':" +
9591
+ n2u(col.expression.toJS('p', query.defaultTableid, query.defcols))
9576
9592
  );
9577
9593
  } else if (col.aggregatorid === 'COUNT') {
9578
9594
  ss.push("'" + escapeq(col.as) + "':1");
@@ -9602,9 +9618,9 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9602
9618
 
9603
9619
  ss.push(
9604
9620
  "'" +
9605
- escapeq(col.as || col.columnid || col.toString()) +
9606
- "':" +
9607
- n2u(col.toJS('p', query.defaultTableid, query.defcols))
9621
+ escapeq(col.as || col.columnid || col.toString()) +
9622
+ "':" +
9623
+ n2u(col.toJS('p', query.defaultTableid, query.defcols))
9608
9624
  );
9609
9625
  // ss.push('\''+escapeq(col.toString())+'\':'+col.toJS("p",query.defaultTableid));
9610
9626
  //if(col instanceof yy.Expression) {
@@ -9708,10 +9724,10 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9708
9724
  self.columns.forEach(function (col) {
9709
9725
 
9710
9726
  if (col instanceof yy.Column && col.columnid === '*') {
9711
- // s += 'for(var k in g){r[k]=g[k]};';
9727
+ // s += 'for(var k in g){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=g[k]};';
9712
9728
  // s += 'for(var k in this.query.groupColumns){r[k]=g[this.query.groupColumns[k]]};';
9713
9729
 
9714
- s += 'for(var k in g) {r[k]=g[k]};';
9730
+ s += 'for(var k in g) {if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=g[k]};';
9715
9731
  return '';
9716
9732
 
9717
9733
  } else {
@@ -10457,8 +10473,8 @@ yy.Over.prototype.toString = function () {
10457
10473
  */
10458
10474
 
10459
10475
  /**
10460
- Expression statement ( = 2*2; )
10461
- @class
10476
+ Expression statement ( = 2*2; )
10477
+ @class
10462
10478
  @param {object} params Initial parameters
10463
10479
  */
10464
10480
  yy.ExpressionStatement = function (params) {
@@ -10833,7 +10849,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10833
10849
  var ljs = '(' + leftJS() + '||{})';
10834
10850
 
10835
10851
  if (typeof this.right === 'string') {
10836
- s = ljs + '["' + this.right + '"]';
10852
+ s = ljs + '["' + escapeq(this.right) + '"]';
10837
10853
  } else if (typeof this.right === 'number') {
10838
10854
  s = ljs + '[' + this.right + ']';
10839
10855
  } else if (this.right instanceof yy.FuncValue) {
@@ -11064,7 +11080,7 @@ yy.VarValue.prototype.toType = function () {
11064
11080
  };
11065
11081
 
11066
11082
  yy.VarValue.prototype.toJS = function () {
11067
- return "alasql.vars['" + this.variable + "']";
11083
+ return "alasql.vars['" + escapeq(this.variable) + "']";
11068
11084
  };
11069
11085
 
11070
11086
  yy.NumValue = function (params) {
@@ -11458,7 +11474,7 @@ yy.AggrValue.prototype.toJS = function (/*context, tableid, defcols*/) {
11458
11474
 
11459
11475
  var colas = this.nick;
11460
11476
  if (colas === undefined) {
11461
- colas = this.toString();
11477
+ colas = escapeq(this.toString());
11462
11478
  }
11463
11479
  return "g['" + colas + "']";
11464
11480
  };
@@ -13028,7 +13044,7 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) {
13028
13044
 
13029
13045
  table.update(
13030
13046
  function (t) {
13031
- for (var f in r) t[f] = r[f];
13047
+ for (var f in r) {if(/^__proto__$/.test(f))Object.defineProperty(t,f,{writable:true,enumerable:true,configurable:true});t[f] = r[f];}
13032
13048
  },
13033
13049
  table.data.indexOf(toreplace),
13034
13050
  params
@@ -14574,7 +14590,7 @@ yy.Insert.prototype.compile = function (databaseid) {
14574
14590
  });
14575
14591
  } else {
14576
14592
 
14577
- // sw = 'var w='+JSONtoJS(values)+';for(var k in w){r[k]=w[k]};';
14593
+ // sw = 'var w='+JSONtoJS(values)+';for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
14578
14594
  sw = JSONtoJS(values);
14579
14595
  }
14580
14596
  }
@@ -17170,8 +17186,7 @@ alasql.into.XLSX = function (filename, opts, data, columns, cb) {
17170
17186
  var colm = 1,
17171
17187
  rowm = 1;
17172
17188
  }
17173
- var zeroColumnFix = columns.length ? 0 : 1;
17174
- var colmax = Math.max(col0 + columns.length - 1 + zeroColumnFix, colm);
17189
+ var colmax = Math.max(col0 + columns.length, colm);
17175
17190
  var rowmax = Math.max(row0 + dataLength + 2, rowm);
17176
17191
 
17177
17192
  var i = row0 + 1;
@@ -18760,6 +18775,7 @@ LS.dropDatabase = function (lsdbid, ifexists, cb) {
18760
18775
 
18761
18776
  LS.attachDatabase = function (lsdbid, databaseid, args, params, cb) {
18762
18777
  var res = 1;
18778
+ // console.warn({lsdbid, databaseid, args, params, cb, dbs: Object.keys(alasql.databases)})
18763
18779
  if (alasql.databases[databaseid]) {
18764
18780
  throw new Error('Unable to attach database as "' + databaseid + '" because it already exists');
18765
18781
  }