Skip to content

allow SelectRaw with Readable #742

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion piccolo/apps/playground/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from piccolo.columns.readable import Readable
from piccolo.engine import PostgresEngine, SQLiteEngine
from piccolo.engine.base import Engine
from piccolo.query import SelectRaw
from piccolo.table import Table
from piccolo.utils.warnings import colored_string

Expand Down Expand Up @@ -111,7 +112,12 @@ class DiscountCode(Table):
def get_readable(cls) -> Readable:
return Readable(
template="%s - %s",
columns=[cls.code, cls.active],
columns=[
cls.code,
SelectRaw(
"CASE WHEN active THEN 'active' ELSE 'inactive' END"
),
],
)


Expand Down
22 changes: 10 additions & 12 deletions piccolo/columns/readable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

from piccolo.columns.base import Selectable

if t.TYPE_CHECKING: # pragma: no cover
from piccolo.columns.base import Column


@dataclass
class Readable(Selectable):
Expand All @@ -18,32 +15,33 @@ class Readable(Selectable):
"""

template: str
columns: t.Sequence[Column]
columns: t.Sequence[Selectable]
output_name: str = "readable"

@property
def _columns_string(self) -> str:
def _get_columns_string(self, engine_type: str) -> str:
return ", ".join(
i._meta.get_full_name(with_alias=False) for i in self.columns
i.get_select_string(engine_type=engine_type, with_alias=False)
for i in self.columns
)

def _get_string(self, operator: str) -> str:
def _get_string(self, operator: str, engine_type: str) -> str:
columns_string = self._get_columns_string(engine_type=engine_type)
return (
f"{operator}('{self.template}', {self._columns_string}) AS "
f"{operator}('{self.template}', {columns_string}) AS "
f"{self.output_name}"
)

@property
def sqlite_string(self) -> str:
return self._get_string(operator="PRINTF")
return self._get_string(operator="PRINTF", engine_type="sqlite")

@property
def postgres_string(self) -> str:
return self._get_string(operator="FORMAT")
return self._get_string(operator="FORMAT", engine_type="postgres")

@property
def cockroach_string(self) -> str:
return self._get_string(operator="FORMAT")
return self._get_string(operator="FORMAT", engine_type="cockroach")

def get_select_string(self, engine_type: str, with_alias=True) -> str:
try:
Expand Down
9 changes: 6 additions & 3 deletions piccolo/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,12 @@ def _get_related_readable(cls, column: ForeignKey) -> Readable:

for readable_column in readable.columns:
output_column = column
for fk in readable_column._meta.call_chain:
output_column = getattr(output_column, fk._meta.name)
output_column = getattr(output_column, readable_column._meta.name)
if isinstance(readable_column, Column):
for fk in readable_column._meta.call_chain:
output_column = getattr(output_column, fk._meta.name)
output_column = getattr(
output_column, readable_column._meta.name
)
output_columns.append(output_column)

output_name = f"{column._meta.name}_readable"
Expand Down