sqlodet 0.2__py3-none-any.whl → 0.3__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.
- sqlodet/__init__.py +14 -0
- sqlodet/adapters/__init__.py +17 -0
- sqlodet/adapters/ageneric.py +72 -0
- sqlodet/adapters/aiosqlite.py +65 -0
- sqlodet/adapters/apyformat.py +11 -0
- sqlodet/adapters/asyncpg.py +129 -0
- sqlodet/adapters/duckdb.py +93 -0
- sqlodet/adapters/generic.py +123 -0
- sqlodet/adapters/mysql.py +30 -0
- sqlodet/adapters/pg8000.py +8 -0
- sqlodet/adapters/pyformat.py +21 -0
- sqlodet/adapters/sqlite3.py +21 -0
- sqlodet/aiosql.py +202 -0
- sqlodet/py.typed +0 -0
- sqlodet/queries.py +343 -0
- sqlodet/query_loader.py +272 -0
- sqlodet/syntax.dat +0 -0
- sqlodet/types.py +159 -0
- sqlodet/utils.py +37 -0
- {sqlodet-0.2.dist-info → sqlodet-0.3.dist-info}/METADATA +1 -1
- sqlodet-0.3.dist-info/RECORD +24 -0
- sqlodet-0.3.dist-info/top_level.txt +1 -0
- sqlodet-0.2.dist-info/RECORD +0 -5
- sqlodet-0.2.dist-info/top_level.txt +0 -1
- {sqlodet-0.2.dist-info → sqlodet-0.3.dist-info}/WHEEL +0 -0
- {sqlodet-0.2.dist-info → sqlodet-0.3.dist-info}/licenses/LICENSE +0 -0
sqlodet/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from .aiosql import from_path, from_str, register_adapter
|
|
2
|
+
from .utils import SQLParseException, SQLLoadException
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
__version__ = version("sqlodet")
|
|
6
|
+
|
|
7
|
+
# exported package interface
|
|
8
|
+
__all__ = [
|
|
9
|
+
"SQLLoadException",
|
|
10
|
+
"SQLParseException",
|
|
11
|
+
"from_path",
|
|
12
|
+
"from_str",
|
|
13
|
+
"register_adapter"
|
|
14
|
+
]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# standard adapters
|
|
2
|
+
from .pyformat import PyFormatAdapter
|
|
3
|
+
from .generic import GenericAdapter
|
|
4
|
+
from .sqlite3 import SQLite3Adapter
|
|
5
|
+
|
|
6
|
+
# async adapters
|
|
7
|
+
from .aiosqlite import AioSQLiteAdapter
|
|
8
|
+
from .asyncpg import AsyncPGAdapter
|
|
9
|
+
|
|
10
|
+
# silence flake8 F401 warning:
|
|
11
|
+
_ALL = [
|
|
12
|
+
PyFormatAdapter,
|
|
13
|
+
GenericAdapter,
|
|
14
|
+
SQLite3Adapter,
|
|
15
|
+
AioSQLiteAdapter,
|
|
16
|
+
AsyncPGAdapter,
|
|
17
|
+
]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from contextlib import asynccontextmanager
|
|
2
|
+
from ..types import AsyncDriverAdapterProtocol
|
|
3
|
+
|
|
4
|
+
# it is unclear how generic is this class
|
|
5
|
+
class AsyncGenericAdapter(AsyncDriverAdapterProtocol):
|
|
6
|
+
|
|
7
|
+
is_aio_driver = True
|
|
8
|
+
|
|
9
|
+
def process_sql(self, query_name, op_type, sql):
|
|
10
|
+
return sql # pragma: no cover
|
|
11
|
+
|
|
12
|
+
# this is an asynchronous generator
|
|
13
|
+
async def select(self, conn, query_name, sql, parameters, record_class=None):
|
|
14
|
+
cur = await conn.execute(sql, parameters)
|
|
15
|
+
try:
|
|
16
|
+
if record_class is not None:
|
|
17
|
+
column_names = [c[0] for c in cur.description]
|
|
18
|
+
for row in await cur.fetchall():
|
|
19
|
+
yield record_class(**dict(zip(column_names, row)))
|
|
20
|
+
else:
|
|
21
|
+
# psycopg 3.3: async for row in cur.results():
|
|
22
|
+
for row in await cur.fetchall():
|
|
23
|
+
yield row
|
|
24
|
+
finally:
|
|
25
|
+
await cur.close()
|
|
26
|
+
|
|
27
|
+
async def select_one(self, conn, query_name, sql, parameters, record_class=None):
|
|
28
|
+
cur = await conn.execute(sql, parameters)
|
|
29
|
+
result = await cur.fetchone()
|
|
30
|
+
if result is not None and record_class is not None:
|
|
31
|
+
column_names = [c[0] for c in cur.description]
|
|
32
|
+
result = record_class(**dict(zip(column_names, result)))
|
|
33
|
+
await cur.close()
|
|
34
|
+
return result
|
|
35
|
+
|
|
36
|
+
async def select_value(self, conn, query_name, sql, parameters):
|
|
37
|
+
cur = await conn.execute(sql, parameters)
|
|
38
|
+
result = await cur.fetchone()
|
|
39
|
+
res = result[0] if result else None
|
|
40
|
+
await cur.close()
|
|
41
|
+
return res
|
|
42
|
+
|
|
43
|
+
@asynccontextmanager
|
|
44
|
+
async def select_cursor(self, conn, query_name, sql, parameters):
|
|
45
|
+
cur = await conn.execute(sql, parameters)
|
|
46
|
+
yield cur
|
|
47
|
+
await cur.close()
|
|
48
|
+
|
|
49
|
+
async def insert_returning(self, conn, query_name, sql, parameters):
|
|
50
|
+
cur = await conn.execute(sql, parameters)
|
|
51
|
+
result = await cur.fetchone()
|
|
52
|
+
# res = result[0] if result else None
|
|
53
|
+
await cur.close()
|
|
54
|
+
return result
|
|
55
|
+
|
|
56
|
+
async def insert_update_delete(self, conn, query_name, sql, parameters):
|
|
57
|
+
cur = await conn.execute(sql, parameters)
|
|
58
|
+
rc = cur.rowcount if hasattr(cur, "rowcount") else None
|
|
59
|
+
await cur.close()
|
|
60
|
+
return rc
|
|
61
|
+
|
|
62
|
+
async def insert_update_delete_many(self, conn, query_name, sql, parameters):
|
|
63
|
+
cur = conn.cursor()
|
|
64
|
+
res = await cur.executemany(sql, parameters)
|
|
65
|
+
await cur.close()
|
|
66
|
+
return res
|
|
67
|
+
|
|
68
|
+
async def execute_script(self, conn, sql):
|
|
69
|
+
cur = await conn.execute(sql)
|
|
70
|
+
msg = cur.statusmessage if hasattr(cur, "statusmessage") else "DONE"
|
|
71
|
+
await cur.close()
|
|
72
|
+
return msg
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from contextlib import asynccontextmanager
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class AioSQLiteAdapter:
|
|
5
|
+
is_aio_driver = True
|
|
6
|
+
|
|
7
|
+
def process_sql(self, _query_name, _op_type, sql):
|
|
8
|
+
"""Pass through function because the ``aiosqlite`` driver can already handle the
|
|
9
|
+
``:var_name`` format used by aiosql and doesn't need any additional processing.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
|
|
13
|
+
- _query_name (str): The name of the sql query.
|
|
14
|
+
- _op_type (SQLOperationType): The type of SQL operation performed by the query.
|
|
15
|
+
- sql (str): The sql as written before processing.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
|
|
19
|
+
- str: Original SQL text unchanged.
|
|
20
|
+
"""
|
|
21
|
+
return sql
|
|
22
|
+
|
|
23
|
+
async def select(self, conn, _query_name, sql, parameters, record_class=None):
|
|
24
|
+
async with conn.execute(sql, parameters) as cur:
|
|
25
|
+
if record_class is not None:
|
|
26
|
+
column_names = [c[0] for c in cur.description]
|
|
27
|
+
for row in await cur.fetchall():
|
|
28
|
+
yield record_class(**dict(zip(column_names, row)))
|
|
29
|
+
else:
|
|
30
|
+
for row in await cur.fetchall():
|
|
31
|
+
yield row
|
|
32
|
+
|
|
33
|
+
async def select_one(self, conn, _query_name, sql, parameters, record_class=None):
|
|
34
|
+
async with conn.execute(sql, parameters) as cur:
|
|
35
|
+
result = await cur.fetchone()
|
|
36
|
+
if result is not None and record_class is not None:
|
|
37
|
+
column_names = [c[0] for c in cur.description]
|
|
38
|
+
result = record_class(**dict(zip(column_names, result)))
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
async def select_value(self, conn, _query_name, sql, parameters):
|
|
42
|
+
async with conn.execute(sql, parameters) as cur:
|
|
43
|
+
result = await cur.fetchone()
|
|
44
|
+
return result[0] if result else None
|
|
45
|
+
|
|
46
|
+
@asynccontextmanager
|
|
47
|
+
async def select_cursor(self, conn, _query_name, sql, parameters):
|
|
48
|
+
async with conn.execute(sql, parameters) as cur:
|
|
49
|
+
yield cur
|
|
50
|
+
|
|
51
|
+
async def insert_returning(self, conn, _query_name, sql, parameters):
|
|
52
|
+
async with conn.execute(sql, parameters) as cur:
|
|
53
|
+
return cur.lastrowid
|
|
54
|
+
|
|
55
|
+
async def insert_update_delete(self, conn, _query_name, sql, parameters):
|
|
56
|
+
async with conn.execute(sql, parameters) as cur:
|
|
57
|
+
return cur.rowcount
|
|
58
|
+
|
|
59
|
+
async def insert_update_delete_many(self, conn, _query_name, sql, parameters):
|
|
60
|
+
cur = await conn.executemany(sql, parameters)
|
|
61
|
+
await cur.close()
|
|
62
|
+
|
|
63
|
+
async def execute_script(self, conn, sql):
|
|
64
|
+
await conn.executescript(sql)
|
|
65
|
+
return "DONE"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .ageneric import AsyncGenericAdapter
|
|
2
|
+
from .pyformat import _replacer
|
|
3
|
+
from ..utils import VAR_REF
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class AsyncPyFormatAdapter(AsyncGenericAdapter):
|
|
7
|
+
"""Convert from named to pyformat parameter style."""
|
|
8
|
+
|
|
9
|
+
def process_sql(self, query_name, op_type, sql):
|
|
10
|
+
"""From named to pyformat."""
|
|
11
|
+
return VAR_REF.sub(_replacer, sql)
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
from collections import defaultdict
|
|
2
|
+
from contextlib import asynccontextmanager
|
|
3
|
+
|
|
4
|
+
from ..utils import VAR_REF
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MaybeAcquire:
|
|
8
|
+
def __init__(self, client, driver=None):
|
|
9
|
+
self.client = client
|
|
10
|
+
self._driver = driver
|
|
11
|
+
|
|
12
|
+
async def __aenter__(self):
|
|
13
|
+
if "acquire" in dir(self.client):
|
|
14
|
+
self._managed_conn = await self.client.acquire()
|
|
15
|
+
return self._managed_conn
|
|
16
|
+
else:
|
|
17
|
+
self._managed_conn = None
|
|
18
|
+
return self.client
|
|
19
|
+
|
|
20
|
+
async def __aexit__(self, exc_type, exc, tb):
|
|
21
|
+
if self._managed_conn is not None:
|
|
22
|
+
await self.client.release(self._managed_conn)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class AsyncPGAdapter:
|
|
26
|
+
is_aio_driver = True
|
|
27
|
+
|
|
28
|
+
def __init__(self):
|
|
29
|
+
self.var_sorted = defaultdict(list)
|
|
30
|
+
|
|
31
|
+
def process_sql(self, query_name, _op_type, sql):
|
|
32
|
+
"""asyncpg seems to only support numeric."""
|
|
33
|
+
adj = 0
|
|
34
|
+
|
|
35
|
+
for match in VAR_REF.finditer(sql):
|
|
36
|
+
gd = match.groupdict()
|
|
37
|
+
# Do nothing if the match is found within quotes.
|
|
38
|
+
if gd["dquote"] is not None or gd["squote"] is not None:
|
|
39
|
+
continue
|
|
40
|
+
|
|
41
|
+
var_name = gd["var_name"]
|
|
42
|
+
if var_name in self.var_sorted[query_name]:
|
|
43
|
+
replacement = f"${self.var_sorted[query_name].index(var_name) + 1}"
|
|
44
|
+
else:
|
|
45
|
+
replacement = f"${len(self.var_sorted[query_name]) + 1}"
|
|
46
|
+
self.var_sorted[query_name].append(var_name)
|
|
47
|
+
|
|
48
|
+
# Determine the offset of the start and end of the original
|
|
49
|
+
# variable that we are replacing, taking into account an adjustment
|
|
50
|
+
# factor based on previous replacements (see the note below).
|
|
51
|
+
start = match.start() + len(gd["lead"]) + adj
|
|
52
|
+
end = match.end() + adj
|
|
53
|
+
|
|
54
|
+
sql = sql[:start] + replacement + sql[end:]
|
|
55
|
+
|
|
56
|
+
# If the replacement and original variable were different lengths,
|
|
57
|
+
# then the offsets of subsequent matches will be wrong by the
|
|
58
|
+
# difference. Calculate an adjustment to apply to reconcile those
|
|
59
|
+
# offsets with the modified string.
|
|
60
|
+
#
|
|
61
|
+
# The "- 1" is to account for the leading ":" character in the
|
|
62
|
+
# original string.
|
|
63
|
+
adj += len(replacement) - len(var_name) - 1
|
|
64
|
+
|
|
65
|
+
return sql
|
|
66
|
+
|
|
67
|
+
def maybe_order_params(self, query_name, parameters):
|
|
68
|
+
if isinstance(parameters, dict):
|
|
69
|
+
return [parameters[rk] for rk in self.var_sorted[query_name]]
|
|
70
|
+
elif isinstance(parameters, tuple):
|
|
71
|
+
return parameters
|
|
72
|
+
else:
|
|
73
|
+
raise TypeError(f"Parameters expected to be dict or tuple, received {parameters}")
|
|
74
|
+
|
|
75
|
+
async def select(self, conn, query_name, sql, parameters, record_class=None):
|
|
76
|
+
parameters = self.maybe_order_params(query_name, parameters)
|
|
77
|
+
async with MaybeAcquire(conn) as connection:
|
|
78
|
+
results = await connection.fetch(sql, *parameters)
|
|
79
|
+
if record_class is not None:
|
|
80
|
+
for rec in results:
|
|
81
|
+
yield record_class(**dict(rec))
|
|
82
|
+
else:
|
|
83
|
+
for rec in results:
|
|
84
|
+
yield rec
|
|
85
|
+
|
|
86
|
+
async def select_one(self, conn, query_name, sql, parameters, record_class=None):
|
|
87
|
+
parameters = self.maybe_order_params(query_name, parameters)
|
|
88
|
+
async with MaybeAcquire(conn) as connection:
|
|
89
|
+
result = await connection.fetchrow(sql, *parameters)
|
|
90
|
+
if result is not None and record_class is not None:
|
|
91
|
+
result = record_class(**dict(result))
|
|
92
|
+
return result
|
|
93
|
+
|
|
94
|
+
async def select_value(self, conn, query_name, sql, parameters):
|
|
95
|
+
parameters = self.maybe_order_params(query_name, parameters)
|
|
96
|
+
async with MaybeAcquire(conn) as connection:
|
|
97
|
+
return await connection.fetchval(sql, *parameters)
|
|
98
|
+
|
|
99
|
+
@asynccontextmanager
|
|
100
|
+
async def select_cursor(self, conn, query_name, sql, parameters):
|
|
101
|
+
parameters = self.maybe_order_params(query_name, parameters)
|
|
102
|
+
async with MaybeAcquire(conn) as connection:
|
|
103
|
+
stmt = await connection.prepare(sql)
|
|
104
|
+
async with connection.transaction():
|
|
105
|
+
yield stmt.cursor(*parameters)
|
|
106
|
+
|
|
107
|
+
async def insert_returning(self, conn, query_name, sql, parameters):
|
|
108
|
+
parameters = self.maybe_order_params(query_name, parameters)
|
|
109
|
+
async with MaybeAcquire(conn) as connection:
|
|
110
|
+
res = await connection.fetchrow(sql, *parameters)
|
|
111
|
+
if res:
|
|
112
|
+
return res[0] if len(res) == 1 else res
|
|
113
|
+
else:
|
|
114
|
+
return None
|
|
115
|
+
|
|
116
|
+
async def insert_update_delete(self, conn, query_name, sql, parameters):
|
|
117
|
+
parameters = self.maybe_order_params(query_name, parameters)
|
|
118
|
+
async with MaybeAcquire(conn) as connection:
|
|
119
|
+
# TODO extract integer last result
|
|
120
|
+
return await connection.execute(sql, *parameters)
|
|
121
|
+
|
|
122
|
+
async def insert_update_delete_many(self, conn, query_name, sql, parameters):
|
|
123
|
+
parameters = [self.maybe_order_params(query_name, params) for params in parameters]
|
|
124
|
+
async with MaybeAcquire(conn) as connection:
|
|
125
|
+
return await connection.executemany(sql, parameters)
|
|
126
|
+
|
|
127
|
+
async def execute_script(self, conn, sql):
|
|
128
|
+
async with MaybeAcquire(conn) as connection:
|
|
129
|
+
return await connection.execute(sql)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from .generic import GenericAdapter
|
|
2
|
+
from ..utils import VAR_REF
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def _colon_to_dollar(ma) -> str:
|
|
6
|
+
"""Convert 'WHERE :id = 1' to 'WHERE $id = 1'."""
|
|
7
|
+
gd = ma.groupdict()
|
|
8
|
+
if gd["dquote"] is not None:
|
|
9
|
+
return gd["dquote"]
|
|
10
|
+
elif gd["squote"] is not None:
|
|
11
|
+
return gd["squote"]
|
|
12
|
+
else:
|
|
13
|
+
return f'{gd["lead"]}${gd["var_name"]}'
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DuckDBAdapter(GenericAdapter):
|
|
17
|
+
"""DuckDB Adapter"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, *args, cursor_as_dict: bool = False, use_cursor: bool = True, **kwargs):
|
|
20
|
+
super().__init__(*args, **kwargs)
|
|
21
|
+
# whether to converts the default tuple response to a dict.
|
|
22
|
+
self._convert_row_to_dict = cursor_as_dict
|
|
23
|
+
self._use_cursor = use_cursor
|
|
24
|
+
|
|
25
|
+
def _cursor(self, conn):
|
|
26
|
+
"""Get a cursor from a connection."""
|
|
27
|
+
# For DuckDB cursor is duplicated connection so we don't want to use it
|
|
28
|
+
if self._use_cursor:
|
|
29
|
+
return conn.cursor(*self._args, **self._kwargs)
|
|
30
|
+
return conn
|
|
31
|
+
|
|
32
|
+
def process_sql(self, query_name, op_type, sql):
|
|
33
|
+
return VAR_REF.sub(_colon_to_dollar, sql)
|
|
34
|
+
|
|
35
|
+
def insert_returning(self, conn, query_name, sql, parameters): # pragma: no cover
|
|
36
|
+
# very similar to select_one but the returned value
|
|
37
|
+
cur = self._cursor(conn)
|
|
38
|
+
try:
|
|
39
|
+
cur.execute(sql, parameters)
|
|
40
|
+
# we have to use fetchall instead of fetchone for now due to this:
|
|
41
|
+
# https://github.com/duckdb/duckdb/issues/6008
|
|
42
|
+
res = cur.fetchall()
|
|
43
|
+
finally:
|
|
44
|
+
if self._use_cursor:
|
|
45
|
+
cur.close()
|
|
46
|
+
if isinstance(res, list):
|
|
47
|
+
res = res[0]
|
|
48
|
+
return res[0] if res and len(res) == 1 else res
|
|
49
|
+
|
|
50
|
+
def select(self, conn, query_name: str, sql: str, parameters, record_class=None):
|
|
51
|
+
column_names: list[str] = []
|
|
52
|
+
cur = self._cursor(conn)
|
|
53
|
+
try:
|
|
54
|
+
cur.execute(sql, parameters)
|
|
55
|
+
if record_class is None:
|
|
56
|
+
first = True
|
|
57
|
+
for row in cur.fetchall():
|
|
58
|
+
if first: # get column names on the fly
|
|
59
|
+
column_names = [c[0] for c in cur.description or []]
|
|
60
|
+
first = False
|
|
61
|
+
if self._convert_row_to_dict: # pragma: no cover
|
|
62
|
+
# strict=False: requires 3.10
|
|
63
|
+
yield dict(zip(column_names, row))
|
|
64
|
+
else:
|
|
65
|
+
yield row
|
|
66
|
+
else: # pragma: no cover
|
|
67
|
+
first = True
|
|
68
|
+
for row in cur.fetchall():
|
|
69
|
+
if first: # only get description on the fly, for apsw
|
|
70
|
+
column_names = [c[0] for c in cur.description or []]
|
|
71
|
+
first = False
|
|
72
|
+
# strict=False: requires 3.10
|
|
73
|
+
yield record_class(**dict(zip(column_names, row)))
|
|
74
|
+
finally:
|
|
75
|
+
if self._use_cursor:
|
|
76
|
+
cur.close()
|
|
77
|
+
|
|
78
|
+
def select_one(self, conn, query_name, sql, parameters, record_class=None):
|
|
79
|
+
cur = self._cursor(conn)
|
|
80
|
+
try:
|
|
81
|
+
cur.execute(sql, parameters)
|
|
82
|
+
result = cur.fetchone()
|
|
83
|
+
if result is not None and record_class is not None: # pragma: no cover
|
|
84
|
+
column_names = [c[0] for c in cur.description or []]
|
|
85
|
+
# strict=False: requires 3.10
|
|
86
|
+
result = record_class(**dict(zip(column_names, result)))
|
|
87
|
+
elif result is not None and self._convert_row_to_dict: # pragma: no cover
|
|
88
|
+
column_names = [c[0] for c in cur.description or []]
|
|
89
|
+
result = dict(zip(column_names, result))
|
|
90
|
+
finally:
|
|
91
|
+
if self._use_cursor:
|
|
92
|
+
cur.close()
|
|
93
|
+
return result
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
from contextlib import contextmanager
|
|
2
|
+
from ..types import SyncDriverAdapterProtocol
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class GenericAdapter(SyncDriverAdapterProtocol):
|
|
6
|
+
"""
|
|
7
|
+
Generic AioSQL Adapter suitable for `named` parameter style and no with support.
|
|
8
|
+
|
|
9
|
+
This class also serves as the base class for other adapters.
|
|
10
|
+
|
|
11
|
+
Miscellaneous parameters are passed to cursor creation.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def __init__(self, *args, **kwargs):
|
|
15
|
+
self._args = args
|
|
16
|
+
self._kwargs = kwargs
|
|
17
|
+
|
|
18
|
+
def process_sql(self, query_name, op_type, sql):
|
|
19
|
+
"""Pass-through SQL query preprocessing."""
|
|
20
|
+
return sql
|
|
21
|
+
|
|
22
|
+
def _cursor(self, conn):
|
|
23
|
+
"""Get a cursor from a connection."""
|
|
24
|
+
return conn.cursor(*self._args, **self._kwargs)
|
|
25
|
+
|
|
26
|
+
def select(self, conn, query_name: str, sql: str, parameters, record_class=None):
|
|
27
|
+
"""Handle a relation-returning SELECT (no suffix)."""
|
|
28
|
+
cur = self._cursor(conn)
|
|
29
|
+
try:
|
|
30
|
+
cur.execute(sql, parameters)
|
|
31
|
+
if record_class is None:
|
|
32
|
+
for row in cur:
|
|
33
|
+
yield row
|
|
34
|
+
else:
|
|
35
|
+
column_names: list[str] = []
|
|
36
|
+
first = True
|
|
37
|
+
for row in cur:
|
|
38
|
+
if first: # only get description on the fly, for apsw
|
|
39
|
+
column_names = [c[0] for c in cur.description]
|
|
40
|
+
first = False
|
|
41
|
+
yield record_class(**dict(zip(column_names, row)))
|
|
42
|
+
finally:
|
|
43
|
+
cur.close()
|
|
44
|
+
|
|
45
|
+
def select_one(self, conn, query_name, sql, parameters, record_class=None):
|
|
46
|
+
"""Handle a tuple-returning (one row) SELECT (``^`` suffix).
|
|
47
|
+
|
|
48
|
+
Return None if empty."""
|
|
49
|
+
cur = self._cursor(conn)
|
|
50
|
+
try:
|
|
51
|
+
cur.execute(sql, parameters)
|
|
52
|
+
result = cur.fetchone()
|
|
53
|
+
if result is not None and record_class is not None:
|
|
54
|
+
column_names = [c[0] for c in cur.description]
|
|
55
|
+
# this fails if result is not a list or tuple
|
|
56
|
+
result = record_class(**dict(zip(column_names, result)))
|
|
57
|
+
finally:
|
|
58
|
+
cur.close()
|
|
59
|
+
return result
|
|
60
|
+
|
|
61
|
+
def select_value(self, conn, query_name, sql, parameters):
|
|
62
|
+
"""Handle a scalar-returning (one value) SELECT (``$`` suffix).
|
|
63
|
+
|
|
64
|
+
Return None if empty."""
|
|
65
|
+
cur = self._cursor(conn)
|
|
66
|
+
try:
|
|
67
|
+
cur.execute(sql, parameters)
|
|
68
|
+
result = cur.fetchone()
|
|
69
|
+
if result:
|
|
70
|
+
if isinstance(result, (list, tuple)):
|
|
71
|
+
return result[0]
|
|
72
|
+
elif isinstance(result, dict): # pragma: no cover
|
|
73
|
+
return next(iter(result.values()))
|
|
74
|
+
else: # pragma: no cover
|
|
75
|
+
raise TypeError(f"unexpected value type: {type(result)}")
|
|
76
|
+
else:
|
|
77
|
+
return None
|
|
78
|
+
finally:
|
|
79
|
+
cur.close()
|
|
80
|
+
|
|
81
|
+
@contextmanager
|
|
82
|
+
def select_cursor(self, conn, query_name, sql, parameters):
|
|
83
|
+
"""Return the raw cursor after a SELECT exec."""
|
|
84
|
+
cur = self._cursor(conn)
|
|
85
|
+
try:
|
|
86
|
+
cur.execute(sql, parameters)
|
|
87
|
+
yield cur
|
|
88
|
+
finally:
|
|
89
|
+
cur.close()
|
|
90
|
+
|
|
91
|
+
def insert_update_delete(self, conn, query_name, sql, parameters):
|
|
92
|
+
"""Handle affected row counts (INSERT UPDATE DELETE) (``!`` suffix)."""
|
|
93
|
+
cur = self._cursor(conn)
|
|
94
|
+
cur.execute(sql, parameters)
|
|
95
|
+
rc = cur.rowcount if hasattr(cur, "rowcount") else -1
|
|
96
|
+
cur.close()
|
|
97
|
+
return rc
|
|
98
|
+
|
|
99
|
+
def insert_update_delete_many(self, conn, query_name, sql, parameters):
|
|
100
|
+
"""Handle affected row counts (INSERT UPDATE DELETE) (``*!`` suffix)."""
|
|
101
|
+
cur = self._cursor(conn)
|
|
102
|
+
cur.executemany(sql, parameters)
|
|
103
|
+
rc = cur.rowcount if hasattr(cur, "rowcount") else -1
|
|
104
|
+
cur.close()
|
|
105
|
+
return rc
|
|
106
|
+
|
|
107
|
+
# FIXME this made sense when SQLite had no RETURNING prefix (v3.35, 2021-03-12)
|
|
108
|
+
def insert_returning(self, conn, query_name, sql, parameters):
|
|
109
|
+
"""Special case for RETURNING (``<!`` suffix) with SQLite."""
|
|
110
|
+
# very similar to select_one but the returned value
|
|
111
|
+
cur = self._cursor(conn)
|
|
112
|
+
cur.execute(sql, parameters)
|
|
113
|
+
res = cur.fetchone()
|
|
114
|
+
cur.close()
|
|
115
|
+
return res[0] if res and len(res) == 1 else res
|
|
116
|
+
|
|
117
|
+
def execute_script(self, conn, sql):
|
|
118
|
+
"""Handle an SQL script (``#`` suffix)."""
|
|
119
|
+
cur = self._cursor(conn)
|
|
120
|
+
cur.execute(sql)
|
|
121
|
+
msg = cur.statusmessage if hasattr(cur, "statusmessage") else "DONE"
|
|
122
|
+
cur.close()
|
|
123
|
+
return msg
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from .pyformat import PyFormatAdapter
|
|
2
|
+
|
|
3
|
+
ParamType = dict|list|None
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class BrokenMySQLAdapter(PyFormatAdapter):
|
|
7
|
+
"""
|
|
8
|
+
Work around PyMySQL and MySQLDB mishandling of empty parameters
|
|
9
|
+
and lack of willingness to fix the issue.
|
|
10
|
+
|
|
11
|
+
See: https://github.com/PyMySQL/PyMySQL/issues/1059
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def select(self, conn, query_name, sql, parameters: ParamType, record_class=None):
|
|
15
|
+
return super().select(conn, query_name, sql, parameters or None, record_class)
|
|
16
|
+
|
|
17
|
+
def select_one(self, conn, query_name, sql, parameters: ParamType, record_class=None):
|
|
18
|
+
return super().select_one(conn, query_name, sql, parameters or None, record_class)
|
|
19
|
+
|
|
20
|
+
def select_value(self, conn, query_name, sql, parameters: ParamType):
|
|
21
|
+
return super().select_value(conn, query_name, sql, parameters or None)
|
|
22
|
+
|
|
23
|
+
def insert_update_delete(self, conn, query_name, sql, parameters: ParamType):
|
|
24
|
+
return super().insert_update_delete(conn, query_name, sql, parameters or None)
|
|
25
|
+
|
|
26
|
+
# only called for mariadb, as mysql does not implement RETURNING
|
|
27
|
+
def insert_returning(self, conn, query_name, sql, parameters: ParamType): # pragma: no cover
|
|
28
|
+
return super().insert_returning(conn, query_name, sql, parameters or None)
|
|
29
|
+
|
|
30
|
+
# left out with parameters: insert_update_delete_many, select_cursor
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from .generic import GenericAdapter
|
|
2
|
+
from ..utils import VAR_REF
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def _replacer(ma):
|
|
6
|
+
"""Regex hook for named to pyformat conversion."""
|
|
7
|
+
gd = ma.groupdict()
|
|
8
|
+
if gd["dquote"] is not None: # "..."
|
|
9
|
+
return gd["dquote"]
|
|
10
|
+
elif gd["squote"] is not None: # '...'
|
|
11
|
+
return gd["squote"]
|
|
12
|
+
else: # :something to %(something)s
|
|
13
|
+
return f'{gd["lead"]}%({gd["var_name"]})s'
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class PyFormatAdapter(GenericAdapter):
|
|
17
|
+
"""Convert from named to pyformat parameter style."""
|
|
18
|
+
|
|
19
|
+
def process_sql(self, query_name, op_type, sql):
|
|
20
|
+
"""From named to pyformat."""
|
|
21
|
+
return VAR_REF.sub(_replacer, sql)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from .generic import GenericAdapter
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SQLite3Adapter(GenericAdapter):
|
|
5
|
+
"""SQLite3 Adapter for AioSQL.
|
|
6
|
+
|
|
7
|
+
Overwrites two methods using sqlite3-specific non-standard methods.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def insert_returning(self, conn, query_name, sql, parameters):
|
|
11
|
+
cur = self._cursor(conn)
|
|
12
|
+
try:
|
|
13
|
+
cur.execute(sql, parameters)
|
|
14
|
+
results = cur.lastrowid
|
|
15
|
+
finally:
|
|
16
|
+
cur.close()
|
|
17
|
+
return results
|
|
18
|
+
|
|
19
|
+
def execute_script(self, conn, sql):
|
|
20
|
+
conn.executescript(sql)
|
|
21
|
+
return "DONE"
|