MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _FunctionGenerator

Class _FunctionGenerator

lib/sqlalchemy/sql/functions.py:861–1275  ·  view source on GitHub ↗

Generate SQL function expressions. :data:`.func` is a special object instance which generates SQL functions based on name-based attributes, e.g.: .. sourcecode:: pycon+sql >>> print(func.count(1)) {printsql}count(:param_1) The returned object is an instance of :cl

Source from the content-addressed store, hash-verified

859
860
861class _FunctionGenerator:
862 """Generate SQL function expressions.
863
864 :data:`.func` is a special object instance which generates SQL
865 functions based on name-based attributes, e.g.:
866
867 .. sourcecode:: pycon+sql
868
869 >>> print(func.count(1))
870 {printsql}count(:param_1)
871
872 The returned object is an instance of :class:`.Function`, and is a
873 column-oriented SQL element like any other, and is used in that way:
874
875 .. sourcecode:: pycon+sql
876
877 >>> print(select(func.count(table.c.id)))
878 {printsql}SELECT count(sometable.id) FROM sometable
879
880 Any name can be given to :data:`.func`. If the function name is unknown to
881 SQLAlchemy, it will be rendered exactly as is. For common SQL functions
882 which SQLAlchemy is aware of, the name may be interpreted as a *generic
883 function* which will be compiled appropriately to the target database:
884
885 .. sourcecode:: pycon+sql
886
887 >>> print(func.current_timestamp())
888 {printsql}CURRENT_TIMESTAMP
889
890 To call functions which are present in dot-separated packages,
891 specify them in the same manner:
892
893 .. sourcecode:: pycon+sql
894
895 >>> print(func.stats.yield_curve(5, 10))
896 {printsql}stats.yield_curve(:yield_curve_1, :yield_curve_2)
897
898 SQLAlchemy can be made aware of the return type of functions to enable
899 type-specific lexical and result-based behavior. For example, to ensure
900 that a string-based function returns a Unicode value and is similarly
901 treated as a string in expressions, specify
902 :class:`~sqlalchemy.types.Unicode` as the type:
903
904 .. sourcecode:: pycon+sql
905
906 >>> print(
907 ... func.my_string("hi", type_=Unicode)
908 ... + " "
909 ... + func.my_string("there", type_=Unicode)
910 ... )
911 {printsql}my_string(:my_string_1) || :my_string_2 || my_string(:my_string_3)
912
913 The object returned by a :data:`.func` call is usually an instance of
914 :class:`.Function`.
915 This object meets the "column" interface, including comparison and labeling
916 functions. The object can also be passed the :meth:`~.Connectable.execute`
917 method of a :class:`_engine.Connection` or :class:`_engine.Engine`,
918 where it will be

Callers 2

__getattr__Method · 0.85
functions.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected