mirror of
https://github.com/knex/knex.git
synced 2026-01-06 03:57:29 +00:00
Fix issue with schema usage with FROM clause contain QueryBuilder, function or Raw (#4268)
This commit is contained in:
parent
57184acd63
commit
09e9fa4193
@ -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
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user