Skip to content

Commit 9b06c8d

Browse files
author
Boris
committed
添加MysqlDB文档
1 parent 7c72eca commit 9b06c8d

2 files changed

Lines changed: 202 additions & 27 deletions

File tree

docs/source_code/MysqlDB.md

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,146 @@
11
# MysqlDB
22

3-
未完待续
3+
MysqlDB具有断开自动重连特性,支持多线程下操作,内置连接池,最大连接数100
4+
5+
## 连接
6+
7+
```python
8+
from feapder.db.mysqldb import MysqlDB
9+
10+
11+
db = MysqlDB(
12+
ip="localhost", port=3306, db="feapder", user_name="feapder", user_pass="feapder123"
13+
)
14+
```
15+
16+
若环境变量中配置了数据库连接方式或者setting中已配置,则可不传参
17+
18+
```python
19+
db = MysqlDB()
20+
```
21+
22+
或者可以根据url连接
23+
24+
```python
25+
db = MysqlDB.from_url("mysql://username:ip:port/db?charset=utf8mb4")
26+
```
27+
28+
## 方法
29+
30+
> MysqlDB封装了增删改查等方法,方便使用
31+
32+
###
33+
34+
```python
35+
def find(self, sql, limit=0, to_json=False):
36+
"""
37+
@summary:
38+
无数据: 返回()
39+
有数据: 若limit == 1 则返回 (data1, data2)
40+
否则返回 ((data1, data2),)
41+
---------
42+
@param sql:
43+
@param limit:
44+
@param to_json 是否将查询结果转为json
45+
---------
46+
@result:
47+
"""
48+
```
49+
50+
51+
###
52+
53+
```python
54+
def add(self, sql, exception_callfunc=None):
55+
"""
56+
Args:
57+
sql:
58+
exception_callfunc: 异常回调
59+
60+
Returns:添加行数
61+
62+
"""
63+
```
64+
65+
```python
66+
def add_smart(self, table, data: Dict, **kwargs):
67+
"""
68+
添加数据, 直接传递json格式的数据,不用拼sql
69+
Args:
70+
table: 表名
71+
data: 字典 {"xxx":"xxx"}
72+
**kwargs:
73+
74+
Returns:添加行数
75+
76+
"""
77+
```
78+
79+
80+
```python
81+
def add_batch(self, sql, datas: List[Dict]):
82+
"""
83+
@summary: 批量添加数据
84+
---------
85+
@ param sql: insert ignore into (xxx,xxx) values (%s, %s, %s)
86+
# param datas: 列表 [[..], [...]]
87+
---------
88+
@result:添加行数
89+
"""
90+
```
91+
92+
```python
93+
def add_batch_smart(self, table, datas: List[Dict], **kwargs):
94+
"""
95+
批量添加数据, 直接传递list格式的数据,不用拼sql
96+
Args:
97+
table: 表名
98+
datas: 列表 [[..], [...]]
99+
**kwargs:
100+
101+
Returns: 添加行数
102+
103+
"""
104+
```
105+
106+
### 更新
107+
108+
```python
109+
def update(self, sql):
110+
pass
111+
```
112+
113+
```python
114+
def update_smart(self, table, data: Dict, condition):
115+
"""
116+
更新, 不用拼sql
117+
Args:
118+
table: 表名
119+
data: 数据 {"xxx":"xxx"}
120+
condition: 更新条件 where后面的条件,如 condition='status=1'
121+
122+
Returns: True / False
123+
124+
"""
125+
```
126+
127+
### 删除
128+
129+
```python
130+
def delete(self, sql):
131+
"""
132+
删除
133+
Args:
134+
sql:
135+
136+
Returns: True / False
137+
138+
"""
139+
```
140+
141+
### 执行其他sql
142+
143+
```python
144+
def execute(self, sql):
145+
pass
146+
```

feapder/db/mysqldb.py

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import datetime
1111
import json
1212
from urllib import parse
13+
from typing import List, Dict
1314

1415
import pymysql
1516
from dbutils.pooled_db import PooledDB
@@ -154,7 +155,7 @@ def size_of_connect_pool(self):
154155
return len(self.connect_pool._idle_cache)
155156

156157
@auto_retry
157-
def find(self, sql, limit=0, to_json=False, cursor=None):
158+
def find(self, sql, limit=0, to_json=False):
158159
"""
159160
@summary:
160161
无数据: 返回()
@@ -163,6 +164,7 @@ def find(self, sql, limit=0, to_json=False, cursor=None):
163164
---------
164165
@param sql:
165166
@param limit:
167+
@param to_json 是否将查询结果转为json
166168
---------
167169
@result:
168170
"""
@@ -206,7 +208,16 @@ def convert(col):
206208

207209
return result
208210

209-
def add(self, sql, exception_callfunc=""):
211+
def add(self, sql, exception_callfunc=None):
212+
"""
213+
214+
Args:
215+
sql:
216+
exception_callfunc: 异常回调
217+
218+
Returns: 添加行数
219+
220+
"""
210221
affect_count = None
211222

212223
try:
@@ -229,18 +240,28 @@ def add(self, sql, exception_callfunc=""):
229240

230241
return affect_count
231242

232-
def add2(self, table, data, **kwargs):
243+
def add_smart(self, table, data: Dict, **kwargs):
244+
"""
245+
添加数据, 直接传递json格式的数据,不用拼sql
246+
Args:
247+
table: 表名
248+
data: 字典 {"xxx":"xxx"}
249+
**kwargs:
250+
251+
Returns: 添加行数
252+
253+
"""
233254
sql = make_insert_sql(table, data, **kwargs)
234255
return self.add(sql)
235256

236-
def add_batch(self, sql, datas):
257+
def add_batch(self, sql, datas: List[Dict]):
237258
"""
238-
@summary:
259+
@summary: 批量添加数据
239260
---------
240261
@ param sql: insert ignore into (xxx,xxx) values (%s, %s, %s)
241-
# param datas:[[..], [...]]
262+
# param datas: 列表 [[..], [...]]
242263
---------
243-
@result:
264+
@result: 添加行数
244265
"""
245266
affect_count = None
246267

@@ -262,7 +283,17 @@ def add_batch(self, sql, datas):
262283

263284
return affect_count
264285

265-
def add_batch2(self, table, datas, **kwargs):
286+
def add_batch_smart(self, table, datas: List[Dict], **kwargs):
287+
"""
288+
批量添加数据, 直接传递list格式的数据,不用拼sql
289+
Args:
290+
table: 表名
291+
datas: 列表 [[..], [...]]
292+
**kwargs:
293+
294+
Returns: 添加行数
295+
296+
"""
266297
sql, datas = make_batch_sql(table, datas, **kwargs)
267298
return self.add_batch(sql, datas)
268299

@@ -286,11 +317,29 @@ def update(self, sql):
286317
finally:
287318
self.close_connection(conn, cursor)
288319

289-
def update2(self, table, data, condition):
320+
def update_smart(self, table, data: Dict, condition):
321+
"""
322+
更新, 不用拼sql
323+
Args:
324+
table: 表名
325+
data: 数据 {"xxx":"xxx"}
326+
condition: 更新条件 where后面的条件,如 condition='status=1'
327+
328+
Returns: True / False
329+
330+
"""
290331
sql = make_update_sql(table, data, condition)
291332
return self.update(sql)
292333

293334
def delete(self, sql):
335+
"""
336+
删除
337+
Args:
338+
sql:
339+
340+
Returns: True / False
341+
342+
"""
294343
try:
295344
conn, cursor = self.get_connection()
296345
cursor.execute(sql)
@@ -329,20 +378,3 @@ def execute(self, sql):
329378
return True
330379
finally:
331380
self.close_connection(conn, cursor)
332-
333-
def set_unique_key(self, table, key):
334-
try:
335-
sql = "alter table %s add unique (%s)" % (table, key)
336-
337-
conn, cursor = self.get_connection()
338-
cursor.execute(sql)
339-
conn.commit()
340-
341-
except Exception as e:
342-
log.error(table + " " + str(e) + " key = " + key)
343-
return False
344-
else:
345-
log.debug("%s表创建唯一索引成功 索引为 %s" % (table, key))
346-
return True
347-
finally:
348-
self.close_connection(conn, cursor)

0 commit comments

Comments
 (0)