Skip to content

Commit

Permalink
Fixes #92: without_distinct() and first() does not work right together
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Oct 28, 2014
1 parent 356743e commit 5111574
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pony/orm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4192,6 +4192,7 @@ def __init__(query, code_key, tree, globals, locals, cells=None, left_join=False
query._next_kwarg_id = 0
query._for_update = query._nowait = False
query._result = None
query._distinct = None
query._prefetch = False
query._entities_to_prefetch = set()
query._attrs_to_prefetch_dict = defaultdict(set)
Expand All @@ -4203,15 +4204,15 @@ def _clone(query, **kwargs):
return new_query
def __reduce__(query):
return unpickle_query, (query._fetch(),)
def _construct_sql_and_arguments(query, range=None, distinct=None, aggr_func_name=None):
def _construct_sql_and_arguments(query, range=None, aggr_func_name=None):
translator = query._translator
sql_key = query._key + (range, distinct, aggr_func_name, query._for_update, query._nowait,
sql_key = query._key + (range, query._distinct, aggr_func_name, query._for_update, query._nowait,
options.INNER_JOIN_SYNTAX)
database = query._database
cache_entry = database._constructed_sql_cache.get(sql_key)
if cache_entry is None:
sql_ast, attr_offsets = translator.construct_sql_ast(
range, distinct, aggr_func_name, query._for_update, query._nowait)
range, query._distinct, aggr_func_name, query._for_update, query._nowait)
cache = database._get_cache()
sql, adapter = database.provider.ast2sql(sql_ast)
cache_entry = sql, adapter, attr_offsets
Expand All @@ -4227,12 +4228,12 @@ def _construct_sql_and_arguments(query, range=None, distinct=None, aggr_func_nam
else: query_key = sql_key + (arguments_key)
else: query_key = None
return sql, arguments, attr_offsets, query_key
def _fetch(query, range=None, distinct=None):
def _fetch(query, range=None):
translator = query._translator
if query._result is not None:
return QueryResult(query._result, translator.expr_type, translator.col_names)

sql, arguments, attr_offsets, query_key = query._construct_sql_and_arguments(range, distinct)
sql, arguments, attr_offsets, query_key = query._construct_sql_and_arguments(range)
database = query._database
cache = database._get_cache()
if query._for_update: cache.immediate = True
Expand Down Expand Up @@ -4364,10 +4365,10 @@ def first(query):
return objects[0]
@cut_traceback
def without_distinct(query):
return query._fetch(distinct=False)
return query._clone(_distinct=False)
@cut_traceback
def distinct(query):
return query._fetch(distinct=True)
return query._clone(_distinct=True)
@cut_traceback
def exists(query):
objects = query[:1]
Expand Down
34 changes: 34 additions & 0 deletions pony/orm/tests/queries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,37 @@ WHERE 0 = 1
SELECT DISTINCT "s"."name"
FROM "Student" "s"
WHERE 1 = 1

>>> select(s.name for s in Student)[:]

SELECT DISTINCT "s"."name"
FROM "Student" "s"

>>> select(s.name for s in Student).without_distinct()[:]

SELECT "s"."name"
FROM "Student" "s"

>>> select(s.name for s in Student).without_distinct().distinct()[:]

SELECT DISTINCT "s"."name"
FROM "Student" "s"

>>> select(s.name for s in Student).without_distinct().distinct().without_distinct()[:]

SELECT "s"."name"
FROM "Student" "s"

>>> select(s.name for s in Student).first()

SELECT DISTINCT "s"."name"
FROM "Student" "s"
ORDER BY 1
LIMIT 1

>>> select(s.name for s in Student).without_distinct().first()

SELECT "s"."name"
FROM "Student" "s"
ORDER BY 1
LIMIT 1

0 comments on commit 5111574

Please sign in to comment.