| 1 |
create table T_comp1 ( |
| 2 |
tkey char(10), |
| 3 |
ref1 int4, |
| 4 |
ref2 char(20) |
| 5 |
); |
| 6 |
create function tcl_composite_arg_ref1(T_comp1) returns int as ' |
| 7 |
return $1(ref1) |
| 8 |
' language pltcl; |
| 9 |
create function tcl_composite_arg_ref2(T_comp1) returns text as ' |
| 10 |
return $1(ref2) |
| 11 |
' language pltcl; |
| 12 |
create function tcl_argisnull(text) returns bool as ' |
| 13 |
argisnull 1 |
| 14 |
' language pltcl; |
| 15 |
create function tcl_int4add(int4,int4) returns int4 as ' |
| 16 |
return [expr $1 + $2] |
| 17 |
' language pltcl; |
| 18 |
-- We use split(n) as a quick-and-dirty way of parsing the input array |
| 19 |
-- value, which comes in as a string like '{1,2}'. There are better ways... |
| 20 |
create function tcl_int4_accum(int4[], int4) returns int4[] as ' |
| 21 |
set state [split $1 "{,}"] |
| 22 |
set newsum [expr {[lindex $state 1] + $2}] |
| 23 |
set newcnt [expr {[lindex $state 2] + 1}] |
| 24 |
return "{$newsum,$newcnt}" |
| 25 |
' language pltcl; |
| 26 |
create function tcl_int4_avg(int4[]) returns int4 as ' |
| 27 |
set state [split $1 "{,}"] |
| 28 |
if {[lindex $state 2] == 0} { return_null } |
| 29 |
return [expr {[lindex $state 1] / [lindex $state 2]}] |
| 30 |
' language pltcl; |
| 31 |
create aggregate tcl_avg ( |
| 32 |
sfunc = tcl_int4_accum, |
| 33 |
basetype = int4, |
| 34 |
stype = int4[], |
| 35 |
finalfunc = tcl_int4_avg, |
| 36 |
initcond = '{0,0}' |
| 37 |
); |
| 38 |
create aggregate tcl_sum ( |
| 39 |
sfunc = tcl_int4add, |
| 40 |
basetype = int4, |
| 41 |
stype = int4, |
| 42 |
initcond1 = 0 |
| 43 |
); |
| 44 |
create function tcl_int4lt(int4,int4) returns bool as ' |
| 45 |
if {$1 < $2} { |
| 46 |
return t |
| 47 |
} |
| 48 |
return f |
| 49 |
' language pltcl; |
| 50 |
create function tcl_int4le(int4,int4) returns bool as ' |
| 51 |
if {$1 <= $2} { |
| 52 |
return t |
| 53 |
} |
| 54 |
return f |
| 55 |
' language pltcl; |
| 56 |
create function tcl_int4eq(int4,int4) returns bool as ' |
| 57 |
if {$1 == $2} { |
| 58 |
return t |
| 59 |
} |
| 60 |
return f |
| 61 |
' language pltcl; |
| 62 |
create function tcl_int4ge(int4,int4) returns bool as ' |
| 63 |
if {$1 >= $2} { |
| 64 |
return t |
| 65 |
} |
| 66 |
return f |
| 67 |
' language pltcl; |
| 68 |
create function tcl_int4gt(int4,int4) returns bool as ' |
| 69 |
if {$1 > $2} { |
| 70 |
return t |
| 71 |
} |
| 72 |
return f |
| 73 |
' language pltcl; |
| 74 |
create operator @< ( |
| 75 |
leftarg = int4, |
| 76 |
rightarg = int4, |
| 77 |
procedure = tcl_int4lt |
| 78 |
); |
| 79 |
create operator @<= ( |
| 80 |
leftarg = int4, |
| 81 |
rightarg = int4, |
| 82 |
procedure = tcl_int4le |
| 83 |
); |
| 84 |
create operator @= ( |
| 85 |
leftarg = int4, |
| 86 |
rightarg = int4, |
| 87 |
procedure = tcl_int4eq |
| 88 |
); |
| 89 |
create operator @>= ( |
| 90 |
leftarg = int4, |
| 91 |
rightarg = int4, |
| 92 |
procedure = tcl_int4ge |
| 93 |
); |
| 94 |
create operator @> ( |
| 95 |
leftarg = int4, |
| 96 |
rightarg = int4, |
| 97 |
procedure = tcl_int4gt |
| 98 |
); |
| 99 |
create function tcl_int4cmp(int4,int4) returns int4 as ' |
| 100 |
if {$1 < $2} { |
| 101 |
return -1 |
| 102 |
} |
| 103 |
if {$1 > $2} { |
| 104 |
return 1 |
| 105 |
} |
| 106 |
return 0 |
| 107 |
' language pltcl; |
| 108 |
CREATE OPERATOR CLASS tcl_int4_ops |
| 109 |
FOR TYPE int4 USING btree AS |
| 110 |
OPERATOR 1 @<, |
| 111 |
OPERATOR 2 @<=, |
| 112 |
OPERATOR 3 @=, |
| 113 |
OPERATOR 4 @>=, |
| 114 |
OPERATOR 5 @>, |
| 115 |
FUNCTION 1 tcl_int4cmp(int4,int4) ; |
| 116 |
-- |
| 117 |
-- Test usage of Tcl's "clock" command. In recent Tcl versions this |
| 118 |
-- command fails without working "unknown" support, so it's a good canary |
| 119 |
-- for initialization problems. |
| 120 |
-- |
| 121 |
create function tcl_date_week(int4,int4,int4) returns text as $$ |
| 122 |
return [clock format [clock scan "$2/$3/$1" -gmt 1] -format "%U" -gmt 1] |
| 123 |
$$ language pltcl immutable; |
| 124 |
select tcl_date_week(2010,1,26); |
| 125 |
tcl_date_week |
| 126 |
--------------- |
| 127 |
04 |
| 128 |
(1 row) |
| 129 |
|
| 130 |
select tcl_date_week(2001,10,24); |
| 131 |
tcl_date_week |
| 132 |
--------------- |
| 133 |
42 |
| 134 |
(1 row) |
| 135 |
|
| 136 |
-- test pltcl event triggers |
| 137 |
create function tclsnitch() returns event_trigger language pltcl as $$ |
| 138 |
elog NOTICE "tclsnitch: $TG_event $TG_tag" |
| 139 |
$$; |
| 140 |
create event trigger tcl_a_snitch on ddl_command_start execute procedure tclsnitch(); |
| 141 |
create event trigger tcl_b_snitch on ddl_command_end execute procedure tclsnitch(); |
| 142 |
create function foobar() returns int language sql as $$select 1;$$; |
| 143 |
NOTICE: tclsnitch: ddl_command_start CREATE FUNCTION |
| 144 |
NOTICE: tclsnitch: ddl_command_end CREATE FUNCTION |
| 145 |
alter function foobar() cost 77; |
| 146 |
NOTICE: tclsnitch: ddl_command_start ALTER FUNCTION |
| 147 |
NOTICE: tclsnitch: ddl_command_end ALTER FUNCTION |
| 148 |
drop function foobar(); |
| 149 |
NOTICE: tclsnitch: ddl_command_start DROP FUNCTION |
| 150 |
NOTICE: tclsnitch: ddl_command_end DROP FUNCTION |
| 151 |
create table foo(); |
| 152 |
NOTICE: tclsnitch: ddl_command_start CREATE TABLE |
| 153 |
NOTICE: tclsnitch: ddl_command_end CREATE TABLE |
| 154 |
drop table foo; |
| 155 |
NOTICE: tclsnitch: ddl_command_start DROP TABLE |
| 156 |
NOTICE: tclsnitch: ddl_command_end DROP TABLE |
| 157 |
drop event trigger tcl_a_snitch; |
| 158 |
drop event trigger tcl_b_snitch; |
| 159 |
create function tcl_test_cube_squared(in int, out squared int, out cubed int) as $$ |
| 160 |
return [list squared [expr {$1 * $1}] cubed [expr {$1 * $1 * $1}]] |
| 161 |
$$ language pltcl; |
| 162 |
create function tcl_test_squared_rows(int,int) returns table (x int, y int) as $$ |
| 163 |
for {set i $1} {$i < $2} {incr i} { |
| 164 |
return_next [list y [expr {$i * $i}] x $i] |
| 165 |
} |
| 166 |
$$ language pltcl; |
| 167 |
create function tcl_test_sequence(int,int) returns setof int as $$ |
| 168 |
for {set i $1} {$i < $2} {incr i} { |
| 169 |
return_next $i |
| 170 |
} |
| 171 |
$$ language pltcl; |
| 172 |
create function tcl_eval(string text) returns text as $$ |
| 173 |
eval $1 |
| 174 |
$$ language pltcl; |
| 175 |
-- test use of errorCode in error handling |
| 176 |
create function tcl_error_handling_test(text) returns text |
| 177 |
language pltcl |
| 178 |
as $function$ |
| 179 |
if {[catch $1 err]} { |
| 180 |
# If not a Postgres error, just return the basic error message |
| 181 |
if {[lindex $::errorCode 0] != "POSTGRES"} { |
| 182 |
return $err |
| 183 |
} |
| 184 |
|
| 185 |
# Get rid of keys that can't be expected to remain constant |
| 186 |
array set myArray $::errorCode |
| 187 |
unset myArray(POSTGRES) |
| 188 |
unset -nocomplain myArray(funcname) |
| 189 |
unset -nocomplain myArray(filename) |
| 190 |
unset -nocomplain myArray(lineno) |
| 191 |
|
| 192 |
# Format into something nicer |
| 193 |
set vals [] |
| 194 |
foreach {key} [lsort [array names myArray]] { |
| 195 |
set value [string map {"\n" "\n\t"} $myArray($key)] |
| 196 |
lappend vals "$key: $value" |
| 197 |
} |
| 198 |
return [join $vals "\n"] |
| 199 |
} else { |
| 200 |
return "no error" |
| 201 |
} |
| 202 |
$function$; |
| 203 |
-- test spi_exec and spi_execp with -array |
| 204 |
create function tcl_spi_exec( |
| 205 |
prepare boolean, |
| 206 |
action text |
| 207 |
) |
| 208 |
returns void language pltcl AS $function$ |
| 209 |
set query "select * from (values (1,'foo'),(2,'bar'),(3,'baz')) v(col1,col2)" |
| 210 |
if {$1 == "t"} { |
| 211 |
set prep [spi_prepare $query {}] |
| 212 |
spi_execp -array A $prep { |
| 213 |
elog NOTICE "col1 $A(col1), col2 $A(col2)" |
| 214 |
|
| 215 |
switch $A(col1) { |
| 216 |
2 { |
| 217 |
elog NOTICE "action: $2" |
| 218 |
switch $2 { |
| 219 |
break { |
| 220 |
break |
| 221 |
} |
| 222 |
continue { |
| 223 |
continue |
| 224 |
} |
| 225 |
return { |
| 226 |
return |
| 227 |
} |
| 228 |
error { |
| 229 |
error "error message" |
| 230 |
} |
| 231 |
} |
| 232 |
error "should not get here" |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} else { |
| 237 |
spi_exec -array A $query { |
| 238 |
elog NOTICE "col1 $A(col1), col2 $A(col2)" |
| 239 |
|
| 240 |
switch $A(col1) { |
| 241 |
2 { |
| 242 |
elog NOTICE "action: $2" |
| 243 |
switch $2 { |
| 244 |
break { |
| 245 |
break |
| 246 |
} |
| 247 |
continue { |
| 248 |
continue |
| 249 |
} |
| 250 |
return { |
| 251 |
return |
| 252 |
} |
| 253 |
error { |
| 254 |
error "error message" |
| 255 |
} |
| 256 |
} |
| 257 |
error "should not get here" |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
elog NOTICE "end of function" |
| 263 |
$function$; |