mirror of
https://github.com/knex/knex.git
synced 2025-12-28 15:38:41 +00:00
Use columnize instead of wrap in using(). (#2713)
* Use columnize instead of wrap in using(). This is an attempt to fix #2136. Also added an integration test, couldn't find any existing. * Exclude MSSQL from test as it does not support .using * Change test to not use subquery * Change test
This commit is contained in:
parent
89d2b3a5e1
commit
1616b2af0d
@ -596,7 +596,7 @@ assign(QueryCompiler.prototype, {
|
||||
},
|
||||
|
||||
onUsing(clause) {
|
||||
return this.formatter.wrap(clause.column);
|
||||
return '(' + this.formatter.columnize(clause.column) + ')';
|
||||
},
|
||||
|
||||
// Where Clause
|
||||
|
||||
@ -1889,5 +1889,55 @@ module.exports = function(knex) {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
if (knex.client.driverName !== 'mssql') {
|
||||
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: 'test3@example.com',
|
||||
testcolumn: 50,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
email: 'random@email.com',
|
||||
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;
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -6721,17 +6721,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")',
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user