Skip to content

Commit

Permalink
feat: Add Table abstraction to reduce Row related boilerplate.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Jan 29, 2024
1 parent 123ec1a commit 1f3fc71
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/source/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

```{eval-rst}
.. autoapimodule:: sqlalchemy_declarative_extensions.row
:members: Row, Rows
:members: Row, Rows, Table
```

## Alembic
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sqlalchemy-declarative-extensions"
version = "0.6.7"
version = "0.6.8"
authors = ["Dan Cardin <ddcardin@gmail.com>"]

description = "Library to declare additional kinds of objects not natively supported by SQLAlchemy/Alembic."
Expand Down
48 changes: 46 additions & 2 deletions src/sqlalchemy_declarative_extensions/row/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass, field, replace
from typing import Iterable
from typing import Any, Iterable


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


@dataclass
class Row:
schema: str | None
tablename: str
column_values: dict[str, Any]

def __init__(self, tablename, **column_values):
try:
schema, table = tablename.split(".", 1)
Expand All @@ -38,5 +43,44 @@ def __init__(self, tablename, **column_values):
self.schema = schema
self.tablename = table

self.qualified_name = tablename
self.column_values = column_values

@property
def qualified_name(self):
if self.schema:
return f"{self.schema}.{self.tablename}"
return self.tablename


@dataclass
class Table:
"""Convenience class for producing multiple rows against the same table.
Examples:
Rows might be created directly, like so:
>>> [
... Row("users", id=1, name="John", active=True),
... Row("users", id=2, name="Bob", active=True),
... ]
[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})]
But use of `Table` can help elide repetition among those rows:
>>> users = Table("users", active=True)
>>> [
... users.row(id=1, name="John"),
... users.row(id=2, name="Bob"),
... ]
[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'})]
"""
name: str
column_values: dict[str, Any]

def __init__(self, name, **column_values):
self.name = name
self.column_values = column_values

def row(self, **column_values) -> Row:
final_values= {**self.column_values, **column_values}
return Row(self.name, **final_values)

0 comments on commit 1f3fc71

Please sign in to comment.