From 4e72ac89a5bc5b642fc39680053f4a331f155461 Mon Sep 17 00:00:00 2001 From: baluyotraf Date: Thu, 21 Mar 2024 21:02:25 +0100 Subject: [PATCH 1/6] Add an alias for non init field --- src/altqq/__init__.py | 1 + src/altqq/structs.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/altqq/__init__.py b/src/altqq/__init__.py index 08f954c..8c6bc8e 100644 --- a/src/altqq/__init__.py +++ b/src/altqq/__init__.py @@ -1,5 +1,6 @@ """Main entry point for the altqq library.""" +from altqq.structs import Calculated as Calculated from altqq.structs import Query from altqq.translators.plain_text import PlainTextTranslator from altqq.translators.pyodbc import PyODBCQuery, PyODBCTranslator diff --git a/src/altqq/structs.py b/src/altqq/structs.py index 558c4d3..a962ab3 100644 --- a/src/altqq/structs.py +++ b/src/altqq/structs.py @@ -1,8 +1,9 @@ """Structures used for defining queries.""" +import dataclasses as dc from typing import Any, ClassVar, Dict, Tuple -import pydantic.dataclasses as dc +import pydantic.dataclasses as pdc from typing_extensions import dataclass_transform QUERY_ATTRIB = "__query__" @@ -42,7 +43,7 @@ def __new__(cls, name: str, bases: Tuple[type, ...], dct: Dict[str, Any]): dataclass = super().__new__(cls, name, bases, dct) if not cls._check_query_attribute(dataclass, dct): raise ValueError(f"A string {QUERY_ATTRIB} must be provided") - return dc.dataclass(dataclass) + return pdc.dataclass(dataclass) class Query(metaclass=QueryMeta): @@ -53,3 +54,6 @@ class metaclass. """ __query__: ClassVar[str] + + +Calculated = dc.field(init=False) From 6bac50ac14c10801e44b33c2344a160fa5dfe8c5 Mon Sep 17 00:00:00 2001 From: baluyotraf Date: Thu, 21 Mar 2024 21:03:27 +0100 Subject: [PATCH 2/6] Add CI badges to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f2bb2a0..e2a151d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) [![Checked with pyright](https://microsoft.github.io/pyright/img/pyright_badge.svg)](https://microsoft.github.io/pyright/) +[![qa](https://github.com/baluyotraf/altqq/actions/workflows/qa.yml/badge.svg)](https://github.com/baluyotraf/altqq/actions/workflows/qa.yml) +[![release](https://github.com/baluyotraf/altqq/actions/workflows/release.yml/badge.svg)](https://github.com/baluyotraf/altqq/actions/workflows/release.yml) # Alternative Queries From e21fd56d1893cd61303cbe342019deb0ec4aa6ab Mon Sep 17 00:00:00 2001 From: baluyotraf Date: Thu, 21 Mar 2024 21:37:32 +0100 Subject: [PATCH 3/6] Update project description and remove roadmap --- README.md | 9 --------- pyproject.toml | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/README.md b/README.md index e2a151d..760988b 100644 --- a/README.md +++ b/README.md @@ -130,12 +130,3 @@ Running the code above should give the result below: ['arietta', 20] ``` - -## Road Map - -Below is the list of things planned for the library - -- Documentation Page -- Tests -- Expansion of Supported Version -- Support for other Python Database Tooling diff --git a/pyproject.toml b/pyproject.toml index 70806bc..6d4e728 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ convention = "google" [tool.poetry] name = "altqq" version = "0.0.1a2" -description = "Alternative Queries: Typed and Composable Tool for Handwritten SQL" +description = "Alternative Queries: Typed and Reusable Handcrafted SQL" authors = ["baluyotraf "] maintainers = ["baluyotraf "] license = "MIT" From fe0e9d32d9bc40f1a9bfa2a630c91e1f58d48cd2 Mon Sep 17 00:00:00 2001 From: baluyotraf Date: Thu, 21 Mar 2024 21:57:47 +0100 Subject: [PATCH 4/6] Add more test case --- tests/queries.py | 68 +++++++++++++++++++++++++++++++++------ tests/test_translation.py | 20 ++++++------ 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/tests/queries.py b/tests/queries.py index 37c86ae..4bee6b0 100644 --- a/tests/queries.py +++ b/tests/queries.py @@ -7,7 +7,7 @@ @dc.dataclass -class TestQuery: +class SampleQuery: """Defines the conversion of query to different translations.""" query: altqq.Query @@ -32,7 +32,7 @@ class OrderQuery(altqq.Query): """Test query that orders a query.""" __query__ = """ - SELECT * FROM ({subquery}) AS tbl + SELECT * FROM ( {subquery} ) AS tbl ORDER BY "{order_column}" {order} """ @@ -45,9 +45,9 @@ class UnionAllQuery(altqq.Query): """Test query that union all two queries.""" __query__ = """ - SELECT * FROM ({query1}) AS tbl1 + SELECT * FROM ( {query1} ) AS tbl1 UNION ALL - SELECT * FROM ({query2}) AS tbl2 + SELECT * FROM ( {query2} ) AS tbl2 """ query1: altqq.Query @@ -55,11 +55,61 @@ class UnionAllQuery(altqq.Query): TEST_DATA = [ - TestQuery( - SelectTableByFilter("Users", "name", "arietta"), + SampleQuery( + SelectTableByFilter("Users", "age", 25), pyodbc=altqq.PyODBCQuery( - query="""SELECT * FROM "Users" WHERE "name" = ?""", parameters=["arietta"] + query=""" + SELECT * FROM "Users" WHERE "age" = ? + """, + parameters=[25], ), - plain_text="""SELECT * FROM "Users" WHERE "name" = 'arietta'""", - ) + plain_text=""" + SELECT * FROM "Users" WHERE "age" = 25 + """, + ), + SampleQuery( + OrderQuery(SelectTableByFilter("Users", "last_name", "Fine"), "age", "asc"), + pyodbc=altqq.PyODBCQuery( + query=""" + SELECT * FROM ( + SELECT * FROM "Users" WHERE "last_name" = ? + ) AS tbl + ORDER BY "age" asc + """, + parameters=["Fine"], + ), + plain_text=""" + SELECT * FROM ( + SELECT * FROM "Users" WHERE "last_name" = 'Fine' + ) AS tbl + ORDER BY "age" asc + """, + ), + SampleQuery( + UnionAllQuery( + SelectTableByFilter("Music", "title", "Secret"), + SelectTableByFilter("Cities", "name", "Piova"), + ), + pyodbc=altqq.PyODBCQuery( + query=""" + SELECT * FROM ( + SELECT * FROM "Music" WHERE "title" = ? + ) AS tbl1 + UNION ALL + SELECT * FROM ( + SELECT * FROM "Cities" WHERE "name" = ? + ) AS tbl2 + """, + parameters=["Secret", "Piova"], + ), + plain_text=""" + SELECT * FROM ( + SELECT * FROM "Music" WHERE "title" = 'Secret' + ) AS tbl1 + UNION ALL + SELECT * FROM ( + SELECT * FROM "Cities" WHERE "name" = 'Piova' + ) AS tbl2 + """, + ), ] diff --git a/tests/test_translation.py b/tests/test_translation.py index cce5573..d4285ba 100644 --- a/tests/test_translation.py +++ b/tests/test_translation.py @@ -3,20 +3,20 @@ import altqq import pytest -from tests.queries import TEST_DATA, TestQuery +from tests.queries import TEST_DATA, SampleQuery from tests.utils import clean_whitespaces as cws -@pytest.mark.parametrize("test_query", TEST_DATA) -def test_to_pyodbc__proper_query__correct_pyodbc_object(test_query: TestQuery): +@pytest.mark.parametrize("query", TEST_DATA) +def test_to_pyodbc__proper_query__correct_pyodbc_object(query: SampleQuery): """If the query parameters are correct, the pyodbc object is returned.""" - res = altqq.to_pyodbc(test_query.query) - assert cws(test_query.pyodbc.query) == cws(res.query) - assert test_query.pyodbc.parameters == res.parameters + res = altqq.to_pyodbc(query.query) + assert cws(query.pyodbc.query) == cws(res.query) + assert query.pyodbc.parameters == res.parameters -@pytest.mark.parametrize("test_query", TEST_DATA) -def test_to_plain_text__proper_query__correct_sqlt(test_query: TestQuery): +@pytest.mark.parametrize("query", TEST_DATA) +def test_to_plain_text__proper_query__correct_sqlt(query: SampleQuery): """If the query parameters are correct, the sql is returned.""" - sql = altqq.to_plain_text(test_query.query) - assert cws(test_query.plain_text) == cws(sql) + sql = altqq.to_plain_text(query.query) + assert cws(query.plain_text) == cws(sql) From 1903f0a8dbbdb755a0fdbb0995b687d68b2b13c7 Mon Sep 17 00:00:00 2001 From: baluyotraf Date: Thu, 21 Mar 2024 21:58:01 +0100 Subject: [PATCH 5/6] Update version number to 0.0.1 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 6d4e728..17f58bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ convention = "google" [tool.poetry] name = "altqq" -version = "0.0.1a2" +version = "0.0.1" description = "Alternative Queries: Typed and Reusable Handcrafted SQL" authors = ["baluyotraf "] maintainers = ["baluyotraf "] From d496c3b057600455b6459c032d370d95fb5806f7 Mon Sep 17 00:00:00 2001 From: baluyotraf Date: Thu, 21 Mar 2024 22:00:07 +0100 Subject: [PATCH 6/6] Remove alpha release notice --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 760988b..35e79a8 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,6 @@ Alternative queries is a library created to help with handcrafted SQL queries. It works by providing a class that represent the queries, with parameters type checked by `Pydantic`. -The library is currently still in development and has an alpha release. - ## Installation The library is available in PyPI