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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow update queries in with statements. Fixes #2263. #2298

Merged
merged 1 commit into from Oct 31, 2017
Merged
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
9 changes: 8 additions & 1 deletion src/formatter.js
Expand Up @@ -87,6 +87,13 @@ export default class Formatter {
}
}

/**
* Creates SQL for a parameter, which might be passed to where() or .with() or
* pretty much anywhere in API.
*
* @param query Callback (for where or complete builder), Raw or QueryBuilder
* @param method Optional at least 'select' or 'update' are valid
*/
rawOrFn(value, method) {
if (typeof value === 'function') {
return this.outputQuery(this.compileCallback(value, method));
Expand Down Expand Up @@ -149,7 +156,7 @@ export default class Formatter {
compiler.formatter = this;

// Return the compiled & parameterized sql.
return compiler.toSQL(method || 'select');
return compiler.toSQL(method || builder._method || 'select');
}

// Ensures the query is aliased if necessary.
Expand Down
30 changes: 9 additions & 21 deletions src/query/builder.js
Expand Up @@ -71,39 +71,27 @@ assign(Builder.prototype, {
// With
// ------

with(alias, statement, bindings) {
with(alias, statement) {
if(typeof alias !== 'string') {
throw new Error('with() first argument must be a string');
}
if (typeof statement === 'function') {
if (
typeof statement === 'function' ||
statement instanceof Builder ||
statement instanceof Raw
) {
return this.withWrapped(alias, statement);
}
// Allow a raw statement to be passed along to the query.
if (statement instanceof Raw && arguments.length >= 2) {
return this.withRaw(alias, statement, bindings);
}
throw new Error('with() second argument must be a function or a raw');
},

// Adds a raw `with` clause to the query.
withRaw(alias, sql, bindings) {
const raw = (sql instanceof Raw ? sql : this.client.raw(sql, bindings));
this._statements.push({
grouping: 'with',
type: 'withRaw',
alias: alias,
value: raw
});
return this;
throw new Error('with() second argument must be a function / QueryBuilder or a raw');
},

// Helper for compiling any advanced `with` queries.
withWrapped(alias, callback) {
withWrapped(alias, query) {
this._statements.push({
grouping: 'with',
type: 'withWrapped',
alias: alias,
value: callback
value: query
});
return this;
},
Expand Down
5 changes: 0 additions & 5 deletions src/query/compiler.js
Expand Up @@ -556,11 +556,6 @@ assign(QueryCompiler.prototype, {
return val && this.formatter.columnize(statement.alias) + ' as (' + val + ')' || '';
},

withRaw(statement) {
return this.formatter.columnize(statement.alias) + ' as (' +
this.formatter.unwrapRaw(statement.value) + ')';
},

// Determines whether to add a "not" prefix to the where clause.
_not(statement, str) {
if (statement.not) return `not ${str}`;
Expand Down
38 changes: 38 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -4587,6 +4587,44 @@ describe("QueryBuilder", function() {
});
});

describe("#2263, update / delete queries in with syntax", () => {
it("with update query passed as raw", () => {
testquery(qb().with('update1', raw('??', [qb().from('accounts').update({ name: 'foo' })])).from('accounts'), {
postgres: `with "update1" as (update "accounts" set "name" = 'foo') select * from "accounts"`,
});
});

it("with update query passed as query builder", () => {
testquery(qb().with('update1', qb().from('accounts').update({ name: 'foo' })).from('accounts'), {
postgres: `with "update1" as (update "accounts" set "name" = 'foo') select * from "accounts"`,
});
});

it("with update query passed as callback", () => {
testquery(qb().with('update1', builder => builder.from('accounts').update({ name: 'foo' })).from('accounts'), {
postgres: `with "update1" as (update "accounts" set "name" = 'foo') select * from "accounts"`,
});
});

it("with delete query passed as raw", () => {
testquery(qb().with('delete1', raw('??', [qb().delete().from('accounts').where('id', 1)])).from('accounts'), {
postgres: `with "delete1" as (delete from "accounts" where "id" = 1) select * from "accounts"`,
});
});

it("with delete query passed as query builder", () => {
testquery(qb().with('delete1', builder => builder.delete().from('accounts').where('id', 1)).from('accounts'), {
postgres: `with "delete1" as (delete from "accounts" where "id" = 1) select * from "accounts"`,
});
});

it("with delete query passed as callback", () => {
testquery(qb().with('delete1', qb().delete().from('accounts').where('id', 1)).from('accounts'), {
postgres: `with "delete1" as (delete from "accounts" where "id" = 1) select * from "accounts"`,
});
});
});

it('#1710, properly escapes arrays in where clauses in postgresql', function() {
testquery(qb().select('*').from('sometable').where('array_field', '&&', [7]), {
postgres: "select * from \"sometable\" where \"array_field\" && '{7}'"
Expand Down