Skip to content

Commit

Permalink
Change uri_path to return text
Browse files Browse the repository at this point in the history
  • Loading branch information
petere committed Apr 4, 2015
1 parent 53789ec commit 527cc23
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 126 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ A number of functions are provided to extract parts of a URI:
Extracts the port of a URI as an integer, for example `5432`. If
no port is specified, the return value is null.

- `uri_path(uri) returns text`

Extracts the path component of a URI. Logically, a URI always
contains a path. The return value can be an empty string but
never null.

- `uri_path_array(uri) returns text[]`

Returns the path component of a URI as an array, with the path
split at the slash characters. This is probably not as useful as
the `uri_path` function, but it is provided here because the
`uriparser` library exposes it.

- `uri_query(uri) returns text`

Extracts the query part of a URI (roughly speaking, everything
Expand Down
289 changes: 165 additions & 124 deletions test/expected/test.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ VALUES ('http://www.postgresql.org/'),
('http://[1080::8:800:200C:417A]/foo'),
('http://host:'),
(''),
('foobar');
('/'),
('foobar'),
('/foobar');
SELECT * FROM test;
a | b
----+-----------------------------------------------------------------------------------------
Expand All @@ -28,8 +30,10 @@ SELECT * FROM test;
9 | http://[1080::8:800:200C:417A]/foo
10 | http://host:
11 |
12 | foobar
(12 rows)
12 | /
13 | foobar
14 | /foobar
(14 rows)

-- error cases
SELECT uri 'http://host:port/';
Expand All @@ -44,135 +48,172 @@ SELECT b AS uri,
uri_host_inet(b),
uri_port(b),
uri_path(b),
uri_path_array(b),
uri_query(b),
uri_fragment(b)
FROM test;
-[ RECORD 1 ]-+----------------------------------------------------------------------------------------
uri | http://www.postgresql.org/
uri_scheme | http
uri_userinfo | _null_
uri_host | www.postgresql.org
uri_host_inet | _null_
uri_port | _null_
uri_path | {""}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 2 ]-+----------------------------------------------------------------------------------------
uri | http://www.postgresql.org/docs/devel/static/xfunc-sql.html#XFUNC-SQL-FUNCTION-ARGUMENTS
uri_scheme | http
uri_userinfo | _null_
uri_host | www.postgresql.org
uri_host_inet | _null_
uri_port | _null_
uri_path | {docs,devel,static,xfunc-sql.html}
uri_query | _null_
uri_fragment | XFUNC-SQL-FUNCTION-ARGUMENTS
-[ RECORD 3 ]-+----------------------------------------------------------------------------------------
uri | https://duckduckgo.com/?q=postgresql&ia=about
uri_scheme | https
uri_userinfo | _null_
uri_host | duckduckgo.com
uri_host_inet | _null_
uri_port | _null_
uri_path | {""}
uri_query | q=postgresql&ia=about
uri_fragment | _null_
-[ RECORD 4 ]-+----------------------------------------------------------------------------------------
uri | ftp://ftp.gnu.org/gnu/bison
uri_scheme | ftp
uri_userinfo | _null_
uri_host | ftp.gnu.org
uri_host_inet | _null_
uri_port | _null_
uri_path | {gnu,bison}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 5 ]-+----------------------------------------------------------------------------------------
uri | mailto:[email protected]
uri_scheme | mailto
uri_userinfo | _null_
uri_host | _null_
uri_host_inet | _null_
uri_port | _null_
uri_path | {[email protected]}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 6 ]-+----------------------------------------------------------------------------------------
uri | ssh://[email protected]:29418/openstack/nova.git
uri_scheme | ssh
uri_userinfo | username
uri_host | review.openstack.org
uri_host_inet | _null_
uri_port | 29418
uri_path | {openstack,nova.git}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 7 ]-+----------------------------------------------------------------------------------------
uri | http://admin:[email protected]
uri_scheme | http
uri_userinfo | admin:password
uri_host | 192.168.0.1
uri_host_inet | 192.168.0.1
uri_port | _null_
uri_path | {}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 8 ]-+----------------------------------------------------------------------------------------
uri | http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html
uri_scheme | http
uri_userinfo | _null_
uri_host | FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
uri_host_inet | fedc:ba98:7654:3210:fedc:ba98:7654:3210
uri_port | 80
uri_path | {index.html}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 9 ]-+----------------------------------------------------------------------------------------
uri | http://[1080::8:800:200C:417A]/foo
uri_scheme | http
uri_userinfo | _null_
uri_host | 1080::8:800:200C:417A
uri_host_inet | 1080::8:800:200c:417a
uri_port | _null_
uri_path | {foo}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 10 ]+----------------------------------------------------------------------------------------
uri | http://host:
uri_scheme | http
uri_userinfo | _null_
uri_host | host
uri_host_inet | _null_
uri_port | _null_
uri_path | {}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 11 ]+----------------------------------------------------------------------------------------
uri |
uri_scheme | _null_
uri_userinfo | _null_
uri_host | _null_
uri_host_inet | _null_
uri_port | _null_
uri_path | {}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 12 ]+----------------------------------------------------------------------------------------
uri | foobar
uri_scheme | _null_
uri_userinfo | _null_
uri_host | _null_
uri_host_inet | _null_
uri_port | _null_
uri_path | {foobar}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 1 ]--+----------------------------------------------------------------------------------------
uri | http://www.postgresql.org/
uri_scheme | http
uri_userinfo | _null_
uri_host | www.postgresql.org
uri_host_inet | _null_
uri_port | _null_
uri_path | /
uri_path_array | {""}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 2 ]--+----------------------------------------------------------------------------------------
uri | http://www.postgresql.org/docs/devel/static/xfunc-sql.html#XFUNC-SQL-FUNCTION-ARGUMENTS
uri_scheme | http
uri_userinfo | _null_
uri_host | www.postgresql.org
uri_host_inet | _null_
uri_port | _null_
uri_path | /docs/devel/static/xfunc-sql.html
uri_path_array | {docs,devel,static,xfunc-sql.html}
uri_query | _null_
uri_fragment | XFUNC-SQL-FUNCTION-ARGUMENTS
-[ RECORD 3 ]--+----------------------------------------------------------------------------------------
uri | https://duckduckgo.com/?q=postgresql&ia=about
uri_scheme | https
uri_userinfo | _null_
uri_host | duckduckgo.com
uri_host_inet | _null_
uri_port | _null_
uri_path | /
uri_path_array | {""}
uri_query | q=postgresql&ia=about
uri_fragment | _null_
-[ RECORD 4 ]--+----------------------------------------------------------------------------------------
uri | ftp://ftp.gnu.org/gnu/bison
uri_scheme | ftp
uri_userinfo | _null_
uri_host | ftp.gnu.org
uri_host_inet | _null_
uri_port | _null_
uri_path | /gnu/bison
uri_path_array | {gnu,bison}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 5 ]--+----------------------------------------------------------------------------------------
uri | mailto:[email protected]
uri_scheme | mailto
uri_userinfo | _null_
uri_host | _null_
uri_host_inet | _null_
uri_port | _null_
uri_path | [email protected]
uri_path_array | {[email protected]}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 6 ]--+----------------------------------------------------------------------------------------
uri | ssh://[email protected]:29418/openstack/nova.git
uri_scheme | ssh
uri_userinfo | username
uri_host | review.openstack.org
uri_host_inet | _null_
uri_port | 29418
uri_path | /openstack/nova.git
uri_path_array | {openstack,nova.git}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 7 ]--+----------------------------------------------------------------------------------------
uri | http://admin:[email protected]
uri_scheme | http
uri_userinfo | admin:password
uri_host | 192.168.0.1
uri_host_inet | 192.168.0.1
uri_port | _null_
uri_path |
uri_path_array | {}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 8 ]--+----------------------------------------------------------------------------------------
uri | http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html
uri_scheme | http
uri_userinfo | _null_
uri_host | FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
uri_host_inet | fedc:ba98:7654:3210:fedc:ba98:7654:3210
uri_port | 80
uri_path | /index.html
uri_path_array | {index.html}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 9 ]--+----------------------------------------------------------------------------------------
uri | http://[1080::8:800:200C:417A]/foo
uri_scheme | http
uri_userinfo | _null_
uri_host | 1080::8:800:200C:417A
uri_host_inet | 1080::8:800:200c:417a
uri_port | _null_
uri_path | /foo
uri_path_array | {foo}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 10 ]-+----------------------------------------------------------------------------------------
uri | http://host:
uri_scheme | http
uri_userinfo | _null_
uri_host | host
uri_host_inet | _null_
uri_port | _null_
uri_path |
uri_path_array | {}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 11 ]-+----------------------------------------------------------------------------------------
uri |
uri_scheme | _null_
uri_userinfo | _null_
uri_host | _null_
uri_host_inet | _null_
uri_port | _null_
uri_path |
uri_path_array | {}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 12 ]-+----------------------------------------------------------------------------------------
uri | /
uri_scheme | _null_
uri_userinfo | _null_
uri_host | _null_
uri_host_inet | _null_
uri_port | _null_
uri_path | /
uri_path_array | {}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 13 ]-+----------------------------------------------------------------------------------------
uri | foobar
uri_scheme | _null_
uri_userinfo | _null_
uri_host | _null_
uri_host_inet | _null_
uri_port | _null_
uri_path | foobar
uri_path_array | {foobar}
uri_query | _null_
uri_fragment | _null_
-[ RECORD 14 ]-+----------------------------------------------------------------------------------------
uri | /foobar
uri_scheme | _null_
uri_userinfo | _null_
uri_host | _null_
uri_host_inet | _null_
uri_port | _null_
uri_path | /foobar
uri_path_array | {foobar}
uri_query | _null_
uri_fragment | _null_

\x off
SELECT DISTINCT b FROM test ORDER BY b;
b
-----------------------------------------------------------------------------------------

/
/foobar
foobar
ftp://ftp.gnu.org/gnu/bison
http://[1080::8:800:200C:417A]/foo
Expand All @@ -184,5 +225,5 @@ SELECT DISTINCT b FROM test ORDER BY b;
https://duckduckgo.com/?q=postgresql&ia=about
mailto:[email protected]
ssh://[email protected]:29418/openstack/nova.git
(12 rows)
(14 rows)

5 changes: 4 additions & 1 deletion test/sql/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ VALUES ('http://www.postgresql.org/'),
('http://[1080::8:800:200C:417A]/foo'),
('http://host:'),
(''),
('foobar');
('/'),
('foobar'),
('/foobar');

SELECT * FROM test;

Expand All @@ -32,6 +34,7 @@ SELECT b AS uri,
uri_host_inet(b),
uri_port(b),
uri_path(b),
uri_path_array(b),
uri_query(b),
uri_fragment(b)
FROM test;
Expand Down
Loading

0 comments on commit 527cc23

Please sign in to comment.