forked from kelsin/mysql-mimic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbapi_proxy.py
35 lines (26 loc) · 871 Bytes
/
dbapi_proxy.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
import logging
import asyncio
import sqlite3
from mysql_mimic import MysqlServer, Session
logger = logging.getLogger(__name__)
class DbapiProxySession(Session):
def __init__(self):
super().__init__()
self.conn = sqlite3.connect(":memory:")
async def query(self, expression, sql, attrs):
cursor = self.conn.cursor()
cursor.execute(expression.sql(dialect="sqlite"))
try:
rows = cursor.fetchall()
if cursor.description:
columns = [c[0] for c in cursor.description]
return rows, columns
return None
finally:
cursor.close()
async def main():
logging.basicConfig(level=logging.DEBUG)
server = MysqlServer(session_factory=DbapiProxySession)
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())