Merge lp:~ack/storm/resultset-get-query into lp:storm

Proposed by Alberto Donato
Status: Work in progress
Proposed branch: lp:~ack/storm/resultset-get-query
Merge into: lp:storm
Diff against target: 66 lines (+37/-1)
2 files modified
storm/store.py (+12/-1)
tests/store/base.py (+25/-0)
To merge this branch: bzr merge lp:~ack/storm/resultset-get-query
Reviewer Review Type Date Requested Status
Christopher Armstrong (community) Approve
Storm Developers Pending
Storm Developers Pending
Review via email: mp+133871@code.launchpad.net

Description of the change

This adds a get_query() method to ResultSet, returning the query and parameters that generated the resultset.
Mostly intended for debugging purpose.

To post a comment you must log in.
Revision history for this message
Christopher Armstrong (radix) wrote :

[1] I think this should use the database-specific compile function to make sure that the query that is returned is exactly what will be sent to the database.

lp:~ack/storm/resultset-get-query updated
455. By Alberto Donato

Use the Connection compile function.

Revision history for this message
Alberto Donato (ack) wrote :

> [1] I think this should use the database-specific compile function to make
> sure that the query that is returned is exactly what will be sent to the
> database.

Done! thanks

Revision history for this message
Christopher Armstrong (radix) wrote :

Looks good.

review: Approve

Unmerged revisions

455. By Alberto Donato

Use the Connection compile function.

454. By Alberto Donato

Add get_query().

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'storm/store.py'
--- storm/store.py 2012-06-12 10:00:51 +0000
+++ storm/store.py 2012-11-13 09:42:19 +0000
@@ -33,7 +33,7 @@
33from storm.expr import (33from storm.expr import (
34 Expr, Select, Insert, Update, Delete, Column, Count, Max, Min,34 Expr, Select, Insert, Update, Delete, Column, Count, Max, Min,
35 Avg, Sum, Eq, And, Asc, Desc, compile_python, compare_columns, SQLRaw,35 Avg, Sum, Eq, And, Asc, Desc, compile_python, compare_columns, SQLRaw,
36 Union, Except, Intersect, Alias, SetExpr)36 Union, Except, Intersect, Alias, SetExpr, State)
37from storm.exceptions import (37from storm.exceptions import (
38 WrongStoreError, NotFlushedError, OrderLoopError, UnorderedError,38 WrongStoreError, NotFlushedError, OrderLoopError, UnorderedError,
39 NotOneError, FeatureError, CompileError, LostObjectError, ClassInfoError)39 NotOneError, FeatureError, CompileError, LostObjectError, ClassInfoError)
@@ -1473,6 +1473,17 @@
1473 result_set._where = And(result_set._where, extra_where)1473 result_set._where = And(result_set._where, extra_where)
1474 return result_set1474 return result_set
14751475
1476 def get_query(self):
1477 """
1478 Return the query associated with this L{ResultSet}.
1479
1480 @return: A tuple with the query and its parameters.
1481 """
1482 s = State()
1483 query = self._store._connection.compile(self._get_select(), s)
1484 params = tuple(param.get() for param in s.parameters)
1485 return query, params
1486
1476 def _set_expr(self, expr_cls, other, all=False):1487 def _set_expr(self, expr_cls, other, all=False):
1477 if not self._find_spec.is_compatible(other._find_spec):1488 if not self._find_spec.is_compatible(other._find_spec):
1478 raise FeatureError("Incompatible results for set operation")1489 raise FeatureError("Incompatible results for set operation")
14791490
=== modified file 'tests/store/base.py'
--- tests/store/base.py 2012-04-06 08:19:07 +0000
+++ tests/store/base.py 2012-11-13 09:42:19 +0000
@@ -1508,6 +1508,31 @@
1508 self.assertEquals(sorted((foo.id, title) for (foo, title) in result),1508 self.assertEquals(sorted((foo.id, title) for (foo, title) in result),
1509 [(20, u"Title 200"), (30, u"Title 100")])1509 [(20, u"Title 200"), (30, u"Title 100")])
15101510
1511 def test_get_query(self):
1512 """
1513 L{ResultSet.get_query} returns the query associated to the
1514 L{ResultSet}.
1515 """
1516 result = self.store.find(Bar, Bar.foo_id == Foo.id)
1517 query, params = result.get_query()
1518 self.assertEqual(
1519 "SELECT bar.foo_id, bar.id, bar.title FROM bar, foo "
1520 "WHERE bar.foo_id = foo.id",
1521 query)
1522 self.assertEqual((), params)
1523
1524 def test_get_query_with_params(self):
1525 """
1526 L{ResultSet.get_query} includes values for query parameters.
1527 """
1528 result = self.store.find(Foo, Foo.id > 3, Foo.title.like(u"foo"))
1529 query, params = result.get_query()
1530 self.assertEqual(
1531 "SELECT foo.id, foo.title FROM foo "
1532 "WHERE foo.id > ? AND foo.title LIKE ?",
1533 query)
1534 self.assertEqual((3, "foo"), params)
1535
1511 def test_get_does_not_validate(self):1536 def test_get_does_not_validate(self):
1512 def validator(object, attr, value):1537 def validator(object, attr, value):
1513 self.fail("validator called with arguments (%r, %r, %r)" %1538 self.fail("validator called with arguments (%r, %r, %r)" %

Subscribers

People subscribed via source and target branches

to status/vote changes: