Skip to content

Commit

Permalink
Add support for "in interable"
Browse files Browse the repository at this point in the history
  • Loading branch information
huntfx committed Jun 25, 2020
1 parent 80d5444 commit 022f6b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ with FTrackQuery() as session:

task = session.Task.where(
entity.parent==session.Episode.first(),
entity.status.name.in_('Lighting', 'Rendering'),
name='My Task',
).order(
entity.type.name.desc(),
Expand Down Expand Up @@ -63,7 +64,9 @@ Limit the amount of results to a certain value.
In the case of using a limit, this applies an offset to the result that is returned.

### .in_(_subquery_) | .not_in(_subquery_)
Perform a subquery to check if an attribute matches any results.
Perform a check to check if an attribute matches any results.

This can accept a subquery such `.in_('select id from table where x is y')`, or a list of items like `.in_('x', 'y')`.

### .\_\_call\_\_(_value_)
If an entity has a primary key, by calling the value of that primary key, the entity or `None` will be returned.
Expand Down
19 changes: 15 additions & 4 deletions ftrack_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

__all__ = ['FTrackQuery', 'entity', 'and_', 'or_']
__version__ = '1.4.2'
__version__ = '1.4.3'

import logging
import os
Expand Down Expand Up @@ -244,11 +244,22 @@ def any(self, *args, **kwargs):
where = parse_inputs(*args, **kwargs)
return self.__class__('{} any ({})'.format(self.value, and_(*where)))

def in_(self, subquery):
def in_(self, *args):
"""The in operator works slightly differently to the others.
It supports subqueries (x in (select y from z)), and multiple
items (x in ("y", "z")).
Since quotation marks are important, this method assumes that
a single argument is a subquery, and multiple arguments are a
list of possible values.
"""
if len(args) == 1:
subquery = args[0]
else:
subquery = ', '.join(map(convert_output_value, args))
return self.__class__('{} in ({})'.format(self.value, subquery))

def not_in(self, subquery):
return ~self.in_(subquery)
def not_in(self, *args):
return ~self.in_(*args)


class Query(object):
Expand Down

0 comments on commit 022f6b6

Please sign in to comment.