Skip to content

Commit

Permalink
2025-01-09T19:19:38Z
Browse files Browse the repository at this point in the history
  • Loading branch information
wrmsr committed Jan 9, 2025
1 parent 2224745 commit a792c1f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions omlish/sql/abc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
https://peps.python.org/pep-0249/
"""
import typing as ta


Expand All @@ -14,6 +17,16 @@
]


class DbapiColumnDescription_(ta.NamedTuple): # noqa
name: str
type_code: DbapiTypeCode
display_size: int | None
internal_size: int | None
precision: int | None
scale: int | None
null_ok: bool | None


class DbapiConnection(ta.Protocol):
def close(self) -> object: ...

Expand Down
38 changes: 38 additions & 0 deletions omlish/sql/queries/tests/test_sqlite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import contextlib
import sqlite3
import typing as ta

from ...abc import DbapiColumnDescription_
from .. import Q
from .. import render


def test_sqlite_query():
with contextlib.closing(sqlite3.connect(':memory:')) as db:
cur = db.cursor()

cur.execute('create table movies(title, year, score)')

cur.execute("""
insert into movies values
('Monty Python and the Holy Grail', 1975, 8.2),
('And Now for Something Completely Different', 1971, 7.5)
""")
db.commit()

def exec_query(qs: str) -> list[dict[str, ta.Any]]:
res = cur.execute(qs)
dsc_lst = [DbapiColumnDescription_(*dt) for dt in res.description]
rows_tups = res.fetchall()
return [
{dsc.name: v for dsc, v in zip(dsc_lst, row)}
for row in rows_tups
]

q = Q.select(
[Q.n.score],
Q.n.movies,
)
rq = render(q)

print(exec_query(rq.s))

0 comments on commit a792c1f

Please sign in to comment.