Created
April 8, 2012 01:06
-
-
Save solidsnack/2333348 to your computer and use it in GitHub Desktop.
Postgres DOMAIN and CHECK for LDH domain names.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Postgres DOMAIN and CHECK for LDH domain names. | |
-- Derived from RFCs 1035 and 1123. | |
CREATE EXTENSION IF NOT EXISTS citext; | |
CREATE DOMAIN dns AS citext | |
DEFAULT '.' | |
CHECK ( VALUE ~ '(?xi) | |
^( [a-z0-9]([a-z0-9-]{0,61}[a-z0-9])? | |
([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?){0,126})? | |
[.]?$' | |
AND length(trim(TRAILING '.' FROM VALUE)) <= 253 ); | |
-- Domain normalization for indexes and comparison. Removes dot unless name is | |
-- empty and converts to lowercase. | |
CREATE FUNCTION dns_normalize(dns) RETURNS dns AS $$ | |
DECLARE | |
trimmed dns := lower(trim(TRAILING '.' FROM $1)); | |
BEGIN | |
IF trimmed = '' THEN RETURN '.'; | |
ELSE RETURN trimmed; | |
END IF; | |
END | |
$$ LANGUAGE plpgsql IMMUTABLE STRICT; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment