]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added extract() function to sql dialect
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 14 Sep 2006 16:41:18 +0000 (16:41 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 14 Sep 2006 16:41:18 +0000 (16:41 +0000)
CHANGES
lib/sqlalchemy/sql.py
test/sql/select.py

diff --git a/CHANGES b/CHANGES
index 67a359b319fe974fc6bd8d5a55630aa17ab32b8d..3f664c498927807bce854f2bf6ad2077b56d6edb 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -15,6 +15,7 @@ when version_id_col is in effect and query.with_lockmode()
 is used to get() an instance thats already loaded
 - fixed bug where Connection wouldnt lose its Transaction
 after commit/rollback
+- added extract() function to sql dialect
 
 0.2.8
 - cleanup on connection methods + documentation.  custom DBAPI
index ab371dcb915ecb7971cf9f78a8b5fd2e4a6af883..1f3d3575ed49975f98c62692cb01a1056b00f85d 100644 (file)
@@ -10,7 +10,7 @@ from sqlalchemy import types as sqltypes
 import string, re, random, sets
 types = __import__('types')
 
-__all__ = ['text', 'table', 'column', 'func', 'select', 'update', 'insert', 'delete', 'join', 'and_', 'or_', 'not_', 'between_', 'case', 'cast', 'union', 'union_all', 'null', 'desc', 'asc', 'outerjoin', 'alias', 'subquery', 'literal', 'bindparam', 'exists']
+__all__ = ['text', 'table', 'column', 'func', 'select', 'update', 'insert', 'delete', 'join', 'and_', 'or_', 'not_', 'between_', 'case', 'cast', 'union', 'union_all', 'null', 'desc', 'asc', 'outerjoin', 'alias', 'subquery', 'literal', 'bindparam', 'exists', 'extract']
 
 def desc(column):
     """returns a descending ORDER BY clause element, e.g.:
@@ -155,7 +155,11 @@ def cast(clause, totype, **kwargs):
     """
     return Cast(clause, totype, **kwargs)
 
-
+def extract(field, expr):
+    """return extract(field FROM expr)"""
+    expr = BinaryClause(text(field), expr, "FROM")
+    return func.extract(expr)
+    
 def exists(*args, **params):
     params['correlate'] = True
     s = select(*args, **params)
index 8a5fc302ccbca0b7e3e924ec5a279c80aad0db54..586184b4a778cfd16c42cb27edacdef8e56a2686 100644 (file)
@@ -404,6 +404,12 @@ FROM mytable, myothertable WHERE foo.id = foofoo(lala) AND datetime(foo) = Today
 
         # test a dotted func off the engine itself
         self.runtest(func.lala.hoho(7), "lala.hoho(:hoho)")
+    
+    def testextract(self):
+        """test the EXTRACT function"""
+        self.runtest(select([extract("month", table3.c.otherstuff)]), "SELECT extract(month FROM thirdtable.otherstuff) FROM thirdtable")
+        
+        self.runtest(select([extract("day", func.to_date("03/20/2005", "MM/DD/YYYY"))]), "SELECT extract(day FROM to_date(:to_date, :to_da_1))")
         
     def testjoin(self):
         self.runtest(