Skip to content

Commit

Permalink
Use columnize instead of wrap in using().
Browse files Browse the repository at this point in the history
This is an attempt to fix knex#2136. Also added an integration test, couldn't find any existing.
  • Loading branch information
Simon Lidén committed Jul 13, 2018
1 parent 5eb0c2f commit 02bf257
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/query/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ assign(QueryCompiler.prototype, {
},

onUsing(clause) {
return this.formatter.wrap(clause.column);
return '(' + this.formatter.columnize(clause.column) + ')';
},

// Where Clause
Expand Down
54 changes: 54 additions & 0 deletions test/integration/builder/joins.js
Original file line number Diff line number Diff line change
Expand Up @@ -1889,5 +1889,59 @@ module.exports = function(knex) {
);
});
});

it('Can use .using()', () => {
let joinName = 'accounts_join_test';

return knex.schema
.dropTableIfExists(joinName)
.then(() =>
knex.schema.createTable(joinName, (table) => {
table.bigint('id');
table.string('email');
table.integer('testcolumn');
})
)
.then(() =>
knex(joinName).insert([
{
id: 3,
email: knex('accounts')
.where({ id: 3 })
.select('email')
.limit(1),
testcolumn: 50,
},
{
id: 3,
email: knex('accounts')
.whereNot({ id: 3 })
.select('email')
.limit(1),
testcolumn: 70,
},
])
)
.then(() =>
knex('accounts').join(joinName, (builder) =>
builder.using(['id', 'email'])
)
)
.then((rows) => {
expect(rows.length).to.equal(1);
expect(rows[0].testcolumn).to.equal(50);

return knex('accounts')
.join(joinName, (builder) => builder.using(['id']))
.orderBy('testcolumn');
})
.then((rows) => {
expect(rows.length).to.equal(2);
expect(rows[0].testcolumn).to.equal(50);
expect(rows[1].testcolumn).to.equal(70);

return true;
});
});
});
};
36 changes: 32 additions & 4 deletions test/unit/query/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6634,17 +6634,45 @@ describe('QueryBuilder', function() {
}),
{
mysql: {
sql: 'select * from `accounts` inner join `table1` using `id`',
sql: 'select * from `accounts` inner join `table1` using (`id`)',
},
mssql: {
//sql: 'select * from [accounts] inner join [table1] on [accounts].[id] = [table1].[id]'
sql: 'select * from [accounts] inner join [table1] using [id]',
sql: 'select * from [accounts] inner join [table1] using ([id])',
},
pg: {
sql: 'select * from "accounts" inner join "table1" using "id"',
sql: 'select * from "accounts" inner join "table1" using ("id")',
},
'pg-redshift': {
sql: 'select * from "accounts" inner join "table1" using "id"',
sql: 'select * from "accounts" inner join "table1" using ("id")',
},
}
);

testsql(
qb()
.select('*')
.from('accounts')
.innerJoin('table1', function() {
this.using(['id', 'test']);
}),
{
mysql: {
sql:
'select * from `accounts` inner join `table1` using (`id`, `test`)',
},
mssql: {
//sql: 'select * from [accounts] inner join [table1] on [accounts].[id] = [table1].[id]'
sql:
'select * from [accounts] inner join [table1] using ([id], [test])',
},
pg: {
sql:
'select * from "accounts" inner join "table1" using ("id", "test")',
},
'pg-redshift': {
sql:
'select * from "accounts" inner join "table1" using ("id", "test")',
},
}
);
Expand Down

0 comments on commit 02bf257

Please sign in to comment.