Skip to content

Commit

Permalink
Fix #1810
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Nov 9, 2023
1 parent 1493d57 commit 74d475f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 71 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "alasql",
"description": "Use SQL to select and filter javascript data - including relational joins and search in nested objects (JSON). Export to and import from Excel and CSV",
"version": "4.1.10",
"version": "4.1.11",
"author": "Andrey Gershun <[email protected]>",
"contributors": [
"Mathias Wulff <[email protected]>"
Expand Down Expand Up @@ -120,4 +120,4 @@
"pre-push": "yarn test-format || (echo please format using 'yarn format' && exit 1)"
}
}
}
}
9 changes: 4 additions & 5 deletions src/38query.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,8 @@ function queryfn3(query) {
var d = query.selectgfn(g, query.params, alasql);

for (const key in query.groupColumns) {
if (query.groupColumns[key] !== key
&& d[query.groupColumns[key]])
delete d[query.groupColumns[key]]
if (query.groupColumns[key] !== key && d[query.groupColumns[key]])
delete d[query.groupColumns[key]];
}
query.data.push(d);
}
Expand Down Expand Up @@ -483,7 +482,7 @@ var preIndex = function (query) {
// Check if index already exists
var ixx =
alasql.databases[source.databaseid].tables[source.tableid].indices[
hash(source.onrightfns + '`' + source.srcwherefns)
hash(source.onrightfns + '`' + source.srcwherefns)
];
if (!alasql.databases[source.databaseid].tables[source.tableid].dirty && ixx) {
source.ix = ixx;
Expand Down Expand Up @@ -539,7 +538,7 @@ var preIndex = function (query) {
// Check if index exists
ixx =
alasql.databases[source.databaseid].tables[source.tableid].indices[
hash(source.wxleftfns + '`')
hash(source.wxleftfns + '`')
];
}
if (!alasql.databases[source.databaseid].tables[source.tableid].dirty && ixx) {
Expand Down
13 changes: 7 additions & 6 deletions src/424select.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,27 @@ function compileSelectStar(query, aliases, joinstar) {

if (columns && columns.length > 0) {
columns.forEach(function (tcol) {
const escapedColumnId = escapeq(tcol.columnid);
if (joinstar && alasql.options.joinstar == 'underscore') {
ss.push(
"'" + alias + '_' + tcol.columnid + "':p['" + alias + "']['" + tcol.columnid + "']"
"'" + alias + '_' + escapedColumnId + "':p['" + alias + "']['" + escapedColumnId + "']"
);
} else if (joinstar && alasql.options.joinstar == 'json') {
// ss.push('\''+alias+'_'+tcol.columnid+'\':p[\''+alias+'\'][\''+tcol.columnid+'\']');
sp +=
"r['" +
alias +
"']['" +
tcol.columnid +
escapedColumnId +
"']=p['" +
alias +
"']['" +
tcol.columnid +
escapedColumnId +
"'];";
} else {
var value = "p['" + alias + "']['" + tcol.columnid + "']";
var value = "p['" + alias + "']['" + escapedColumnId + "']";
if (!columnIds[tcol.columnid]) {
var key = "'" + tcol.columnid + "':";
var key = "'" + escapedColumnId + "':";
ss.push(key + value);
columnIds[tcol.columnid] = {
id: ss.length - 1,
Expand All @@ -82,7 +83,7 @@ function compileSelectStar(query, aliases, joinstar) {
}
}

query.selectColumns[escapeq(tcol.columnid)] = true;
query.selectColumns[escapedColumnId] = true;

// console.log('ok',tcol);

Expand Down
6 changes: 3 additions & 3 deletions src/50expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
var ljs = '(' + leftJS() + '||{})';

if (typeof this.right === 'string') {
s = ljs + '["' + this.right + '"]';
s = ljs + '["' + escapeq(this.right) + '"]';
} else if (typeof this.right === 'number') {
s = ljs + '[' + this.right + ']';
} else if (this.right instanceof yy.FuncValue) {
Expand Down Expand Up @@ -607,7 +607,7 @@
}

toJS() {
return "alasql.vars['" + this.variable + "']";
return "alasql.vars['" + escapeq(this.variable) + "']";
}
}

Expand Down Expand Up @@ -1014,7 +1014,7 @@
toJS() {
var colas = this.nick;
if (colas === undefined) {
colas = this.toString();
colas = escapeq(this.toString());
}
return "g['" + colas + "']";
}
Expand Down
43 changes: 19 additions & 24 deletions test/test1263.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,36 @@ if (typeof exports === 'object') {

describe('Test 1263 - Nested SELECT', function () {
var t1 = [
{ id: '1', a: 'one' },
{ id: '2', a: 'two' },
{ id: '3', a: 'three' },
{ id: '4', a: 'four' },
{id: '1', a: 'one'},
{id: '2', a: 'two'},
{id: '3', a: 'three'},
{id: '4', a: 'four'},
];
var t2 = [
{ id: '1', b: 'A' },
{ id: '2', b: 'B' },
{ id: '5', b: 'E' },
{ id: '6', b: 'F' },
{id: '1', b: 'A'},
{id: '2', b: 'B'},
{id: '5', b: 'E'},
{id: '6', b: 'F'},
];

it('1. JOIN', function (done) {
var expected = [
{ id: '1', a: 'one', b: 'A' },
{ id: '2', a: 'two', b: 'B' },
{ id: '3', a: 'three' },
{ id: '4', a: 'four' },
{ id: '5', b: 'E' },
{ id: '6', b: 'F' },
{id: '1', a: 'one', b: 'A'},
{id: '2', a: 'two', b: 'B'},
{id: '3', a: 'three'},
{id: '4', a: 'four'},
{id: '5', b: 'E'},
{id: '6', b: 'F'},
];

var res = alasql(
'SELECT * FROM ? T1 OUTER JOIN ? T2 ON T1.id = T2.id',
[t1, t2]
);
var res = alasql('SELECT * FROM ? T1 OUTER JOIN ? T2 ON T1.id = T2.id', [t1, t2]);
assert.deepEqual(res, expected);

var res = alasql(
'SELECT * FROM ? T1 OUTER JOIN (SELECT * FROM ?) T2 ON T1.id = T2.id',
[t1, t2]
);
var res = alasql('SELECT * FROM ? T1 OUTER JOIN (SELECT * FROM ?) T2 ON T1.id = T2.id', [
t1,
t2,
]);
assert.deepEqual(res, expected);
done();
});

});

5 changes: 1 addition & 4 deletions test/test1415.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ describe('Test 1415 - UNION Expression with empty query columns bug', function (
var data1 = [{a: 'abc'}, {a: 'xyz'}];
var data2 = [{a: '123'}, {a: '987'}];

var res = alasql('SELECT * FROM :a UNION SELECT * FROM :b', {
a: data1,
b: data2,
});
var res = alasql('SELECT * FROM :a UNION SELECT * FROM :b', {a: data1, b: data2});
assert.deepEqual(res, [{a: '123'}, {a: '987'}, {a: 'abc'}, {a: 'xyz'}]);

var res = alasql(
Expand Down
61 changes: 34 additions & 27 deletions test/test1797.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
if (typeof exports === 'object') {
var assert = require('assert');
var alasql = require('..');
var assert = require('assert');
var alasql = require('..');
}

var test = '1797';

describe('Test ' + test + ' - select * with alias colname', function () {
it('Join with simple subquery', function () {
var expected = [{ a: 1, b: 1, c: 1, d: 3 }, { a: 2, b: 1, c: 1, d: 1 }];
var data = [
{ a: 1, b: 1, c: 1 },
{ a: 1, b: 2, c: 1 },
{ a: 1, b: 3, c: 1 },
{ a: 2, b: 1, c: 1 }];
var res = alasql(
`SELECT *, COUNT(a) as d FROM ? GROUP BY a`,
[data]
);
assert.deepEqual(res, expected);
});
it('Join with simple subquery', function () {
var expected = [
{a: 1, b: 1, c: 1, d: 3},
{a: 2, b: 1, c: 1, d: 1},
];
var data = [
{a: 1, b: 1, c: 1},
{a: 1, b: 2, c: 1},
{a: 1, b: 3, c: 1},
{a: 2, b: 1, c: 1},
];
var res = alasql(`SELECT *, COUNT(a) as d FROM ? GROUP BY a`, [data]);
assert.deepEqual(res, expected);
});

it('Join with simple subquery', function () {
var expected = [{ a: 1, b: 1, c: 1, d: 5 }, { a: 2, b: 1, c: 1, d: 2 }];
var data = [{ a: 1, b: 1, c: 1 }, { a: 1, b: 1, c: 2 }, { a: 1, b: 1, c: 3 }, { a: 1, b: 2, c: 1 }, { a: 1, b: 3, c: 1 }, { a: 2, b: 1, c: 1 }, { a: 2, b: 1, c: 2 }];
var res = alasql(
`SELECT *, COUNT(a) as d FROM ? GROUP BY a`,
[data]
);
assert.deepEqual(res, expected);
});


});
it('Join with simple subquery', function () {
var expected = [
{a: 1, b: 1, c: 1, d: 5},
{a: 2, b: 1, c: 1, d: 2},
];
var data = [
{a: 1, b: 1, c: 1},
{a: 1, b: 1, c: 2},
{a: 1, b: 1, c: 3},
{a: 1, b: 2, c: 1},
{a: 1, b: 3, c: 1},
{a: 2, b: 1, c: 1},
{a: 2, b: 1, c: 2},
];
var res = alasql(`SELECT *, COUNT(a) as d FROM ? GROUP BY a`, [data]);
assert.deepEqual(res, expected);
});
});

0 comments on commit 74d475f

Please sign in to comment.