From 455dffbb73f9875c39f2ab544420454168a8c68c Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 4 Dec 2008 17:51:28 +0000 Subject: Default values for function arguments Pavel Stehule, with some tweaks by Peter Eisentraut --- doc/src/sgml/func.sgml | 20 +++++++++++-- doc/src/sgml/ref/create_function.sgml | 26 ++++++++++++++-- doc/src/sgml/xfunc.sgml | 56 ++++++++++++++++++++++++++++++++++- 3 files changed, 96 insertions(+), 6 deletions(-) (limited to 'doc/src/sgml') diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 67500340b0f..23241c24697 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,4 +1,4 @@ - + Functions and Operators @@ -11710,6 +11710,10 @@ SELECT pg_type_is_visible('myschema.widget'::regtype); pg_get_function_arguments + + pg_get_function_identity_arguments + + pg_get_function_result @@ -11799,7 +11803,12 @@ SELECT pg_type_is_visible('myschema.widget'::regtype); pg_get_function_arguments(func_oid) text - get argument list for function + get argument list of function's definition (with default values) + + + pg_get_function_identity_arguments(func_oid) + text + get argument list to identify a function (without argument names, default values) pg_get_function_result(func_oid) @@ -11920,7 +11929,12 @@ SELECT pg_type_is_visible('myschema.widget'::regtype); of a function, in the form it would need to appear in within CREATE FUNCTION. pg_get_function_result similarly returns the - appropriate RETURNS clause for the function. + appropriate RETURNS clause for the function. + pg_get_function_identity_arguments returns the + argument list necessary to identify a function, in the form it + would need to appear in within ALTER FUNCTION, for + instance. This form omits default values and argument names, for + example. diff --git a/doc/src/sgml/ref/create_function.sgml b/doc/src/sgml/ref/create_function.sgml index a6af57a22bf..5de4967789d 100644 --- a/doc/src/sgml/ref/create_function.sgml +++ b/doc/src/sgml/ref/create_function.sgml @@ -1,5 +1,5 @@ @@ -21,7 +21,7 @@ $PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.81 2008/11/14 10:22 CREATE [ OR REPLACE ] FUNCTION - name ( [ [ argmode ] [ argname ] argtype [, ...] ] ) + name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } defexpr] [, ...] ] ) [ RETURNS rettype | RETURNS TABLE ( colname coltype [, ...] ) ] { LANGUAGE langname @@ -154,6 +154,20 @@ CREATE [ OR REPLACE ] FUNCTION + + defexpr + + + + An expression to be used as default value if the parameter is + not specified. The expression has to be convertable to the + argument type of the parameter. All parameters after a + parameter with default value have to be parameters with default + values as well. + + + + rettype @@ -667,6 +681,14 @@ COMMIT; either before or after argname. But only the first way is standard-compliant. + + + The SQL standard does not specify parameter defaults. The syntax + with the DEFAULT key word is from Oracle, and it + is somewhat in the spirit of the standard: SQL/PSM uses it for + variable default values. The syntax with = is + used in T-SQL and Firebird. + diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml index a5fb62e18b9..44c83e8dbc7 100644 --- a/doc/src/sgml/xfunc.sgml +++ b/doc/src/sgml/xfunc.sgml @@ -1,4 +1,4 @@ - + User-Defined Functions @@ -663,6 +663,60 @@ SELECT mleast(VARIADIC ARRAY[10, -1, 5, 4.4]); + + <acronym>SQL</> Functions with Parameters Default Values + + + default values + + + + Functions can be declared with parameters with default values or + expressions. The default expressions are used as parameter value + if the parameter is not explicitly specified in a function call. + All parameters after a a parameter with default value have to be + parameters with default values as well. + + + + For example: + +CREATE FUNCTION foo(a int DEFAULT 1, b int DEFAULT 2, c int DEFAULT 3) +RETURNS int +LANGUAGE SQL +AS $$ + SELECT $1 + $2 + $3; +$$; + +SELECT foo(10, 20, 30); + foo +----- + 60 +(1 row) + +SELECT foo(10, 20); + foo +----- + 33 +(1 row) + +SELECT foo(10); + foo +----- + 15 +(1 row) + +SELECT foo(); + foo +----- + 6 +(1 row) + + Instead of the key word DEFAULT, + the = sign can also be used. + + + <acronym>SQL</acronym> Functions as Table Sources -- cgit v1.2.3