forked from pramsey/pgsql-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http--1.0--1.1.sql
68 lines (55 loc) · 1.73 KB
/
http--1.0--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
-- Remove old 1.0 functions
DROP FUNCTION http_get(varchar, varchar);
DROP FUNCTION http_post(varchar, varchar, varchar, varchar);
CREATE DOMAIN http_method AS text
CHECK (
VALUE ILIKE 'get' OR
VALUE ILIKE 'post' OR
VALUE ILIKE 'put' OR
VALUE ILIKE 'delete'
);
CREATE DOMAIN content_type AS text
CHECK (
VALUE ~ '^\S+\/\S+'
);
CREATE TYPE http_header AS (
field VARCHAR,
value VARCHAR
);
CREATE TYPE http_request AS (
method http_method,
uri VARCHAR,
headers http_header[],
content_type VARCHAR,
content VARCHAR
);
ALTER TYPE http_response ALTER ATTRIBUTE headers TYPE http_header[];
CREATE OR REPLACE FUNCTION http_header (field VARCHAR, value VARCHAR)
RETURNS http_header
AS $$ SELECT $1, $2 $$
LANGUAGE 'sql';
CREATE OR REPLACE FUNCTION http(request http_request)
RETURNS http_response
AS 'MODULE_PATHNAME', 'http_request'
LANGUAGE 'c';
CREATE OR REPLACE FUNCTION http_get(uri VARCHAR)
RETURNS http_response
AS $$ SELECT http(('GET', $1, NULL, NULL, NULL)::http_request) $$
LANGUAGE 'sql';
CREATE OR REPLACE FUNCTION http_post(uri VARCHAR, content VARCHAR, content_type VARCHAR)
RETURNS http_response
AS $$ SELECT http(('POST', $1, NULL, $3, $2)::http_request) $$
LANGUAGE 'sql';
CREATE OR REPLACE FUNCTION http_put(uri VARCHAR, content VARCHAR, content_type VARCHAR)
RETURNS http_response
AS $$ SELECT http(('PUT', $1, NULL, $3, $2)::http_request) $$
LANGUAGE 'sql';
CREATE OR REPLACE FUNCTION http_delete(uri VARCHAR)
RETURNS http_response
AS $$ SELECT http(('DELETE', $1, NULL, NULL, NULL)::http_request) $$
LANGUAGE 'sql';
CREATE OR REPLACE FUNCTION urlencode(string VARCHAR)
RETURNS TEXT
AS 'MODULE_PATHNAME'
LANGUAGE 'c'
IMMUTABLE STRICT;