Can I create a TYPE (or DOMAIN) with arguments?

Started by R.A.over 18 years ago6 messagesgeneral
Jump to latest
#1R.A.
adeveloper@bluebottle.com

Hello,

I'm trying to create a composite type with an argument, to create one field of this type like character varying(x), but I don't know if this can be done with PostgreSQL. I want something like:
CREATE TYPE mytype AS (
tx character varying(x),
t2nd integer
);
I need to limit the number of chars in this field tx. And different lengths will be used when creating tables with this type (so a Domain will suffer the same problem). I'm not looking for a solution like a separate column with maxChars and a Check, because lots of columns will be required (and others design reasons).

Any suggestions?

Thanks.

----------------------------------------------------------------------
Free pop3 email with a spam filter.
http://www.bluebottle.com/tag/5

#2Erik Jones
erik@myemma.com
In reply to: R.A. (#1)
Re: Can I create a TYPE (or DOMAIN) with arguments?

On Jan 9, 2008, at 3:53 AM, R.A. wrote:

Hello,

I'm trying to create a composite type with an argument, to create
one field of this type like character varying(x), but I don't know
if this can be done with PostgreSQL. I want something like:
CREATE TYPE mytype AS (
tx character varying(x),
t2nd integer
);
I need to limit the number of chars in this field tx. And different
lengths will be used when creating tables with this type (so a
Domain will suffer the same problem). I'm not looking for a
solution like a separate column with maxChars and a Check, because
lots of columns will be required (and others design reasons).

Postgres doesn't support parameterized type declarations directly
(that I've ever heard of), but you could probably write a function
that uses EXECUTE to do this.

Erik Jones

DBA | Emma®
erik@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com

#3Merlin Moncure
mmoncure@gmail.com
In reply to: R.A. (#1)
Re: Can I create a TYPE (or DOMAIN) with arguments?

On Jan 9, 2008 4:53 AM, R.A. <adeveloper@bluebottle.com> wrote:

Hello,

I'm trying to create a composite type with an argument, to create one field of this type like character varying(x), but I don't know if this can be done with PostgreSQL. I want something like:
CREATE TYPE mytype AS (
tx character varying(x),
t2nd integer
);
I need to limit the number of chars in this field tx. And different lengths will be used when creating tables with this type (so a Domain will suffer the same problem). I'm not looking for a solution like a separate column with maxChars and a Check, because lots of columns will be required (and others design reasons).

It would be really neat if you could do that, but you can't :-(. What
you can do is make a trigger function taking mytype which and apply
the constraint that way...

merlin

#4Martijn van Oosterhout
kleptog@svana.org
In reply to: Erik Jones (#2)
Re: Can I create a TYPE (or DOMAIN) with arguments?

On Thu, Jan 10, 2008 at 08:58:07AM -0600, Erik Jones wrote:

Postgres doesn't support parameterized type declarations directly
(that I've ever heard of), but you could probably write a function
that uses EXECUTE to do this.

IIRC 8.3 will include the user-defined typmod which will allow such
constructs...

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

Those who make peaceful revolution impossible will make violent revolution inevitable.
-- John F Kennedy

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Martijn van Oosterhout (#4)
Re: Can I create a TYPE (or DOMAIN) with arguments?

Martijn van Oosterhout <kleptog@svana.org> writes:

On Thu, Jan 10, 2008 at 08:58:07AM -0600, Erik Jones wrote:

Postgres doesn't support parameterized type declarations directly
(that I've ever heard of), but you could probably write a function
that uses EXECUTE to do this.

IIRC 8.3 will include the user-defined typmod which will allow such
constructs...

That won't help for this particular problem, though --- composite types
don't take typmods, and there'd be no mechanism to pass it down to the
varchar field if they did. I don't think the OP can solve his problem
just with spare parts. In 8.3 he could write a primitive type that
behaves the way he wants, but it'd take an annoyingly large amount
of custom C code :-(

regards, tom lane

#6R.A.
adeveloper@bluebottle.com
In reply to: Tom Lane (#5)
Re: Can I create a TYPE (or DOMAIN) with arguments?

I think the function-trigger approach will be useful to me to bypass this problem.

Thanks to all again for your suggestions!

----------------------------------------------------------------------
Free pop3 email with a spam filter.
http://www.bluebottle.com/tag/5