Skip to content
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

Sub queries for is_in and not_in #786

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
21 changes: 16 additions & 5 deletions piccolo/columns/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

if t.TYPE_CHECKING: # pragma: no cover
from piccolo.columns.column_types import ForeignKey
from piccolo.query.methods.select import Select
from piccolo.table import Table


Expand Down Expand Up @@ -595,11 +596,21 @@ def _validate_choices(

return True

def is_in(self, values: t.List[t.Any]) -> Where:
if len(values) == 0:
raise ValueError(
"The `values` list argument must contain at least one value."
)
def is_in(self, values: t.Union[Select, t.List[t.Any]]) -> Where:
from piccolo.query.methods.select import Select

if isinstance(values, list):
if len(values) == 0:
raise ValueError(
"The `values` list argument must contain at least one "
"value."
)
elif isinstance(values, Select):
if len(values.columns_delegate.selected_columns) != 1:
raise ValueError(
"A sub select must only return a single column."
)

return Where(column=self, values=values, operator=In)

def not_in(self, values: t.List[t.Any]) -> Where:
Expand Down
12 changes: 11 additions & 1 deletion piccolo/columns/combination.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

if t.TYPE_CHECKING:
from piccolo.columns.base import Column
from piccolo.query.methods.select import Select


class CombinableMixin(object):
Expand Down Expand Up @@ -146,18 +147,22 @@ def __init__(
self,
column: Column,
value: t.Any = UNDEFINED,
values: t.Union[Iterable, Undefined] = UNDEFINED,
values: t.Union[Iterable, Undefined, Select] = UNDEFINED,
operator: t.Type[ComparisonOperator] = ComparisonOperator,
) -> None:
"""
We use the UNDEFINED value to show the value was deliberately
omitted, vs None, which is a valid value for a where clause.
"""
from piccolo.query.methods.select import Select

self.column = column

self.value = value if value == UNDEFINED else self.clean_value(value)
if values == UNDEFINED:
self.values = values
elif isinstance(values, Select):
self.values = values
else:
self.values = [self.clean_value(i) for i in values] # type: ignore

Expand Down Expand Up @@ -190,11 +195,16 @@ def clean_value(self, value: t.Any) -> t.Any:

@property
def values_querystring(self) -> QueryString:
from piccolo.query.methods.select import Select

values = self.values

if isinstance(values, Undefined):
raise ValueError("values is undefined")

if isinstance(values, Select):
return values.querystrings[0]

template = ", ".join("{}" for _ in values)
return QueryString(template, *values)

Expand Down