Skip to content

Commit

Permalink
添加join方法,并打包生产
Browse files Browse the repository at this point in the history
  • Loading branch information
gphp0071201 committed Dec 22, 2020
1 parent 77dda1a commit 12ac6f7
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 25 deletions.
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [2.12.DISTINCT](/docs/chain/distinct.md)
* [2.13.LOCK](/docs/chain/lock.md)
* [2.14.COMMENT](/docs/chain/comment.md)
* [2.14.JOIN](/docs/chain/join.md)
* [3.CURD调用](/docs/curd/README.md)
* [3.1.SELECT](/docs/curd/select.md)
* [3.2.UPDATE](/docs/curd/update.md)
Expand Down
2 changes: 1 addition & 1 deletion build/common.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/uitl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions docs/chain/join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
join方法属于链式调用方法之一, 主要用于联表查询


### 案例

join参数为Array<object> 或 object类型, 例如:


Array类型参数
```js
sql.table('node_table').join([{
dir: 'left',
table: ('left_table'),
where: [{'node_talbe.id': ['join_table.id']}]
}, {
dir: 'right',
table: ('right_table'),
where: [{'right_table.id': ['left_table.id']}]
}]).where('node_table.id=1').select()
```

最终得到
```js
SELECT * FROM node_table LEFT JOIN left_table ON ((node_talbe.id=join_table.id) ) RIGHT JOIN right_table ON ((right_table.id=left_table.id) ) WHERE node_table.id=1
```

object类型参数
```js
sql.table('node_table').join({
dir: 'left',
table: ('left_table'),
where: [{'node_talbe.id': ['join_table.id']}]
}).where('node_table.id=1').select()
```

最终得到
```js
SELECT * FROM node_table LEFT JOIN left_table ON ((node_talbe.id=join_table.id) ) WHERE node_table.id=1
```

9 changes: 9 additions & 0 deletions docs/chain/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,14 @@ SELECT * FROM node_table a WHERE id=1
```

多表同时查询:
```js
sql.table({'node_table1': 'a', 'node_table2': 'b'}).where('id=1').select()
```
得到:
```
SELECT * FROM node_table1 AS a, node_table2 AS b WHERE id=1
```


join方法还需要补充...
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,27 @@ console.log(insertSql)
content.innerHTML = `${insertSql1} <br/> ${insertSql}`;

console.group('普通查询语句')
console.log(sql.table('atables').field('id, name, hello_boy').select())
console.log(sql.table({'user': 'a', 'menu': 'b'}).select())
console.log(sql.table({'gp_table1': 'a', 'gp_table2': 'b'}).join({
dir: 'left outer',
table: 'hello',
where: [{'a.id': ['hello.id'], 'b.id': ['hello.id'], _type: 'or', _nexttype: 'or'}, {'hello.name': ['b.name']}]
}).field('id, name, hello_boy').select())
console.groupEnd()

console.log(sql.table('node_table').group('user_id').join({
dir: 'left',
table: ('join_table'),
where: [{'node_talbe.id': ['join_table.id']}]
}).where('id=1').having('count(number)>3').select(), '516516516516')
console.log(sql.table('node_table').join([{
dir: 'left',
table: ('left_table'),
where: [{'node_talbe.id': ['join_table.id']}]
}, {
dir: 'right',
table: ('right_table'),
where: [{'right_table.id': ['left_table.id']}]
}]).where('node_table.id=1').select(), '516516516516')
console.group('数组类型')
console.log('Array<string>', sql.table('atables').field(['a.id', 'name', 'hello_boy', 'remarks']).select())
console.log('Array<string|object>', sql.table('atables a, btables b').field(['id', 'name', { 'a.hello_boy': 'helloBoy', 'b.user_id': 'userId' }, 'remarks']).select())
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-mysqls",
"version": "0.0.3",
"version": "0.0.5",
"description": "快速生成mysql语句",
"main": "./build/main.js",
"keywords": [
Expand Down
Loading

0 comments on commit 12ac6f7

Please sign in to comment.