Skip to content
Merged
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
10 changes: 8 additions & 2 deletions ooquery/ooquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class OOQuery(object):
def __init__(self, table, foreign_key=None):
self._fields = []
self._resolved_fields = []
self.table = Table(table)
self.foreign_key = foreign_key
self._select = self.table.select()
Expand Down Expand Up @@ -111,11 +112,16 @@ def select(self, fields=None, **kwargs):
kwargs['group_by'].append(
table_field
)
self._select = self.select_on.select(*self.fields, **self.select_opts)
self._resolved_fields = self.fields
self._select = self.select_on.select(
*self._resolved_fields, **self.select_opts
)
return self

def where(self, domain):
where = self.parser.parse(domain)
self._select = self.select_on.select(*self.fields, **self.select_opts)
self._select = self.select_on.select(
*self._resolved_fields, **self.select_opts
)
self._select.where = where
return self._select
23 changes: 23 additions & 0 deletions spec/ooquery_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ooquery import OOQuery
from ooquery.expression import Field
from ooquery.operators import *
from ooquery.parser import Parser
from sql import Table, Literal, NullsFirst, NullsLast
from sql.operators import And, Concat
from sql.aggregate import Max
Expand Down Expand Up @@ -29,6 +30,28 @@
sel.where = And((t.field3 == 4,))
expect(tuple(sql)).to(equal(tuple(sel)))

with it('should resolve projection fields only once'):
class CountingParser(Parser):
def __init__(self, *args, **kwargs):
super(CountingParser, self).__init__(*args, **kwargs)
self.resolved_fields = []

def get_table_field(self, table, field):
self.resolved_fields.append(field)
return super(CountingParser, self).get_table_field(
table, field
)

class CountingOOQuery(OOQuery):
def create_parser(self):
return CountingParser(self.table, self.foreign_key)

q = CountingOOQuery('table')
q.select(['field1', 'field2']).where([('field3', '=', 4)])

expect(q.parser.resolved_fields.count('field1')).to(equal(1))
expect(q.parser.resolved_fields.count('field2')).to(equal(1))

with it('should have where method and compare two fields of the table'):
q = OOQuery('table')
sql = q.select(['field1', 'field2']).where([('field3', '>', Field('field4'))])
Expand Down
Loading