File: 404_create_function_pg_examples.sql

package info (click to toggle)
sqlfmt 0.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,580 kB
  • sloc: python: 10,007; sql: 5,626; makefile: 39
file content (100 lines) | stat: -rw-r--r-- 2,254 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- COPYRIGHT POSTGRESQL
-- SEE https://www.postgresql.org/docs/15/sql-createfunction.html

CREATE FUNCTION add(integer, integer) RETURNS integer
    AS 'select $1 + $2;'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;

CREATE FUNCTION add(a integer, b integer) RETURNS integer
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT
    RETURN a + b;

CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$
        BEGIN
                RETURN i + 1;
        END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION dup(in int, out f1 int, out f2 text)
    AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$
    LANGUAGE SQL;

CREATE FUNCTION dup(int) RETURNS dup_result
    AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$
    LANGUAGE SQL;

CREATE FUNCTION check_password(uname TEXT, pass TEXT)
RETURNS BOOLEAN AS $$
DECLARE passed BOOLEAN;
BEGIN
        SELECT  (pwd = $2) INTO passed
        FROM    pwds
        WHERE   username = $1;

        RETURN passed;
END;
$$  LANGUAGE plpgsql
    SECURITY DEFINER
    -- Set a secure search_path: trusted schema(s), then 'pg_temp'.
    SET search_path = admin, pg_temp;
)))))__SQLFMT_OUTPUT__(((((
-- COPYRIGHT POSTGRESQL
-- SEE https://www.postgresql.org/docs/15/sql-createfunction.html
create function add(integer, integer)
returns integer
as 'select $1 + $2;'
language sql
immutable
returns null on null input
;

create function add(a integer, b integer)
returns integer
language sql
immutable
returns null on null input
return a + b
;

create or replace function increment(i integer)
returns integer
as $$
        BEGIN
                RETURN i + 1;
        END;
$$
language plpgsql
;

create function dup(in int, out f1 int, out f2 text)
as $$ SELECT $1, CAST($1 AS text) || ' is text' $$
language sql
;

create function dup(int)
returns dup_result
as $$ SELECT $1, CAST($1 AS text) || ' is text' $$
language sql
;

create function check_password(uname text, pass text)
returns boolean
as $$
DECLARE passed BOOLEAN;
BEGIN
        SELECT  (pwd = $2) INTO passed
        FROM    pwds
        WHERE   username = $1;

        RETURN passed;
END;
$$
language plpgsql
security definer
-- Set a secure search_path: trusted schema(s), then 'pg_temp'.
set search_path = admin, pg_temp
;