Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjian committed Apr 29, 2020
1 parent 69d442c commit 1857941
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ hMysql.table()

## [API 文档](./docs/api.md)
* CURD操作
* [.select()]() ⇒ Promise
* [.find()]() ⇒ Promise
* [.select()]() ⇒ Promise
* [.update()]() ⇒ Promise
* [.updateMany()]() ⇒ Promise
* [.insert()]() ⇒ Promise
Expand Down
17 changes: 7 additions & 10 deletions dist/h-mysql.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* h-tools v1.0.0
* (c) 2018-2020 haizlin https://github.com/haizlin/h-tools
* h-mysql v1.0.3
* (c) 2018-2020 haizlin https://github.com/haizlin/h-mysql
* Licensed MIT
* Released on: February 1, 2018
*/
Expand Down Expand Up @@ -92,6 +92,7 @@ function checkType(opt, key) {
result = opt.trim();
}

console.log(11, result);
return result;
}
function checkObjType(pre_key, val) {
Expand Down Expand Up @@ -260,12 +261,13 @@ function sortArray(data) {
return result;
}

function handleInsertData(data) {
function insertData(data) {
if (!data) return '';
if (Array.isArray(data) && data.length === 1) data = data[0];
let keys = '';
let values = '';
let datastr = '';
console.log(33, data);

if (Array.isArray(data)) {
data = sortArray(data);
Expand Down Expand Up @@ -831,28 +833,23 @@ class Curd {
keys.forEach((item, index) => {
datastr += `${item}='${checkType(newData[item], item)}'` + (index === keys.length - 1 ? ' ' : ',');
});
console.log(22, datastr);
result = `UPDATE ${this.sqlObj.table} SET ${datastr} WHERE ${this.sqlObj.where}`;
const sqlStr = result.replace(/'/g, '\'');
this.sqlObj.sqlStr = sqlStr;
return this;
}

updateMany() {
this.sqlObj.queryType = 'updateMany';
}

insert() {
this.sqlObj.queryType = 'insert';
let newData = this.sqlObj.data || {};
const datastr = handleInsertData(newData);
const datastr = insertData(newData);
let result = `INSERT INTO ${this.sqlObj.table} ${datastr}`;
const sqlStr = result.replace(/'/g, '\'');
this.sqlObj.sqlStr = sqlStr;
return this;
}

insertMany(list, type = false) {}

delete() {
if (isEmptyObj(this.sqlObj.where) || this.sqlObj.where === '') {
return false;
Expand Down
91 changes: 83 additions & 8 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,84 @@
## API列表
* 创建Mysql实例
```javascript
new Mysql(config)
```

| 参数 | 类型 | 描述 |
| --- | --- | --- |
| config | <code>object</code> | 数据库配置 |
* CURD操作 [查看更多](./curd.md)
* [.select()]() ⇒ Promise
* 查询多条数据,结合.where, .limit避免数据量过大
* [.find()]() ⇒ Promise
* 查询单条数据,是.select()的语法糖
* [.update()]() ⇒ Promise
* 更新操作
* [.insert()]() ⇒ Promise
* [.delete()]() ⇒ Promise
* [.query(sql: string)]() ⇒ Promise

* 链式方法
* [.table(tableName: string)]() ⇒ Mysql
* 设置要查找的表名
* [.alias(tableAlias: string)]()⇒ Mysql
* 给table添加别名
```javascript
sql.table('demo').alias('d').field('id', 'age')
=> SELECT id, age FROM demo as d
```
* [.field(opt: string | any[])]() ⇒ Mysql
* 要查询的字段
```javascript
sql.table('demo').field(['id', 'age'])
=> SELECT id, age FROM demo
```
* [.where(opt: string | any[])]() ⇒ Mysql
* 条件
* [.limit(limit)]() ⇒ Mysql
* 返回的数量
* [.page(pageSize, pageNum)]() ⇒ Mysql
* 条件
* [.data(data:object | any[])]() ⇒ Mysql
* 用作单条(批量)插入或更新的数据
```javascript
// 单条插入
sql.table('demo').data({age: 18, name: 'wanglin'}).insert()
=> INSERT INTO (age,name) VALUES (18, 'wanglin')
// 批量插入
sql.table('demo').data([{age: 18, name: 'wanglin'}, {age: 20, name: 'langzishenjian'}]).insert()
=> INSERT INTO (age,name) VALUES (18, 'wanglin'), (20, 'langzishenjian')
// 单条更新
sql.table('demo').data({age: 18, name: 'wanglin'}).upate()
=> UPDATE demo SET age=18, name='wanglin'
// 批量更新
sql.table('demo').data([{age: 18, name: 'wanglin'}, {age: 20, name: 'langzishenjian'}]).upate()
=> UPDATE demo SET age=18, name='wanglin'
```
* [.order(order)]() ⇒ Mysql
* 条件
* [.join()]() ⇒ Mysql
* 联合查询
* [.union()]() ⇒ Mysql
* 条件

* 条件
* [.group(field)]() ⇒ Mysql
* 条件
* [.having(field)]() ⇒ Mysql
* 聚合条件

* [.count()]() ⇒ Promise
* 统计数量
* [.max(field)]() ⇒ Mysql
* 获取最大值
* [.min(field)]() ⇒ Mysql
* 获取最小值
* [.avg(field)]() ⇒ Mysql
* 获取平均值
* [.sum(field)]() ⇒ Mysql
* 获取总数

* [.lock(field, step)]() ⇒ Promise
*
* [.distinct(field, step)]() ⇒ Promise
* 去除查询结果中重复的数据
* [.comment(str:string)]() ⇒ Promise
* 注释
* [.query()]() ⇒ string
* 执行原生语句
186 changes: 186 additions & 0 deletions docs/curd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
## CURD操作

#### 原生
```js
let sql = 'select id, age, name from demo where id=123'
hMsql.query(sql);

```
#### 插入
> 调用.insert()方法插入数据,由[.data()]()提供数据
```javascript
// data为字符串,使用&相连
hMsql.table('demo')
.data('age=18&name=wanglin')
.insert()

=> INSERT INTO demo (age,name) VALUES (`18`,`wanglin`)

// data为对象
hMsql.table('demo')
.data({
age:18,
name: 'wanglin'
})
.insert()
=> INSERT INTO demo (age,name) VALUES (`18`,`wanglin`)

// 批量插入 data为数组,提示:批量插入时会自动进行优化,当超过一定数量时,会对数组进行拆分和切换模式,具体可以查看.data()
hMsql.table('demo')
.data([{
age: 18,
name: 'wanglin'
},
{
age: 18,
name: 'wanglin'
}])
.insert()
=> INSERT INTO demo (age,name) VALUES (`18`,`wanglin`), (`20`,`langzishenjian`)

```

#### 更新
> 调用 .delete() 方法,必填.where()
```javascript

// 单条更新
hMsql.table('demo')
.data({
age: 18,
name: 'wanglin'
})
.where({id: 123})
.update()
=> UPDATE demo SET age=18, name='wanglin' WHERE id=123

// 批量更新
hMsql.table('demo')
.data([{
age: 18,
name: 'wanglin'
},
{
age: 20,
name: 'langzishenjian'
}])
.where({IN: [2,3]})
.update()
=>
UPDATE demo
SET age = CASE id
WHEN 2 THEN 18
WHEN 3 THEN 20
END,
name = CASE id
WHEN 2 THEN 'wanglin'
WHEN 3 THEN 'langzishenjian'
END
WHERE id IN (2,3)

```

#### 删除
> 调用 .delete() 方法,必填.where()
```javascript
// 单条删除
hMsql.table('demo')
.where({id: 123})
.delete()
=> DELETE FROM demo WHERE id='123'

// 批量删除
hMsql.table('demo')
.where({IN: [123, 456, 789]})
.delete()
=> DELETE FROM demo WHERE id IN (123,456, 789)

```

#### 查询
* 普通查询
```javascript
// 单条查询
hMsql.table('demo')
.field('id, age, name')
.where({id: 123})
.find()

=> SELECT id, age, name FROM demo where id='123' limit 0, 1

// 多条查询
hMsql.table('demo')
.field('id, age, name')
.where({id: 123})
.select()

=> SELECT id, age, name FROM demo where id='123'
```
* 表达式查询

|名称(不区分大小写) | 描述 |
| ------------- |:------------- |
| EQ | 等于(=) |
| NEQ | 不等于(<>) |
| GT | 大于(>) |
| EGT | 大于等于(>=) |
| LT | 小于(<) |
| ELT | 小于等于(<=) |
| LIKE | 模糊查询 |
| [NOT] BETWEEN | (不在)区间查询 |
| [NOT] IN | (不在)IN 查询 |

```javascript
// 等于(EQ)
hMsql.table('demo')
.field('id, age, name')
.where({id: {EQ: 123}})
.select()

=> SELECT id, age, name FROM demo where id=123

// 不等于(NEQ)
hMsql.table('demo')
.field('id, age, name')
.where({id: {NEQ: 123}})
.select()

=> SELECT id, age, name FROM demo where id<>123
```
* 统计查询

|名称(不区分大小写) | 描述 |
| ------------- |:------------- |
| count | 统计数量,参数是要统计的字段名(可选) |
| max | 获取最大值,参数是要统计的字段名(必须) |
| min | 获取最小值,参数是要统计的字段名(必须) |
| avg | 获取平均值,参数是要统计的字段名(必须) |
| sum | 获取总分,参数是要统计的字段名(必须) |

```javascript
// 统计数量(count)
hMsql.table('demo')
.field('id, age, name')
.where({id: 123})
.count()

=> SELECT count(id) FROM demo where id=123

```
* 组合查询
* 复合查询
* 子查询
* 联合查询
* 区间查询
```javascript
hMsql.table('demo')
.field('id, age, name')
.where({IN: [1,2,3]})
.select()

=> SELECT id, age, name FROM demo where id IN (1,2,3)
```

Loading

0 comments on commit 1857941

Please sign in to comment.