Skip site navigation (1) Skip section navigation (2)

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: (view raw, whole thread or download thread mbox)
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

pgsql-general by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group