Skip to content

Commit de0a45d

Browse files
committed
Fix reindex extending support of #2184
1 parent 51d8009 commit de0a45d

File tree

4 files changed

+75
-15
lines changed

4 files changed

+75
-15
lines changed

src/65createindex.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ yy.CreateIndex.prototype.toString = function () {
1919

2020
// CREATE TABLE
2121
yy.CreateIndex.prototype.execute = function (databaseid, params, cb) {
22-
// var self = this;
2322
var db = alasql.databases[this.table.databaseid || databaseid];
2423
var tableid = this.table.tableid;
2524
var table = db.tables[tableid];
@@ -43,14 +42,14 @@ yy.CreateIndex.prototype.execute = function (databaseid, params, cb) {
4342
for (var i = 0, ilen = table.data.length; i < ilen; i++) {
4443
var addr = rightfns(table.data[i]);
4544
if (!ux[addr]) {
46-
ux[addr] = {num: 0};
45+
ux[addr] = { num: 0 };
4746
}
4847
ux[addr].num++;
4948
}
5049
}
5150
} else {
5251
var hh = hash(rightfns);
53-
table.inddefs[indexid] = {rightfns: rightfns, hh: hh};
52+
table.inddefs[indexid] = { rightfns: rightfns, hh: hh };
5453
table.indices[hh] = {};
5554

5655
var ix = (table.indices[hh] = {});
@@ -77,13 +76,16 @@ yy.Reindex.prototype.toString = function () {
7776
return s;
7877
};
7978

80-
// CREATE TABLE
79+
// REINDEX
8180
yy.Reindex.prototype.execute = function (databaseid, params, cb) {
82-
// var self = this;
83-
var db = alasql.databases[databaseid];
81+
var db = alasql.databases[this.databaseid || databaseid];
8482
var indexid = this.indexid;
85-
// console.log(db.indices);
8683
var tableid = db.indices[indexid];
84+
85+
if (!tableid) {
86+
throw new Error('Index "' + indexid + '" not found in db "' + db.databaseid + '"');
87+
}
88+
8789
var table = db.tables[tableid];
8890
table.indexColumns();
8991
var res = 1;

src/alasqlparser.jison

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3021,7 +3021,9 @@ DropTrigger
30213021
;
30223022

30233023
Reindex
3024-
: REINDEX Literal
3024+
: REINDEX Literal DOT Literal
3025+
{ $$ = new yy.Reindex({databaseid: $2, indexid: $4});}
3026+
| REINDEX Literal
30253027
{ $$ = new yy.Reindex({indexid:$2});}
30263028
;
30273029

src/alasqlparser.js

Lines changed: 10 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test2184.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {describe, expect, test, beforeAll, afterAll} from 'bun:test';
2+
import alasql from '..';
3+
4+
describe('Test 2184 CREATE INDEX with qualified table name', function () {
5+
test('1. CREATE DATABASE', function (done) {
6+
alasql('CREATE DATABASE test403');
7+
done();
8+
});
9+
10+
test('2. Create table and index before insert with qualified name (no USE)', function (done) {
11+
alasql('CREATE TABLE test403.one (a INT)');
12+
alasql('CREATE INDEX xone ON test403.one (a)');
13+
alasql('INSERT INTO test403.one (a) VALUES (100), (200), (300)');
14+
done();
15+
});
16+
17+
test('3. Create table and index after insert with qualified name (no USE)', function (done) {
18+
alasql('CREATE TABLE test403.two (a INT)');
19+
alasql('INSERT INTO test403.two (a) VALUES (100), (200), (300)');
20+
alasql('CREATE INDEX xtwo ON test403.two (a)');
21+
done();
22+
});
23+
24+
test('4. Create unique index with qualified name (before insert)', function (done) {
25+
alasql('CREATE TABLE test403.three (a INT)');
26+
alasql('CREATE UNIQUE INDEX xthree ON test403.three (a)');
27+
alasql('INSERT INTO test403.three (a) VALUES (100), (200), (300)');
28+
done();
29+
});
30+
31+
test('5. REINDEX from test403 database', function (done) {
32+
alasql('USE test403');
33+
var res = alasql('REINDEX xone');
34+
expect(res == 1);
35+
var res = alasql('REINDEX xtwo');
36+
expect(res == 1);
37+
done();
38+
});
39+
40+
test('5. REINDEX from test403 database', function (done) {
41+
alasql('USE test403');
42+
var res = alasql('REINDEX test403.xone');
43+
expect(res == 1);
44+
var res = alasql('REINDEX test403.xtwo');
45+
expect(res == 1);
46+
done();
47+
});
48+
49+
test('99. DROP DATABASE', function (done) {
50+
alasql('DROP DATABASE test403');
51+
done();
52+
});
53+
});

0 commit comments

Comments
 (0)