Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #213

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Dev #213

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/aiosql-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
matrix:
# https://github.com/actions/python-versions (versions-manifest.json)
# https://downloads.python.org/pypy/versions.json
# 3.14 KO on pydantic build dependences 2024-10-27
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "pypy3.10"]
steps:
- uses: actions/checkout@v4
Expand Down
20 changes: 14 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ WAIT = ./tests/wait.py
help:
@echo "useful targets:"
echo " - help: show this help"
echo " - venv: generate python virtual environment directory"
echo " - venv.dev: make venv suitable for development"
echo " - dev: make environment suitable for development"
echo " - venv.prod: make venv suitable for production"
echo " - venv.last: fill venv with latests packages"
echo " - clean: clean up various generated files and directories"
echo " - clean.venv: also remove the venv directory"
echo " - clean.dev: also remove the development environment"
echo " - check.pytest: run pytest tests"
echo " - check.mypy: run mypy type checker"
echo " - check.flake8: run flake8 code style checks"
Expand All @@ -47,27 +46,33 @@ help:
# so the result is kind of a mess, so we attempt at doing nearly nothing and
# hope for the best, i.e. dependencies will not break the library.
#
.PHONY: venv.dev venv.prod venv.last

venv:
$(PYTHON) -m venv venv
source venv/bin/activate
$(PIP) install --upgrade pip

venv.dev: venv
venv/.dev: venv
source venv/bin/activate
$(PIP) install .[dev,dev-postgres,dev-sqlite,dev-duckdb]
touch $@

venv.dist: venv
venv/.dist: venv
source venv/bin/activate
$(PIP) install .[dist]
touch $@

.PHONY: venv.prod
venv.prod: venv

.PHONY: venv.last
venv.last: venv
source venv/bin/activate
$(PIP) install $$($(PIP) freeze | cut -d= -f1 | grep -v -- '^-e') -U

.PHONY: dev
dev: venv/.dev

# direct module installation for github or docker
ifdef VENV
PIP = $(VENV)/bin/pip
Expand All @@ -94,6 +99,9 @@ clean:
clean.venv: clean
$(RM) -r venv $(MODULE).egg-info

.PHONY: clean.dev
clean.dev: clean.venv

.PHONY: clean.docker
clean.docker: clean docker.rm
$(MAKE) -C docker clean
Expand Down
10 changes: 9 additions & 1 deletion aiosql/queries.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import inspect
from pathlib import Path
from types import MethodType
Expand All @@ -6,7 +7,7 @@
from typing import Any, Callable, List, Optional, Set, Tuple, Union, Dict, cast

from .types import DriverAdapterProtocol, QueryDatum, QueryDataTree, QueryFn, SQLOperationType
from .utils import SQLLoadException
from .utils import SQLLoadException, log


class Queries:
Expand Down Expand Up @@ -72,6 +73,10 @@ def _params(
else:
return args

def _look_like_a_select(self, sql: str) -> bool:
# skipped: VALUES, SHOW
return re.search(r"(?i)\b(SELECT|RETURNING|TABLE|EXECUTE)\b", sql) is not None

def _query_fn(
self,
fn: Callable[..., Any],
Expand All @@ -93,6 +98,9 @@ def _query_fn(
qfn.sql = sql
qfn.operation = operation
qfn.attributes = attributes
# sanity check in passing…
if operation == SQLOperationType.SELECT and not self._look_like_a_select(sql):
log.warning(f"query {fname} may not be a select, consider adding an operator, eg '!'")
return qfn

# NOTE about coverage: because __code__ is set to reflect the actual SQL file
Expand Down
4 changes: 2 additions & 2 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ using ``aiosqlite`` and ``asyncio``:
It may seem inconvenient to provide a connection on each call.
You may have a look at the `AnoDB <https://github.com/zx80/anodb>`__ `DB`
class which wraps both a database connection *and* queries in one
connection-like extended object, including managing a pool and performing
automatic reconnection if needed.
connection-like extended object, including performing automatic reconnection
when needed.

Why you might want to use this
------------------------------
Expand Down
9 changes: 9 additions & 0 deletions docs/source/versions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ TODO
- tests with other database and drivers?
- rethink record classes? we just really want a row conversion function?
- add documentation about docker runs.
- allow tagging queries, eg whether it can be cached

? on ?
------

- improve Makefile.
- warn on probable mission operation.
- add *psycopg2* to CI.
- improve documentation.

12.2 on 2024-10-02
------------------
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ dev-postgres = [
"pytest-postgresql",
"asyncpg; python_version < '3.13' and implementation_name != 'pypy'",
"psycopg>=3",
"psycopg2; python_version < '3.13' and implementation_name != 'pypy'",
# 2.9.10 needed for 3.13
"psycopg2 >= 2.9.10; implementation_name != 'pypy'",
"pygresql",
"pg8000"
]
Expand Down
3 changes: 3 additions & 0 deletions tests/blogdb/sql/misc/misc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ SELECT :numerator % :denominator AS modulo;

-- name: square$
select :val::int * :val::int as squared;

-- name: not_a_select
INSERT INTO Foo VALUES (1);