Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
--
-- check static and global data (SD and GD)
--
CREATE FUNCTION global_test_one() returns text
    AS
'if "global_test" not in SD:
    SD["global_test"] = "set by global_test_one"
if "global_test" not in GD:
    GD["global_test"] = "set by global_test_one"
return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
    LANGUAGE plpython3u;
CREATE FUNCTION global_test_two() returns text
    AS
'if "global_test" not in SD:
    SD["global_test"] = "set by global_test_two"
if "global_test" not in GD:
    GD["global_test"] = "set by global_test_two"
return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
    LANGUAGE plpython3u;
CREATE FUNCTION static_test() returns int4
    AS
'if "call" in SD:
    SD["call"] = SD["call"] + 1
else:
    SD["call"] = 1
return SD["call"]
'
    LANGUAGE plpython3u;
SELECT static_test();
 static_test 
-------------
           1
(1 row)
 
SELECT static_test();
 static_test 
-------------
           2
(1 row)
 
SELECT global_test_one();
                    global_test_one                     
--------------------------------------------------------
 SD: set by global_test_one, GD: set by global_test_one
(1 row)
 
SELECT global_test_two();
                    global_test_two                     
--------------------------------------------------------
 SD: set by global_test_two, GD: set by global_test_one
(1 row)
 
-- SD is shared by all calls of a function, whatever the input collation,
-- and survives CREATE OR REPLACE FUNCTION
CREATE FUNCTION static_test_coll(t text) RETURNS int4
AS $$
SD["n"] = SD.get("n", 0) + 1
return SD["n"]
$$ LANGUAGE plpython3u;
SELECT static_test_coll('a'), static_test_coll('a' COLLATE "C");
 static_test_coll | static_test_coll 
------------------+------------------
                1 |                2
(1 row)
 
CREATE OR REPLACE FUNCTION static_test_coll(t text) RETURNS int4
AS $$
SD["n"] = SD.get("n", 0) + 1
return SD["n"]
$$ LANGUAGE plpython3u;
SELECT static_test_coll('a');
 static_test_coll 
------------------
                3
(1 row)