-
Notifications
You must be signed in to change notification settings - Fork 0
/
url--1.0.sql
230 lines (190 loc) · 5.2 KB
/
url--1.0.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION url" to load this file. \quit
CREATE OR REPLACE FUNCTION url_in(cstring)
RETURNS url
AS '$libdir/url'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url_out(url)
RETURNS cstring
AS '$libdir/url'
LANGUAGE C IMMUTABLE STRICT;
CREATE TYPE url (
INPUT = url_in,
OUTPUT = url_out,
INTERNALLENGTH = 1024
);
COMMENT ON TYPE url IS 'url datatype for PostgreSQL';
-- Constructors
CREATE OR REPLACE FUNCTION url(cstring)
RETURNS url
AS '$libdir/url', 'url_raw_constructor'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url(cstring, cstring, int, cstring)
RETURNS url
AS '$libdir/url', 'url_all_field_constructor'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url(cstring, cstring, cstring)
RETURNS url
AS '$libdir/url', 'url_some_field_constructor'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url(url, cstring)
RETURNS url
AS '$libdir/url', 'url_copy_constructor'
LANGUAGE C IMMUTABLE STRICT;
-- Getters
CREATE OR REPLACE FUNCTION getAuthority(url)
RETURNS TEXT
AS '$libdir/url', 'getAuthority'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getDefaultPort(url)
RETURNS INTEGER
AS '$libdir/url', 'getDefaultPort'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getFile(url)
RETURNS TEXT
AS '$libdir/url', 'getFile'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getHost(url)
RETURNS TEXT
AS '$libdir/url', 'getHost'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getPath(url)
RETURNS TEXT
AS '$libdir/url', 'getPath'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getPort(url)
RETURNS INTEGER
AS '$libdir/url', 'getPort'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getProtocol(url)
RETURNS TEXT
AS '$libdir/url', 'getProtocol'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getQuery(url)
RETURNS TEXT
AS '$libdir/url', 'getQuery'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getRef(url)
RETURNS TEXT
AS '$libdir/url', 'getRef'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION getUserInfo(url)
RETURNS TEXT
AS '$libdir/url', 'getUserInfo'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION toString(url)
RETURNS cstring
AS '$libdir/url', 'toString'
LANGUAGE C IMMUTABLE STRICT;
-- Index Compatible Comparison
CREATE OR REPLACE FUNCTION equals(url1 url, url2 url)
RETURNS BOOLEAN
AS 'SELECT url1 = url2'
LANGUAGE SQL IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION sameHost(url1 url, url2 url)
RETURNS BOOLEAN
AS 'SELECT getHost(url1) = getHost(url2)'
LANGUAGE SQL IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION sameFile(url1 url, url2 url)
RETURNS BOOLEAN
AS 'SELECT getAuthority(url1) = getAuthority(url2) AND getProtocol(url1) = getProtocol(url2) AND getFile(url1) = getFile(url2)'
LANGUAGE SQL IMMUTABLE STRICT;
-- Comparison
CREATE OR REPLACE FUNCTION url_eq(url, url)
RETURNS BOOLEAN
AS '$libdir/url', 'url_eq'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url_ne(url, url)
RETURNS BOOLEAN
AS '$libdir/url', 'url_ne'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url_lt(url, url)
RETURNS BOOLEAN
AS '$libdir/url', 'url_lt'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url_le(url, url)
RETURNS BOOLEAN
AS '$libdir/url', 'url_le'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url_gt(url, url)
RETURNS BOOLEAN
AS '$libdir/url', 'url_gt'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url_ge(url, url)
RETURNS BOOLEAN
AS '$libdir/url', 'url_ge'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION url_cmp(url, url)
RETURNS INTEGER
AS '$libdir/url', 'url_cmp'
LANGUAGE C IMMUTABLE STRICT;
-- Comparison index
CREATE OPERATOR = (
LEFTARG = url,
RIGHTARG = url,
PROCEDURE = url_eq,
COMMUTATOR = =,
NEGATOR = <>,
RESTRICT = eqsel,
JOIN = eqjoinsel
);
COMMENT ON OPERATOR =(url, url) IS 'equals?';
CREATE OPERATOR <> (
LEFTARG = url,
RIGHTARG = url,
PROCEDURE = url_ne,
COMMUTATOR = <>,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR <>(url, url) IS 'not equals?';
CREATE OPERATOR < (
LEFTARG = url,
RIGHTARG = url,
PROCEDURE = url_lt,
COMMUTATOR = > ,
NEGATOR = >= ,
RESTRICT = scalarltsel,
JOIN = scalarltjoinsel
);
COMMENT ON OPERATOR <(url, url) IS 'less-than';
CREATE OPERATOR <= (
LEFTARG = url,
RIGHTARG = url,
PROCEDURE = url_le,
COMMUTATOR = >= ,
NEGATOR = > ,
RESTRICT = scalarltsel,
JOIN = scalarltjoinsel
);
COMMENT ON OPERATOR <=(url, url) IS 'less-than-or-equal';
CREATE OPERATOR > (
LEFTARG = url,
RIGHTARG = url,
PROCEDURE = url_gt,
COMMUTATOR = < ,
NEGATOR = <= ,
RESTRICT = scalargtsel,
JOIN = scalargtjoinsel
);
COMMENT ON OPERATOR >(url, url) IS 'greater-than';
CREATE OPERATOR >= (
LEFTARG = url,
RIGHTARG = url,
PROCEDURE = url_ge,
COMMUTATOR = <= ,
NEGATOR = < ,
RESTRICT = scalargtsel,
JOIN = scalargtjoinsel
);
COMMENT ON OPERATOR >=(url, url) IS 'greater-than-or-equal';
CREATE OPERATOR CLASS btree_url_ops
DEFAULT FOR TYPE url USING btree
AS
OPERATOR 1 < ,
OPERATOR 2 <= ,
OPERATOR 3 = ,
OPERATOR 4 >= ,
OPERATOR 5 > ,
FUNCTION 1 url_cmp(url, url);