Skip to content

Commit 9b3888d

Browse files
Copilotmathiasrw
andauthored
Qualified table names for SHOW COLUMNS and SHOW INDEX to fix #2200 (#2201)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]> Co-authored-by: Mathias Wulff <[email protected]>
1 parent 5a2538d commit 9b3888d

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

src/78show.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ yy.ShowColumns.prototype.toString = function () {
7272
};
7373

7474
yy.ShowColumns.prototype.execute = function (databaseid, params, cb) {
75-
var db = alasql.databases[this.databaseid || databaseid];
75+
var db = alasql.databases[this.table.databaseid || this.databaseid || databaseid];
7676
var table = db.tables[this.table.tableid];
7777

7878
if (table && table.columns) {
@@ -101,7 +101,7 @@ yy.ShowIndex.prototype.toString = function () {
101101
return s;
102102
};
103103
yy.ShowIndex.prototype.execute = function (databaseid, params, cb) {
104-
var db = alasql.databases[this.databaseid || databaseid];
104+
var db = alasql.databases[this.table.databaseid || this.databaseid || databaseid];
105105
var table = db.tables[this.table.tableid];
106106
var res = [];
107107
if (table && table.indices) {

test/test2200.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
var testId = '2200';
7+
8+
describe(
9+
'Test ' + testId + ' - SHOW COLUMNS and SHOW INDEX with qualified table names',
10+
function () {
11+
before(function () {
12+
alasql('CREATE DATABASE test2200a');
13+
alasql('CREATE DATABASE test2200b');
14+
alasql('USE test2200a');
15+
alasql('CREATE TABLE table1 (col1 INT, col2 VARCHAR(50))');
16+
alasql('CREATE INDEX idx_col1 ON table1(col1)');
17+
alasql('USE test2200b');
18+
alasql('CREATE TABLE table2 (col3 DECIMAL(10,2), col4 BOOLEAN)');
19+
alasql('CREATE INDEX idx_col3 ON table2(col3)');
20+
});
21+
22+
it('1. SHOW COLUMNS with unqualified name (USE context)', function () {
23+
alasql('USE test2200a');
24+
var res = alasql('SHOW COLUMNS FROM table1');
25+
assert.equal(res.length, 2);
26+
assert.equal(res[0].columnid, 'col1');
27+
assert.equal(res[0].dbtypeid, 'INT');
28+
assert.equal(res[1].columnid, 'col2');
29+
assert.equal(res[1].dbtypeid, 'VARCHAR');
30+
assert.equal(res[1].dbsize, 50);
31+
});
32+
33+
it('2. SHOW COLUMNS with qualified name (database.table)', function () {
34+
// This test should work regardless of current USE context
35+
alasql('USE test2200b');
36+
var res = alasql('SHOW COLUMNS FROM test2200a.table1');
37+
assert.equal(res.length, 2, 'Should return 2 columns');
38+
assert.equal(res[0].columnid, 'col1');
39+
assert.equal(res[0].dbtypeid, 'INT');
40+
assert.equal(res[1].columnid, 'col2');
41+
assert.equal(res[1].dbtypeid, 'VARCHAR');
42+
assert.equal(res[1].dbsize, 50);
43+
});
44+
45+
it('3. SHOW INDEX with unqualified name (USE context)', function () {
46+
alasql('USE test2200a');
47+
var res = alasql('SHOW INDEX FROM table1');
48+
assert.equal(res.length, 1);
49+
assert.ok(res[0].hh, 'Should have index hash');
50+
});
51+
52+
it('4. SHOW INDEX with qualified name (database.table)', function () {
53+
// This test should work regardless of current USE context
54+
alasql('USE test2200b');
55+
var res = alasql('SHOW INDEX FROM test2200a.table1');
56+
assert.equal(res.length, 1, 'Should return 1 index');
57+
assert.ok(res[0].hh, 'Should have index hash');
58+
});
59+
60+
it('5. SHOW COLUMNS with qualified name in different database', function () {
61+
alasql('USE test2200a');
62+
var res = alasql('SHOW COLUMNS FROM test2200b.table2');
63+
assert.equal(res.length, 2, 'Should return 2 columns');
64+
assert.equal(res[0].columnid, 'col3');
65+
assert.equal(res[0].dbtypeid, 'DECIMAL');
66+
assert.equal(res[1].columnid, 'col4');
67+
assert.equal(res[1].dbtypeid, 'BOOLEAN');
68+
});
69+
70+
it('6. SHOW INDEX with qualified name in different database', function () {
71+
alasql('USE test2200a');
72+
var res = alasql('SHOW INDEX FROM test2200b.table2');
73+
assert.equal(res.length, 1, 'Should return 1 index');
74+
assert.ok(res[0].hh, 'Should have index hash');
75+
});
76+
77+
after(function () {
78+
alasql('DROP DATABASE test2200a');
79+
alasql('DROP DATABASE test2200b');
80+
});
81+
}
82+
);

0 commit comments

Comments
 (0)