[CalendarServer-changes] [6787] CalendarServer/branches/users/glyph/dal/txdav/base/datastore

source_changes at macosforge.org source_changes at macosforge.org
Wed Jan 19 13:01:11 PST 2011


Revision: 6787
          http://trac.macosforge.org/projects/calendarserver/changeset/6787
Author:   glyph at apple.com
Date:     2011-01-19 13:01:11 -0800 (Wed, 19 Jan 2011)
Log Message:
-----------
parameter binding

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py	2011-01-19 21:01:00 UTC (rev 6786)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py	2011-01-19 21:01:11 UTC (rev 6787)
@@ -281,8 +281,6 @@
         self.columns = columns
 
 
-
-
     def toSQL(self, placeholder="?", quote=lambda x: x):
         """
         @return: a 2-tuple of (sql, args).
@@ -313,6 +311,16 @@
         self.parameters = parameters
 
 
+    def bind(self, **kw):
+        params = []
+        for parameter in self.parameters:
+            if isinstance(parameter, Parameter):
+                params.append(kw[parameter.name])
+            else:
+                params.append(parameter)
+        return SQLStatement(self.text, params)
+
+
     def append(self, anotherStatement):
         self.text += anotherStatement.text
         self.parameters += anotherStatement.parameters
@@ -338,3 +346,9 @@
         return self
 
 
+class Parameter(object):
+    def __init__(self, name):
+        self.name = name
+
+    def __repr__(self):
+        return 'Parameter(%r)' % (self.name,)

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py	2011-01-19 21:01:00 UTC (rev 6786)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py	2011-01-19 21:01:11 UTC (rev 6787)
@@ -21,7 +21,7 @@
 from txdav.base.datastore.sqlmodel import Schema
 from txdav.base.datastore.sqlparser import addSQLToSchema
 from txdav.base.datastore.sqlsyntax import (
-    SchemaSyntax, Select, SQLStatement, TableMismatch)
+    SchemaSyntax, Select, SQLStatement, TableMismatch, Parameter)
 
 from twisted.trial.unittest import TestCase
 
@@ -183,3 +183,18 @@
             )
         )
 
+
+    def test_bindParameters(self):
+        """
+        L{SQLStatement.bind} returns a copy of that L{SQLStatement} with the
+        L{Parameter} objects in its parameter list replaced with the keyword
+        arguments to C{bind}.
+        """
+
+        self.assertEquals(
+            Select(From=self.schema.FOO,
+                   Where=(self.schema.FOO.BAR > Parameter("testing")).And(
+                   self.schema.FOO.BAZ < 7)).toSQL().bind(testing=173),
+            SQLStatement("select * from FOO where BAR > ? and BAZ < ?",
+                         [173, 7])
+        )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110119/b7eac032/attachment-0001.html>


More information about the calendarserver-changes mailing list