-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbase.sql
More file actions
178 lines (149 loc) · 4.43 KB
/
Copy pathbase.sql
File metadata and controls
178 lines (149 loc) · 4.43 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
--
-- Base functionality.
--
-- Basic things: scalars and arrays.
CREATE FUNCTION test_void() RETURNS integer
LANGUAGE plphp AS $$
return;
$$;
SELECT test_void();
CREATE FUNCTION test_an_int() RETURNS integer
LANGUAGE plphp AS $$
return 1;
$$;
SELECT test_an_int();
SELECT * FROM test_an_int();
CREATE FUNCTION test_an_array() RETURNS int[]
LANGUAGE plphp AS $$
return array(1);
$$;
SELECT test_an_array();
SELECT * FROM test_an_array();
CREATE FUNCTION test_a_bogus_array() RETURNS int[]
LANGUAGE plphp AS $$
return 1;
$$;
SELECT test_a_bogus_array();
SELECT * FROM test_a_bogus_array();
CREATE FUNCTION test_a_bogus_int() RETURNS integer
LANGUAGE plphp AS $$
return array(1);
$$;
SELECT test_a_bogus_int();
SELECT * FROM test_a_bogus_int();
CREATE FUNCTION test_ndim_array(int, int) RETURNS int[]
LANGUAGE plphp AS $$
if (!function_exists('bar')) {
function bar($a, $b) {
if ($a == 1) {
return array($b, $b+1);
}
return array(bar($a-1, $b), bar($a-1, $b+1));
}
}
$return = bar($args[0], $args[1]);
return $return;
$$;
SELECT test_ndim_array(1, 1);
SELECT test_ndim_array(2, 1);
SELECT test_ndim_array(3, 1);
SELECT test_ndim_array(4, 1);
SELECT test_ndim_array(5, 1);
SELECT test_ndim_array(6, 1);
SELECT test_ndim_array(7, 1);
CREATE FUNCTION php_max (integer, integer) RETURNS integer
STRICT LANGUAGE plphp AS $$
if ($args[0] > $args[1]) {
return $args[0];
} else return $args[1];
$$;
CREATE FUNCTION php_max_null (integer, integer) RETURNS integer
CALLED ON NULL INPUT LANGUAGE plphp AS $$
if (!isset($args[0])) {
if (!isset($args[1])) {
return;
}
return $args[1];
} else if (!isset($args[1])) {
return $args[0];
}
if ($args[0] > $args[1]) {
return $args[0];
} else return $args[1];
$$;
SELECT php_max(1, 2);
SELECT php_max(2, 1);
SELECT php_max(-1, -999999999);
SELECT php_max(0, 0);
SELECT php_max(NULL, 0);
SELECT php_max(0, NULL);
SELECT php_max(NULL, NULL);
SELECT php_max_null(NULL, 0);
SELECT php_max_null(0, NULL);
SELECT php_max_null(NULL, NULL);
SELECT php_max_null(1, -2);
CREATE FUNCTION php_str_max (text, text) RETURNS text
STRICT LANGUAGE plphp AS $$
if ($args[0] > $args[1]) {
return $args[0];
}
return $args[1];
$$;
SELECT php_str_max('foo', 'bar');
SELECT php_str_max($$After the presentation, we headed down to the restaurant in the building for and evening reception with beer, buffet and even a little bingo. Lot's of business cards were exchanged with a variety of PostgreSQL users and developers, and even one of the Firebird team!!$$, ' foo');
SELECT php_str_max('', NULL);
CREATE FUNCTION php_substr(text, int, int) RETURNS text
STRICT LANGUAGE plphp AS $$
return substr($args[0], $args[1], $args[2]);
$$;
CREATE FUNCTION php_concat(text, text) RETURNS text
STRICT LANGUAGE plphp AS $$
return $args[0] . $args[1];
$$;
CREATE FUNCTION php_lc(text) RETURNS text
STRICT LANGUAGE plphp AS $$
return strtolower($args[0]);
$$;
SELECT php_concat(
php_substr($$On Saturday, we spent a day sight-seeing, mainly in Kamakura where we visted a couple of temples and a very large cast iron Buddha. Lunch was Korean barbeque, following which we took a trip back to Tokyo station on the Shinkansen, or Bullet Train.$$, 16, 25),
php_lc($$A few beers in the 'Victorian Pub'$$)
);
CREATE FUNCTION php_arr_in(integer[], integer, integer)
returns integer language plphp AS $$
return $args[0][$args[1]][$args[2]];
$$;
SELECT php_arr_in(ARRAY[[1, 10], [2, 4], [3, 5]], 0, 0);
SELECT php_arr_in(ARRAY[[1, 10], [2, 4], [3, 5]], 1, 1);
SELECT php_arr_in(ARRAY[[1, 10], [2, 4], [3, 5]], 2, 1);
SELECT php_arr_in(ARRAY[[1, 10], [2, 4], [3, 5]], 1, 2);
CREATE FUNCTION php_array() RETURNS integer[] AS $$
$ret1 = array(1, 3, 5);
$ret2 = array(2, 4, 6);
return array($ret1, $ret2);
$$ language plphp;
select php_array();
create function foo() returns record language plphp as $$
return 1;
$$;
select foo();
select * FROM foo() as (a int);
create or replace function foo(anyelement) returns anyarray
language plphp as $$
return 1;
$$;
select foo(1);
-- test recursive functions
create function php_fib(int) returns int language plphp as $$
if ($args[0] <= 1) { return 1; }
$r = spi_exec("select php_fib({$args[0]} - 1) as a");
$row = spi_fetch_row($r);
$a = $row['a'];
$r = spi_exec("select php_fib({$args[0]} - 2) as b");
$row = spi_fetch_row($r);
$b = $row['b'];
return $a + $b;
$$;
select php_fib(1);
select php_fib(3);
select php_fib(5);
select php_fib(7);