-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_support.py
executable file
·156 lines (141 loc) · 4.68 KB
/
db_support.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
import asyncio
import db
import json
import sqlalchemy as sa
from sqlalchemy.sql import select, insert, update, delete
from aiomysql.sa import create_engine
import decimal, datetime
import aiohttp
import logging
def alchemyencoder(obj):
"""JSON encoder function for SQLAlch emy special classes."""
if isinstance(obj, datetime.date):
return obj.isoformat()
elif isinstance(obj, decimal.Decimal):
return float(obj)
def do_log(level, info):
if level == 5:
logging.critical(info)
elif level == 4:
logging.error(info)
elif level == 3:
logging.warning(info)
elif level == 2:
logging.info(info)
elif level == 1:
logging.debug(info)
async def aio_post(url, data):
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data) as resp:
return await resp.json()
except Exception as e:
# print(e)
return False
async def aio_delete(url):
try:
async with aiohttp.ClientSession() as session:
async with session.delete(url) as resp:
# print(resp)
return await resp.json()
except Exception as e:
# print(e)
return False
async def aio_put(url, data):
try:
async with aiohttp.ClientSession() as session:
async with session.put(url, json=data) as resp:
return await resp.json()
except Exception as e:
# print(e)
return False
async def aio_patch(url, data):
try:
async with aiohttp.ClientSession() as session:
async with session.patch(url, json=data) as resp:
return await resp.json()
except Exception as e:
# print(e)
return False
async def db_select(form_name, find=["*"], **kwargs):
try:
select_engine = await create_engine(**db.config)
cell = getattr(db, form_name)
if find[0] == "*":
finds = [cell]
else:
finds = []
for i in find:
finds.append(getattr(cell.c, i))
async with select_engine.acquire() as conn:
sql = select(finds)
for i in kwargs:
sql = sql.where(getattr(cell.c, i) == kwargs[i])
select_info = await conn.execute(sql)
result = json.dumps([(dict(row.items()))
for row in select_info], default=alchemyencoder)
# print(result)
return json.loads(result)
except Exception as e:
do_log(4, "select error:" + str(e))
return []
async def db_insert(form_name, **kwargs):
try:
insert_engine = await create_engine(**db.config)
cell = getattr(db, form_name)
async with insert_engine.acquire() as conn:
trans = await conn.begin()
try:
await conn.execute(insert(cell).values(kwargs))
except Exception as e:
main.do_log(4, "insert error:" + str(e))
await trans.rollback()
return False
else:
await trans.commit()
return True
except Exception as e:
do_log(4, "insert error:" + str(e))
return False
async def db_update(form_name, set={}, **kwargs):
try:
update_engine = await create_engine(**db.config)
cell = getattr(db, form_name)
async with update_engine.acquire() as conn:
sql = update(cell).values(set)
for i in kwargs:
sql = sql.where(getattr(cell.c, i) == kwargs[i])
trans = await conn.begin()
try:
await conn.execute(sql)
except Exception as e:
main.do_log(4, "update error:" + str(e))
await trans.rollback()
return False
else:
await trans.commit()
return True
except Exception as e:
do_log(4, "update error:" + str(e))
return False
async def db_delete(form_name, **kwargs):
try:
delete_engine = await create_engine(**db.config)
cell = getattr(db, form_name)
async with delete_engine.acquire() as conn:
sql = delete(cell)
for i in kwargs:
sql = sql.where(getattr(cell.c, i) == kwargs[i])
trans = await conn.begin()
try:
await conn.execute(sql)
except Exception as e:
main.do_log(4, "delete error:" + str(e))
await trans.rollback()
return False
else:
await trans.commit()
return True
except Exception as e:
do_log(4, "delete error:" + str(e))
return False