Thread: Can I create a TYPE (or DOMAIN) with arguments?
Hello, I'm trying to create a composite type with an argument, to create one field of this type like character varying(x), but Idon'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 aCheck, 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
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
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), butI 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 thistype (so a Domain will suffer the same problem). I'm not looking for a solution like a separate column with maxCharsand 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
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/ > Those who make peaceful revolution impossible will make violent revolution inevitable. > -- John F Kennedy
Attachment
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
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