ModelQuery.union(): fix missing subquery variables

This commit is contained in:
Alice Gaudon 2020-09-06 16:02:53 +02:00
parent 17685a44be
commit e86356ae74
3 changed files with 5 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "wms-core", "name": "wms-core",
"version": "0.22.0-rc.6", "version": "0.22.0-rc.7",
"description": "Node web application framework and toolbelt.", "description": "Node web application framework and toolbelt.",
"repository": "https://gitlab.com/ArisuOngaku/wms-core", "repository": "https://gitlab.com/ArisuOngaku/wms-core",
"author": "Alice Gaudon <alice@gaudon.pro>", "author": "Alice Gaudon <alice@gaudon.pro>",

View File

@ -219,6 +219,7 @@ export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M
.forEach(v => variables.push(v)); .forEach(v => variables.push(v));
this._where.flatMap(v => this.getVariables(v)) this._where.flatMap(v => this.getVariables(v))
.forEach(v => variables.push(v)); .forEach(v => variables.push(v));
this._union?.query.variables.forEach(v => variables.push(v));
return variables; return variables;
} }

View File

@ -69,8 +69,10 @@ describe('Test ModelQuery', () => {
test('union queries', () => { test('union queries', () => {
const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory<Model>, '*'); const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory<Model>, '*');
const query2 = ModelQuery.select({table: 'model2'} as unknown as ModelFactory<Model>, '*'); const query2 = ModelQuery.select({table: 'model2'} as unknown as ModelFactory<Model>, '*');
query2.where('f2', 'v2');
query.union(query2, 'model.f1', 'DESC', false, 8); query.union(query2, 'model.f1', 'DESC', false, 8);
expect(query.toString(true)).toBe("(SELECT `model`.* FROM `model`) UNION (SELECT `model2`.* FROM `model2`) ORDER BY `model`.`f1` DESC LIMIT 8"); expect(query.toString(true)).toBe("(SELECT `model`.* FROM `model`) UNION (SELECT `model2`.* FROM `model2` WHERE `f2`=?) ORDER BY `model`.`f1` DESC LIMIT 8");
expect(query.variables).toStrictEqual(['v2'])
}); });
}); });