Skip to content

Commit

Permalink
Merge pull request #297 from gi0baro/set-where
Browse files Browse the repository at this point in the history
Added .where method to Set and DAL
  • Loading branch information
mdipierro committed Sep 27, 2015
2 parents 72ae118 + d810b6a commit d443aa6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
8 changes: 6 additions & 2 deletions pydal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,13 +936,17 @@ def smart_query(self, fields, text):
return Set(self, smart_query(fields, text))

def __call__(self, query=None, ignore_common_filters=None):
return self.where(query, ignore_common_filters)

def where(self, query=None, ignore_common_filters=None):
if isinstance(query, Table):
query = self._adapter.id_query(query)
elif isinstance(query, Field):
query = query!=None
query = query != None
elif isinstance(query, dict):
icf = query.get("ignore_common_filters")
if icf: ignore_common_filters = icf
if icf:
ignore_common_filters = icf
return Set(self, query, ignore_common_filters=ignore_common_filters)

def commit(self):
Expand Down
52 changes: 38 additions & 14 deletions pydal/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1870,21 +1870,25 @@ def __repr__(self):
return '<Set %s>' % BaseAdapter.expand(self.db._adapter,self.query)

def __call__(self, query, ignore_common_filters=False):
return self.where(query, ignore_common_filters)

def where(self, query, ignore_common_filters=False):
if query is None:
return self
elif isinstance(query,Table):
elif isinstance(query, Table):
query = self.db._adapter.id_query(query)
elif isinstance(query,str):
query = Expression(self.db,query)
elif isinstance(query,Field):
query = query!=None
elif isinstance(query, str):
query = Expression(self.db, query)
elif isinstance(query, Field):
query = query != None
if self.query:
return Set(self.db, self.query & query,
ignore_common_filters=ignore_common_filters)
else:
return Set(self.db, query,
ignore_common_filters=ignore_common_filters)


def _count(self,distinct=None):
return self.db._adapter._count(self.query,distinct)

Expand Down Expand Up @@ -2136,6 +2140,7 @@ def delete_uploaded_files(self, upload_fields=None):
class LazyReferenceGetter(object):
def __init__(self, table, id):
self.db, self.tablename, self.id = table._db, table._tablename, id

def __call__(self, other_tablename):
if self.db._lazy_tables is False:
raise AttributeError()
Expand All @@ -2151,37 +2156,56 @@ class LazySet(object):
def __init__(self, field, id):
self.db, self.tablename, self.fieldname, self.id = \
field.db, field._tablename, field.name, id

def _getset(self):
query = self.db[self.tablename][self.fieldname]==self.id
return Set(self.db,query)
query = self.db[self.tablename][self.fieldname] == self.id
return Set(self.db, query)

def __repr__(self):
return repr(self._getset())

def __call__(self, query, ignore_common_filters=False):
return self.where(query, ignore_common_filters)

def where(self, query, ignore_common_filters=False):
return self._getset()(query, ignore_common_filters)
def _count(self,distinct=None):

def _count(self, distinct=None):
return self._getset()._count(distinct)

def _select(self, *fields, **attributes):
return self._getset()._select(*fields,**attributes)
return self._getset()._select(*fields, **attributes)

def _delete(self):
return self._getset()._delete()

def _update(self, **update_fields):
return self._getset()._update(**update_fields)

def isempty(self):
return self._getset().isempty()
def count(self,distinct=None, cache=None):
return self._getset().count(distinct,cache)

def count(self, distinct=None, cache=None):
return self._getset().count(distinct, cache)

def select(self, *fields, **attributes):
return self._getset().select(*fields,**attributes)
def nested_select(self,*fields,**attributes):
return self._getset().nested_select(*fields,**attributes)
return self._getset().select(*fields, **attributes)

def nested_select(self, *fields, **attributes):
return self._getset().nested_select(*fields, **attributes)

def delete(self):
return self._getset().delete()

def update(self, **update_fields):
return self._getset().update(**update_fields)

def update_naive(self, **update_fields):
return self._getset().update_naive(**update_fields)

def validate_and_update(self, **update_fields):
return self._getset().validate_and_update(**update_fields)

def delete_uploaded_files(self, upload_fields=None):
return self._getset().delete_uploaded_files(upload_fields)

Expand Down

0 comments on commit d443aa6

Please sign in to comment.