Include _debug property in QueryBuilder#clone.

Adds a test for `QueryBuilder#clone`. Changes `clone()` behaviour to transfer the debug flag (`QueryBuilder#_debug`) to the cloned instance.

Fixes #1080
This commit is contained in:
Rhys van der Waerden 2015-11-30 00:21:08 +11:00
parent 360ffc77d3
commit 8ef5d5899d
3 changed files with 59 additions and 16 deletions

View File

@ -11,6 +11,8 @@ var EventEmitter = require('events').EventEmitter;
var Raw = require('../raw');
var helpers = require('../helpers');
var JoinClause = require('./joinclause');
var _clone = require('lodash/lang/clone');
var isUndefined = require('lodash/lang/isUndefined');
var assign = require('lodash/object/assign');
// Typically called from `knex.builder`,
@ -20,13 +22,13 @@ function Builder(client) {
this.and = this;
this._single = {};
this._statements = [];
this._method = 'select';
this._debug = client.config && client.config.debug;
// Internal flags used in the builder.
this._method = 'select';
this._joinFlag = 'inner';
this._boolFlag = 'and';
this._notFlag = false;
this._debug = client.config && client.config.debug;
}
inherits(Builder, EventEmitter);
@ -42,13 +44,17 @@ assign(Builder.prototype, {
},
// Create a shallow clone of the current query builder.
// TODO: Test this!!
clone: function clone() {
var cloned = new this.constructor(this.client);
cloned._method = this._method;
cloned._single = _.clone(this._single);
cloned._options = _.clone(this._options);
cloned._statements = this._statements.slice();
cloned._single = _clone(this._single);
cloned._statements = _clone(this._statements);
cloned._debug = this._debug;
// `_option` is assigned by the `Interface` mixin.
if (!isUndefined(this._options)) {
cloned._options = _clone(this._options);
}
return cloned;
},

View File

@ -9,6 +9,8 @@ var EventEmitter = require('events').EventEmitter
var Raw = require('../raw')
var helpers = require('../helpers')
var JoinClause = require('./joinclause')
var clone = require('lodash/lang/clone');
var isUndefined = require('lodash/lang/isUndefined');
var assign = require('lodash/object/assign');
// Typically called from `knex.builder`,
@ -18,13 +20,13 @@ function Builder(client) {
this.and = this;
this._single = {};
this._statements = [];
this._method = 'select'
this._debug = client.config && client.config.debug;
// Internal flags used in the builder.
this._method = 'select'
this._joinFlag = 'inner';
this._boolFlag = 'and';
this._notFlag = false;
this._debug = client.config && client.config.debug;
}
inherits(Builder, EventEmitter);
@ -40,13 +42,18 @@ assign(Builder.prototype, {
},
// Create a shallow clone of the current query builder.
// TODO: Test this!!
clone: function() {
var cloned = new this.constructor(this.client);
cloned._method = this._method;
cloned._single = _.clone(this._single);
cloned._options = _.clone(this._options);
cloned._statements = this._statements.slice();
clone() {
const cloned = new this.constructor(this.client);
cloned._method = this._method;
cloned._single = clone(this._single);
cloned._statements = clone(this._statements);
cloned._debug = this._debug;
// `_option` is assigned by the `Interface` mixin.
if (!isUndefined(this._options)) {
cloned._options = clone(this._options);
}
return cloned;
},

View File

@ -1,6 +1,7 @@
'use strict';
var tape = require('tape')
var omit = require('lodash/object/omit')
var QueryBuilder = require('../../lib/query/builder')
var Client = require('../../lib/client')
@ -18,6 +19,35 @@ tape('allows for object syntax in join', function(t) {
'accounts.id': 'users.account_id',
'accounts.owner_id': 'users.id'
}).toSQL('join')
t.equal(sql.sql,
t.equal(sql.sql,
'inner join "accounts" on "accounts"."id" = "users"."account_id" and "accounts"."owner_id" = "users"."id"')
})
tape('clones correctly', function(t) {
var qb = new QueryBuilder(new Client())
var original = qb.table('users').debug().innerJoin('accounts', {
'accounts.id': 'users.account_id',
'accounts.owner_id': 'users.id'
});
var cloned = original.clone();
t.notEqual(original, cloned);
// `deepEqual` freezes when it encounters circular references,
// so they must be omitted.
t.deepEqual(
omit(cloned, 'client', 'and'),
omit(original, 'client', 'and')
);
t.equal(
cloned.client,
original.client,
'clone references same client'
);
t.equal(cloned.and, cloned, 'cloned `and` references self');
t.end();
});