Skip to content

Commit

Permalink
Fix: fix parse join when table name has keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
welefen committed Feb 24, 2016
1 parent 4c9def1 commit 48b885a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
16 changes: 13 additions & 3 deletions src/adapter/db/_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,24 @@ export default class extends think.base {
joinStr += joinType + table;
} else {
table = options.tablePrefix + table;
joinStr += joinType + '`' + table + '`';
if(table.indexOf('.') === -1){
joinStr += joinType + '`' + table + '`';
}else{
joinStr += joinType + table;
}
}
if (item.as) {
joinStr += ' AS ' + item.as;
joinStr += ' AS `' + item.as + '`';
}
if (item.on) {
let mTable = options.alias || options.table;
if(mTable.indexOf('.') === -1){
mTable = '`' + mTable + '`';
}
let jTable = item.as || table;
if(jTable.indexOf('.') === -1){
jTable = '`' + jTable + '`';
}
if (think.isObject(item.on)) {
let where = [];
for(let key in item.on){
Expand All @@ -459,7 +469,7 @@ export default class extends think.base {
item.on = item.on.split(/\s*,\s*/);
}
joinStr += ' ON ' + (item.on[0].indexOf('.') > -1 ? item.on[0] : (mTable + '.`' + item.on[0] + '`'));
joinStr += '=' + (item.on[1].indexOf('.') > -1 ? item.on[1] : (jTable + '.`' + item.on[1] + '`'));
joinStr += ' = ' + (item.on[1].indexOf('.') > -1 ? item.on[1] : (jTable + '.`' + item.on[1] + '`'));
}
}
});
Expand Down
48 changes: 35 additions & 13 deletions test/adapter/db/_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' INNER JOIN `cate` AS c ON user.`cate_id`=c.`id`');
assert.equal(data, ' INNER JOIN `cate` AS `c` ON `user`.`cate_id` = `c`.`id`');
})
it('parseJoin, array, no on', function(){
var instance = new Parse();
Expand All @@ -323,7 +323,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' INNER JOIN `cate` AS c');
assert.equal(data, ' INNER JOIN `cate` AS `c`');
})
it('parseJoin, array, ignore not object', function(){
var instance = new Parse();
Expand All @@ -335,7 +335,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' INNER JOIN `cate` AS c');
assert.equal(data, ' INNER JOIN `cate` AS `c`');
})
it('parseJoin, array, multi', function(){
var instance = new Parse();
Expand All @@ -353,7 +353,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN `cate` AS c ON user.`cate_id`=c.`id` LEFT JOIN `group_tag` AS d ON user.`id`=d.`group_id`');
assert.equal(data, ' LEFT JOIN `cate` AS `c` ON `user`.`cate_id` = `c`.`id` LEFT JOIN `group_tag` AS `d` ON `user`.`id` = `d`.`group_id`');
})
it('parseJoin, array, multi 1', function(){
var instance = new Parse();
Expand All @@ -372,7 +372,8 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN `cate` AS c ON user.`id`=c.`id` LEFT JOIN `group_tag` AS d ON user.`id`=d.`group_id`');

assert.equal(data, ' LEFT JOIN `cate` AS `c` ON `user`.`id` = `c`.`id` LEFT JOIN `group_tag` AS `d` ON `user`.`id` = `d`.`group_id`');
})
it('parseJoin, array, multi 2', function(){
var instance = new Parse();
Expand All @@ -387,7 +388,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN `cate` ON user.`id`=cate.`id` LEFT JOIN `group_tag` ON user.`id`=group_tag.`group_id`');
assert.equal(data, ' LEFT JOIN `cate` ON `user`.`id` = `cate`.`id` LEFT JOIN `group_tag` ON `user`.`id` = `group_tag`.`group_id`');
})
it('parseJoin, array, multi 3', function(){
var instance = new Parse();
Expand All @@ -408,7 +409,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN `cate` ON user.`id`=cate.`id` LEFT JOIN `group_tag` ON user.`id`=group_tag.`group_id` LEFT JOIN `tag` ON (user.`id`=tag.`id` AND user.`title`=tag.`name`)');
assert.equal(data, ' LEFT JOIN `cate` ON `user`.`id` = `cate`.`id` LEFT JOIN `group_tag` ON `user`.`id` = `group_tag`.`group_id` LEFT JOIN `tag` ON (`user`.`id`=`tag`.`id` AND `user`.`title`=`tag`.`name`)');
})
it('parseJoin, array, multi 4, on has table name', function(){
var instance = new Parse();
Expand All @@ -429,7 +430,28 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN `cate` ON user.`id`=cate.`id` LEFT JOIN `group_tag` ON user.`id`=group_tag.`group_id` LEFT JOIN `tag` ON (user.`id`=tag.`id` AND user.`title`=tag.name)');
assert.equal(data, ' LEFT JOIN `cate` ON `user`.`id` = `cate`.`id` LEFT JOIN `group_tag` ON `user`.`id` = `group_tag`.`group_id` LEFT JOIN `tag` ON (`user`.`id`=`tag`.`id` AND `user`.`title`=tag.name)');
})
it('parseJoin, array, multi 4, on has table name 1', function(){
var instance = new Parse();
var data = instance.parseJoin([{
cate: {
on: 'id, id'
},
group_tag: {
on: ['id', 'group_id']
},
tag: {
on: {
id: 'id',
title: '`tag`.`name`'
}
}
}], {
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN `cate` ON `user`.`id` = `cate`.`id` LEFT JOIN `group_tag` ON `user`.`id` = `group_tag`.`group_id` LEFT JOIN `tag` ON (`user`.`id`=`tag`.`id` AND `user`.`title`=`tag`.`name`)');
})
it('parseJoin, array, multi 4', function(){
var instance = new Parse();
Expand All @@ -450,7 +472,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN `cate` ON user.`id`=cate.`id` LEFT JOIN `group_tag` ON user.`id`=group_tag.`group_id` LEFT JOIN `tag` ON (user.`id`=tag.`id` AND u1.title=tag.name)');
assert.equal(data, ' LEFT JOIN `cate` ON `user`.`id` = `cate`.`id` LEFT JOIN `group_tag` ON `user`.`id` = `group_tag`.`group_id` LEFT JOIN `tag` ON (`user`.`id`=`tag`.`id` AND u1.title=tag.name)');
})
it('parseJoin, array, table is sql', function(){
var instance = new Parse();
Expand All @@ -463,7 +485,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN (SELECT * FROM test WHERE 1=1) AS temp ON user.`id`=temp.team_id');
assert.equal(data, ' LEFT JOIN (SELECT * FROM test WHERE 1=1) AS `temp` ON `user`.`id` = temp.team_id');
})
it('parseJoin, array, table is sql 1', function(){
var instance = new Parse();
Expand All @@ -476,7 +498,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN (SELECT * FROM test WHERE 1=1) AS temp ON u.id=temp.team_id');
assert.equal(data, ' LEFT JOIN (SELECT * FROM test WHERE 1=1) AS `temp` ON u.id = temp.team_id');
})
it('parseJoin, array, table is sql 2', function(){
var instance = new Parse();
Expand All @@ -489,7 +511,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN (SELECT * FROM test WHERE 1=1) AS temp ON user.`id`=temp.`team_id`');
assert.equal(data, ' LEFT JOIN (SELECT * FROM test WHERE 1=1) AS `temp` ON `user`.`id` = `temp`.`team_id`');
})
it('parseJoin, array, table is sql 3', function(){
var instance = new Parse();
Expand All @@ -502,7 +524,7 @@ describe('adapter/db/_parse.js', function(){
tablePrefix: '',
table: 'user'
});
assert.equal(data, ' LEFT JOIN (SELECT * FROM test WHERE 1=1) AS temp ON user.`id`=temp.`team_id`');
assert.equal(data, ' LEFT JOIN (SELECT * FROM test WHERE 1=1) AS `temp` ON `user`.`id` = `temp`.`team_id`');
})
it('parseThinkWhere, key is empty, ignore valud', function(){
var instance = new Parse();
Expand Down
8 changes: 4 additions & 4 deletions test/model/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ describe('model/base.js', function(){
done();
})
})
it('count select, with count, beyond pages', function(done){
it('count select, with count, beyond pages 2', function(done){
instance.where({name: 'test'}).page(300).countSelect(false).then(function(data){
assert.deepEqual(data, {"count":399,"totalPages":40,"currentPage":40,"numsPerPage":10,"data":[{"id":7565,"title":"title1","cate_id":1,"cate_no":0},{"id":7564,"title":"title2","cate_id":2,"cate_no":977},{"id":7563,"title":"title3","cate_id":7,"cate_no":281},{"id":7562,"title":"title4","cate_id":6,"cate_no":242},{"id":7561,"title":"title5","cate_id":3,"cate_no":896},{"id":7560,"title":"title6","cate_id":3,"cate_no":897},{"id":7559,"title":"title7","cate_id":3,"cate_no":898},{"id":7558,"title":"title8","cate_id":17,"cate_no":151},{"id":7557,"title":"title9","cate_id":17,"cate_no":152}]})
done();
Expand All @@ -886,11 +886,11 @@ describe('model/base.js', function(){
'app.appnm as appnm'
].join(',')).page(1, 20).count('c.channel_id').then(function(){
var sql = instance.getLastSql();
assert.equal(sql, "SELECT COUNT(c.channel_id) AS think_count FROM think_user AS c LEFT JOIN `think_product.app` AS app ON c.`channel_id`=app.`app_id` LIMIT 1")
assert.equal(sql, "SELECT COUNT(c.channel_id) AS think_count FROM think_user AS c LEFT JOIN think_product.app AS `app` ON `c`.`channel_id` = `app`.`app_id` LIMIT 1")
done();
})
})
it('countSelect, with join', function(done){
it('countSelect, with join 2', function(done){
instance.alias('c').join({
table: 'product.app',
join: 'left',
Expand All @@ -905,7 +905,7 @@ describe('model/base.js', function(){
'app.appnm as appnm'
].join(',')).page(1, 20).countSelect().then(function(){
var sql = instance.getLastSql();
assert.equal(sql, "SELECT c.channel_id as id,c.name as name,c.identifier as identifier,app.app_id as app_id,app.app_name as app_name,app.appnm as appnm FROM think_user AS c LEFT JOIN `think_product.app` AS app ON c.`channel_id`=app.`app_id` LIMIT 0,20");
assert.equal(sql, "SELECT c.channel_id as id,c.name as name,c.identifier as identifier,app.app_id as app_id,app.app_name as app_name,app.appnm as appnm FROM think_user AS c LEFT JOIN think_product.app AS `app` ON `c`.`channel_id` = `app`.`app_id` LIMIT 0,20");
done();
})
})
Expand Down

0 comments on commit 48b885a

Please sign in to comment.