-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodules.sql
More file actions
52 lines (43 loc) · 1.83 KB
/
Copy pathmodules.sql
File metadata and controls
52 lines (43 loc) · 1.83 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
--
-- Module autoloading (plphp_modules) and the plphp.start_proc GUC
-- (ported from PL/Tcl).
--
-- Creating any plphp function runs the validator, which loads the shared
-- library so the plphp.start_proc GUC becomes available.
CREATE FUNCTION _load() RETURNS void LANGUAGE plphp AS $$ return; $$;
-- A start proc, run once when the interpreter initializes this session.
CREATE FUNCTION my_start() RETURNS void LANGUAGE plphp AS $$
pg_raise('notice', 'start_proc ran');
$$;
SET plphp.start_proc = 'my_start';
-- An on_init snippet, run just before the modules are loaded.
SET plphp.on_init = 'function oninit_greet($who) { return "hello, $who"; }';
-- Two module rows (loaded in modseq order): one defines a function, the other
-- a class -- both must become available session-wide.
CREATE TABLE plphp_modules (modname text, modseq int, modsrc text);
INSERT INTO plphp_modules VALUES
('util', 0, 'function plphp_triple($n) { return $n * 3; }'),
('util', 1, 'class PlphpGreeter { public static function hi($who) { return "hi $who"; } }');
-- The first PL/php *call* in this session triggers module loading and the
-- start proc, then runs the body using the module-defined helper.
CREATE FUNCTION use_mod(int) RETURNS int LANGUAGE plphp AS $$
return plphp_triple($args[0]);
$$;
SELECT use_mod(14);
-- A class defined by a module is usable too.
CREATE FUNCTION use_class(text) RETURNS text LANGUAGE plphp AS $$
return PlphpGreeter::hi($args[0]);
$$;
SELECT use_class('bob');
-- A helper defined by plphp.on_init is available session-wide.
CREATE FUNCTION use_on_init() RETURNS text LANGUAGE plphp AS $$
return oninit_greet('world');
$$;
SELECT use_on_init();
RESET plphp.on_init;
DROP TABLE plphp_modules;
DROP FUNCTION use_on_init();
DROP FUNCTION use_class(text);
DROP FUNCTION use_mod(int);
DROP FUNCTION my_start();
DROP FUNCTION _load();