Skip to content

Commit

Permalink
Refactored toSQL method implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
elhigu committed Sep 27, 2017
1 parent 35f0a4f commit 8f395cb
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/query/compiler.js
Expand Up @@ -44,43 +44,41 @@ assign(QueryCompiler.prototype, {
this._undefinedInWhereClause = false;

method = method || this.method
let val = this[method]()
const defaults = {
const val = this[method]() || '';

const query = {
method,
options: reduce(this.options, assign, {}),
timeout: this.timeout,
cancelOnTimeout: this.cancelOnTimeout,
bindings: this.formatter.bindings,
__knexQueryUid: uuid.v4()
bindings: this.formatter.bindings || [],
__knexQueryUid: uuid.v4(),
toNative: () => ({
sql: this.client.positionBindings(query.sql),
bindings: this.client.prepBindings(query.bindings)
})
};

if (isString(val)) {
val = {sql: val};
query.sql = val;
} else {
assign(query, val);
}

defaults.bindings = defaults.bindings || [];

if (method === 'select' || method === 'first') {
if(this.single.as) {
defaults.as = this.single.as;
query.as = this.single.as;
}
}

if(this._undefinedInWhereClause) {
debugBindings(defaults.bindings)
debugBindings(query.bindings)
throw new Error(
`Undefined binding(s) detected when compiling ` +
`${method.toUpperCase()} query: ${val.sql}`
`${method.toUpperCase()} query: ${query.sql}`
);
}

const query = assign(defaults, val);
query.toNative = () => {
return {
sql: this.client.positionBindings(query.sql),
bindings: this.client.prepBindings(query.bindings)
};
};

return query;
},

Expand Down

0 comments on commit 8f395cb

Please sign in to comment.