Re: Using a domain - Mailing list pgsql-general

From Tom Lane
Subject Re: Using a domain
Date
Msg-id 3318.1322700586@sss.pgh.pa.us
Whole thread Raw
In response to Using a domain  (Daniele Varrazzo <daniele.varrazzo@gmail.com>)
List pgsql-general
Daniele Varrazzo <daniele.varrazzo@gmail.com> writes:
> I'm trying to use a domain to define a data type constraint, let's say
> an hypothetical uk_post_code with pattern LNNLL. I'd enforce no
> whitespaces, all uppercase.

> I would also need a way to normalize before validate: given an input
> such as "w3 6bq", normalize it to W36BQ before trying to apply the
> check. It would be great if I could give this function the same name
> of the domain, so that uk_post_code('w3 6bq') would return W36BQ cast
> to the domain.

That particular case isn't going to work unless you choose a different
function name --- as you've found out, the parser prefers the
interpretation that this means the same as 'w3 6bq'::uk_post_code,
which is not a cast but just a literal of the named type.

If you were willing to write something like uk_post_code('w3 6bq'::text)
and define your function as taking text (or varchar if that turns you on),
it should work.  Likewise anytime the argument is a variable/expression
of known type text.  But with a bare untyped literal, no.

            regards, tom lane

pgsql-general by date:

Previous
From: Daniele Varrazzo
Date:
Subject: Using a domain
Next
From: Maxim Boguk
Date:
Subject: Problem with custom aggregates and record pseudo-type