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

source_changes at macosforge.org source_changes at macosforge.org
Wed Jan 19 12:58:12 PST 2011


Revision: 6770
          http://trac.macosforge.org/projects/calendarserver/changeset/6770
Author:   glyph at apple.com
Date:     2011-01-19 12:58:12 -0800 (Wed, 19 Jan 2011)
Log Message:
-----------
starting on SELECT statement

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

Added Paths:
-----------
    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 20:58:02 UTC (rev 6769)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py	2011-01-19 20:58:12 UTC (rev 6770)
@@ -1,3 +1,4 @@
+# -*- test-case-name: txdav.base.datastore.test.test_sqlsyntax -*-
 ##
 # Copyright (c) 2010 Apple Inc. All rights reserved.
 #
@@ -80,3 +81,43 @@
 
     modelType = Column
 
+    def __eq__(self, other):
+        return ConstantComparison(self.model.name, '=', other)
+
+
+
+class ConstantComparison(object):
+
+    def __init__(self, a, op, b):
+        self.a = a
+        self.op = op
+        self.b = b
+
+
+    def toSQL(self):
+        return (' '.join([self.a, self.op, '?']), [self.b])
+
+
+
+class Select(object):
+    """
+    'select' statement.
+    """
+
+    def __init__(self, Where=None, From=None):
+        self.From = From
+        self.Where = Where
+
+
+    def toSQL(self):
+        """
+        @return: a 2-tuple of (sql, args).
+        """
+        sql = "select * from " + self.From.model.name
+        args = []
+        if self.Where is not None:
+            moreSQL, moreArgs = self.Where.toSQL()
+            sql += (" where " + moreSQL)
+            args.extend(moreArgs)
+        return (sql, args)
+

Added: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py	                        (rev 0)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py	2011-01-19 20:58:12 UTC (rev 6770)
@@ -0,0 +1,54 @@
+##
+# Copyright (c) 2010 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+
+from txdav.base.datastore.sqlmodel import Schema
+from txdav.base.datastore.sqlparser import addSQLToSchema
+from txdav.base.datastore.sqlsyntax import SchemaSyntax, Select
+
+from twisted.trial.unittest import TestCase
+
+class GenerationTests(TestCase):
+    """
+    Tests for syntactic helpers to generate SQL queries.
+    """
+
+    def setUp(self):
+        s = Schema(self.id())
+        addSQLToSchema(schema=s, schemaData="""
+                       create table FOO (BAR integer);
+                       """)
+        self.schema = SchemaSyntax(s)
+
+
+    def test_simplestSelect(self):
+        """
+        L{Select} will generate a 'select' statement, by default, asking for all
+        rows in a table.
+        """
+        self.assertEquals(Select(From=self.schema.FOO).toSQL(),
+                          ("select * from FOO", []))
+
+
+    def test_simpleWhereClause(self):
+        """
+        L{Select} will generate a 'select' statement with a 'where' clause
+        containing an expression.
+        """
+        self.assertEquals(Select(From=self.schema.FOO,
+                                 Where=self.schema.FOO.BAR == 1).toSQL(),
+                          ("select * from FOO where BAR = ?", [1]))
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110119/5338bac6/attachment-0001.html>


More information about the calendarserver-changes mailing list