Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating .fromRaw API & its tests #4429

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/query/method-constants.js
Expand Up @@ -8,6 +8,7 @@ module.exports = [
'columns',
'column',
'from',
'fromRaw',
'fromJS',
'into',
'withSchema',
Expand Down
11 changes: 11 additions & 0 deletions lib/query/querybuilder.js
Expand Up @@ -203,6 +203,17 @@ class Builder extends EventEmitter {
return this;
}

fromRaw(sql, bindings) {
//Verify if is rawInstance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is not implemented just

const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
return this.from(raw)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LucasHFS Ping?..

const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);

//Add wrapper to fromRaw function
const wrappedRaw = raw.wrap('(', ')');

this._single.table = wrappedRaw;
return this;
}

// Adds a `distinct` clause to the query.
distinct(...args) {
this._statements.push({
Expand Down
48 changes: 48 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -835,6 +835,54 @@ describe('QueryBuilder', () => {
);
});

it('uses fromRaw api, #1767', () => {
testsql(qb().select('*').fromRaw('select * from users where age > 18'), {
mysql: 'select * from (select * from users where age > 18)',
mssql: 'select * from (select * from users where age > 18)',
pg: 'select * from (select * from users where age > 18)',
'pg-redshift': 'select * from (select * from users where age > 18)',
});
});

it('uses fromRaw api with a query function, #1767', () => {
const subquery = qb()
.select(raw('? as f', ['inner raw select']))
.as('g');
testsql(
qb()
.select(raw('?', ['outer raw select']), 'g.f')
.from(subquery)
.where('g.secret', 123),
{
mysql: {
sql:
'select ?, `g`.`f` from (select ? as f) as `g` where `g`.`secret` = ?',
bindings: ['outer raw select', 'inner raw select', 123],
},
mssql: {
sql:
'select ?, [g].[f] from (select ? as f) as [g] where [g].[secret] = ?',
bindings: ['outer raw select', 'inner raw select', 123],
},
oracledb: {
sql:
'select ?, "g"."f" from (select ? as f) "g" where "g"."secret" = ?',
bindings: ['outer raw select', 'inner raw select', 123],
},
pg: {
sql:
'select ?, "g"."f" from (select ? as f) as "g" where "g"."secret" = ?',
bindings: ['outer raw select', 'inner raw select', 123],
},
'pg-redshift': {
sql:
'select ?, "g"."f" from (select ? as f) as "g" where "g"."secret" = ?',
bindings: ['outer raw select', 'inner raw select', 123],
},
}
);
});

it('basic wheres', () => {
testsql(qb().select('*').from('users').where('id', '=', 1), {
mysql: {
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Expand Up @@ -496,6 +496,7 @@ export declare namespace Knex {
column: Select<TRecord, TResult>;
hintComment: HintComment<TRecord, TResult>;
from: Table<TRecord, TResult>;
fromRaw: Table<TRecord, TResult>;
into: Table<TRecord, TResult>;
table: Table<TRecord, TResult>;
distinct: Distinct<TRecord, TResult>;
Expand Down