Re: Using a domain

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Using a domain
Date: 2011-12-01 00:49:46
Message-ID: 3318.1322700586@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Maxim Boguk 2011-12-01 05:46:56 Problem with custom aggregates and record pseudo-type
Previous Message Daniele Varrazzo 2011-12-01 00:39:04 Using a domain