• Stars
    star
    297
  • Rank 140,075 (Top 3 %)
  • Language
    C
  • License
    Other
  • Created almost 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

uri type for PostgreSQL

uri type for PostgreSQL

https://twitter.com/pvh/status/567395527357001728

This is an extension for PostgreSQL that provides a uri data type. Advantages over using plain text for storing URIs include:

  • URI syntax checking
  • functions for extracting URI components
  • human-friendly sorting

The actual URI parsing is provided by the uriparser library, which supports URI syntax as per RFC 3986.

Note that this might not be the right data type to use if you want to store user-provided URI data, such as HTTP referrers, since they might contain arbitrary junk.

Installation

You need to have the above-mentioned uriparser library installed. It is included in many operating system distributions and package management systems. pkg-config will be used to find it. I recommend at least version 0.8.0. Older versions will also work, but they apparently contain some bugs and might fail to correctly accept or reject URI syntax corner cases. This is mainly a problem if your application needs to be robust against junk input.

To build and install this module:

make
make install

or selecting a specific PostgreSQL installation:

make PG_CONFIG=/some/where/bin/pg_config
make PG_CONFIG=/some/where/bin/pg_config install

And finally inside the database:

CREATE EXTENSION uri;

Using

This module provides a data type uri that you can use like a normal type. For example:

CREATE TABLE links (
    id int PRIMARY KEY,
    link uri
);

INSERT INTO links VALUES (1, 'https://github.com/petere/pguri');

A number of functions are provided to extract parts of a URI:

  • uri_scheme(uri) returns text

    Extracts the scheme of a URI, for example http or ftp or mailto.

  • uri_userinfo(uri) returns text

    Extracts the user info part of a URI. This is normally a user name, but could also be of the form username:password. If the URI does not contain a user info part, then this will return null.

  • uri_host(uri) returns text

    Extracts the host of a URI, for example www.example.com or 192.168.0.1. (For IPv6 addresses, the brackets are not included here.) If there is no host, the return value is null.

  • uri_host_inet(uri) returns inet

    If the host is a raw IP address, then this will return it as an inet datum. Otherwise (not an IP address or no host at all), the return value is null.

  • uri_port(uri) returns integer

    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 after the ?). If there is no query part, returns null.

  • uri_fragment(uri) returns text

    Extracts the fragment part of a URI (roughly speaking, everything after the #). If there is no fragment part, returns null.

Other functions:

  • uri_normalize(uri) returns uri

    Performs syntax-based normalization of the URI. This includes case normalization, percent-encoding normalization, and removing redundant . and .. path segments. See RFC 3986 section 6.2.2 for the full details.

    Note that this module (and similar modules in other programming languages) compares URIs for equality in their original form, without normalization. If you want to consider distinct URIs without regard for mostly irrelevant syntax differences, pass them through this function.

  • uri_escape(text, space_to_plus boolean DEFAULT false, normalize_breaks boolean DEFAULT false) returns text

    Percent-encodes all reserved characters from the text. This can be useful for constructing URIs from strings.

    If space_to_plus is true, then spaces are replaced by plus signs. If normalize_breaks is true, then line breaks are converted to CR LF pairs (and subsequently percent-encoded). Note that these two conversions come from the HTML standard for encoding form data but are not part of the specification for URIs.

  • uri_unescape(text, plus_to_space boolean DEFAULT false, break_conversion boolean DEFAULT false) returns text

    Decodes all percent-encodings in the text.

    If plus_to_space is true, then plus signs are converted to spaces. If break_conversion is true, then CR LF pairs are converted to simple newlines (\n).

More Repositories

1

homebrew-postgresql

🐘 PostgreSQL formulae for the Homebrew package manager
Ruby
288
star
2

pguint

unsigned integer types extension for PostgreSQL
Python
231
star
3

plsh

PL/sh is a procedural language handler for PostgreSQL that allows you to write stored procedures in a shell of your choice.
C
154
star
4

pex

light-weight package manager for PostgreSQL
Shell
95
star
5

postgresqlfs

FUSE driver to access PostgreSQL databases as a file system
C
80
star
6

pgemailaddr

email address type for PostgreSQL
C
43
star
7

git-whoami

essential Git command
Shell
24
star
8

pgtrashcan

PostgreSQL trash can
C
23
star
9

homebrew-auto-update

automatically runs brew update
Shell
22
star
10

plxslt

XSLT procedural language for PostgreSQL
C
18
star
11

pgpcre

PCRE functions for PostgreSQL
C
17
star
12

getent-osx

simplistic getent tool emulation for OS X
Perl
14
star
13

pex-packages

package library for pex (https://github.com/petere/pex)
13
star
14

veung

visual explain ΓΌber-next generation (for PostgreSQL)
Python
12
star
15

postgresql-common

PostgreSQL database-cluster manager β€” You might be interested in https://github.com/petere/homebrew-postgresql as well.
Perl
12
star
16

homebrew-sgml

SGML tools for Homebrew
Ruby
10
star
17

autopex

PostgreSQL extension installation magic
C
9
star
18

pglockviz

tool to visualize PostgreSQL locks
Python
8
star
19

plpydbapi

Python DB-API interface on top of PL/Python
Python
7
star
20

pgbloomagg

Bloom filter aggregate function for PostgreSQL
PLpgSQL
7
star
21

logging-hooks-presentation

material for presentation about logging hooks in PostgreSQL
Puppet
7
star
22

adventofcode-2020

https://adventofcode.com/2020
Raku
6
star
23

pgci

continuous integration service for PostgreSQL
Python
4
star
24

markdown-formatter-for-chrome

extension for Google Chrome that formats Markdown text as HTML
JavaScript
4
star
25

pgvihash

version-independent hash functions for PostgreSQL
C
4
star
26

emacs-save-packages

save and restore installed ELPA packages
Emacs Lisp
4
star
27

pglogjsonsrvgo

pg_logforward JSON consumer written in Go
Go
2
star
28

peter.eisentraut.org

Peter Eisentraut's blog
Ruby
2
star
29

emacs-ssh-file-modes

Emacs major modes for ssh authorized_keys and known_hosts files
Emacs Lisp
2
star
30

debnorepo

find installed Debian packages without repository
2
star
31

pglibuuid

PostgreSQL wrapper for libuuid (obsolete, use PostgreSQL's configure --with-uuid=e2fs for equivalent functionality)
C
2
star
32

sgml-spell-checker

SGML spell-checking package
Shell
2
star
33

pgcatviz

generate schema diagram of PostgreSQL system catalogs using Graphviz
Python
2
star
34

my-gettext-scripts

some small wrapper scripts around gettext tools
Shell
1
star
35

updatable_views

updatable views extension for PostgreSQL
C
1
star
36

samsung-q45-support

support for Samsung Q45 laptops on Debian/Ubuntu
1
star
37

sgml-spell-checker-word-lists

additional word lists to use with sgml-spell-checker
Makefile
1
star
38

plpylint

runs pylint over PostgreSQL PL/Python functions
Python
1
star
39

emacs-eruby-mode

Emacs minor mode for eRuby template (.erb) files
Emacs Lisp
1
star
40

nodes-cookbook

Chef cookbook to store node attributes in data bag items
Ruby
1
star
41

commitfest-tools

tools for messing with PostgreSQL commit fests
Python
1
star
42

adventofcode-2021

πŸŽ„
Python
1
star
43

homebrew-icu

ICU formulae for the Homebrew package manager
Ruby
1
star