|
| 1 | +/* plx 1.0: registers the plx dialect languages and the string-builder type */ |
| 2 | + |
| 3 | +\echo Use "CREATE EXTENSION plx" to load this file. \quit |
| 4 | + |
| 5 | +/* |
| 6 | + * plx_strbuild: an expanded-object string builder. Its flattened form is a |
| 7 | + * text-compatible varlena, so it casts to and from text without a function. |
| 8 | + * plx_sb_append grows the buffer in place across a loop (amortized O(1)), |
| 9 | + * avoiding the quadratic cost of `s := s || 'x'` on plain text. |
| 10 | + */ |
| 11 | +CREATE TYPE plx_strbuild; |
| 12 | + |
| 13 | +CREATE FUNCTION plx_sb_in(cstring) RETURNS plx_strbuild |
| 14 | + AS '$libdir/plx', 'plx_sb_in' LANGUAGE C IMMUTABLE STRICT; |
| 15 | +CREATE FUNCTION plx_sb_out(plx_strbuild) RETURNS cstring |
| 16 | + AS '$libdir/plx', 'plx_sb_out' LANGUAGE C IMMUTABLE STRICT; |
| 17 | + |
| 18 | +CREATE TYPE plx_strbuild ( |
| 19 | + INPUT = plx_sb_in, |
| 20 | + OUTPUT = plx_sb_out, |
| 21 | + LIKE = text, |
| 22 | + STORAGE = extended |
| 23 | +); |
| 24 | + |
| 25 | +CREATE FUNCTION plx_sb_append_support(internal) RETURNS internal |
| 26 | + AS '$libdir/plx', 'plx_sb_append_support' LANGUAGE C; |
| 27 | + |
| 28 | +CREATE FUNCTION plx_sb_append(plx_strbuild, text) RETURNS plx_strbuild |
| 29 | + AS '$libdir/plx', 'plx_sb_append' LANGUAGE C IMMUTABLE |
| 30 | + SUPPORT plx_sb_append_support; |
| 31 | + |
| 32 | +-- plx_strbuild is implicitly usable as text (so it works in RETURN, string |
| 33 | +-- functions, and concatenation). text becomes a builder only in assignment |
| 34 | +-- context, which avoids operator ambiguity such as `builder || text`. |
| 35 | +CREATE CAST (plx_strbuild AS text) WITHOUT FUNCTION AS IMPLICIT; |
| 36 | +CREATE CAST (text AS plx_strbuild) WITHOUT FUNCTION AS ASSIGNMENT; |
| 37 | + |
| 38 | +/* |
| 39 | + * Runtime call handler = plpgsql's OWN handler. Binding a fresh pg_proc row to |
| 40 | + * plpgsql.so's exported symbol is legal (CreateProceduralLanguage only checks |
| 41 | + * the handler RETURNS language_handler). Execution is therefore 100% stock |
| 42 | + * plpgsql; plx contributes zero call-path C. |
| 43 | + */ |
| 44 | +CREATE FUNCTION plx_call_handler() |
| 45 | + RETURNS language_handler |
| 46 | + AS '$libdir/plpgsql', 'plpgsql_call_handler' |
| 47 | + LANGUAGE C; |
| 48 | + |
| 49 | +/* Ruby dialect validator + inline handler live in plx.so */ |
| 50 | +CREATE FUNCTION plx_ruby_validator(oid) |
| 51 | + RETURNS void |
| 52 | + AS '$libdir/plx', 'plx_ruby_validator' |
| 53 | + LANGUAGE C STRICT; |
| 54 | + |
| 55 | +CREATE FUNCTION plx_ruby_inline_handler(internal) |
| 56 | + RETURNS void |
| 57 | + AS '$libdir/plx', 'plx_ruby_inline_handler' |
| 58 | + LANGUAGE C; |
| 59 | + |
| 60 | +CREATE TRUSTED LANGUAGE plxruby |
| 61 | + HANDLER plx_call_handler |
| 62 | + INLINE plx_ruby_inline_handler |
| 63 | + VALIDATOR plx_ruby_validator; |
| 64 | + |
| 65 | +COMMENT ON LANGUAGE plxruby IS 'plx Ruby dialect (transpiles to plpgsql)'; |
| 66 | + |
| 67 | +/* PHP dialect */ |
| 68 | +CREATE FUNCTION plx_php_validator(oid) |
| 69 | + RETURNS void |
| 70 | + AS '$libdir/plx', 'plx_php_validator' |
| 71 | + LANGUAGE C STRICT; |
| 72 | + |
| 73 | +CREATE FUNCTION plx_php_inline_handler(internal) |
| 74 | + RETURNS void |
| 75 | + AS '$libdir/plx', 'plx_php_inline_handler' |
| 76 | + LANGUAGE C; |
| 77 | + |
| 78 | +CREATE TRUSTED LANGUAGE plxphp |
| 79 | + HANDLER plx_call_handler |
| 80 | + INLINE plx_php_inline_handler |
| 81 | + VALIDATOR plx_php_validator; |
| 82 | + |
| 83 | +COMMENT ON LANGUAGE plxphp IS 'plx PHP dialect (transpiles to plpgsql)'; |
| 84 | + |
| 85 | +/* JavaScript dialect */ |
| 86 | +CREATE FUNCTION plx_js_validator(oid) |
| 87 | + RETURNS void |
| 88 | + AS '$libdir/plx', 'plx_js_validator' |
| 89 | + LANGUAGE C STRICT; |
| 90 | + |
| 91 | +CREATE FUNCTION plx_js_inline_handler(internal) |
| 92 | + RETURNS void |
| 93 | + AS '$libdir/plx', 'plx_js_inline_handler' |
| 94 | + LANGUAGE C; |
| 95 | + |
| 96 | +CREATE TRUSTED LANGUAGE plxjs |
| 97 | + HANDLER plx_call_handler |
| 98 | + INLINE plx_js_inline_handler |
| 99 | + VALIDATOR plx_js_validator; |
| 100 | + |
| 101 | +COMMENT ON LANGUAGE plxjs IS 'plx JavaScript dialect (transpiles to plpgsql)'; |
| 102 | + |
| 103 | +/* Python dialect */ |
| 104 | +CREATE FUNCTION plx_py_validator(oid) |
| 105 | + RETURNS void |
| 106 | + AS '$libdir/plx', 'plx_py_validator' |
| 107 | + LANGUAGE C STRICT; |
| 108 | + |
| 109 | +CREATE FUNCTION plx_py_inline_handler(internal) |
| 110 | + RETURNS void |
| 111 | + AS '$libdir/plx', 'plx_py_inline_handler' |
| 112 | + LANGUAGE C; |
| 113 | + |
| 114 | +CREATE TRUSTED LANGUAGE plxpython3 |
| 115 | + HANDLER plx_call_handler |
| 116 | + INLINE plx_py_inline_handler |
| 117 | + VALIDATOR plx_py_validator; |
| 118 | + |
| 119 | +COMMENT ON LANGUAGE plxpython3 IS 'plx Python dialect (transpiles to plpgsql)'; |
| 120 | + |
| 121 | +/* COBOL dialect (ISO/IEC 1989:2023) validator + inline handler live in plx.so */ |
| 122 | +CREATE FUNCTION plx_cob_validator(oid) |
| 123 | + RETURNS void |
| 124 | + AS '$libdir/plx', 'plx_cob_validator' |
| 125 | + LANGUAGE C STRICT; |
| 126 | + |
| 127 | +CREATE FUNCTION plx_cob_inline_handler(internal) |
| 128 | + RETURNS void |
| 129 | + AS '$libdir/plx', 'plx_cob_inline_handler' |
| 130 | + LANGUAGE C; |
| 131 | + |
| 132 | +CREATE TRUSTED LANGUAGE plxcobol |
| 133 | + HANDLER plx_call_handler |
| 134 | + INLINE plx_cob_inline_handler |
| 135 | + VALIDATOR plx_cob_validator; |
| 136 | + |
| 137 | +COMMENT ON LANGUAGE plxcobol IS 'plx COBOL dialect (ISO/IEC 1989:2023, transpiles to plpgsql)'; |
| 138 | + |
| 139 | +/* PL/SQL dialect (Oracle) validator + inline handler live in plx.so */ |
| 140 | +CREATE FUNCTION plx_plsql_validator(oid) |
| 141 | + RETURNS void |
| 142 | + AS '$libdir/plx', 'plx_plsql_validator' |
| 143 | + LANGUAGE C STRICT; |
| 144 | + |
| 145 | +CREATE FUNCTION plx_plsql_inline_handler(internal) |
| 146 | + RETURNS void |
| 147 | + AS '$libdir/plx', 'plx_plsql_inline_handler' |
| 148 | + LANGUAGE C; |
| 149 | + |
| 150 | +CREATE TRUSTED LANGUAGE plxplsql |
| 151 | + HANDLER plx_call_handler |
| 152 | + INLINE plx_plsql_inline_handler |
| 153 | + VALIDATOR plx_plsql_validator; |
| 154 | + |
| 155 | +COMMENT ON LANGUAGE plxplsql IS 'plx Oracle PL/SQL dialect (transpiles to plpgsql)'; |
| 156 | + |
| 157 | +/* TypeScript dialect validator + inline handler live in plx.so */ |
| 158 | +CREATE FUNCTION plx_ts_validator(oid) |
| 159 | + RETURNS void |
| 160 | + AS '$libdir/plx', 'plx_ts_validator' |
| 161 | + LANGUAGE C STRICT; |
| 162 | + |
| 163 | +CREATE FUNCTION plx_ts_inline_handler(internal) |
| 164 | + RETURNS void |
| 165 | + AS '$libdir/plx', 'plx_ts_inline_handler' |
| 166 | + LANGUAGE C; |
| 167 | + |
| 168 | +CREATE TRUSTED LANGUAGE plxts |
| 169 | + HANDLER plx_call_handler |
| 170 | + INLINE plx_ts_inline_handler |
| 171 | + VALIDATOR plx_ts_validator; |
| 172 | + |
| 173 | +COMMENT ON LANGUAGE plxts IS 'plx TypeScript dialect (transpiles to plpgsql)'; |
| 174 | + |
| 175 | +/* T-SQL dialect (Transact-SQL) validator + inline handler live in plx.so */ |
| 176 | +CREATE FUNCTION plx_tsql_validator(oid) |
| 177 | + RETURNS void |
| 178 | + AS '$libdir/plx', 'plx_tsql_validator' |
| 179 | + LANGUAGE C STRICT; |
| 180 | + |
| 181 | +CREATE FUNCTION plx_tsql_inline_handler(internal) |
| 182 | + RETURNS void |
| 183 | + AS '$libdir/plx', 'plx_tsql_inline_handler' |
| 184 | + LANGUAGE C; |
| 185 | + |
| 186 | +CREATE TRUSTED LANGUAGE plxtsql |
| 187 | + HANDLER plx_call_handler |
| 188 | + INLINE plx_tsql_inline_handler |
| 189 | + VALIDATOR plx_tsql_validator; |
| 190 | + |
| 191 | +COMMENT ON LANGUAGE plxtsql IS 'plx Transact-SQL (SQL Server) dialect (transpiles to plpgsql)'; |
| 192 | + |
| 193 | +/* Go dialect validator + inline handler live in plx.so */ |
| 194 | +CREATE FUNCTION plx_go_validator(oid) |
| 195 | + RETURNS void |
| 196 | + AS '$libdir/plx', 'plx_go_validator' |
| 197 | + LANGUAGE C STRICT; |
| 198 | + |
| 199 | +CREATE FUNCTION plx_go_inline_handler(internal) |
| 200 | + RETURNS void |
| 201 | + AS '$libdir/plx', 'plx_go_inline_handler' |
| 202 | + LANGUAGE C; |
| 203 | + |
| 204 | +CREATE TRUSTED LANGUAGE plxgo |
| 205 | + HANDLER plx_call_handler |
| 206 | + INLINE plx_go_inline_handler |
| 207 | + VALIDATOR plx_go_validator; |
| 208 | + |
| 209 | +COMMENT ON LANGUAGE plxgo IS 'plx Go dialect (transpiles to plpgsql)'; |
0 commit comments