-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync_db.py
187 lines (135 loc) · 6.29 KB
/
async_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Name: async_db
# Author: Reacubeth
# Time: 2020/8/10 16:38
# Mail: [email protected]
# Site: www.omegaxyz.com
# *_*coding:utf-8 *_*
import json
import time
import aiomysql
POOL = {}
Cursor = aiomysql.Cursor
DictCursor = aiomysql.DictCursor
SSCursor = aiomysql.SSCursor
SSDictCursor = aiomysql.SSDictCursor
async def get_pool(db):
global POOL
if db not in POOL:
POOL[db] = await aiomysql.create_pool(
maxsize=20,
host='localhost',
port=3306,
user='root',
password='123456',
db=db,
charset='utf8mb4',
autocommit=True
)
return POOL[db]
async def mysql_select(sql, *params, db, cursor_type=Cursor):
pool = await get_pool(db)
async with pool.acquire() as conn:
async with conn.cursor(cursor_type) as cursor:
await cursor.execute(sql, params)
return await cursor.fetchall()
async def get_project():
sql = "SELECT * FROM project"
ret = await mysql_select(sql, db='label_sys', cursor_type=DictCursor)
return ret
async def delete_project(p_id):
sql = "DELETE FROM project WHERE p_id = %s"
sql2 = "DELETE FROM entity_class WHERE p_id = %s"
sql3 = "DELETE FROM file WHERE p_id = %s"
ret = await mysql_select(sql, p_id, db='label_sys')
ret2 = await mysql_select(sql2, p_id, db='label_sys')
ret3 = await mysql_select(sql3, p_id, db='label_sys')
return 'success'
async def insert_project(p_id, path, name, total):
time_str = time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(time.time()))
sql = "INSERT INTO project(p_id, path, name, time, total, parse_done) VALUES(%s, %s, %s, %s, %s, %s)"
ret = await mysql_select(sql, p_id, path, name, time_str, total, '0', db='label_sys')
return 'success'
async def update_project(p_id, file_num):
sql = "UPDATE project SET total = total + %s WHERE p_id = %s"
ret = await mysql_select(sql, file_num, p_id, db='label_sys')
return 'success'
async def update_project_log(p_id, log):
sql = "UPDATE project SET log = CONCAT(IFNULL(log,''), %s) WHERE p_id = %s"
ret = await mysql_select(sql, log, p_id, db='label_sys')
return 'success'
async def insert_file(name, path, text, p_id, entity_list, hint):
sql = "INSERT INTO file(file_name, file_path, version, entity_list, text, p_id, is_edit, hint, Mark_relation) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)"
ret = await mysql_select(sql, name, path, 0, json.dumps(entity_list), json.dumps(text), p_id, 0, json.dumps(hint), json.dumps(eval('[]')), db='label_sys')
return 'success'
async def get_entity_class(p_id):
sql = "select * from entity_class where p_id = %s"
ret = await mysql_select(sql, p_id, db='label_sys', cursor_type=DictCursor)
return ret
async def insert_entity_class(label, color, des, p_id):
sql = "INSERT INTO entity_class(label, color, description, p_id) VALUES(%s, %s, %s, %s)"
ret = await mysql_select(sql, label, color, des, p_id, db='label_sys')
return 'success'
async def update_entity_class(label, color, des, c_id):
sql = "UPDATE entity_class SET label=%s, color=%s, description=%s WHERE c_id=%s"
ret = await mysql_select(sql, label, color, des, c_id, db='label_sys')
return 'success'
async def delete_entity_class(c_id):
sql = "DELETE FROM entity_class WHERE c_id = %s"
ret = await mysql_select(sql, c_id, db='label_sys')
return 'success'
async def fetch_file_limit(p_id, anchor, page_size):
sql = "SELECT * FROM file where p_id = %s limit %s, %s"
ret = await mysql_select(sql, p_id, anchor, page_size, db='label_sys', cursor_type=DictCursor)
return ret
async def fetch_files(p_id):
sql = "SELECT * FROM file where p_id = %s"
ret = await mysql_select(sql, p_id, db='label_sys', cursor_type=DictCursor)
return ret
async def update_entity_list(f_id, entity_list):
sql = "UPDATE file SET entity_list = %s WHERE f_id = %s"
sql2 = "UPDATE file SET version = version + 1 WHERE f_id = %s"
ret = await mysql_select(sql, json.dumps(entity_list), f_id, db='label_sys')
ret2 = await mysql_select(sql2, f_id, db='label_sys')
sql_log = "INSERT INTO project_history(f_id, p_id, version, entity_list, Mark_relation) select f_id, p_id, version, entity_list, Mark_relation from file where f_id = %s"
ret_log = await mysql_select(sql_log, f_id, db='label_sys')
return 'success'
async def update_relation_list(f_id, relation_list):
sql = "UPDATE file SET Mark_relation = %s WHERE f_id = %s"
ret = await mysql_select(sql, json.dumps(relation_list), f_id, db='label_sys')
return 'success'
async def change_status(f_id, status):
sql = "UPDATE file SET is_edit = %s WHERE f_id = %s"
ret = await mysql_select(sql, status, f_id, db='label_sys')
return 'success'
async def update_file_check(f_id, checked):
sql = "UPDATE file SET checked = %s WHERE f_id = %s"
ret = await mysql_select(sql, checked, f_id, db='label_sys')
return 'success'
async def get_figure_class():
sql = "select * from figure_class"
ret = await mysql_select(sql, db='label_sys', cursor_type=DictCursor)
return ret
async def get_rand_figure():
sql = "select * from figure ORDER BY rand() limit 1"
ret = await mysql_select(sql, db='label_sys', cursor_type=DictCursor)
return ret
async def get_figure(figure_id):
sql = "select * from figure WHERE figure_id = %s"
ret = await mysql_select(sql, figure_id, db='label_sys', cursor_type=DictCursor)
return ret
async def update_figure_class(figure_id, figure_c):
sql = "UPDATE figure SET label = %s WHERE figure_id = %s"
sql2 = "UPDATE figure SET version = version + 1 WHERE figure_id = %s"
ret = await mysql_select(sql, figure_c, figure_id, db='label_sys')
ret2 = await mysql_select(sql2, figure_id, db='label_sys')
return 'success'
async def insert_figure_class(label, color):
sql = "INSERT INTO figure_class(label, color) VALUES(%s, %s)"
ret = await mysql_select(sql, label, color, db='label_sys')
return 'success'
async def parsing_status(p_id, status, file_num):
sql = "UPDATE project SET parse_done = %s WHERE p_id = %s"
ret = await mysql_select(sql, status, p_id, db='label_sys')
sql2 = "UPDATE project SET total = %s WHERE p_id = %s"
ret2 = await mysql_select(sql2, file_num, p_id, db='label_sys')
return 'success'