Skip to content

Commit 7f4d490

Browse files
authored
Merge pull request #44 from DanCardin/dc/row-table
feat: Add Table abstraction to reduce Row related boilerplate.
2 parents 0ef638e + 781779e commit 7f4d490

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

docs/source/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
```{eval-rst}
3939
.. autoapimodule:: sqlalchemy_declarative_extensions.row
40-
:members: Row, Rows
40+
:members: Row, Rows, Table
4141
```
4242

4343
## Alembic

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "sqlalchemy-declarative-extensions"
3-
version = "0.6.7"
3+
version = "0.6.8"
44
authors = ["Dan Cardin <ddcardin@gmail.com>"]
55

66
description = "Library to declare additional kinds of objects not natively supported by SQLAlchemy/Alembic."

src/sqlalchemy_declarative_extensions/row/base.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass, field, replace
4-
from typing import Iterable
4+
from typing import Any, Iterable
55

66

77
@dataclass
@@ -27,7 +27,12 @@ def are(self, *rows: Row):
2727
return replace(self, rows=rows)
2828

2929

30+
@dataclass
3031
class Row:
32+
schema: str | None
33+
tablename: str
34+
column_values: dict[str, Any]
35+
3136
def __init__(self, tablename, **column_values):
3237
try:
3338
schema, table = tablename.split(".", 1)
@@ -38,5 +43,44 @@ def __init__(self, tablename, **column_values):
3843
self.schema = schema
3944
self.tablename = table
4045

41-
self.qualified_name = tablename
4246
self.column_values = column_values
47+
48+
@property
49+
def qualified_name(self):
50+
if self.schema:
51+
return f"{self.schema}.{self.tablename}"
52+
return self.tablename
53+
54+
55+
@dataclass
56+
class Table:
57+
"""Convenience class for producing multiple rows against the same table.
58+
59+
Examples:
60+
Rows might be created directly, like so:
61+
62+
>>> [
63+
... Row("users", id=1, name="John", active=True),
64+
... Row("users", id=2, name="Bob", active=True),
65+
... ]
66+
[Row(schema=None, tablename='users', column_values={'id': 1, 'name': 'John', 'active': True}), Row(schema=None, tablename='users', column_values={'id': 2, 'name': 'Bob', 'active': True})]
67+
68+
But use of `Table` can help elide repetition among those rows:
69+
70+
>>> users = Table("users", active=True)
71+
>>> [
72+
... users.row(id=1, name="John"),
73+
... users.row(id=2, name="Bob"),
74+
... ]
75+
[Row(schema=None, tablename='users', column_values={'active': True, 'id': 1, 'name': 'John'}), Row(schema=None, tablename='users', column_values={'active': True, 'id': 2, 'name': 'Bob'})]
76+
"""
77+
name: str
78+
column_values: dict[str, Any]
79+
80+
def __init__(self, name, **column_values):
81+
self.name = name
82+
self.column_values = column_values
83+
84+
def row(self, **column_values) -> Row:
85+
final_values= {**self.column_values, **column_values}
86+
return Row(self.name, **final_values)

0 commit comments

Comments
 (0)