alasql 4.23.0 → 4.23.1

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.
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.23.0 build: develop-d4d329c3 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.23.1 build: develop-dd51011d | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.23.0
4
+ @version 4.23.1
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 Andrey Gershun & Mathias Wulff
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.23.0 build: develop-d4d329c3 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.23.1 build: develop-dd51011d | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.23.0
4
+ @version 4.23.1
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 Andrey Gershun & Mathias Wulff
@@ -141,8 +141,8 @@ SOFTWARE.
141
141
  Current version of alasql
142
142
  @constant {string}
143
143
  */
144
- alasql.version = '4.23.0';
145
- alasql.build = 'develop-d4d329c3';
144
+ alasql.version = '4.23.1';
145
+ alasql.build = 'develop-dd51011d';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -980,7 +980,7 @@ case 305:
980
980
  this.$ = new yy.Column({columnid: $$[$0]});
981
981
  break;
982
982
  case 320:
983
- this.$ = new yy.DomainValueValue();
983
+ this.$ = new yy.Column({columnid: alasql.options.casesensitive ? $$[$0] : $$[$0].toLowerCase(), valueKeyword: true});
984
984
  break;
985
985
  case 321:
986
986
  this.$ = new yy.Json({value:$$[$0]});
@@ -7987,15 +7987,20 @@ var preIndex = function (query) {
7987
7987
  var scope, i, ilen, dataw, ixx, addr, group, res;
7988
7988
  // If there is indexation rule
7989
7989
  if (k > 0 && source.optimization == 'ix' && source.onleftfn && source.onrightfn) {
7990
+ // An index whose key reads a parameter, a variable or a user function is only
7991
+ // valid for the values of this run, so it is neither reused nor kept (#3075)
7992
+ var ixkeyfns = source.onrightfns + '`' + source.srcwherefns;
7993
+ var ixparams = /\bparams\[|\balasql\.(vars|fn)\[/.test(ixkeyfns);
7990
7994
  // If there is no table.indices - create it
7991
- if (source.databaseid && alasql.databases[source.databaseid].tables[source.tableid]) {
7995
+ if (
7996
+ !ixparams &&
7997
+ source.databaseid &&
7998
+ alasql.databases[source.databaseid].tables[source.tableid]
7999
+ ) {
7992
8000
  if (!alasql.databases[source.databaseid].tables[source.tableid].indices)
7993
8001
  query.database.tables[source.tableid].indices = {};
7994
8002
  // Check if index already exists
7995
- ixx =
7996
- alasql.databases[source.databaseid].tables[source.tableid].indices[
7997
- hash(source.onrightfns + '`' + source.srcwherefns)
7998
- ];
8003
+ ixx = alasql.databases[source.databaseid].tables[source.tableid].indices[hash(ixkeyfns)];
7999
8004
  // A view is re-executed on every query, so an index cached on it
8000
8005
  // can be stale after its base tables change (#3065)
8001
8006
  if (
@@ -8026,9 +8031,9 @@ var preIndex = function (query) {
8026
8031
  scope[source.alias || source.tableid] = dataw;
8027
8032
 
8028
8033
  // Check if it apply to where function
8029
- if (source.srcwherefn(scope, query.params, alasql)) {
8034
+ if (source.srcwherefn.call(query, scope, query.params, alasql)) {
8030
8035
  // Create index entry for each address
8031
- var addr = source.onrightfn(scope, query.params, alasql);
8036
+ var addr = source.onrightfn.call(query, scope, query.params, alasql);
8032
8037
  var group = source.ix[addr];
8033
8038
  if (!group) {
8034
8039
  group = source.ix[addr] = [];
@@ -8039,15 +8044,15 @@ var preIndex = function (query) {
8039
8044
  }
8040
8045
 
8041
8046
  if (
8047
+ !ixparams &&
8042
8048
  source.databaseid &&
8043
8049
  alasql.databases[source.databaseid].tables[source.tableid] &&
8044
8050
  !alasql.databases[source.databaseid].tables[source.tableid].view
8045
8051
  ) {
8046
8052
  // Save index to original table (but not to a view, whose rows
8047
8053
  // change with its base tables, see #3065)
8048
- alasql.databases[source.databaseid].tables[source.tableid].indices[
8049
- hash(source.onrightfns + '`' + source.srcwherefns)
8050
- ] = source.ix;
8054
+ alasql.databases[source.databaseid].tables[source.tableid].indices[hash(ixkeyfns)] =
8055
+ source.ix;
8051
8056
  }
8052
8057
  }
8053
8058
 
@@ -8105,7 +8110,7 @@ var preIndex = function (query) {
8105
8110
  scope = {};
8106
8111
  source.data = source.data.filter(function (r) {
8107
8112
  scope[source.alias] = r;
8108
- return source.srcwherefn(scope, query.params, alasql);
8113
+ return source.srcwherefn.call(query, scope, query.params, alasql);
8109
8114
  });
8110
8115
  } else {
8111
8116
  source.data = [];
@@ -8119,7 +8124,7 @@ var preIndex = function (query) {
8119
8124
 
8120
8125
  source.data = source.data.filter(function (r) {
8121
8126
  scope[source.alias] = r;
8122
- return source.srcwherefn(scope, query.params, alasql);
8127
+ return source.srcwherefn.call(query, scope, query.params, alasql);
8123
8128
  });
8124
8129
 
8125
8130
  scope = {};
@@ -8134,7 +8139,7 @@ var preIndex = function (query) {
8134
8139
  ) {
8135
8140
  if (source.getfn && !source.dontcache) source.data[i] = dataw;
8136
8141
  scope[source.alias] = dataw;
8137
- if (source.srcwherefn(scope, query.params, alasql)) res.push(dataw);
8142
+ if (source.srcwherefn.call(query, scope, query.params, alasql)) res.push(dataw);
8138
8143
  i++;
8139
8144
  }
8140
8145
  source.data = res;
@@ -8211,7 +8216,7 @@ function doJoin(query, scope, h) {
8211
8216
  source.joinmode != 'ANTI' &&
8212
8217
  source.optimization == 'ix'
8213
8218
  ) {
8214
- data = source.ix[source.onleftfn(scope, query.params, alasql)] || [];
8219
+ data = source.ix[source.onleftfn.call(query, scope, query.params, alasql)] || [];
8215
8220
  opt = true;
8216
8221
  }
8217
8222
  }
@@ -8229,11 +8234,12 @@ function doJoin(query, scope, h) {
8229
8234
  scope[tableid] = dataw;
8230
8235
  if (query.joinrows) query.joinat[h] = opt ? source.rowat.get(dataw) : i;
8231
8236
 
8232
- // Reduce with ON and USING clause
8237
+ // Reduce with ON and USING clause. These run with the query as this, as WHERE does,
8238
+ // so a subquery in ON finds this.queriesfn (#2795)
8233
8239
  var usingPassed = !source.onleftfn;
8234
8240
  if (!usingPassed) {
8235
- var left = source.onleftfn(scope, query.params, alasql);
8236
- var right = source.onrightfn(scope, query.params, alasql);
8241
+ var left = source.onleftfn.call(query, scope, query.params, alasql);
8242
+ var right = source.onrightfn.call(query, scope, query.params, alasql);
8237
8243
  if (left instanceof String || left instanceof Number) left = left.valueOf();
8238
8244
  if (right instanceof String || right instanceof Number) right = left.valueOf();
8239
8245
  usingPassed = left == right;
@@ -8241,7 +8247,7 @@ function doJoin(query, scope, h) {
8241
8247
 
8242
8248
  if (usingPassed) {
8243
8249
  // For all non-standard JOINs like a-b=0
8244
- if (source.onmiddlefn(scope, query.params, alasql)) {
8250
+ if (source.onmiddlefn.call(query, scope, query.params, alasql)) {
8245
8251
  // Recursively call new join
8246
8252
  if (source.joinmode != 'SEMI' && source.joinmode != 'ANTI') {
8247
8253
  doJoin(query, scope, h + 1);
@@ -9702,6 +9708,10 @@ yy.Select.prototype.compileJoins = function (query) {
9702
9708
  middlef = true;
9703
9709
  }
9704
9710
 
9711
+ // A subquery is not a fixed key: its result can change without the joined table
9712
+ // changing, so an index on it would go stale, and a correlated one needs the row
9713
+ if (/this\.(queriesfn|existsfn)/.test(ls + rs)) middlef = true;
9714
+
9705
9715
  if (middlef) {
9706
9716
  rights = '';
9707
9717
  lefts = '';
@@ -12378,6 +12388,11 @@ yy.Over = class Over {
12378
12388
  class Op {
12379
12389
  constructor(params) {
12380
12390
  assign(this, params);
12391
+ // The VALUE keyword is a column (#2670), but VALUE->x in a CHECK still reads the checked row.
12392
+ // [value] and `value` are plain columns, so they keep -> on the column
12393
+ if (this.op === '->' && this.left instanceof yy.Column && this.left.valueKeyword) {
12394
+ this.left = new yy.DomainValueValue();
12395
+ }
12381
12396
  }
12382
12397
 
12383
12398
  toString() {
@@ -12945,6 +12960,10 @@ yy.Over = class Over {
12945
12960
  }
12946
12961
 
12947
12962
  toJS(context, tableid, defcols) {
12963
+ // SEARCH compiles against x, the current value, so the VALUE keyword stays that value there
12964
+ if (this.valueKeyword && context === 'x') {
12965
+ return context;
12966
+ }
12948
12967
  if (!this.tableid && tableid === '' && !defcols) {
12949
12968
  return this.columnid !== '_'
12950
12969
  ? `${context}['${this.columnid}']`
@@ -14316,10 +14335,17 @@ yy.ColumnDef.prototype.toString = function () {
14316
14335
  // Whether the referenced table has a row with the key values in rr. Without a primary key
14317
14336
  // the named columns are searched, as SQL-92 allows a foreign key to a UNIQUE column (#2779)
14318
14337
  function foreignKeyExists(fktable, rr) {
14338
+ var columnids = Object.keys(rr);
14319
14339
  if (fktable.pk && fktable.pk.columns && fktable.pk.columns.length > 0) {
14320
- return !!fktable.uniqs[fktable.pk.hh][fktable.pk.onrightfn(rr)];
14340
+ // The key can hold only a prefix of the values, so compare the whole values (#2883)
14341
+ var parent = fktable.uniqs[fktable.pk.hh][fktable.pk.onrightfn(rr)];
14342
+ return (
14343
+ !!parent &&
14344
+ columnids.every(function (columnid) {
14345
+ return String(parent[columnid]) === String(rr[columnid]);
14346
+ })
14347
+ );
14321
14348
  }
14322
- var columnids = Object.keys(rr);
14323
14349
  return fktable.data.some(function (row) {
14324
14350
  return columnids.every(function (columnid) {
14325
14351
  return row[columnid] != null && String(row[columnid]) === String(rr[columnid]);
@@ -17318,6 +17344,12 @@ yy.Delete.prototype.compile = function (databaseid) {
17318
17344
  };
17319
17345
  } else {
17320
17346
  statement = function (params, cb) {
17347
+ if (db.engineid && alasql.engines[db.engineid].deleteFromTable) {
17348
+ return alasql.engines[db.engineid].deleteFromTable(databaseid, tableid, null, params, cb);
17349
+ }
17350
+
17351
+ // Any engine with loadTableData and saveTableData can hold the table, also one
17352
+ // registered by the user, so the engine is not limited by name here
17321
17353
  if (alasql.options.autocommit && db.engineid) {
17322
17354
  alasql.engines[db.engineid].loadTableData(databaseid, tableid);
17323
17355
  }
@@ -21263,7 +21295,23 @@ LS.removeTable = function (databaseid, tableid) {
21263
21295
  LS.createDatabase = function (lsdbid, args, ifnotexists, databaseid, cb) {
21264
21296
  var res = 1;
21265
21297
  var ls = LS.get('alasql'); // Read list of all databases
21266
- if (!(ifnotexists && ls && ls.databases && ls.databases[lsdbid])) {
21298
+ var listed = !!(ls && ls.databases && ls.databases[lsdbid]);
21299
+ var lsdb = LS.get(lsdbid);
21300
+ if (!listed && lsdb && lsdb.databaseid === lsdbid && lsdb.tables) {
21301
+ // The index record lost the entry while the database record is still there, so
21302
+ // repair the index and never overwrite the database. Only a record with the shape
21303
+ // of a database counts, as another app can keep JSON under the same key (#3070)
21304
+ if (!ls) ls = {databases: {}};
21305
+ if (!ls.databases) ls.databases = {};
21306
+ ls.databases[lsdbid] = true;
21307
+ LS.set('alasql', ls);
21308
+ if (!ifnotexists) {
21309
+ throw new Error(
21310
+ 'localStorage: Cannot create new database "' + lsdbid + '" because it already exists'
21311
+ );
21312
+ }
21313
+ res = 0;
21314
+ } else if (!(ifnotexists && listed)) {
21267
21315
  if (!ls) ls = {databases: {}}; // Empty record
21268
21316
  if (ls.databases && ls.databases[lsdbid]) {
21269
21317
  throw new Error(
@@ -21275,7 +21323,7 @@ LS.createDatabase = function (lsdbid, args, ifnotexists, databaseid, cb) {
21275
21323
  LS.set(lsdbid, {databaseid: lsdbid, tables: {}}); // Create database record
21276
21324
  } else {
21277
21325
  // The database is listed but its record can be gone (e.g. removed from storage)
21278
- if (!LS.get(lsdbid)) LS.set(lsdbid, {databaseid: lsdbid, tables: {}});
21326
+ if (!lsdb) LS.set(lsdbid, {databaseid: lsdbid, tables: {}});
21279
21327
  res = 0;
21280
21328
  }
21281
21329
  if (cb) res = cb(res);
@@ -21872,27 +21920,58 @@ FS.attachDatabase = function (fsdbid, dbid, args, params, cb) {
21872
21920
  if (alasql.databases[dbid]) {
21873
21921
  throw new Error('Unable to attach database as "' + dbid + '" because it already exists');
21874
21922
  }
21923
+ var prev = alasql.databases[dbid || fsdbid];
21875
21924
  var db = new alasql.Database(dbid || fsdbid);
21876
21925
  db.engineid = 'FILESTORAGE';
21877
21926
  db.filename = args[0].value;
21878
- loadFile(db.filename, !!cb, function (s) {
21879
- try {
21880
- db.data = JSON.parse(s);
21881
- } catch (err) {
21882
- throw new Error('Data in FileStorage database are corrupted');
21883
- }
21884
- // Live tables are kept apart from the schema stored in the file, so storing a
21885
- // schema does not drop the keys and checks of the live table (#1846)
21886
- db.tables = {};
21887
- for (var tbid in db.data.tables) {
21888
- db.tables[tbid] = extend({}, db.data.tables[tbid]);
21889
- // IF AUTOCOMMIT IS OFF then copy data to memory
21890
- if (!alasql.options.autocommit) {
21891
- db.tables[tbid].data = db.data[tbid];
21927
+ var attached = false;
21928
+
21929
+ // Do not leave a database without data behind when the file can not be used
21930
+ function fail(err) {
21931
+ if (attached) throw err; // an error thrown by the callback of a finished ATTACH
21932
+ if (prev) alasql.databases[db.databaseid] = prev;
21933
+ else delete alasql.databases[db.databaseid];
21934
+ if (alasql.useid === db.databaseid && !prev) alasql.use();
21935
+ if (!cb) throw err;
21936
+ res = cb(null, err);
21937
+ }
21938
+
21939
+ loadFile(
21940
+ db.filename,
21941
+ !!cb,
21942
+ function (s) {
21943
+ try {
21944
+ db.data = JSON.parse(s);
21945
+ } catch (err) {
21946
+ return fail(new Error('Data in FileStorage database are corrupted'));
21947
+ }
21948
+ if (!db.data || typeof db.data.tables !== 'object' || db.data.tables === null) {
21949
+ return fail(new Error('Data in FileStorage database are corrupted'));
21950
+ }
21951
+ // Live tables are kept apart from the schema stored in the file, so storing a
21952
+ // schema does not drop the keys and checks of the live table (#1846)
21953
+ db.tables = {};
21954
+ for (var tbid in db.data.tables) {
21955
+ db.tables[tbid] = extend({}, db.data.tables[tbid]);
21956
+ // IF AUTOCOMMIT IS OFF then copy data to memory
21957
+ if (!alasql.options.autocommit) {
21958
+ db.tables[tbid].data = db.data[tbid];
21959
+ }
21892
21960
  }
21961
+ attached = true;
21962
+ if (cb) res = cb(res);
21963
+ },
21964
+ function (e) {
21965
+ fail(
21966
+ new Error(
21967
+ 'Cannot attach FileStorage database, because file "' +
21968
+ db.filename +
21969
+ '" can not be read' +
21970
+ (e && e.message ? ': ' + e.message : '')
21971
+ )
21972
+ );
21893
21973
  }
21894
- if (cb) res = cb(res);
21895
- });
21974
+ );
21896
21975
  return res;
21897
21976
  };
21898
21977
 
@@ -21911,7 +21990,7 @@ FS.createTable = function (databaseid, tableid, ifnotexists, cb) {
21911
21990
  var res = 1;
21912
21991
 
21913
21992
  if (tb && !ifnotexists) {
21914
- throw new Error('Table "' + tableid + '" alsready exists in the database "' + fsdbid + '"');
21993
+ throw new Error('Table "' + tableid + '" already exists in the database "' + databaseid + '"');
21915
21994
  }
21916
21995
  var table = alasql.databases[databaseid].tables[tableid];
21917
21996
  db.data.tables[tableid] = fsTableSchema(table);
package/dist/alasql.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.23.0 build: develop-d4d329c3 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.23.1 build: develop-dd51011d | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.23.0
4
+ @version 4.23.1
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 Andrey Gershun & Mathias Wulff
@@ -141,8 +141,8 @@ SOFTWARE.
141
141
  Current version of alasql
142
142
  @constant {string}
143
143
  */
144
- alasql.version = '4.23.0';
145
- alasql.build = 'develop-d4d329c3';
144
+ alasql.version = '4.23.1';
145
+ alasql.build = 'develop-dd51011d';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -980,7 +980,7 @@ case 305:
980
980
  this.$ = new yy.Column({columnid: $$[$0]});
981
981
  break;
982
982
  case 320:
983
- this.$ = new yy.DomainValueValue();
983
+ this.$ = new yy.Column({columnid: alasql.options.casesensitive ? $$[$0] : $$[$0].toLowerCase(), valueKeyword: true});
984
984
  break;
985
985
  case 321:
986
986
  this.$ = new yy.Json({value:$$[$0]});
@@ -7987,15 +7987,20 @@ var preIndex = function (query) {
7987
7987
  var scope, i, ilen, dataw, ixx, addr, group, res;
7988
7988
  // If there is indexation rule
7989
7989
  if (k > 0 && source.optimization == 'ix' && source.onleftfn && source.onrightfn) {
7990
+ // An index whose key reads a parameter, a variable or a user function is only
7991
+ // valid for the values of this run, so it is neither reused nor kept (#3075)
7992
+ var ixkeyfns = source.onrightfns + '`' + source.srcwherefns;
7993
+ var ixparams = /\bparams\[|\balasql\.(vars|fn)\[/.test(ixkeyfns);
7990
7994
  // If there is no table.indices - create it
7991
- if (source.databaseid && alasql.databases[source.databaseid].tables[source.tableid]) {
7995
+ if (
7996
+ !ixparams &&
7997
+ source.databaseid &&
7998
+ alasql.databases[source.databaseid].tables[source.tableid]
7999
+ ) {
7992
8000
  if (!alasql.databases[source.databaseid].tables[source.tableid].indices)
7993
8001
  query.database.tables[source.tableid].indices = {};
7994
8002
  // Check if index already exists
7995
- ixx =
7996
- alasql.databases[source.databaseid].tables[source.tableid].indices[
7997
- hash(source.onrightfns + '`' + source.srcwherefns)
7998
- ];
8003
+ ixx = alasql.databases[source.databaseid].tables[source.tableid].indices[hash(ixkeyfns)];
7999
8004
  // A view is re-executed on every query, so an index cached on it
8000
8005
  // can be stale after its base tables change (#3065)
8001
8006
  if (
@@ -8026,9 +8031,9 @@ var preIndex = function (query) {
8026
8031
  scope[source.alias || source.tableid] = dataw;
8027
8032
 
8028
8033
  // Check if it apply to where function
8029
- if (source.srcwherefn(scope, query.params, alasql)) {
8034
+ if (source.srcwherefn.call(query, scope, query.params, alasql)) {
8030
8035
  // Create index entry for each address
8031
- var addr = source.onrightfn(scope, query.params, alasql);
8036
+ var addr = source.onrightfn.call(query, scope, query.params, alasql);
8032
8037
  var group = source.ix[addr];
8033
8038
  if (!group) {
8034
8039
  group = source.ix[addr] = [];
@@ -8039,15 +8044,15 @@ var preIndex = function (query) {
8039
8044
  }
8040
8045
 
8041
8046
  if (
8047
+ !ixparams &&
8042
8048
  source.databaseid &&
8043
8049
  alasql.databases[source.databaseid].tables[source.tableid] &&
8044
8050
  !alasql.databases[source.databaseid].tables[source.tableid].view
8045
8051
  ) {
8046
8052
  // Save index to original table (but not to a view, whose rows
8047
8053
  // change with its base tables, see #3065)
8048
- alasql.databases[source.databaseid].tables[source.tableid].indices[
8049
- hash(source.onrightfns + '`' + source.srcwherefns)
8050
- ] = source.ix;
8054
+ alasql.databases[source.databaseid].tables[source.tableid].indices[hash(ixkeyfns)] =
8055
+ source.ix;
8051
8056
  }
8052
8057
  }
8053
8058
 
@@ -8105,7 +8110,7 @@ var preIndex = function (query) {
8105
8110
  scope = {};
8106
8111
  source.data = source.data.filter(function (r) {
8107
8112
  scope[source.alias] = r;
8108
- return source.srcwherefn(scope, query.params, alasql);
8113
+ return source.srcwherefn.call(query, scope, query.params, alasql);
8109
8114
  });
8110
8115
  } else {
8111
8116
  source.data = [];
@@ -8119,7 +8124,7 @@ var preIndex = function (query) {
8119
8124
 
8120
8125
  source.data = source.data.filter(function (r) {
8121
8126
  scope[source.alias] = r;
8122
- return source.srcwherefn(scope, query.params, alasql);
8127
+ return source.srcwherefn.call(query, scope, query.params, alasql);
8123
8128
  });
8124
8129
 
8125
8130
  scope = {};
@@ -8134,7 +8139,7 @@ var preIndex = function (query) {
8134
8139
  ) {
8135
8140
  if (source.getfn && !source.dontcache) source.data[i] = dataw;
8136
8141
  scope[source.alias] = dataw;
8137
- if (source.srcwherefn(scope, query.params, alasql)) res.push(dataw);
8142
+ if (source.srcwherefn.call(query, scope, query.params, alasql)) res.push(dataw);
8138
8143
  i++;
8139
8144
  }
8140
8145
  source.data = res;
@@ -8211,7 +8216,7 @@ function doJoin(query, scope, h) {
8211
8216
  source.joinmode != 'ANTI' &&
8212
8217
  source.optimization == 'ix'
8213
8218
  ) {
8214
- data = source.ix[source.onleftfn(scope, query.params, alasql)] || [];
8219
+ data = source.ix[source.onleftfn.call(query, scope, query.params, alasql)] || [];
8215
8220
  opt = true;
8216
8221
  }
8217
8222
  }
@@ -8229,11 +8234,12 @@ function doJoin(query, scope, h) {
8229
8234
  scope[tableid] = dataw;
8230
8235
  if (query.joinrows) query.joinat[h] = opt ? source.rowat.get(dataw) : i;
8231
8236
 
8232
- // Reduce with ON and USING clause
8237
+ // Reduce with ON and USING clause. These run with the query as this, as WHERE does,
8238
+ // so a subquery in ON finds this.queriesfn (#2795)
8233
8239
  var usingPassed = !source.onleftfn;
8234
8240
  if (!usingPassed) {
8235
- var left = source.onleftfn(scope, query.params, alasql);
8236
- var right = source.onrightfn(scope, query.params, alasql);
8241
+ var left = source.onleftfn.call(query, scope, query.params, alasql);
8242
+ var right = source.onrightfn.call(query, scope, query.params, alasql);
8237
8243
  if (left instanceof String || left instanceof Number) left = left.valueOf();
8238
8244
  if (right instanceof String || right instanceof Number) right = left.valueOf();
8239
8245
  usingPassed = left == right;
@@ -8241,7 +8247,7 @@ function doJoin(query, scope, h) {
8241
8247
 
8242
8248
  if (usingPassed) {
8243
8249
  // For all non-standard JOINs like a-b=0
8244
- if (source.onmiddlefn(scope, query.params, alasql)) {
8250
+ if (source.onmiddlefn.call(query, scope, query.params, alasql)) {
8245
8251
  // Recursively call new join
8246
8252
  if (source.joinmode != 'SEMI' && source.joinmode != 'ANTI') {
8247
8253
  doJoin(query, scope, h + 1);
@@ -9702,6 +9708,10 @@ yy.Select.prototype.compileJoins = function (query) {
9702
9708
  middlef = true;
9703
9709
  }
9704
9710
 
9711
+ // A subquery is not a fixed key: its result can change without the joined table
9712
+ // changing, so an index on it would go stale, and a correlated one needs the row
9713
+ if (/this\.(queriesfn|existsfn)/.test(ls + rs)) middlef = true;
9714
+
9705
9715
  if (middlef) {
9706
9716
  rights = '';
9707
9717
  lefts = '';
@@ -12378,6 +12388,11 @@ yy.Over = class Over {
12378
12388
  class Op {
12379
12389
  constructor(params) {
12380
12390
  assign(this, params);
12391
+ // The VALUE keyword is a column (#2670), but VALUE->x in a CHECK still reads the checked row.
12392
+ // [value] and `value` are plain columns, so they keep -> on the column
12393
+ if (this.op === '->' && this.left instanceof yy.Column && this.left.valueKeyword) {
12394
+ this.left = new yy.DomainValueValue();
12395
+ }
12381
12396
  }
12382
12397
 
12383
12398
  toString() {
@@ -12945,6 +12960,10 @@ yy.Over = class Over {
12945
12960
  }
12946
12961
 
12947
12962
  toJS(context, tableid, defcols) {
12963
+ // SEARCH compiles against x, the current value, so the VALUE keyword stays that value there
12964
+ if (this.valueKeyword && context === 'x') {
12965
+ return context;
12966
+ }
12948
12967
  if (!this.tableid && tableid === '' && !defcols) {
12949
12968
  return this.columnid !== '_'
12950
12969
  ? `${context}['${this.columnid}']`
@@ -14316,10 +14335,17 @@ yy.ColumnDef.prototype.toString = function () {
14316
14335
  // Whether the referenced table has a row with the key values in rr. Without a primary key
14317
14336
  // the named columns are searched, as SQL-92 allows a foreign key to a UNIQUE column (#2779)
14318
14337
  function foreignKeyExists(fktable, rr) {
14338
+ var columnids = Object.keys(rr);
14319
14339
  if (fktable.pk && fktable.pk.columns && fktable.pk.columns.length > 0) {
14320
- return !!fktable.uniqs[fktable.pk.hh][fktable.pk.onrightfn(rr)];
14340
+ // The key can hold only a prefix of the values, so compare the whole values (#2883)
14341
+ var parent = fktable.uniqs[fktable.pk.hh][fktable.pk.onrightfn(rr)];
14342
+ return (
14343
+ !!parent &&
14344
+ columnids.every(function (columnid) {
14345
+ return String(parent[columnid]) === String(rr[columnid]);
14346
+ })
14347
+ );
14321
14348
  }
14322
- var columnids = Object.keys(rr);
14323
14349
  return fktable.data.some(function (row) {
14324
14350
  return columnids.every(function (columnid) {
14325
14351
  return row[columnid] != null && String(row[columnid]) === String(rr[columnid]);
@@ -17318,6 +17344,12 @@ yy.Delete.prototype.compile = function (databaseid) {
17318
17344
  };
17319
17345
  } else {
17320
17346
  statement = function (params, cb) {
17347
+ if (db.engineid && alasql.engines[db.engineid].deleteFromTable) {
17348
+ return alasql.engines[db.engineid].deleteFromTable(databaseid, tableid, null, params, cb);
17349
+ }
17350
+
17351
+ // Any engine with loadTableData and saveTableData can hold the table, also one
17352
+ // registered by the user, so the engine is not limited by name here
17321
17353
  if (alasql.options.autocommit && db.engineid) {
17322
17354
  alasql.engines[db.engineid].loadTableData(databaseid, tableid);
17323
17355
  }
@@ -21263,7 +21295,23 @@ LS.removeTable = function (databaseid, tableid) {
21263
21295
  LS.createDatabase = function (lsdbid, args, ifnotexists, databaseid, cb) {
21264
21296
  var res = 1;
21265
21297
  var ls = LS.get('alasql'); // Read list of all databases
21266
- if (!(ifnotexists && ls && ls.databases && ls.databases[lsdbid])) {
21298
+ var listed = !!(ls && ls.databases && ls.databases[lsdbid]);
21299
+ var lsdb = LS.get(lsdbid);
21300
+ if (!listed && lsdb && lsdb.databaseid === lsdbid && lsdb.tables) {
21301
+ // The index record lost the entry while the database record is still there, so
21302
+ // repair the index and never overwrite the database. Only a record with the shape
21303
+ // of a database counts, as another app can keep JSON under the same key (#3070)
21304
+ if (!ls) ls = {databases: {}};
21305
+ if (!ls.databases) ls.databases = {};
21306
+ ls.databases[lsdbid] = true;
21307
+ LS.set('alasql', ls);
21308
+ if (!ifnotexists) {
21309
+ throw new Error(
21310
+ 'localStorage: Cannot create new database "' + lsdbid + '" because it already exists'
21311
+ );
21312
+ }
21313
+ res = 0;
21314
+ } else if (!(ifnotexists && listed)) {
21267
21315
  if (!ls) ls = {databases: {}}; // Empty record
21268
21316
  if (ls.databases && ls.databases[lsdbid]) {
21269
21317
  throw new Error(
@@ -21275,7 +21323,7 @@ LS.createDatabase = function (lsdbid, args, ifnotexists, databaseid, cb) {
21275
21323
  LS.set(lsdbid, {databaseid: lsdbid, tables: {}}); // Create database record
21276
21324
  } else {
21277
21325
  // The database is listed but its record can be gone (e.g. removed from storage)
21278
- if (!LS.get(lsdbid)) LS.set(lsdbid, {databaseid: lsdbid, tables: {}});
21326
+ if (!lsdb) LS.set(lsdbid, {databaseid: lsdbid, tables: {}});
21279
21327
  res = 0;
21280
21328
  }
21281
21329
  if (cb) res = cb(res);
@@ -21872,27 +21920,58 @@ FS.attachDatabase = function (fsdbid, dbid, args, params, cb) {
21872
21920
  if (alasql.databases[dbid]) {
21873
21921
  throw new Error('Unable to attach database as "' + dbid + '" because it already exists');
21874
21922
  }
21923
+ var prev = alasql.databases[dbid || fsdbid];
21875
21924
  var db = new alasql.Database(dbid || fsdbid);
21876
21925
  db.engineid = 'FILESTORAGE';
21877
21926
  db.filename = args[0].value;
21878
- loadFile(db.filename, !!cb, function (s) {
21879
- try {
21880
- db.data = JSON.parse(s);
21881
- } catch (err) {
21882
- throw new Error('Data in FileStorage database are corrupted');
21883
- }
21884
- // Live tables are kept apart from the schema stored in the file, so storing a
21885
- // schema does not drop the keys and checks of the live table (#1846)
21886
- db.tables = {};
21887
- for (var tbid in db.data.tables) {
21888
- db.tables[tbid] = extend({}, db.data.tables[tbid]);
21889
- // IF AUTOCOMMIT IS OFF then copy data to memory
21890
- if (!alasql.options.autocommit) {
21891
- db.tables[tbid].data = db.data[tbid];
21927
+ var attached = false;
21928
+
21929
+ // Do not leave a database without data behind when the file can not be used
21930
+ function fail(err) {
21931
+ if (attached) throw err; // an error thrown by the callback of a finished ATTACH
21932
+ if (prev) alasql.databases[db.databaseid] = prev;
21933
+ else delete alasql.databases[db.databaseid];
21934
+ if (alasql.useid === db.databaseid && !prev) alasql.use();
21935
+ if (!cb) throw err;
21936
+ res = cb(null, err);
21937
+ }
21938
+
21939
+ loadFile(
21940
+ db.filename,
21941
+ !!cb,
21942
+ function (s) {
21943
+ try {
21944
+ db.data = JSON.parse(s);
21945
+ } catch (err) {
21946
+ return fail(new Error('Data in FileStorage database are corrupted'));
21947
+ }
21948
+ if (!db.data || typeof db.data.tables !== 'object' || db.data.tables === null) {
21949
+ return fail(new Error('Data in FileStorage database are corrupted'));
21950
+ }
21951
+ // Live tables are kept apart from the schema stored in the file, so storing a
21952
+ // schema does not drop the keys and checks of the live table (#1846)
21953
+ db.tables = {};
21954
+ for (var tbid in db.data.tables) {
21955
+ db.tables[tbid] = extend({}, db.data.tables[tbid]);
21956
+ // IF AUTOCOMMIT IS OFF then copy data to memory
21957
+ if (!alasql.options.autocommit) {
21958
+ db.tables[tbid].data = db.data[tbid];
21959
+ }
21892
21960
  }
21961
+ attached = true;
21962
+ if (cb) res = cb(res);
21963
+ },
21964
+ function (e) {
21965
+ fail(
21966
+ new Error(
21967
+ 'Cannot attach FileStorage database, because file "' +
21968
+ db.filename +
21969
+ '" can not be read' +
21970
+ (e && e.message ? ': ' + e.message : '')
21971
+ )
21972
+ );
21893
21973
  }
21894
- if (cb) res = cb(res);
21895
- });
21974
+ );
21896
21975
  return res;
21897
21976
  };
21898
21977
 
@@ -21911,7 +21990,7 @@ FS.createTable = function (databaseid, tableid, ifnotexists, cb) {
21911
21990
  var res = 1;
21912
21991
 
21913
21992
  if (tb && !ifnotexists) {
21914
- throw new Error('Table "' + tableid + '" alsready exists in the database "' + fsdbid + '"');
21993
+ throw new Error('Table "' + tableid + '" already exists in the database "' + databaseid + '"');
21915
21994
  }
21916
21995
  var table = alasql.databases[databaseid].tables[tableid];
21917
21996
  db.data.tables[tableid] = fsTableSchema(table);