Merge lp:~jelmer/storm/cast into lp:storm

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jamu Kakar
Approved revision: 384
Merged at revision: 384
Proposed branch: lp:~jelmer/storm/cast
Merge into: lp:storm
Diff against target: 48 lines (+27/-0)
2 files modified
storm/expr.py (+20/-0)
tests/expr.py (+7/-0)
To merge this branch: bzr merge lp:~jelmer/storm/cast
Reviewer Review Type Date Requested Status
Jamu Kakar (community) Approve
Gustavo Niemeyer Approve
Review via email: mp+42093@code.launchpad.net

Commit message

Add storm.expr.Cast.

Description of the change

This branch adds storm.expr.Cast.

To post a comment you must log in.
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Looks good. +1!

review: Approve
Revision history for this message
Jamu Kakar (jkakar) wrote :

[1]

Please add docstrings to the various bits you've added.

[2]

Maybe it's overkill, but I wonder if we should allow the second
parameter to optionally be a Python type? In such a case, we'd pick
the appropriate database type, depending on the backend.

+1 on this branch! If [2] seems interesting please file a bug about
it.

review: Approve
lp:~jelmer/storm/cast updated
385. By Jelmer Vernooij

Add docstrings.

Revision history for this message
Jelmer Vernooij (jelmer) wrote :

I've added the docstring and filed a bug about supporting Python type objects. Is there anything else I need to do to help land this?

Revision history for this message
Jamu Kakar (jkakar) wrote :

Harassment was all that was needed, I've merged the changes. :)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'storm/expr.py'
2--- storm/expr.py 2009-11-02 11:11:20 +0000
3+++ storm/expr.py 2010-11-30 12:31:36 +0000
4@@ -1286,6 +1286,26 @@
5 name = "COALESCE"
6
7
8+class Cast(FuncExpr):
9+ """A representation of "CAST" clauses. e.g., CAST(bar AS TEXT).
10+ """
11+ __slots__ = ("column", "type")
12+ name = "CAST"
13+
14+ def __init__(self, column, type):
15+ """Create a cast of C{column} as C{type}."""
16+ self.column = column
17+ self.type = type
18+
19+
20+@compile.when(Cast)
21+def compile_cast(compile, cast, state):
22+ state.push("context", EXPR)
23+ column = compile(cast.column, state)
24+ state.pop()
25+ return "CAST(%s AS %s)" % (column, cast.type)
26+
27+
28 # --------------------------------------------------------------------
29 # Prefix and suffix expressions
30
31
32=== modified file 'tests/expr.py'
33--- tests/expr.py 2009-11-02 11:11:20 +0000
34+++ tests/expr.py 2010-11-30 12:31:36 +0000
35@@ -1380,6 +1380,13 @@
36 def test_count_distinct_all(self):
37 self.assertRaises(ValueError, Count, distinct=True)
38
39+ def test_cast(self):
40+ expr = Cast(Func1(), "TEXT")
41+ state = State()
42+ statement = compile(expr, state)
43+ self.assertEquals(statement, "CAST(func1() AS TEXT)")
44+ self.assertEquals(state.parameters, [])
45+
46 def test_max(self):
47 expr = Max(Func1())
48 state = State()

Subscribers

People subscribed via source and target branches

to status/vote changes: