This repository was archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsanicdb.py
161 lines (149 loc) · 5.74 KB
/
sanicdb.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
#!/usr/bin/env python
"""A lightweight wrapper around aiomysql.Pool for easy to use
"""
import traceback
import aiomysql
import pymysql
version = "0.3"
version_info = (0, 3, 0, 0)
class SanicDB:
"""A lightweight wrapper around aiomysql.Pool for easy to use
"""
def __init__(self, host, database, user, password,
loop=None, sanic=None,
minsize=3, maxsize=5,
return_dict=True,
pool_recycle=7*3600,
autocommit=True,
charset = "utf8mb4", **kwargs):
'''
kwargs: all parameters that aiomysql.connect() accept.
'''
self.db_args = {
'host': host,
'db': database,
'user': user,
'password': password,
'minsize': minsize,
'maxsize': maxsize,
'charset': charset,
'loop': loop,
'autocommit': autocommit,
'pool_recycle': pool_recycle,
}
self.sanic = sanic
if sanic:
sanic.db = self
if return_dict:
self.db_args['cursorclass']=aiomysql.cursors.DictCursor
if kwargs:
self.db_args.update(kwargs)
self.pool = None
async def init_pool(self):
if self.sanic:
self.db_args['loop'] = self.sanic.loop
self.pool = await aiomysql.create_pool(**self.db_args)
async def query_many(self, queries):
"""query man SQL, Returns all result."""
if not self.pool:
await self.init_pool()
async with self.pool.acquire() as conn:
async with conn.cursor() as cur:
results = []
for query in queries:
try:
await cur.execute(query)
ret = await cur.fetchall()
except pymysql.err.InternalError:
await conn.ping()
await cur.execute(query)
ret = await cur.fetchall()
results.append(ret)
return results
async def query(self, query, *parameters, **kwparameters):
"""Returns a row list for the given query and parameters."""
if not self.pool:
await self.init_pool()
async with self.pool.acquire() as conn:
async with conn.cursor() as cur:
try:
await cur.execute(query, kwparameters or parameters)
ret = await cur.fetchall()
except pymysql.err.InternalError:
await conn.ping()
await cur.execute(query, kwparameters or parameters)
ret = await cur.fetchall()
return ret
async def get(self, query, *parameters, **kwparameters):
"""Returns the (singular) row returned by the given query.
"""
if not self.pool:
await self.init_pool()
async with self.pool.acquire() as conn:
async with conn.cursor() as cur:
try:
await cur.execute(query, kwparameters or parameters)
ret = await cur.fetchone()
except pymysql.err.InternalError:
await conn.ping()
await cur.execute(query, kwparameters or parameters)
ret = await cur.fetchone()
return ret
async def execute(self, query, *parameters, **kwparameters):
"""Executes the given query, returning the lastrowid from the query."""
if not self.pool:
await self.init_pool()
async with self.pool.acquire() as conn:
async with conn.cursor() as cur:
try:
await cur.execute(query, kwparameters or parameters)
except Exception:
# https://github.com/aio-libs/aiomysql/issues/340
await conn.ping()
await cur.execute(query, kwparameters or parameters)
return cur.lastrowid
# high level interface
async def table_has(self, table_name, field, value):
sql = 'SELECT {} FROM {} WHERE {}=%s limit 1'.format(field, table_name, field)
d = await self.get(sql, value)
return d
async def table_insert(self, table_name, item, ignore_duplicated=True):
'''item is a dict : key is mysql table field'''
fields = list(item.keys())
values = list(item.values())
fieldstr = ','.join(fields)
valstr = ','.join(['%s'] * len(item))
sql = 'INSERT INTO %s (%s) VALUES(%s)' % (table_name, fieldstr, valstr)
try:
last_id = await self.execute(sql, *values)
return last_id
except Exception as e:
if ignore_duplicated and e.args[0] == 1062:
# just skip duplicated item
return 0
traceback.print_exc()
print('sql:', sql)
print('item:')
for i in range(len(fields)):
vs = str(values[i])
if len(vs) > 300:
print(fields[i], ' : ', len(vs), type(values[i]))
else:
print(fields[i], ' : ', vs, type(values[i]))
raise e
async def table_update(self, table_name, updates,
field_where, value_where):
'''updates is a dict of {field_update:value_update}'''
upsets = []
values = []
for k, v in updates.items():
s = '%s=%%s' % k
upsets.append(s)
values.append(v)
upsets = ','.join(upsets)
sql = 'UPDATE %s SET %s WHERE %s="%s"' % (
table_name,
upsets,
field_where, value_where,
)
await self.execute(sql, *(values))