forked from EnterpriseDB/mysql_fdw
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmysql_fdw--1.1.sql
105 lines (98 loc) · 3.45 KB
/
mysql_fdw--1.1.sql
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
/*-------------------------------------------------------------------------
*
* mysql_fdw--1.1.sql
* Foreign-data wrapper for remote MySQL servers
*
* Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
* Portions Copyright (c) 2004-2022, EnterpriseDB Corporation.
*
* IDENTIFICATION
* mysql_fdw--1.1.sql
*
*-------------------------------------------------------------------------
*/
CREATE FUNCTION mysql_fdw_handler()
RETURNS fdw_handler
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
CREATE FUNCTION mysql_fdw_validator(text[], oid)
RETURNS void
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
CREATE FOREIGN DATA WRAPPER mysql_fdw
HANDLER mysql_fdw_handler
VALIDATOR mysql_fdw_validator;
CREATE OR REPLACE FUNCTION mysql_fdw_version()
RETURNS pg_catalog.int4 STRICT
AS 'MODULE_PATHNAME' LANGUAGE C;
CREATE PROCEDURE mysql_create_or_replace_stub(func_type text, name_arg text, return_type regtype) AS $$
DECLARE
proname_raw text := split_part(name_arg, '(', 1);
proname text := ltrim(rtrim(proname_raw));
BEGIN
IF lower(func_type) = 'aggregation' OR lower(func_type) = 'aggregate' OR lower(func_type) = 'agg' OR lower(func_type) = 'a' THEN
DECLARE
proargs_raw text := right(name_arg, length(name_arg) - length(proname_raw));
proargs text := ltrim(rtrim(proargs_raw));
proargs_types text := right(left(proargs, length(proargs) - 1), length(proargs) - 2);
aggproargs text := format('(%s, %s)', return_type, proargs_types);
BEGIN
BEGIN
EXECUTE format('
CREATE FUNCTION %s_sfunc%s RETURNS %s IMMUTABLE AS $inner$
BEGIN
RAISE EXCEPTION ''stub %s_sfunc%s is called'';
RETURN NULL;
END $inner$ LANGUAGE plpgsql;',
proname, aggproargs, return_type, proname, aggproargs);
EXCEPTION
WHEN duplicate_function THEN
RAISE DEBUG 'stub function for aggregation already exists (ignored)';
END;
BEGIN
EXECUTE format('
CREATE AGGREGATE %s
(
sfunc = %s_sfunc,
stype = %s
);', name_arg, proname, return_type);
EXCEPTION
WHEN duplicate_function THEN
RAISE DEBUG 'stub aggregation already exists (ignored)';
WHEN others THEN
RAISE EXCEPTION 'stub aggregation exception';
END;
END;
ELSIF lower(func_type) = 'function' OR lower(func_type) = 'func' OR lower(func_type) = 'f' THEN
BEGIN
EXECUTE format('
CREATE FUNCTION %s RETURNS %s IMMUTABLE AS $inner$
BEGIN
RAISE EXCEPTION ''stub %s is called'';
RETURN NULL;
END $inner$ LANGUAGE plpgsql;',
name_arg, return_type, name_arg);
EXCEPTION
WHEN duplicate_function THEN
RAISE DEBUG 'stub already exists (ignored)';
END;
ELSE
RAISE EXCEPTION 'not supported function type %', func_type;
BEGIN
EXECUTE format('
CREATE FUNCTION %s_sfunc RETURNS %s AS $inner$
BEGIN
RAISE EXCEPTION ''stub %s is called'';
RETURN NULL;
END $inner$ LANGUAGE plpgsql;',
name_arg, return_type, name_arg);
EXCEPTION
WHEN duplicate_function THEN
RAISE DEBUG 'stub already exists (ignored)';
END;
END IF;
END
$$ LANGUAGE plpgsql;
call mysql_create_or_replace_stub('f', 'match_against(varidiadic text[])', 'float');
call mysql_create_or_replace_stub('f', 'log(bigint, bigint)', 'float8');
call mysql_create_or_replace_stub('f', 'log(float8, float8)', 'float8');