Исходный код СУБД 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
-- Test tied Perl values via handmade classes, so as not to require
-- Tie::Hash and Tie::Array; some popular platforms don't have those
-- in their base Perl installations.
 
DO $$
{
    package PLPerlTiedHash;
    sub TIEHASH  { bless {}, $_[0] }
    sub STORE    { $_[0]{$_[1]} = $_[2] }
    sub FETCH    { $_[0]{$_[1]} }
    sub FIRSTKEY { my $a = keys %{$_[0]}; each %{$_[0]} }
    sub NEXTKEY  { each %{$_[0]} }
 
    package PLPerlTiedArray;
    sub TIEARRAY  { bless [], $_[0] }
    sub STORE     { $_[0][$_[1]] = $_[2] }
    sub FETCH     { $_[0][$_[1]] }
    sub FETCHSIZE { scalar @{$_[0]} }
 
    package PLPerlTiedScalar;
    sub TIESCALAR { my $s; bless \$s, $_[0] }
    sub STORE     { ${$_[0]} = $_[1] }
    sub FETCH     { ${$_[0]} }
 
    package PLPerlTiedReturnNext;
    sub TIESCALAR { my $n = 0; bless \$n, $_[0] }
    sub FETCH     { main::return_next({f1 => 2, f2 => 'inner'})
                      unless ${$_[0]}++; 'outer' }
}
$$ LANGUAGE plperl;
 
CREATE FUNCTION tied_setof() RETURNS SETOF text AS $$
    my @a;
    tie @a, 'PLPerlTiedArray';
    $a[0] = 'a'; $a[1] = 'b'; $a[2] = 'c';
    return \@a;
$$ LANGUAGE plperl;
 
SELECT * FROM tied_setof();
 
CREATE FUNCTION tied_scalar() RETURNS text AS $$
    my $s;
    tie $s, 'PLPerlTiedScalar';
    $s = 'hello';
    return $s;
$$ LANGUAGE plperl;
 
SELECT tied_scalar();
 
CREATE TYPE tiedrowperl AS (f1 integer, f2 text, f3 text);
 
CREATE FUNCTION tied_composite() RETURNS tiedrowperl AS $$
    my %h;
    tie %h, 'PLPerlTiedHash';
    $h{f1} = 1; $h{f2} = 'hello'; $h{f3} = 'world';
    return \%h;
$$ LANGUAGE plperl;
 
SELECT tied_composite();
 
CREATE FUNCTION tied_int_array() RETURNS integer[] AS $$
    my @a;
    tie @a, 'PLPerlTiedArray';
    $a[0] = 1; $a[1] = 2; $a[2] = 3;
    return \@a;
$$ LANGUAGE plperl;
 
SELECT tied_int_array();
 
-- Tied scalar whose FETCH returns an arrayref.
CREATE FUNCTION tied_scalar_setof() RETURNS SETOF text AS $$
    my @a;
    tie @a, 'PLPerlTiedArray';
    $a[0] = 'x'; $a[1] = 'y';
    my $s;
    tie $s, 'PLPerlTiedScalar';
    $s = \@a;
    return $s;
$$ LANGUAGE plperl;
 
SELECT * FROM tied_scalar_setof();
 
CREATE FUNCTION tied_scalar_int_array() RETURNS integer[] AS $$
    my @a;
    tie @a, 'PLPerlTiedArray';
    $a[0] = 4; $a[1] = 5;
    my $s;
    tie $s, 'PLPerlTiedScalar';
    $s = \@a;
    return $s;
$$ LANGUAGE plperl;
 
SELECT tied_scalar_int_array();
 
-- Artificial: tied scalar whose FETCH recursively calls return_next
-- while the outer return_next is still converting the row containing it.
CREATE FUNCTION tied_reentrant_setof() RETURNS SETOF tiedrowperl AS $$
    my %h;
    $h{f1} = 1;
    tie $h{f2}, 'PLPerlTiedReturnNext';
    return_next(\%h);
    return undef;
$$ LANGUAGE plperl;
 
SELECT * FROM tied_reentrant_setof();
 
-- Artificial: $_TD->{new} and the trigger return value are tied scalars.
CREATE TABLE tied_trigger_test (i int, v text);
 
CREATE FUNCTION tied_modify() RETURNS trigger AS $$
    my %h;
    tie %h, 'PLPerlTiedHash';
    $h{i} = $_TD->{new}{i};
    $h{v} = 'from_tie';
    my $new;
    tie $new, 'PLPerlTiedScalar';
    $new = \%h;
    $_TD->{new} = $new;
    my $ret;
    tie $ret, 'PLPerlTiedScalar';
    $ret = 'MODIFY';
    return $ret;
$$ LANGUAGE plperl;
 
CREATE TRIGGER tied_modify_trig BEFORE INSERT ON tied_trigger_test
FOR EACH ROW EXECUTE PROCEDURE tied_modify();
 
INSERT INTO tied_trigger_test (i, v) VALUES (1, 'orig');
 
SELECT i, v FROM tied_trigger_test;
 
-- Verify that we can cope with errors thrown within get magic.
DO $$
{
    package PLPerlTiedHash;
    sub TIEHASH  { bless {}, $_[0] }
    sub STORE    { $_[0]{$_[1]} = $_[2] }
    sub FETCH    { die("tied hash fetch failure"); $_[0]{$_[1]} }
    sub FIRSTKEY { my $a = keys %{$_[0]}; each %{$_[0]} }
    sub NEXTKEY  { each %{$_[0]} }
 
    package PLPerlTiedArray;
    sub TIEARRAY  { bless [], $_[0] }
    sub STORE     { $_[0][$_[1]] = $_[2] }
    sub FETCH     { die("tied array fetch failure"); $_[0][$_[1]] }
    sub FETCHSIZE { scalar @{$_[0]} }
 
    package PLPerlTiedScalar;
    sub TIESCALAR { my $s; bless \$s, $_[0] }
    sub STORE     { ${$_[0]} = $_[1] }
    sub FETCH     { die("tied scalar fetch failure"); ${$_[0]} }
}
$$ LANGUAGE plperl;
 
SELECT * FROM tied_setof();
SELECT tied_scalar();
SELECT tied_composite();
SELECT tied_int_array();
SELECT * FROM tied_scalar_setof();
SELECT tied_scalar_int_array();
INSERT INTO tied_trigger_test (i, v) VALUES (2, 'orig');