ahi-sql 0.2.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.
- ahi/sql/__init__.py +5 -0
- ahi/sql/identifiers.py +51 -0
- ahi_sql-0.2.0.dist-info/METADATA +30 -0
- ahi_sql-0.2.0.dist-info/RECORD +5 -0
- ahi_sql-0.2.0.dist-info/WHEEL +4 -0
ahi/sql/__init__.py
ADDED
ahi/sql/identifiers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Helpers for building SQL fragments safely.
|
|
2
|
+
|
|
3
|
+
These cover the two things people most often hand-roll (and get wrong):
|
|
4
|
+
quoting an identifier, and generating the right number of bind placeholders.
|
|
5
|
+
Both default to ANSI/PostgreSQL-style quoting, which also matches Redshift.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def quote_identifier(name: str, *, quote: str = '"') -> str:
|
|
12
|
+
"""Safely quote a SQL identifier (table/column name).
|
|
13
|
+
|
|
14
|
+
Wraps ``name`` in ``quote`` characters and escapes any embedded quote by
|
|
15
|
+
doubling it, the ANSI rule. Use this for identifiers only — never for
|
|
16
|
+
values, which belong in bind parameters.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
name: The identifier to quote. Must be non-empty.
|
|
20
|
+
quote: The quote character (``"`` for ANSI/Redshift, `` ` `` for MySQL).
|
|
21
|
+
|
|
22
|
+
Raises:
|
|
23
|
+
ValueError: If ``name`` is empty.
|
|
24
|
+
"""
|
|
25
|
+
if not name:
|
|
26
|
+
raise ValueError("identifier must be non-empty")
|
|
27
|
+
escaped = name.replace(quote, quote * 2)
|
|
28
|
+
return f"{quote}{escaped}{quote}"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def placeholders(count: int, *, style: str = "qmark") -> str:
|
|
32
|
+
"""Return a comma-separated list of bind placeholders for an ``IN`` clause.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
count: Number of placeholders (must be >= 1).
|
|
36
|
+
style: ``"qmark"`` -> ``?, ?, ?`` (Athena),
|
|
37
|
+
``"named"`` -> ``:1, :2, :3`` (Redshift Data API),
|
|
38
|
+
``"format"`` -> ``%s, %s, %s`` (psycopg).
|
|
39
|
+
|
|
40
|
+
Raises:
|
|
41
|
+
ValueError: If ``count`` < 1 or ``style`` is unknown.
|
|
42
|
+
"""
|
|
43
|
+
if count < 1:
|
|
44
|
+
raise ValueError("count must be >= 1")
|
|
45
|
+
if style == "qmark":
|
|
46
|
+
return ", ".join(["?"] * count)
|
|
47
|
+
if style == "format":
|
|
48
|
+
return ", ".join(["%s"] * count)
|
|
49
|
+
if style == "named":
|
|
50
|
+
return ", ".join(f":{i}" for i in range(1, count + 1))
|
|
51
|
+
raise ValueError(f"unknown placeholder style: {style!r}")
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ahi-sql
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Small, dependency-light SQL helpers for AHI (safe identifiers, placeholders).
|
|
5
|
+
Project-URL: Homepage, https://github.com/adaptivehealthintelligence/ahi-python-packages
|
|
6
|
+
Project-URL: Documentation, https://adaptivehealthintelligence.github.io/ahi-python-packages/
|
|
7
|
+
Project-URL: Repository, https://github.com/adaptivehealthintelligence/ahi-python-packages
|
|
8
|
+
Author: Adaptive Health Intelligence
|
|
9
|
+
License: MIT
|
|
10
|
+
Keywords: ahi,sql
|
|
11
|
+
Requires-Python: >=3.11
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# ahi-sql
|
|
15
|
+
|
|
16
|
+
Small, dependency-light SQL helpers — the bits people otherwise hand-roll and
|
|
17
|
+
get subtly wrong.
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from ahi.sql import quote_identifier, placeholders
|
|
21
|
+
|
|
22
|
+
quote_identifier("user table") # '"user table"'
|
|
23
|
+
placeholders(3, style="qmark") # '?, ?, ?' (Athena)
|
|
24
|
+
placeholders(3, style="named") # ':1, :2, :3' (Redshift Data API)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
> Quote **identifiers** with `quote_identifier`. Quote **values** by passing them
|
|
28
|
+
> as bind parameters to your driver — never by string-formatting them in.
|
|
29
|
+
|
|
30
|
+
Part of [ahi-python-packages](https://github.com/adaptivehealthintelligence/ahi-python-packages).
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
ahi/sql/__init__.py,sha256=SP0W9dq1lSwlEoaupdsDxfh8EtiaLktQssDY6ON662k,155
|
|
2
|
+
ahi/sql/identifiers.py,sha256=T7EA7dYeR8aG6EqGi0w8irMPd6Qo60ZvggFvyjaemU0,1810
|
|
3
|
+
ahi_sql-0.2.0.dist-info/METADATA,sha256=QyNaEa-bnf-zmeezpLpHrkDohY_yr5y-b2UkESajCmw,1174
|
|
4
|
+
ahi_sql-0.2.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
5
|
+
ahi_sql-0.2.0.dist-info/RECORD,,
|