mysqlib 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mysqlib/__init__.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# simplemysql/core.py
|
|
2
|
+
from contextlib import contextmanager
|
|
3
|
+
from mysql.connector import pooling
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MySQL:
|
|
7
|
+
def __init__(self, config: dict, pool_size: int = 10, pool_name: str = "pool"):
|
|
8
|
+
"""
|
|
9
|
+
Args:
|
|
10
|
+
config : host, port, user, password, database, charset
|
|
11
|
+
pool_size: range 1~32
|
|
12
|
+
"""
|
|
13
|
+
self.pool = pooling.MySQLConnectionPool(
|
|
14
|
+
pool_name=pool_name,
|
|
15
|
+
pool_size=pool_size,
|
|
16
|
+
pool_reset_session=True,
|
|
17
|
+
**config,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
@contextmanager
|
|
21
|
+
def _cursor(self, dictionary=False):
|
|
22
|
+
conn = self.pool.get_connection()
|
|
23
|
+
try:
|
|
24
|
+
cursor = conn.cursor(dictionary=dictionary)
|
|
25
|
+
try:
|
|
26
|
+
yield conn, cursor
|
|
27
|
+
finally:
|
|
28
|
+
cursor.close()
|
|
29
|
+
finally:
|
|
30
|
+
conn.close()
|
|
31
|
+
|
|
32
|
+
def query(self, sql, values=None):
|
|
33
|
+
with self._cursor(dictionary=True) as (_, cur):
|
|
34
|
+
cur.execute(sql, values)
|
|
35
|
+
return cur.fetchall()
|
|
36
|
+
|
|
37
|
+
def execute(self, sql, values=None):
|
|
38
|
+
with self._cursor() as (conn, cur):
|
|
39
|
+
cur.execute(sql, values)
|
|
40
|
+
conn.commit()
|
|
41
|
+
return cur.lastrowid, cur.rowcount
|
|
42
|
+
|
|
43
|
+
def transaction(self, statements):
|
|
44
|
+
with self._cursor() as (conn, cur):
|
|
45
|
+
try:
|
|
46
|
+
for sql, values in statements:
|
|
47
|
+
cur.execute(sql, values)
|
|
48
|
+
conn.commit()
|
|
49
|
+
return cur.lastrowid
|
|
50
|
+
except Exception:
|
|
51
|
+
conn.rollback()
|
|
52
|
+
raise
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
mysqlib/__init__.py,sha256=0EhP6X38MR36kRl1rRCH2OxbPFOYttxC1KTXJUPra3o,1602
|
|
2
|
+
mysqlib-0.1.0.dist-info/METADATA,sha256=r7MfMoAsYp5F5e9kVZol1bv5mx-vUJnn0OjE8ERqDQM,210
|
|
3
|
+
mysqlib-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
mysqlib-0.1.0.dist-info/top_level.txt,sha256=tvpoMRjUxmd-A2bj5Wjx4i82SFxZuH0SCG3ZvP8Xa5o,8
|
|
5
|
+
mysqlib-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mysqlib
|