mirror of
				https://github.com/knex/knex.git
				synced 2025-11-04 03:39:39 +00:00 
			
		
		
		
	added option() to resolve overlapping column names on joins
This commit is contained in:
		
							parent
							
								
									b5c1696ae9
								
							
						
					
					
						commit
						0d1593373f
					
				@ -32,7 +32,7 @@ _.extend(MysqlClient.prototype, base.protoProps, {
 | 
			
		||||
        if (debug) base.debug(builder, conn);
 | 
			
		||||
 | 
			
		||||
        // Call the querystring and then release the client
 | 
			
		||||
        conn.query(builder.sql, builder.bindings, function (err, resp) {
 | 
			
		||||
        conn.query(_.extend({sql: builder.sql}, builder.opts), builder.bindings, function (err, resp) {
 | 
			
		||||
 | 
			
		||||
          if (err) return dfd.reject(err);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -38,7 +38,7 @@ _.extend(PostgresClient.prototype, base.protoProps, {
 | 
			
		||||
        if (debug) base.debug(builder, conn);
 | 
			
		||||
 | 
			
		||||
        // Call the querystring and then release the client
 | 
			
		||||
        conn.query(builder.sql, builder.bindings, function (err, resp) {
 | 
			
		||||
        conn.query(_.extend({text: builder.sql}, builder.opts), builder.bindings, function (err, resp) {
 | 
			
		||||
          if (err) return dfd.reject(err);
 | 
			
		||||
          resp || (resp = {});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -86,6 +86,7 @@ define(function(require, exports) {
 | 
			
		||||
      this.orders   = [];
 | 
			
		||||
      this.columns  = [];
 | 
			
		||||
      this.bindings = [];
 | 
			
		||||
      this.opts     = [];
 | 
			
		||||
      this.isDistinct  = false;
 | 
			
		||||
      this.isReturning = false;
 | 
			
		||||
    },
 | 
			
		||||
@ -401,6 +402,11 @@ define(function(require, exports) {
 | 
			
		||||
      return this._setType('delete');
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    option: function(opts) {
 | 
			
		||||
      this.opts = _.extend(this.opts, opts);
 | 
			
		||||
      return this;
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    // Truncate
 | 
			
		||||
    truncate: function() {
 | 
			
		||||
      return this._setType('truncate');
 | 
			
		||||
 | 
			
		||||
@ -107,6 +107,7 @@ define(function(require, exports) {
 | 
			
		||||
 | 
			
		||||
      // Prep the SQL associated with the this.
 | 
			
		||||
      this.sql = this.toSql();
 | 
			
		||||
      this.opts = this.opts;
 | 
			
		||||
      this.bindings = this._cleanBindings();
 | 
			
		||||
      if (!_.isArray(this.sql)) this.sql = [this.sql];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -10,6 +10,7 @@ define(function(require, exports) {
 | 
			
		||||
  var Raw = function(sql, bindings) {
 | 
			
		||||
    this.bindings = (!_.isArray(bindings) ? (bindings ? [bindings] : []) : bindings);
 | 
			
		||||
    this.sql = sql;
 | 
			
		||||
    this.opts = {};
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  _.extend(Raw.prototype, {
 | 
			
		||||
 | 
			
		||||
@ -43,6 +43,18 @@ module.exports = function(Knex, dbName, resolver) {
 | 
			
		||||
            .select(['accounts.email as e1', 'a2.email as e2'])
 | 
			
		||||
            .then(resolver(ok), ok);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('supports joins with overlapping column names', function(ok) {
 | 
			
		||||
      var blah = Knex('accounts as a1')
 | 
			
		||||
        .join('accounts as a2', function() {
 | 
			
		||||
          this.on('a1.email', '<>', 'a2.email');
 | 
			
		||||
        }, 'left')
 | 
			
		||||
        .select(['a1.email', 'a2.email'])
 | 
			
		||||
        .where(Knex.Raw('a1.id = 1'))
 | 
			
		||||
        .option({ nestTables: true, rowMode: 'array' })
 | 
			
		||||
        .limit(2)
 | 
			
		||||
        .then(resolver(ok), ok);
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
@ -672,6 +672,20 @@ module.exports = {
 | 
			
		||||
        bindings: []
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    'joins.6': {
 | 
			
		||||
      mysql: {
 | 
			
		||||
        sql: ['select `a1`.`email`, `a2`.`email` from `accounts` as `a1` left join `accounts` as `a2` on `a1`.`email` <> `a2`.`email` where a1.id = 1 limit 2'],
 | 
			
		||||
        bindings: []
 | 
			
		||||
      },
 | 
			
		||||
      postgres: {
 | 
			
		||||
        sql: ['select "a1"."email", "a2"."email" from "accounts" as "a1" left join "accounts" as "a2" on "a1"."email" <> "a2"."email" where a1.id = 1 limit 2'],
 | 
			
		||||
        bindings: []
 | 
			
		||||
      },
 | 
			
		||||
      sqlite3: {
 | 
			
		||||
        sql: ['select "a1"."email", "a2"."email" from "accounts" as "a1" left join "accounts" as "a2" on "a1"."email" <> "a2"."email" where a1.id = 1 limit 2'],
 | 
			
		||||
        bindings: []
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    'deletes.1': {
 | 
			
		||||
      mysql: {
 | 
			
		||||
        sql: ['delete from `accounts` where `email` = ?'],
 | 
			
		||||
@ -3184,6 +3198,35 @@ module.exports = {
 | 
			
		||||
        e2: 'test5@example.com'
 | 
			
		||||
      }]
 | 
			
		||||
    },
 | 
			
		||||
    'joins.6': {
 | 
			
		||||
      mysql: [{
 | 
			
		||||
        a1: {
 | 
			
		||||
          email: 'test100@example.com'
 | 
			
		||||
        },
 | 
			
		||||
        a2: {
 | 
			
		||||
          email: 'test2@example.com'
 | 
			
		||||
        }
 | 
			
		||||
      },{
 | 
			
		||||
        a1: {
 | 
			
		||||
          email: 'test100@example.com'
 | 
			
		||||
        },
 | 
			
		||||
        a2: {
 | 
			
		||||
          email: 'test3@example.com'
 | 
			
		||||
        }
 | 
			
		||||
      }],
 | 
			
		||||
      postgres: [{
 | 
			
		||||
        0: 'test100@example.com',
 | 
			
		||||
        1: 'test2@example.com'
 | 
			
		||||
      },{
 | 
			
		||||
        0: 'test100@example.com',
 | 
			
		||||
        1: 'test3@example.com'
 | 
			
		||||
      }],
 | 
			
		||||
      sqlite3: [{
 | 
			
		||||
        email: 'test2@example.com'
 | 
			
		||||
      },{
 | 
			
		||||
        email: 'test3@example.com'
 | 
			
		||||
      }]
 | 
			
		||||
    },
 | 
			
		||||
    'deletes.1': {
 | 
			
		||||
      mysql: 1,
 | 
			
		||||
      postgres: 1,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user