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
| 900 | |
| 901 | |
| 902 | class _FunctionGenerator: |
| 903 | """Generate SQL function expressions. |
| 904 | |
| 905 | :data:`.func` is a special object instance which generates SQL |
| 906 | functions based on name-based attributes, e.g.: |
| 907 | |
| 908 | .. sourcecode:: pycon+sql |
| 909 | |
| 910 | >>> print(func.count(1)) |
| 911 | {printsql}count(:param_1) |
| 912 | |
| 913 | The returned object is an instance of :class:`.Function`, and is a |
| 914 | column-oriented SQL element like any other, and is used in that way: |
| 915 | |
| 916 | .. sourcecode:: pycon+sql |
| 917 | |
| 918 | >>> print(select(func.count(table.c.id))) |
| 919 | {printsql}SELECT count(sometable.id) FROM sometable |
| 920 | |
| 921 | Any name can be given to :data:`.func`. If the function name is unknown to |
| 922 | SQLAlchemy, it will be rendered exactly as is. For common SQL functions |
| 923 | which SQLAlchemy is aware of, the name may be interpreted as a *generic |
| 924 | function* which will be compiled appropriately to the target database: |
| 925 | |
| 926 | .. sourcecode:: pycon+sql |
| 927 | |
| 928 | >>> print(func.current_timestamp()) |
| 929 | {printsql}CURRENT_TIMESTAMP |
| 930 | |
| 931 | To call functions which are present in dot-separated packages, |
| 932 | specify them in the same manner: |
| 933 | |
| 934 | .. sourcecode:: pycon+sql |
| 935 | |
| 936 | >>> print(func.stats.yield_curve(5, 10)) |
| 937 | {printsql}stats.yield_curve(:yield_curve_1, :yield_curve_2) |
| 938 | |
| 939 | SQLAlchemy can be made aware of the return type of functions to enable |
| 940 | type-specific lexical and result-based behavior. For example, to ensure |
| 941 | that a string-based function returns a Unicode value and is similarly |
| 942 | treated as a string in expressions, specify |
| 943 | :class:`~sqlalchemy.types.Unicode` as the type: |
| 944 | |
| 945 | .. sourcecode:: pycon+sql |
| 946 | |
| 947 | >>> print( |
| 948 | ... func.my_string("hi", type_=Unicode) |
| 949 | ... + " " |
| 950 | ... + func.my_string("there", type_=Unicode) |
| 951 | ... ) |
| 952 | {printsql}my_string(:my_string_1) || :my_string_2 || my_string(:my_string_3) |
| 953 | |
| 954 | The object returned by a :data:`.func` call is usually an instance of |
| 955 | :class:`.Function`. |
| 956 | This object meets the "column" interface, including comparison and labeling |
| 957 | functions. The object can also be passed the :meth:`~.Connectable.execute` |
| 958 | method of a :class:`_engine.Connection` or :class:`_engine.Engine`, |
| 959 | where it will be |
no outgoing calls
no test coverage detected