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
1=== modified file 'storm/store.py'
2--- storm/store.py 2012-06-12 10:00:51 +0000
3+++ storm/store.py 2012-11-13 09:42:19 +0000
4@@ -33,7 +33,7 @@
5 from storm.expr import (
6 Expr, Select, Insert, Update, Delete, Column, Count, Max, Min,
7 Avg, Sum, Eq, And, Asc, Desc, compile_python, compare_columns, SQLRaw,
8- Union, Except, Intersect, Alias, SetExpr)
9+ Union, Except, Intersect, Alias, SetExpr, State)
10 from storm.exceptions import (
11 WrongStoreError, NotFlushedError, OrderLoopError, UnorderedError,
12 NotOneError, FeatureError, CompileError, LostObjectError, ClassInfoError)
13@@ -1473,6 +1473,17 @@
14 result_set._where = And(result_set._where, extra_where)
15 return result_set
16
17+ def get_query(self):
18+ """
19+ Return the query associated with this L{ResultSet}.
20+
21+ @return: A tuple with the query and its parameters.
22+ """
23+ s = State()
24+ query = self._store._connection.compile(self._get_select(), s)
25+ params = tuple(param.get() for param in s.parameters)
26+ return query, params
27+
28 def _set_expr(self, expr_cls, other, all=False):
29 if not self._find_spec.is_compatible(other._find_spec):
30 raise FeatureError("Incompatible results for set operation")
31
32=== modified file 'tests/store/base.py'
33--- tests/store/base.py 2012-04-06 08:19:07 +0000
34+++ tests/store/base.py 2012-11-13 09:42:19 +0000
35@@ -1508,6 +1508,31 @@
36 self.assertEquals(sorted((foo.id, title) for (foo, title) in result),
37 [(20, u"Title 200"), (30, u"Title 100")])
38
39+ def test_get_query(self):
40+ """
41+ L{ResultSet.get_query} returns the query associated to the
42+ L{ResultSet}.
43+ """
44+ result = self.store.find(Bar, Bar.foo_id == Foo.id)
45+ query, params = result.get_query()
46+ self.assertEqual(
47+ "SELECT bar.foo_id, bar.id, bar.title FROM bar, foo "
48+ "WHERE bar.foo_id = foo.id",
49+ query)
50+ self.assertEqual((), params)
51+
52+ def test_get_query_with_params(self):
53+ """
54+ L{ResultSet.get_query} includes values for query parameters.
55+ """
56+ result = self.store.find(Foo, Foo.id > 3, Foo.title.like(u"foo"))
57+ query, params = result.get_query()
58+ self.assertEqual(
59+ "SELECT foo.id, foo.title FROM foo "
60+ "WHERE foo.id > ? AND foo.title LIKE ?",
61+ query)
62+ self.assertEqual((3, "foo"), params)
63+
64 def test_get_does_not_validate(self):
65 def validator(object, attr, value):
66 self.fail("validator called with arguments (%r, %r, %r)" %

Subscribers

People subscribed via source and target branches

to status/vote changes: