Thread: Namespace issues
Given a user defined type foo... I've created several casts to/from foo and built-in types. I had adopted a naming convention of: baz foo_to_baz(foo); foo foo_from_baz(baz); But: ...it is recommended that you continue to follow this old convention of naming cast implementation functions after the target data type. Many users are used to being able to cast data types using a function-style notation, that is typename(x). This notation is in fact nothing more nor less than a call of the cast implementation function; it is not specially treated as a cast. If your conversion functions are not named to support this convention then you will have surprised users. Since PostgreSQL allows overloading of the same function name with different argument types, there is no difficulty in having multiple conversion functions from different types that all use the target type’s name. This suggests: baz baz(foo); foo foo(baz); I don't see how I can do this in my declarations. E.g., if I have baz = {int4, text, float8, ...} then I end up with several (C) functions all named foo() but each taking a different argument type (baz). Since C doesn't support more than a single namespace for functions, this just won't work. What am I failing to see, here? Thanks! --don
On Tue, May 16, 2006 at 10:29:27AM -0700, Don Y wrote: > Given a user defined type foo... > I've created several casts to/from foo and built-in types. > I had adopted a naming convention of: > baz foo_to_baz(foo); > foo foo_from_baz(baz); > > But: <snip> > I don't see how I can do this in my declarations. E.g., > if I have > baz = {int4, text, float8, ...} > then I end up with several (C) functions all named foo() > but each taking a different argument type (baz). Since > C doesn't support more than a single namespace for functions, > this just won't work. > > What am I failing to see, here? That the name of the function in C doesn't have to be the same as the name of the function in SQL. You can even define many SQL functions that all refer to the same C function. So in your C file yo call them: cast_foo_to_baz() and in the SQL you declare as just: baz() Have a nice day, -- Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/ > From each according to his ability. To each according to his ability to litigate.
Attachment
Martijn van Oosterhout wrote: > On Tue, May 16, 2006 at 10:29:27AM -0700, Don Y wrote: >> Given a user defined type foo... >> I've created several casts to/from foo and built-in types. >> I had adopted a naming convention of: >> baz foo_to_baz(foo); >> foo foo_from_baz(baz); >> >> But: > > <snip> > >> I don't see how I can do this in my declarations. E.g., >> if I have >> baz = {int4, text, float8, ...} >> then I end up with several (C) functions all named foo() >> but each taking a different argument type (baz). Since >> C doesn't support more than a single namespace for functions, >> this just won't work. >> >> What am I failing to see, here? > > That the name of the function in C doesn't have to be the same as the > name of the function in SQL. You can even define many SQL functions > that all refer to the same C function. > > So in your C file yo call them: > > cast_foo_to_baz() > > and in the SQL you declare as just: > > baz() But what *binds* my C declaration to the corresponding SQL "CREATE CAST"? E.g., CREATE FUNCTION foo_from_baz(baz) RETURNS foo AS '...' LANGUAGE 'C' IMMUTABLE STRICT; yet, to support the foo(baz) syntax, I would then need (?) to continue with: CREATE CAST (baz AS foo) WITH FUNCTION foo(baz); or, do I use: CREATE CAST (baz as foo) WITH FUNCTION foo_from_baz(baz); but then how does the "foo(baz)" syntax originate? or, is this a built in "feature" of the interpreter? (In which case, why all the commentary <snip>ed that tells me to name my functions in this way?) --don
On Tue, May 16, 2006 at 10:53:10AM -0700, Don Y wrote: > But what *binds* my C declaration to the corresponding SQL > "CREATE CAST"? > > E.g., > > CREATE FUNCTION foo_from_baz(baz) > RETURNS foo > AS '...' > LANGUAGE 'C' IMMUTABLE STRICT; Your create functions would look like: CREATE FUNCTION foo(baz) RETURNS foo AS '$libdir/yourlib','cast_foo_from_baz' LANGUAGE 'C' IMMUTABLE STRICT; CREATE FUNCTION foo(baz2) RETURNS foo AS '$libdir/yourlib','cast_foo_from_baz2' LANGUAGE 'C' IMMUTABLE STRICT; > yet, to support the foo(baz) syntax, I would then need (?) > to continue with: > > CREATE CAST (baz AS foo) > WITH FUNCTION foo(baz); Indeed, and also: CREATE CAST (baz2 TO foo) WITH FUNCTION foo(baz2) Hope this helps, -- Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/ > From each according to his ability. To each according to his ability to litigate.