This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
mysql.py
81 lines (64 loc) · 2.45 KB
/
mysql.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
# -*- coding:utf-8 -*-
from __future__ import absolute_import
import sys
import socket
from pymysql.connections import Connection
from gtornado import AsyncSocket, green, utils
__all__ = ("AsyncConnection", "MySQLConnectionPool", "patch_pymysql")
class AsyncConnection(Connection):
def __init__(self, *args, **kwargs):
super(AsyncConnection, self).__init__(*args, **kwargs)
def connect(self, sock=None):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket = AsyncSocket(sock)
self.socket.connect((self.host, self.port))
self.socket.set_nodelay(True)
self._rfile = self.socket
self._get_server_information()
self._request_authentication()
if self.sql_mode is not None:
c = self.cursor()
c.execute("SET sql_mode=%s", (self.sql_mode,))
if self.init_command is not None:
c = self.cursor()
c.execute(self.init_command)
c.close()
self.commit()
if self.autocommit_mode is not None:
self.autocommit(self.autocommit_mode)
except socket.error:
if self.socket:
self.socket.close()
raise
class MySQLConnectionPool(green.Pool):
__metaclass__ = utils.Singleton
def __init__(self, max_size=-1, mysql_params={}):
super(MySQLConnectionPool, self).__init__(max_size, mysql_params)
def create_raw_conn(self):
return AsyncConnection(
host=self._conn_params["host"],
port=self._conn_params["port"],
user=self._conn_params["username"],
db=self._conn_params["db"],
password=self._conn_params["password"],
charset=self._conn_params.get("charset", "utf8")
)
class ConnectionProxy(object):
def __init__(self, raw_conn):
self._raw_conn = raw_conn
self._pool = MySQLConnectionPool()
def close(self):
self._pool.release(self._raw_conn)
def __getattr__(self, key):
print("call method", key)
if key == "close":
return self.close
else:
return getattr(self._raw_conn, key)
def connect(*args, **kwargs):
pool = MySQLConnectionPool()
raw_conn = pool.get_conn()
return ConnectionProxy(raw_conn)
def patch_pymysql():
sys.modules["pymysql"].connect = connect