alasql 2.5.0 → 2.5.2

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.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v2.4.2 build: develop-b8c7bd59 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v2.5.1 build: develop-a60aa658 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 2.4.2
4
+ @version 2.5.1
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2023 Andrey Gershun & Mathias Wulff
@@ -36,7 +36,7 @@ SOFTWARE.
36
36
 
37
37
  /**
38
38
  @fileoverview AlaSQL JavaScript SQL library
39
- @see http://github.com/agershun/alasql
39
+ @see http://github.com/alasql/alasql
40
40
  */
41
41
 
42
42
  /**
@@ -142,8 +142,8 @@ var alasql = function(sql, params, cb, scope) {
142
142
  Current version of alasql
143
143
  @constant {string}
144
144
  */
145
- alasql.version = '2.4.2';
146
- alasql.build = 'develop-b8c7bd59';
145
+ alasql.version = '2.5.1';
146
+ alasql.build = 'develop-a60aa658';
147
147
 
148
148
  /**
149
149
  Debug flag
@@ -4480,61 +4480,79 @@ alasql.databasenum = 0;
4480
4480
  /**
4481
4481
  Alasql options object
4482
4482
  */
4483
- alasql.options = {};
4484
- alasql.options.errorlog = false; // Log or throw error
4485
- alasql.options.valueof = false; // Use valueof in orderfn
4486
- alasql.options.dropifnotexists = false; // DROP database in any case
4487
- alasql.options.datetimeformat = 'sql'; // How to handle DATE and DATETIME types
4488
- // Another value is 'javascript'
4489
- alasql.options.casesensitive = true; // Table and column names are case sensitive and converted to lower-case
4490
- alasql.options.logtarget = 'output'; // target for log. Values: 'console', 'output', 'id' of html tag
4491
- alasql.options.logprompt = true; // Print SQL at log
4483
+ alasql.options = {
4484
+ /** Log or throw error */
4485
+ errorlog: false,
4492
4486
 
4493
- alasql.options.progress = false; // Callback for async queries progress
4487
+ /** Use valueof in orderfn */
4488
+ valueof: false,
4494
4489
 
4495
- // Default modifier
4496
- // values: RECORDSET, VALUE, ROW, COLUMN, MATRIX, TEXTSTRING, INDEX
4497
- alasql.options.modifier = undefined;
4498
- // How many rows to lookup to define columns
4499
- alasql.options.columnlookup = 10;
4500
- // Create vertex if not found
4501
- alasql.options.autovertex = true;
4490
+ /** DROP database in any case */
4491
+ dropifnotexists: false,
4502
4492
 
4503
- // Use dbo as current database (for partial T-SQL comaptibility)
4504
- alasql.options.usedbo = true;
4493
+ /** How to handle DATE and DATETIME types */
4494
+ datetimeformat: 'sql',
4505
4495
 
4506
- // AUTOCOMMIT ON | OFF
4507
- alasql.options.autocommit = true;
4496
+ /** Table and column names are case sensitive and converted to lower-case */
4497
+ casesensitive: true,
4508
4498
 
4509
- // Use cache
4510
- alasql.options.cache = true;
4499
+ /** target for log. Values: 'console', 'output', 'id' of html tag */
4500
+ logtarget: 'output',
4511
4501
 
4512
- // Compatibility flags
4513
- alasql.options.tsql = true;
4502
+ /** Print SQL at log */
4503
+ logprompt: true,
4514
4504
 
4515
- alasql.options.mysql = true;
4505
+ /** Callback for async queries progress */
4506
+ progress: false,
4516
4507
 
4517
- alasql.options.postgres = true;
4508
+ /**
4509
+ * Default modifier
4510
+ * values: RECORDSET, VALUE, ROW, COLUMN, MATRIX, TEXTSTRING, INDEX
4511
+ * @type {'RECORDSET'|'VALUE'|'ROW'|'COLUMN'|'MATRIX'|'TEXTSTRING'|'INDEX'|undefined}
4512
+ */
4513
+ modifier: undefined,
4514
+
4515
+ /** How many rows to lookup to define columns */
4516
+ columnlookup: 10,
4517
+
4518
+ /** Create vertex if not found */
4519
+ autovertex: true,
4520
+
4521
+ /** Use dbo as current database (for partial T-SQL comaptibility) */
4522
+ usedbo: true,
4518
4523
 
4519
- alasql.options.oracle = true;
4524
+ /** AUTOCOMMIT ON | OFF */
4525
+ autocommit: true,
4520
4526
 
4521
- alasql.options.sqlite = true;
4527
+ /** Use cache */
4528
+ cache: true,
4522
4529
 
4523
- alasql.options.orientdb = true;
4530
+ /** Compatibility flags */
4531
+ tsql: true,
4524
4532
 
4525
- // for SET NOCOUNT OFF
4526
- alasql.options.nocount = false;
4533
+ mysql: true,
4527
4534
 
4528
- // Check for NaN and convert it to undefined
4529
- alasql.options.nan = false;
4535
+ postgres: true,
4530
4536
 
4531
- alasql.options.excel = {cellDates: true};
4537
+ oracle: true,
4532
4538
 
4533
- alasql.options.joinstar = 'overwrite'; // Option for SELECT * FROM a,b
4539
+ sqlite: true,
4534
4540
 
4535
- alasql.options.loopbreak = 100000;
4541
+ orientdb: true,
4536
4542
 
4537
- alasql.options;
4543
+ /** for SET NOCOUNT OFF */
4544
+ nocount: false,
4545
+
4546
+ /** Check for NaN and convert it to undefined */
4547
+ nan: false,
4548
+
4549
+ excel: {cellDates: true},
4550
+
4551
+ /** Option for SELECT * FROM a,b */
4552
+ joinstar: 'overwrite',
4553
+
4554
+ loopbreak: 100000,
4555
+ };
4538
4556
 
4539
4557
  //alasql.options.worker = false;
4540
4558
 
@@ -5135,15 +5153,19 @@ Table.prototype.indexColumns = function () {
5135
5153
  */
5136
5154
 
5137
5155
  // Table class
5138
- var View = (alasql.View = function (params) {
5139
- // Columns
5140
- this.columns = [];
5141
- this.xcolumns = {};
5142
- // Data array
5143
- this.query = [];
5156
+ class View {
5157
+ constructor(params) {
5158
+ // Columns
5159
+ this.columns = [];
5160
+ this.xcolumns = {};
5161
+ // Data array
5162
+ this.query = [];
5144
5163
 
5145
- Object.assign(this, params);
5146
- });
5164
+ Object.assign(this, params);
5165
+ }
5166
+ }
5167
+
5168
+ alasql.View = View;
5147
5169
 
5148
5170
  /*
5149
5171
  //
@@ -5158,25 +5180,32 @@ var View = (alasql.View = function (params) {
5158
5180
  /**
5159
5181
  @class Query Main query class
5160
5182
  */
5161
- var Query = (alasql.Query = function (params) {
5162
- this.alasql = alasql;
5183
+ class Query {
5184
+ constructor(params) {
5185
+ this.alasql = alasql;
5163
5186
 
5164
- // Columns
5165
- this.columns = [];
5166
- this.xcolumns = {};
5167
- this.selectGroup = [];
5168
- this.groupColumns = {};
5169
- // Data array
5170
- extend(this, params);
5171
- });
5187
+ // Columns
5188
+ this.columns = [];
5189
+ this.xcolumns = {};
5190
+ this.selectGroup = [];
5191
+ this.groupColumns = {};
5192
+ // Data array
5193
+ Object.assign(this, params);
5194
+ }
5195
+ }
5172
5196
 
5173
5197
  /**
5174
5198
  @class Recordset data object
5175
5199
  */
5176
- var Recordset = (alasql.Recordset = function (params) {
5177
- // Data array
5178
- extend(this, params);
5179
- });
5200
+ class Recordset {
5201
+ constructor(params) {
5202
+ // Data array
5203
+ Object.assign(this, params);
5204
+ }
5205
+ }
5206
+
5207
+ alasql.Recordset = Recordset;
5208
+ alasql.Query = Query;
5180
5209
 
5181
5210
  /*
5182
5211
  //
@@ -7222,388 +7251,420 @@ function swapSources(query, h) {
7222
7251
 
7223
7252
  /* global yy */
7224
7253
 
7225
- yy.Select = function (params) {
7226
- return Object.assign(this, params);
7227
- };
7228
- yy.Select.prototype.toString = function () {
7229
- var s;
7230
- s = '';
7231
- if (this.explain) {
7232
- s += 'EXPLAIN ';
7233
- }
7234
- s += 'SELECT ';
7235
- if (this.modifier) {
7236
- s += this.modifier + ' ';
7237
- }
7238
- if (this.distinct) {
7239
- s += 'DISTINCT ';
7254
+ yy.Select = class Select {
7255
+ constructor(params) {
7256
+ Object.assign(this, params);
7240
7257
  }
7241
- if (this.top) {
7242
- s += 'TOP ' + this.top.value + ' ';
7243
- if (this.percent) {
7244
- s += 'PERCENT ';
7258
+
7259
+ toString() {
7260
+ var s;
7261
+ s = '';
7262
+ if (this.explain) {
7263
+ s += 'EXPLAIN ';
7245
7264
  }
7246
- }
7247
- s += this.columns
7248
- .map(function (col) {
7249
- var s;
7250
- s = col.toString();
7251
- if (typeof col.as !== 'undefined') {
7252
- s += ' AS ' + col.as;
7265
+ s += 'SELECT ';
7266
+ if (this.modifier) {
7267
+ s += this.modifier + ' ';
7268
+ }
7269
+ if (this.distinct) {
7270
+ s += 'DISTINCT ';
7271
+ }
7272
+ if (this.top) {
7273
+ s += 'TOP ' + this.top.value + ' ';
7274
+ if (this.percent) {
7275
+ s += 'PERCENT ';
7253
7276
  }
7254
- return s;
7255
- })
7256
- .join(', ');
7257
- if (this.from) {
7258
- s +=
7259
- ' FROM ' +
7260
- this.from
7261
- .map(function (f) {
7277
+ }
7278
+ s += this.columns
7279
+ .map(function (col) {
7280
+ var s;
7281
+ s = col.toString();
7282
+ if (typeof col.as !== 'undefined') {
7283
+ s += ' AS ' + col.as;
7284
+ }
7285
+ return s;
7286
+ })
7287
+ .join(', ');
7288
+ if (this.from) {
7289
+ s +=
7290
+ ' FROM ' +
7291
+ this.from
7292
+ .map(function (f) {
7293
+ var ss;
7294
+ ss = f.toString();
7295
+ if (f.as) {
7296
+ ss += ' AS ' + f.as;
7297
+ }
7298
+ return ss;
7299
+ })
7300
+ .join(',');
7301
+ }
7302
+ if (this.joins) {
7303
+ s += this.joins
7304
+ .map(function (jn) {
7262
7305
  var ss;
7263
- ss = f.toString();
7264
- if (f.as) {
7265
- ss += ' AS ' + f.as;
7306
+ ss = ' ';
7307
+ if (jn.joinmode) {
7308
+ ss += jn.joinmode + ' ';
7309
+ }
7310
+ if (jn.table) {
7311
+ ss += 'JOIN ' + jn.table.toString();
7312
+ } else if (jn.select) {
7313
+ ss += 'JOIN (' + jn.select.toString() + ')';
7314
+ } else if (jn instanceof alasql.yy.Apply) {
7315
+ ss += jn.toString();
7316
+ } else {
7317
+ throw new Error('Wrong type in JOIN mode');
7318
+ }
7319
+ if (jn.as) {
7320
+ ss += ' AS ' + jn.as;
7321
+ }
7322
+ if (jn.using) {
7323
+ ss += ' USING ' + jn.using.toString();
7324
+ }
7325
+ if (jn.on) {
7326
+ ss += ' ON ' + jn.on.toString();
7266
7327
  }
7267
7328
  return ss;
7268
7329
  })
7269
- .join(',');
7270
- }
7271
- if (this.joins) {
7272
- s += this.joins
7273
- .map(function (jn) {
7274
- var ss;
7275
- ss = ' ';
7276
- if (jn.joinmode) {
7277
- ss += jn.joinmode + ' ';
7278
- }
7279
- if (jn.table) {
7280
- ss += 'JOIN ' + jn.table.toString();
7281
- } else if (jn.select) {
7282
- ss += 'JOIN (' + jn.select.toString() + ')';
7283
- } else if (jn instanceof alasql.yy.Apply) {
7284
- ss += jn.toString();
7285
- } else {
7286
- throw new Error('Wrong type in JOIN mode');
7287
- }
7288
- if (jn.as) {
7289
- ss += ' AS ' + jn.as;
7290
- }
7291
- if (jn.using) {
7292
- ss += ' USING ' + jn.using.toString();
7293
- }
7294
- if (jn.on) {
7295
- ss += ' ON ' + jn.on.toString();
7296
- }
7297
- return ss;
7298
- })
7299
- .join('');
7300
- }
7301
- if (this.where) {
7302
- s += ' WHERE ' + this.where.toString();
7303
- }
7304
- if (this.group && this.group.length > 0) {
7305
- s +=
7306
- ' GROUP BY ' +
7307
- this.group
7308
- .map(function (grp) {
7309
- return grp.toString();
7310
- })
7311
- .join(', ');
7312
- }
7313
- if (this.having) {
7314
- s += ' HAVING ' + this.having.toString();
7315
- }
7316
- if (this.order && this.order.length > 0) {
7317
- s +=
7318
- ' ORDER BY ' +
7319
- this.order
7320
- .map(function (ord) {
7321
- return ord.toString();
7322
- })
7323
- .join(', ');
7324
- }
7325
- if (this.limit) {
7326
- s += ' LIMIT ' + this.limit.value;
7327
- }
7328
- if (this.offset) {
7329
- s += ' OFFSET ' + this.offset.value;
7330
- }
7331
- if (this.union) {
7332
- s += ' UNION ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.union.toString();
7333
- }
7334
- if (this.unionall) {
7335
- s += ' UNION ALL ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.unionall.toString();
7336
- }
7337
- if (this.except) {
7338
- s += ' EXCEPT ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.except.toString();
7339
- }
7340
- if (this.intersect) {
7341
- s += ' INTERSECT ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.intersect.toString();
7330
+ .join('');
7331
+ }
7332
+ if (this.where) {
7333
+ s += ' WHERE ' + this.where.toString();
7334
+ }
7335
+ if (this.group && this.group.length > 0) {
7336
+ s +=
7337
+ ' GROUP BY ' +
7338
+ this.group
7339
+ .map(function (grp) {
7340
+ return grp.toString();
7341
+ })
7342
+ .join(', ');
7343
+ }
7344
+ if (this.having) {
7345
+ s += ' HAVING ' + this.having.toString();
7346
+ }
7347
+ if (this.order && this.order.length > 0) {
7348
+ s +=
7349
+ ' ORDER BY ' +
7350
+ this.order
7351
+ .map(function (ord) {
7352
+ return ord.toString();
7353
+ })
7354
+ .join(', ');
7355
+ }
7356
+ if (this.limit) {
7357
+ s += ' LIMIT ' + this.limit.value;
7358
+ }
7359
+ if (this.offset) {
7360
+ s += ' OFFSET ' + this.offset.value;
7361
+ }
7362
+ if (this.union) {
7363
+ s += ' UNION ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.union.toString();
7364
+ }
7365
+ if (this.unionall) {
7366
+ s += ' UNION ALL ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.unionall.toString();
7367
+ }
7368
+ if (this.except) {
7369
+ s += ' EXCEPT ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.except.toString();
7370
+ }
7371
+ if (this.intersect) {
7372
+ s += ' INTERSECT ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.intersect.toString();
7373
+ }
7374
+ return s;
7342
7375
  }
7343
- return s;
7344
- };
7345
-
7346
- /**
7347
- Select statement in expression
7348
- */
7349
- yy.Select.prototype.toJS = function (context) {
7350
7376
 
7351
- // if(this.expression.reduced) return 'true';
7352
- // return this.expression.toJS(context, tableid, defcols);
7353
-
7354
- // var s = 'this.queriesdata['+(this.queriesidx-1)+'][0]';
7377
+ /**
7378
+ Select statement in expression
7379
+ */
7380
+ toJS(context) {
7355
7381
 
7356
- var s =
7357
- 'alasql.utils.flatArray(this.queriesfn[' +
7358
- (this.queriesidx - 1) +
7359
- '](this.params,null,' +
7360
- context +
7361
- '))[0]';
7382
+ // if(this.expression.reduced) return 'true';
7383
+ // return this.expression.toJS(context, tableid, defcols);
7362
7384
 
7363
- return s;
7364
- };
7385
+ // var s = 'this.queriesdata['+(this.queriesidx-1)+'][0]';
7386
+ var s =
7387
+ 'alasql.utils.flatArray(this.queriesfn[' +
7388
+ (this.queriesidx - 1) +
7389
+ '](this.params,null,' +
7390
+ context +
7391
+ '))[0]';
7365
7392
 
7366
- // Compile SELECT statement
7367
- yy.Select.prototype.compile = function (databaseid, params) {
7368
- var db = alasql.databases[databaseid];
7369
- // Create variable for query
7370
- var query = new Query();
7393
+ return s;
7394
+ }
7371
7395
 
7372
- // Array with columns to be removed
7373
- query.removeKeys = [];
7374
- query.aggrKeys = [];
7396
+ // Compile SELECT statement
7397
+ compile(databaseid, params) {
7398
+ var db = alasql.databases[databaseid];
7399
+ // Create variable for query
7400
+ var query = new Query();
7375
7401
 
7376
- query.explain = this.explain; // Explain
7377
- query.explaination = [];
7378
- query.explid = 1;
7402
+ // Array with columns to be removed
7403
+ query.removeKeys = [];
7404
+ query.aggrKeys = [];
7379
7405
 
7380
- query.modifier = this.modifier;
7406
+ query.explain = this.explain; // Explain
7407
+ query.explaination = [];
7408
+ query.explid = 1;
7381
7409
 
7382
- query.database = db;
7383
- // 0. Precompile whereexists
7384
- this.compileWhereExists(query);
7410
+ query.modifier = this.modifier;
7385
7411
 
7386
- // 0. Precompile queries for IN, NOT IN, ANY and ALL operators
7387
- this.compileQueries(query);
7412
+ query.database = db;
7413
+ // 0. Precompile whereexists
7414
+ this.compileWhereExists(query);
7388
7415
 
7389
- query.defcols = this.compileDefCols(query, databaseid);
7416
+ // 0. Precompile queries for IN, NOT IN, ANY and ALL operators
7417
+ this.compileQueries(query);
7390
7418
 
7391
- // 1. Compile FROM clause
7392
- query.fromfn = this.compileFrom(query);
7419
+ query.defcols = this.compileDefCols(query, databaseid);
7393
7420
 
7394
- // 2. Compile JOIN clauses
7395
- if (this.joins) {
7396
- this.compileJoins(query);
7397
- }
7421
+ // 1. Compile FROM clause
7422
+ query.fromfn = this.compileFrom(query);
7398
7423
 
7399
- // todo?: 3. Compile SELECT clause
7424
+ // 2. Compile JOIN clauses
7425
+ if (this.joins) {
7426
+ this.compileJoins(query);
7427
+ }
7400
7428
 
7401
- // For ROWNUM()
7402
- query.rownums = [];
7429
+ // todo?: 3. Compile SELECT clause
7430
+ // For ROWNUM()
7431
+ query.rownums = [];
7403
7432
 
7404
- this.compileSelectGroup0(query);
7433
+ this.compileSelectGroup0(query);
7405
7434
 
7406
- if (this.group || query.selectGroup.length > 0) {
7407
- query.selectgfns = this.compileSelectGroup1(query);
7408
- } else {
7409
- query.selectfns = this.compileSelect1(query, params);
7410
- }
7435
+ if (this.group || query.selectGroup.length > 0) {
7436
+ query.selectgfns = this.compileSelectGroup1(query);
7437
+ } else {
7438
+ query.selectfns = this.compileSelect1(query, params);
7439
+ }
7411
7440
 
7412
- // Remove columns clause
7413
- this.compileRemoveColumns(query);
7441
+ // Remove columns clause
7442
+ this.compileRemoveColumns(query);
7414
7443
 
7415
- // 5. Optimize WHERE and JOINS
7416
- if (this.where) {
7417
- this.compileWhereJoins(query);
7418
- }
7444
+ // 5. Optimize WHERE and JOINS
7445
+ if (this.where) {
7446
+ this.compileWhereJoins(query);
7447
+ }
7419
7448
 
7420
- // 4. Compile WHERE clause
7421
- query.wherefn = this.compileWhere(query);
7449
+ // 4. Compile WHERE clause
7450
+ query.wherefn = this.compileWhere(query);
7422
7451
 
7423
- // 6. Compile GROUP BY
7424
- if (this.group || query.selectGroup.length > 0) {
7425
- query.groupfn = this.compileGroup(query);
7426
- }
7452
+ // 6. Compile GROUP BY
7453
+ if (this.group || query.selectGroup.length > 0) {
7454
+ query.groupfn = this.compileGroup(query);
7455
+ }
7427
7456
 
7428
- // 6. Compile HAVING
7429
- if (this.having) {
7430
- query.havingfn = this.compileHaving(query);
7431
- }
7457
+ // 6. Compile HAVING
7458
+ if (this.having) {
7459
+ query.havingfn = this.compileHaving(query);
7460
+ }
7432
7461
 
7433
- // 8. Compile ORDER BY clause
7434
- if (this.order) {
7435
- query.orderfn = this.compileOrder(query, params);
7436
- }
7462
+ // 8. Compile ORDER BY clause
7463
+ if (this.order) {
7464
+ query.orderfn = this.compileOrder(query, params);
7465
+ }
7437
7466
 
7438
- if (this.group || query.selectGroup.length > 0) {
7439
- query.selectgfn = this.compileSelectGroup2(query);
7440
- } else {
7441
- query.selectfn = this.compileSelect2(query, params);
7442
- }
7467
+ if (this.group || query.selectGroup.length > 0) {
7468
+ query.selectgfn = this.compileSelectGroup2(query);
7469
+ } else {
7470
+ query.selectfn = this.compileSelect2(query, params);
7471
+ }
7443
7472
 
7444
- // 7. Compile DISTINCT, LIMIT and OFFSET
7445
- query.distinct = this.distinct;
7473
+ // 7. Compile DISTINCT, LIMIT and OFFSET
7474
+ query.distinct = this.distinct;
7446
7475
 
7447
- // 9. Compile PIVOT clause
7448
- if (this.pivot) query.pivotfn = this.compilePivot(query);
7449
- if (this.unpivot) query.pivotfn = this.compileUnpivot(query);
7476
+ // 9. Compile PIVOT clause
7477
+ if (this.pivot) query.pivotfn = this.compilePivot(query);
7478
+ if (this.unpivot) query.pivotfn = this.compileUnpivot(query);
7450
7479
 
7451
- // 10. Compile TOP/LIMIT/OFFSET/FETCH clause
7452
- if (this.top) {
7453
- query.limit = this.top.value;
7454
- } else if (this.limit) {
7455
- query.limit = this.limit.value;
7456
- if (this.offset) {
7457
- query.offset = this.offset.value;
7480
+ // 10. Compile TOP/LIMIT/OFFSET/FETCH clause
7481
+ if (this.top) {
7482
+ query.limit = this.top.value;
7483
+ } else if (this.limit) {
7484
+ query.limit = this.limit.value;
7485
+ if (this.offset) {
7486
+ query.offset = this.offset.value;
7487
+ }
7458
7488
  }
7459
- }
7460
7489
 
7461
- query.percent = this.percent;
7490
+ query.percent = this.percent;
7462
7491
 
7463
- // 9. Compile ordering function for UNION and UNIONALL
7464
- query.corresponding = this.corresponding; // If CORRESPONDING flag exists
7465
- if (this.union) {
7466
- query.unionfn = this.union.compile(databaseid);
7467
- if (this.union.order) {
7468
- query.orderfn = this.union.compileOrder(query, params);
7469
- } else {
7470
- query.orderfn = null;
7471
- }
7472
- } else if (this.unionall) {
7473
- query.unionallfn = this.unionall.compile(databaseid);
7474
- if (this.unionall.order) {
7475
- query.orderfn = this.unionall.compileOrder(query, params);
7476
- } else {
7477
- query.orderfn = null;
7478
- }
7479
- } else if (this.except) {
7480
- query.exceptfn = this.except.compile(databaseid);
7481
- if (this.except.order) {
7482
- query.orderfn = this.except.compileOrder(query, params);
7483
- } else {
7484
- query.orderfn = null;
7485
- }
7486
- } else if (this.intersect) {
7487
- query.intersectfn = this.intersect.compile(databaseid);
7488
- if (this.intersect.order) {
7489
- query.intersectfn = this.intersect.compileOrder(query, params);
7490
- } else {
7491
- query.orderfn = null;
7492
+ // 9. Compile ordering function for UNION and UNIONALL
7493
+ query.corresponding = this.corresponding; // If CORRESPONDING flag exists
7494
+ if (this.union) {
7495
+ query.unionfn = this.union.compile(databaseid);
7496
+ if (this.union.order) {
7497
+ query.orderfn = this.union.compileOrder(query, params);
7498
+ } else {
7499
+ query.orderfn = null;
7500
+ }
7501
+ } else if (this.unionall) {
7502
+ query.unionallfn = this.unionall.compile(databaseid);
7503
+ if (this.unionall.order) {
7504
+ query.orderfn = this.unionall.compileOrder(query, params);
7505
+ } else {
7506
+ query.orderfn = null;
7507
+ }
7508
+ } else if (this.except) {
7509
+ query.exceptfn = this.except.compile(databaseid);
7510
+ if (this.except.order) {
7511
+ query.orderfn = this.except.compileOrder(query, params);
7512
+ } else {
7513
+ query.orderfn = null;
7514
+ }
7515
+ } else if (this.intersect) {
7516
+ query.intersectfn = this.intersect.compile(databaseid);
7517
+ if (this.intersect.order) {
7518
+ query.intersectfn = this.intersect.compileOrder(query, params);
7519
+ } else {
7520
+ query.orderfn = null;
7521
+ }
7492
7522
  }
7493
- }
7494
7523
 
7495
- // SELECT INTO
7496
- if (this.into) {
7497
- if (this.into instanceof yy.Table) {
7498
- //
7499
- // Save into the table in database
7500
- //
7501
- if (
7502
- alasql.options.autocommit &&
7503
- alasql.databases[this.into.databaseid || databaseid].engineid
7504
- ) {
7505
- // For external database when AUTOCOMMIT is ONs
7524
+ // SELECT INTO
7525
+ if (this.into) {
7526
+ if (this.into instanceof yy.Table) {
7527
+ //
7528
+ // Save into the table in database
7529
+ //
7530
+ if (
7531
+ alasql.options.autocommit &&
7532
+ alasql.databases[this.into.databaseid || databaseid].engineid
7533
+ ) {
7534
+ // For external database when AUTOCOMMIT is ONs
7535
+ query.intoallfns =
7536
+ 'return alasql.engines["' +
7537
+ alasql.databases[this.into.databaseid || databaseid].engineid +
7538
+ '"]' +
7539
+ '.intoTable("' +
7540
+ (this.into.databaseid || databaseid) +
7541
+ '","' +
7542
+ this.into.tableid +
7543
+ '",this.data, columns, cb);';
7544
+ } else {
7545
+ // Into AlaSQL tables
7546
+ query.intofns =
7547
+ "alasql.databases['" +
7548
+ (this.into.databaseid || databaseid) +
7549
+ "'].tables" +
7550
+ "['" +
7551
+ this.into.tableid +
7552
+ "'].data.push(r);";
7553
+ }
7554
+ } else if (this.into instanceof yy.VarValue) {
7555
+ //
7556
+ // Save into local variable
7557
+ // SELECT * INTO @VAR1 FROM ?
7558
+ //
7506
7559
  query.intoallfns =
7507
- 'return alasql.engines["' +
7508
- alasql.databases[this.into.databaseid || databaseid].engineid +
7509
- '"]' +
7510
- '.intoTable("' +
7511
- (this.into.databaseid || databaseid) +
7512
- '","' +
7513
- this.into.tableid +
7514
- '",this.data, columns, cb);';
7515
- } else {
7516
- // Into AlaSQL tables
7517
- query.intofns =
7518
- "alasql.databases['" +
7519
- (this.into.databaseid || databaseid) +
7520
- "'].tables" +
7521
- "['" +
7522
- this.into.tableid +
7523
- "'].data.push(r);";
7524
- }
7525
- } else if (this.into instanceof yy.VarValue) {
7526
- //
7527
- // Save into local variable
7528
- // SELECT * INTO @VAR1 FROM ?
7529
- //
7530
- query.intoallfns =
7531
- 'alasql.vars["' +
7532
- this.into.variable +
7533
- '"]=this.data;res=this.data.length;if(cb)res=cb(res);return res;';
7534
- } else if (this.into instanceof yy.FuncValue) {
7535
- //
7536
- // If this is INTO() function, then call it
7537
- // with one or two parameters
7538
- //
7539
- var qs = 'return alasql.into[' + JSON.stringify(this.into.funcid.toUpperCase()) + '](';
7540
- if (this.into.args && this.into.args.length > 0) {
7541
- qs += this.into.args[0].toJS() + ',';
7542
- if (this.into.args.length > 1) {
7543
- qs += this.into.args[1].toJS() + ',';
7560
+ 'alasql.vars["' +
7561
+ this.into.variable +
7562
+ '"]=this.data;res=this.data.length;if(cb)res=cb(res);return res;';
7563
+ } else if (this.into instanceof yy.FuncValue) {
7564
+ //
7565
+ // If this is INTO() function, then call it
7566
+ // with one or two parameters
7567
+ //
7568
+ var qs = 'return alasql.into[' + JSON.stringify(this.into.funcid.toUpperCase()) + '](';
7569
+ if (this.into.args && this.into.args.length > 0) {
7570
+ qs += this.into.args[0].toJS() + ',';
7571
+ if (this.into.args.length > 1) {
7572
+ qs += this.into.args[1].toJS() + ',';
7573
+ } else {
7574
+ qs += 'undefined,';
7575
+ }
7544
7576
  } else {
7545
- qs += 'undefined,';
7577
+ qs += 'undefined, undefined,';
7546
7578
  }
7547
- } else {
7548
- qs += 'undefined, undefined,';
7549
- }
7550
- query.intoallfns = qs + 'this.data,columns,cb)';
7579
+ query.intoallfns = qs + 'this.data,columns,cb)';
7551
7580
 
7552
- } else if (this.into instanceof yy.ParamValue) {
7553
- //
7554
- // Save data into parameters array
7555
- // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7556
- //
7557
- query.intofns = "params['" + this.into.param + "'].push(r)";
7558
- }
7581
+ } else if (this.into instanceof yy.ParamValue) {
7582
+ //
7583
+ // Save data into parameters array
7584
+ // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7585
+ //
7586
+ query.intofns = "params['" + this.into.param + "'].push(r)";
7587
+ }
7559
7588
 
7560
- if (query.intofns) {
7561
- // Create intofn function
7589
+ if (query.intofns) {
7590
+ // Create intofn function
7562
7591
 
7563
- query.intofn = new Function('r,i,params,alasql', 'var y;' + query.intofns);
7564
- } else if (query.intoallfns) {
7565
- // Create intoallfn function
7592
+ query.intofn = new Function('r,i,params,alasql', 'var y;' + query.intofns);
7593
+ } else if (query.intoallfns) {
7594
+ // Create intoallfn function
7566
7595
 
7567
- query.intoallfn = new Function('columns,cb,params,alasql', 'var y;' + query.intoallfns);
7596
+ query.intoallfn = new Function('columns,cb,params,alasql', 'var y;' + query.intoallfns);
7597
+ }
7568
7598
  }
7569
- }
7570
7599
 
7571
- // Now, compile all togeather into one function with query object in scope
7572
- var statement = function (params, cb, oldscope) {
7573
- query.params = params;
7574
- // Note the callback function has the data and error reversed due to existing code in promiseExec which has the
7575
- // err and data swapped. This trickles down into alasql.exec and further. Rather than risk breaking the whole thing,
7576
- // the (data, err) standard is maintained here.
7577
- var res1 = queryfn(query, oldscope, function (res, err) {
7578
- if (err) {
7579
- return cb(err, null);
7580
- }
7581
- if (query.rownums.length > 0) {
7582
- for (var i = 0, ilen = res.length; i < ilen; i++) {
7583
- for (var j = 0, jlen = query.rownums.length; j < jlen; j++) {
7584
- res[i][query.rownums[j]] = i + 1;
7600
+ // Now, compile all togeather into one function with query object in scope
7601
+ var statement = function (params, cb, oldscope) {
7602
+ query.params = params;
7603
+ // Note the callback function has the data and error reversed due to existing code in promiseExec which has the
7604
+ // err and data swapped. This trickles down into alasql.exec and further. Rather than risk breaking the whole thing,
7605
+ // the (data, err) standard is maintained here.
7606
+ var res1 = queryfn(query, oldscope, function (res, err) {
7607
+ if (err) {
7608
+ return cb(err, null);
7609
+ }
7610
+ if (query.rownums.length > 0) {
7611
+ for (var i = 0, ilen = res.length; i < ilen; i++) {
7612
+ for (var j = 0, jlen = query.rownums.length; j < jlen; j++) {
7613
+ res[i][query.rownums[j]] = i + 1;
7614
+ }
7585
7615
  }
7586
7616
  }
7587
- }
7588
7617
 
7589
- var res2 = modify(query, res);
7618
+ var res2 = modify(query, res);
7590
7619
 
7591
- if (cb) {
7592
- cb(res2);
7593
- }
7620
+ if (cb) {
7621
+ cb(res2);
7622
+ }
7594
7623
 
7595
- return res2;
7596
- });
7624
+ return res2;
7625
+ });
7626
+
7627
+ // if(typeof res1 != 'undefined') res1 = modify(query,res1);
7628
+ return res1;
7629
+ };
7597
7630
 
7598
- // if(typeof res1 != 'undefined') res1 = modify(query,res1);
7631
+ // statement.dbversion = ;
7599
7632
 
7600
- return res1;
7601
- };
7633
+ statement.query = query;
7634
+ return statement;
7635
+ }
7602
7636
 
7603
- // statement.dbversion = ;
7637
+ execute(databaseid, params, cb) {
7638
+ return this.compile(databaseid)(params, cb);
7639
+ // throw new Error('Insert statement is should be compiled')
7640
+ }
7604
7641
 
7605
- statement.query = query;
7606
- return statement;
7642
+ compileWhereExists(query) {
7643
+ if (!this.exists) return;
7644
+ query.existsfn = this.exists.map(function (ex) {
7645
+ var nq = ex.compile(query.database.databaseid);
7646
+
7647
+ // if(!nq.query.modifier) nq.query.modifier = 'RECORDSET';
7648
+ nq.query.modifier = 'RECORDSET';
7649
+ return nq;
7650
+ });
7651
+ }
7652
+
7653
+ compileQueries(query) {
7654
+ if (!this.queries) return;
7655
+ query.queriesfn = this.queries.map(function (q) {
7656
+ var nq = q.compile(query.database.databaseid);
7657
+
7658
+ // if(!nq.query) nq.query = {};
7659
+ nq.query.modifier = 'RECORDSET';
7660
+ // if(!nq.query.modifier) nq.query.modifier = 'RECORDSET';
7661
+ return nq;
7662
+ });
7663
+ }
7664
+
7665
+ // exec(databaseid) {
7666
+ // throw new Error('Select statement should be precompiled');
7667
+ // }
7607
7668
  };
7608
7669
 
7609
7670
  /**
@@ -7733,11 +7794,6 @@ function modify(query, res) {
7733
7794
  return res;
7734
7795
  }
7735
7796
 
7736
- yy.Select.prototype.execute = function (databaseid, params, cb) {
7737
- return this.compile(databaseid)(params, cb);
7738
- // throw new Error('Insert statement is should be compiled')
7739
- };
7740
-
7741
7797
  /*
7742
7798
  //
7743
7799
  // EXISTS and other subqueries functions functions for Alasql.js
@@ -7746,43 +7802,23 @@ yy.Select.prototype.execute = function (databaseid, params, cb) {
7746
7802
  //
7747
7803
  */
7748
7804
 
7749
- yy.ExistsValue = function (params) {
7750
- return Object.assign(this, params);
7751
- };
7752
- yy.ExistsValue.prototype.toString = function () {
7753
- return 'EXISTS(' + this.value.toString() + ')';
7754
- };
7755
-
7756
- yy.ExistsValue.prototype.toType = function () {
7757
- return 'boolean';
7758
- };
7759
-
7760
- yy.ExistsValue.prototype.toJS = function (context, tableid, defcols) {
7761
-
7762
- return 'this.existsfn[' + this.existsidx + '](params,null,' + context + ').data.length';
7763
- };
7805
+ yy.ExistsValue = class ExistsValue {
7806
+ constructor(params) {
7807
+ Object.assign(this, params);
7808
+ }
7764
7809
 
7765
- yy.Select.prototype.compileWhereExists = function (query) {
7766
- if (!this.exists) return;
7767
- query.existsfn = this.exists.map(function (ex) {
7768
- var nq = ex.compile(query.database.databaseid);
7810
+ toString() {
7811
+ return 'EXISTS(' + this.value.toString() + ')';
7812
+ }
7769
7813
 
7770
- // if(!nq.query.modifier) nq.query.modifier = 'RECORDSET';
7771
- nq.query.modifier = 'RECORDSET';
7772
- return nq;
7773
- });
7774
- };
7814
+ toType() {
7815
+ return 'boolean';
7816
+ }
7775
7817
 
7776
- yy.Select.prototype.compileQueries = function (query) {
7777
- if (!this.queries) return;
7778
- query.queriesfn = this.queries.map(function (q) {
7779
- var nq = q.compile(query.database.databaseid);
7818
+ toJS(context, tableid, defcols) {
7780
7819
 
7781
- // if(!nq.query) nq.query = {};
7782
- nq.query.modifier = 'RECORDSET';
7783
- // if(!nq.query.modifier) nq.query.modifier = 'RECORDSET';
7784
- return nq;
7785
- });
7820
+ return 'this.existsfn[' + this.existsidx + '](params,null,' + context + ').data.length';
7821
+ }
7786
7822
  };
7787
7823
 
7788
7824
  //
@@ -8825,25 +8861,27 @@ yy.Select.prototype.compileGroup = function (query) {
8825
8861
  if ('funcid' in col.expression) {
8826
8862
  let colexp1 = colExpIfFunIdExists(col.expression);
8827
8863
  return (
8828
- pre +
8864
+ pre +
8829
8865
  `if(typeof g['${colas}'] == 'string' && !isNaN(g['${colas}']) && typeof Number(g['${colas}']) == 'number' &&
8830
8866
  typeof ${colexp1} == 'string' && !isNaN(${colexp1}) && typeof Number(${colexp1}) == 'number'){g['${colas}'] = Number(g['${colas}']) + Number(${colexp1})}
8831
8867
  else if(typeof g['${colas}'] == 'string' && typeof ${colexp1} == 'string'){g['${colas}'] = 0}
8832
8868
  else if(typeof g['${colas}'] == 'string' && typeof ${colexp1} == 'number'){g['${colas}'] = ${colexp1}}
8833
8869
  else if(typeof ${colexp1} == 'string' && typeof g['${colas}'] == 'number'){g['${colas}'] = g['${colas}']}
8834
- else{g['${colas}'] += ${colexp1}||0}`
8835
- + post
8870
+ else{g['${colas}'] += ${colexp1}||0}` +
8871
+ post
8836
8872
  );
8837
8873
  }
8838
- return pre +
8839
- `if(typeof g['${colas}'] == 'string' && !isNaN(g['${colas}']) && typeof Number(g['${colas}']) == 'number' &&
8874
+ return (
8875
+ pre +
8876
+ `if(typeof g['${colas}'] == 'string' && !isNaN(g['${colas}']) && typeof Number(g['${colas}']) == 'number' &&
8840
8877
  typeof ${colexp} == 'string' && !isNaN(${colexp}) && typeof Number(${colexp}) == 'number'){g['${colas}'] = Number(g['${colas}']) + Number(${colexp})}
8841
8878
  else if(typeof g['${colas}'] == 'string' && typeof ${colexp} == 'string'){g['${colas}'] = 0}
8842
8879
  else if(typeof g['${colas}'] == 'string' && typeof ${colexp} == 'number'){g['${colas}'] = ${colexp}}
8843
8880
  else if(typeof ${colexp} == 'string' && typeof g['${colas}'] == 'number'){g['${colas}'] = g['${colas}']}
8844
- else{g['${colas}'] += ${colexp}||0}`
8845
- + post
8846
- }else if (col.aggregatorid === 'COUNT') {
8881
+ else{g['${colas}'] += ${colexp}||0}` +
8882
+ post
8883
+ );
8884
+ } else if (col.aggregatorid === 'COUNT') {
8847
8885
 
8848
8886
  if (col.expression.columnid === '*') {
8849
8887
  return pre + "g['" + colas + "']++;" + post;
@@ -10104,15 +10142,18 @@ yy.Select.prototype.compileDefCols = function (query, databaseid) {
10104
10142
 
10105
10143
  // SELECT UNION statement
10106
10144
 
10107
- yy.Union = function (params) {
10108
- return Object.assign(this, params);
10109
- };
10110
- yy.Union.prototype.toString = function () {
10111
- return 'UNION';
10112
- };
10145
+ yy.Union = class Union {
10146
+ constructor(params) {
10147
+ Object.assign(this, params);
10148
+ }
10113
10149
 
10114
- yy.Union.prototype.compile = function (tableid) {
10115
- return null;
10150
+ toString() {
10151
+ return 'UNION';
10152
+ }
10153
+
10154
+ compile(tableid) {
10155
+ return null;
10156
+ }
10116
10157
  };
10117
10158
 
10118
10159
  /*
@@ -10123,16 +10164,18 @@ yy.Union.prototype.compile = function (tableid) {
10123
10164
  //
10124
10165
  */
10125
10166
 
10126
- yy.Apply = function (params) {
10127
- return Object.assign(this, params);
10128
- };
10167
+ yy.Apply = class Apply {
10168
+ constructor(params) {
10169
+ Object.assign(this, params);
10170
+ }
10129
10171
 
10130
- yy.Apply.prototype.toString = function () {
10131
- var s = this.applymode + ' APPLY (' + this.select.toString() + ')';
10172
+ toString() {
10173
+ var s = this.applymode + ' APPLY (' + this.select.toString() + ')';
10132
10174
 
10133
- if (this.as) s += ' AS ' + this.as;
10175
+ if (this.as) s += ' AS ' + this.as;
10134
10176
 
10135
- return s;
10177
+ return s;
10178
+ }
10136
10179
  };
10137
10180
 
10138
10181
  /*
@@ -10143,20 +10186,23 @@ yy.Apply.prototype.toString = function () {
10143
10186
  //
10144
10187
  */
10145
10188
 
10146
- yy.Over = function (params) {
10147
- return Object.assign(this, params);
10148
- };
10149
- yy.Over.prototype.toString = function () {
10150
- var s = 'OVER (';
10151
- if (this.partition) {
10152
- s += 'PARTITION BY ' + this.partition.toString();
10153
- if (this.order) s += ' ';
10189
+ yy.Over = class Over {
10190
+ constructor(params) {
10191
+ Object.assign(this, params);
10154
10192
  }
10155
- if (this.order) {
10156
- s += 'ORDER BY ' + this.order.toString();
10193
+
10194
+ toString() {
10195
+ var s = 'OVER (';
10196
+ if (this.partition) {
10197
+ s += 'PARTITION BY ' + this.partition.toString();
10198
+ if (this.order) s += ' ';
10199
+ }
10200
+ if (this.order) {
10201
+ s += 'ORDER BY ' + this.order.toString();
10202
+ }
10203
+ s += ')';
10204
+ return s;
10157
10205
  }
10158
- s += ')';
10159
- return s;
10160
10206
  };
10161
10207
 
10162
10208
  /*
@@ -10556,21 +10602,9 @@ yy.Over.prototype.toString = function () {
10556
10602
  this.right instanceof yy.NullValue ||
10557
10603
  (this.right.op === 'NOT' && this.right.right instanceof yy.NullValue)
10558
10604
  ) {
10559
- s = `(
10560
- (${leftOperand}==null) // Cant be ===
10561
- ===
10562
- (${rightOperand}==null) // Cant be ===
10563
- )`;
10605
+ s = `((${leftOperand} == null) === (${rightOperand} == null))`; // == null cant be ===
10564
10606
  } else {
10565
- s = `(
10566
- (${leftOperand} == ${rightOperand})
10567
- ||
10568
- (
10569
- ${leftOperand} < 0
10570
- &&
10571
- true == ${rightOperand}
10572
- )
10573
- )`;
10607
+ s = `((${leftOperand} == ${rightOperand}) || (${leftOperand} < 0 && true == ${rightOperand}))`;
10574
10608
  }
10575
10609
  }
10576
10610
 
@@ -11147,8 +11181,9 @@ yy.Over.prototype.toString = function () {
11147
11181
 
11148
11182
  toType() {
11149
11183
  if (
11150
- ['SUM', 'COUNT', 'AVG', 'MIN', 'MAX', 'AGGR', 'VAR', 'STDDEV', 'TOTAL'].indexOf(this.aggregatorid) >
11151
- -1
11184
+ ['SUM', 'COUNT', 'AVG', 'MIN', 'MAX', 'AGGR', 'VAR', 'STDDEV', 'TOTAL'].indexOf(
11185
+ this.aggregatorid
11186
+ ) > -1
11152
11187
  ) {
11153
11188
  return 'number';
11154
11189
  }