From 3dd5d834ad6d3ae7f515ff20f3b8756d46e2347f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 14 Sep 2006 16:41:18 +0000 Subject: [PATCH] added extract() function to sql dialect --- CHANGES | 1 + lib/sqlalchemy/sql.py | 8 ++++++-- test/sql/select.py | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 67a359b319..3f664c4989 100644 --- 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 diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index ab371dcb91..1f3d3575ed 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -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) diff --git a/test/sql/select.py b/test/sql/select.py index 8a5fc302cc..586184b4a7 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -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( -- 2.47.2