>>> print func.count(1)
count(:param_1)
+ The element is a column-oriented SQL element like any other, and is
+ used in that way::
+
+ >>> print select([func.count(table.c.id)])
+ SELECT count(sometable.id) FROM sometable
+
Any name can be given to ``func``. If the function name is unknown to
SQLAlchemy, it will be rendered exactly as is. For common SQL functions
which SQLAlchemy is aware of, the name may be interpreted as a *generic
This object meets the "column" interface, including comparison and labeling
functions. The object can also be passed the :meth:`~.Connectable.execute`
method of a :class:`.Connection` or :class:`.Engine`, where it will be
- wrapped inside of a SELECT statement first.
+ wrapped inside of a SELECT statement first::
+
+ print connection.execute(func.current_timestamp()).scalar()
+
+ A function can also be "bound" to a :class:`.Engine` or :class:`.Connection`
+ using the ``bind`` keyword argument, providing an execute() as well
+ as a scalar() method::
+
+ myfunc = func.current_timestamp(bind=some_engine)
+ print myfunc.scalar()
Functions which are interpreted as "generic" functions know how to
calculate their return type automatically. For a listing of known generic
def2 = sa.text("getdate()")
else:
def2 = sa.text("current_date")
- ts = db.func.current_date().scalar()
+ ts = db.scalar(func.current_date())
else:
f = len('abcdef')
f2 = len('abcdefghijk')
def tearDown(self):
pass
+ @testing.uses_deprecated
def test_standalone_execute(self):
x = testing.db.func.current_date().execute().scalar()
y = testing.db.func.current_date().select().execute().scalar()
@testing.fails_on_everything_except('postgresql')
def test_as_from(self):
# TODO: shouldnt this work on oracle too ?
- x = testing.db.func.current_date().execute().scalar()
- y = testing.db.func.current_date().select().execute().scalar()
- z = testing.db.func.current_date().scalar()
- w = select(['*'], from_obj=[testing.db.func.current_date()]).scalar()
+ x = func.current_date(bind=testing.db).execute().scalar()
+ y = func.current_date(bind=testing.db).select().execute().scalar()
+ z = func.current_date(bind=testing.db).scalar()
+ w = select(['*'], from_obj=[func.current_date(bind=testing.db)]).scalar()
# construct a column-based FROM object out of a function, like in [ticket:172]
- s = select([sql.column('date', type_=DateTime)], from_obj=[testing.db.func.current_date()])
+ s = select([sql.column('date', type_=DateTime)], from_obj=[func.current_date(bind=testing.db)])
q = s.execute().first()[s.c.date]
r = s.alias('datequery').select().scalar()
'DateTest mismatch: got:%s expected:%s' % (l, insert_data))
def testtextdate(self):
- x = testing.db.text(
+ x = testing.db.execute(text(
"select user_datetime from query_users_with_date",
- typemap={'user_datetime':DateTime}).execute().fetchall()
+ typemap={'user_datetime':DateTime})).fetchall()
self.assert_(isinstance(x[0][0], datetime.datetime))
- x = testing.db.text(
+ x = testing.db.execute(text(
"select * from query_users_with_date where user_datetime=:somedate",
- bindparams=[bindparam('somedate', type_=types.DateTime)]).execute(
+ bindparams=[bindparam('somedate', type_=types.DateTime)]),
somedate=datetime.datetime(2005, 11, 10, 11, 52, 35)).fetchall()
def testdate2(self):