Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.4.0-alpha-11
current_version = 0.4.0-alpha-12
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(-(?P<release>.*)-(?P<build>\d+))?
serialize =
{major}.{minor}.{patch}-{release}-{build}
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies = [
"typing-extensions",
]
name = "sql-athame"
version = "0.4.0-alpha-11"
version = "0.4.0-alpha-12"
description = "Python tool for slicing and dicing SQL"
readme = "README.md"

Expand Down
25 changes: 24 additions & 1 deletion sql_athame/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,21 @@ def field_names(cls, *, exclude: FieldNamesSet = ()) -> list[str]:

@classmethod
def field_names_sql(
cls, *, prefix: Optional[str] = None, exclude: FieldNamesSet = ()
cls,
*,
prefix: Optional[str] = None,
exclude: FieldNamesSet = (),
as_prepended: Optional[str] = None,
) -> list[Fragment]:
if as_prepended:
return [
sql(
"{} AS {}",
sql.identifier(f, prefix=prefix),
sql.identifier(f"{as_prepended}{f}"),
)
for f in cls.field_names(exclude=exclude)
]
return [
sql.identifier(f, prefix=prefix) for f in cls.field_names(exclude=exclude)
]
Expand Down Expand Up @@ -300,6 +313,16 @@ def from_mapping(cls: type[T], mapping: Mapping[str, Any], /) -> T:
cls.from_mapping = from_mapping_fn # type: ignore
return from_mapping_fn(mapping)

@classmethod
def from_prepended_mapping(
cls: type[T], mapping: Mapping[str, Any], prepend: str
) -> T:
filtered_dict: dict[str, Any] = {}
for k, v in mapping.items():
if k.startswith(prepend):
filtered_dict[k[len(prepend) :]] = v
return cls.from_mapping(filtered_dict)

@classmethod
def ensure_model(cls: type[T], row: Union[T, Mapping[str, Any]]) -> T:
if isinstance(row, cls):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ class Test(ModelBase, table_name="table"):
sql(",").join(t.field_values_sql()),
).query() == ('INSERT INTO table ("foo","bar") VALUES ($1,$2)', [42, "hi"])

assert list(
sql(
"SELECT {fields} FROM {tbl}",
fields=sql.list(Test.field_names_sql(as_prepended="p_")),
tbl=Test.table_name_sql(),
)
) == ['SELECT "foo" AS "p_foo", "bar" AS "p_bar" FROM "table"']


def test_modelclass_implicit_types():
@dataclass
Expand Down Expand Up @@ -202,3 +210,7 @@ class Test(ModelBase, table_name="table"):
assert Test.from_mapping({"foo": "FOO", "bar": "BAR"}) == Test("foo", "BAR")
# make sure the monkey patching didn't screw things up
assert Test.from_mapping({"foo": "FOO", "bar": "BAR"}) == Test("foo", "BAR")

assert Test.from_prepended_mapping(
{"p_foo": "FOO", "p_bar": "BAR", "foo": "not foo", "other": "other"}, "p_"
) == Test("foo", "BAR")
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.