Skip to content

Commit

Permalink
Merge f3d8453 into 8a42936
Browse files Browse the repository at this point in the history
  • Loading branch information
bmuller committed Jul 11, 2013
2 parents 8a42936 + f3d8453 commit 5fa1438
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 13 additions & 5 deletions silverberg/marshal.py
Expand Up @@ -42,11 +42,19 @@


def prepare(query, params):
# For every match of the form ":param_name", call marshal
# on kwargs['param_name'] and replace that section of the query
# with the result
new, count = re.subn(_param_re,
lambda m: marshal(params[m.group(1)[1:]]), query)
"""
For every match of the form ":param_name", call marshal
on kwargs['param_name'] and replace that section of the query
with the result
"""
def repl(match):
name = match.group(1)[1:]
if name in params:
return marshal(params[name])
return ":%s" % name

new, count = re.subn(_param_re, repl, query)

if len(params) > count:
raise cql.ProgrammingError("More keywords were provided "
"than parameters")
Expand Down
16 changes: 15 additions & 1 deletion silverberg/test/test_marshal.py
Expand Up @@ -24,7 +24,21 @@

from twisted.trial.unittest import TestCase

from silverberg.marshal import marshal, unmarshal_timestamp, unmarshal_int, unmarshal_initializable_int
from silverberg.marshal import marshal, unmarshal_timestamp, unmarshal_int, \
unmarshal_initializable_int, prepare


class StatementPreparation(TestCase):
"""
Test preparint a query with optional parameters.
"""

def test_prepare(self):
result = prepare("string :with a colon :with", {'with': 'value'})
self.assertEqual(result, "string 'value' a colon 'value'")

result = prepare("string :with a colon :with", {})
self.assertEqual(result, "string :with a colon :with")


class MarshallingUnmarshallingDatetime(TestCase):
Expand Down

0 comments on commit 5fa1438

Please sign in to comment.