Fix issue with schema usage with FROM clause contain QueryBuilder, function or Raw (#4268)

This commit is contained in:
Ihor Sakailiuk 2021-02-03 19:16:00 +02:00 committed by GitHub
parent 57184acd63
commit 09e9fa4193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 2 deletions

View File

@ -340,7 +340,10 @@ class QueryCompiler {
if (!joins) return '';
while (++i < joins.length) {
const join = joins[i];
const table = join.schema ? `${join.schema}.${join.table}` : join.table;
const table =
join.schema && !(join.table instanceof Raw)
? `${join.schema}.${join.table}`
: join.table;
if (i > 0) sql += ' ';
if (join.joinType === 'raw') {
sql += this.formatter.unwrapRaw(join.table);
@ -1203,7 +1206,15 @@ class QueryCompiler {
let tableName = this.single.table;
const schemaName = this.single.schema;
if (tableName && schemaName) tableName = `${schemaName}.${tableName}`;
if (tableName && schemaName) {
const isQueryBuilder = tableName instanceof QueryBuilder;
const isRawQuery = tableName instanceof Raw;
const isFunction = typeof tableName === 'function';
if (!isQueryBuilder && !isRawQuery && !isFunction) {
tableName = `${schemaName}.${tableName}`;
}
}
this._tableName = tableName
? // Wrap subQuery with parenthesis, #3485

View File

@ -10327,6 +10327,102 @@ describe('QueryBuilder', () => {
);
});
it('should not prepend schema to a subquery', () => {
testsql(
qb()
.withSchema('schema')
.select()
.from(qb().from('foo').select().as('bar'))
.join('baz', 'foo.id', 'bar.foo_id'),
{
pg:
'select * from (select * from "foo") as "bar" inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
'pg-redshift':
'select * from (select * from "foo") as "bar" inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
mysql:
'select * from (select * from `foo`) as `bar` inner join `schema`.`baz` on `foo`.`id` = `bar`.`foo_id`',
mssql:
'select * from (select * from [foo]) as [bar] inner join [schema].[baz] on [foo].[id] = [bar].[foo_id]',
oracledb:
'select * from (select * from "foo") "bar" inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
sqlite3:
'select * from (select * from `foo`) as `bar` inner join `schema`.`baz` on `foo`.`id` = `bar`.`foo_id`',
}
);
});
it('should not prepend schema to a subquery specified by a function', () => {
testsql(
qb()
.withSchema('schema')
.select()
.from((q) => q.from('foo').select().as('bar'))
.join('baz', 'foo.id', 'bar.foo_id'),
{
pg:
'select * from (select * from "foo") as "bar" inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
'pg-redshift':
'select * from (select * from "foo") as "bar" inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
mysql:
'select * from (select * from `foo`) as `bar` inner join `schema`.`baz` on `foo`.`id` = `bar`.`foo_id`',
mssql:
'select * from (select * from [foo]) as [bar] inner join [schema].[baz] on [foo].[id] = [bar].[foo_id]',
oracledb:
'select * from (select * from "foo") "bar" inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
sqlite3:
'select * from (select * from `foo`) as `bar` inner join `schema`.`baz` on `foo`.`id` = `bar`.`foo_id`',
}
);
});
it('should not prepend schema to a raw FROM clause', () => {
testsql(
qb()
.withSchema('schema')
.select()
.from(raw('bar'))
.join('baz', 'foo.id', 'bar.foo_id'),
{
pg:
'select * from bar inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
'pg-redshift':
'select * from bar inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
mysql:
'select * from bar inner join `schema`.`baz` on `foo`.`id` = `bar`.`foo_id`',
mssql:
'select * from bar inner join [schema].[baz] on [foo].[id] = [bar].[foo_id]',
oracledb:
'select * from bar inner join "schema"."baz" on "foo"."id" = "bar"."foo_id"',
sqlite3:
'select * from bar inner join `schema`.`baz` on `foo`.`id` = `bar`.`foo_id`',
}
);
});
it('should not prepend schema to a raw JOIN clause', () => {
testsql(
qb()
.withSchema('schema')
.select()
.from('bar')
.join(raw('baz'), 'foo.id', 'bar.foo_id'),
{
pg:
'select * from "schema"."bar" inner join baz on "foo"."id" = "bar"."foo_id"',
'pg-redshift':
'select * from "schema"."bar" inner join baz on "foo"."id" = "bar"."foo_id"',
mysql:
'select * from `schema`.`bar` inner join baz on `foo`.`id` = `bar`.`foo_id`',
mssql:
'select * from [schema].[bar] inner join baz on [foo].[id] = [bar].[foo_id]',
oracledb:
'select * from "schema"."bar" inner join baz on "foo"."id" = "bar"."foo_id"',
sqlite3:
'select * from `schema`.`bar` inner join baz on `foo`.`id` = `bar`.`foo_id`',
}
);
});
it('join with onVal andOnVal orOnVal', () => {
testsql(
qb()