pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feed(How to) Make SQLData of UUID?
5+ messages / 4 participants
[nested] [flat]
* (How to) Make SQLData of UUID?
@ 2019-07-21 21:58 Alexander Myodov <amyodov@gmail.com>
0 siblings, 2 replies; 5+ messages in thread
From: Alexander Myodov @ 2019-07-21 21:58 UTC (permalink / raw)
To: pgsql-jdbc@lists.postgresql.org
Hello!
I seem to miss something obvious maybe, but I cannot find a way to use UUID
as SQLData; for example, to make a proper JDBC handling of custom
PostgreSQL type containing an UUID.
Imagine I made a custom type returned from some PL/PgSQL function:
CREATE TYPE MYFUNC_RETURN_TYPE AS
(
key UUID
);
Now, to handle it in JDBC, I need to make a custom type like
MyfuncReturnType implements
SQLData, and then add it to the type map of the connection. In
MyfuncReturnType, I’ll need to implement void writeSQL(SQLOutput stream)
method, and at some point I’ll need to do something like this: to call
either
SQLOutput::writeObject(SQLData x)
or
SQLOutput::writeObject(Object x, SQLType targetSqlType),
passing my UUID in somehow.
But UUID doesn’t satisfy SQLData interface in any way; and if I use the
second method, I do not have a proper SQLType for UUID anywhere.
I previously used the pgjdbc-ng driver, and they had a collection of
postgresql-specific SQLType's; but that driver had other problems, so I
decided to switch to the mainstream JDBC driver, and faced this lack.
Any hints please?
--
Alex Myodov
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: (How to) Make SQLData of UUID?
@ 2019-07-21 22:44 Rob Sargent <robjsargent@gmail.com>
parent: Alexander Myodov <amyodov@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Rob Sargent @ 2019-07-21 22:44 UTC (permalink / raw)
To: Alexander Myodov <amyodov@gmail.com>; +Cc: pgsql-jdbc@lists.postgresql.org
> On Jul 21, 2019, at 3:58 PM, Alexander Myodov <amyodov@gmail.com> wrote:
>
> Hello!
>
> I seem to miss something obvious maybe, but I cannot find a way to use UUID as SQLData; for example, to make a proper JDBC handling of custom PostgreSQL type containing an UUID.
>
> Imagine I made a custom type returned from some PL/PgSQL function:
>
> CREATE TYPE MYFUNC_RETURN_TYPE AS
> (
> key UUID
> );
>
> Now, to handle it in JDBC, I need to make a custom type like MyfuncReturnType implements SQLData, and then add it to the type map of the connection. In MyfuncReturnType, I’ll need to implement void writeSQL(SQLOutput stream) method, and at some point I’ll need to do something like this: to call either
> SQLOutput::writeObject(SQLData x)
> or
> SQLOutput::writeObject(Object x, SQLType targetSqlType),
> passing my UUID in somehow.
>
> But UUID doesn’t satisfy SQLData interface in any way; and if I use the second method, I do not have a proper SQLType for UUID anywhere.
>
> I previously used the pgjdbc-ng driver, and they had a collection of postgresql-specific SQLType's; but that driver had other problems, so I decided to switch to the mainstream JDBC driver, and faced this lack.
>
> Any hints please?
>
> --
Can you look into how JOOQ does it in there SQLDataType classes? It’s worked flawlessly for me.
> Alex Myodov
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: (How to) Make SQLData of UUID?
@ 2019-07-21 23:08 Basil Bourque <basil.bourque@gmail.com>
parent: Alexander Myodov <amyodov@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Basil Bourque @ 2019-07-21 23:08 UTC (permalink / raw)
To: pgsql-jdbc@lists.postgresql.org
> Any hints please?
>
Does this Answer by Gopinagh.R on Stack Overflow help?
https://stackoverflow.com/a/14197445/642706 <https://stackoverflow.com/a/14197445/642706;
CREATE OR REPLACE FUNCTION generatesurrogat() RETURNS uuid AS
$BODY$DECLARE
uid UUID;
BEGIN
uid:=(select uuid_generate_v1());
RAISE NOTICE 'My UUID is %',uid;
return uid;
END$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
—BB=
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: (How to) Make SQLData of UUID?
@ 2019-07-21 23:43 Alexander Myodov <amyodov@gmail.com>
parent: Basil Bourque <basil.bourque@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Alexander Myodov @ 2019-07-21 23:43 UTC (permalink / raw)
To: Basil Bourque <basil.bourque@gmail.com>; +Cc: pgsql-jdbc@lists.postgresql.org
Not so much, sorry :(
The problem is not in making a PL/PgSQL function returning an UUID. Or even
returning a custom type containing an UUID (like MYFUNC_RETURN_TYPE in my
case). The problem is on Java level, when I need to make a Java
handler/mapping of PostgreSQL custom type (which contains an UUID
internally).
For now I, instead of out.writeObject, made it through
out.writeString(key.toString()), but I am very unsure if this is how it
should be done. And if the opposite way, key = (UUID) in.readObject() is
also safe.
пн, 22 июл. 2019 г. в 02:09, Basil Bourque <basil.bourque@gmail.com>:
> Any hints please?
>
>
> Does this Answer by Gopinagh.R on Stack Overflow help?
>
> https://stackoverflow.com/a/14197445/642706
>
> CREATE OR REPLACE FUNCTION generatesurrogat() RETURNS uuid AS$BODY$DECLARE
> uid UUID;BEGIN
> uid:=(select uuid_generate_v1());
> RAISE NOTICE 'My UUID is %',uid;return uid;END$BODY$
> LANGUAGE plpgsql VOLATILE
> COST 100;
>
>
>
> —BB
>
--
Alex Myodov
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: (How to) Make SQLData of UUID?
@ 2019-07-22 06:22 Thomas Kellerer <spam_eater@gmx.net>
parent: Alexander Myodov <amyodov@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Thomas Kellerer @ 2019-07-22 06:22 UTC (permalink / raw)
To: pgsql-jdbc@lists.postgresql.org
Alexander Myodov schrieb am 22.07.2019 um 01:43:
> The problem is not in making a PL/PgSQL function returning an UUID.
> Or even returning a custom type containing an UUID (like
> MYFUNC_RETURN_TYPE in my case). The problem is on Java level, when I
> need to make a Java handler/mapping of PostgreSQL custom type (which
> contains an UUID internally).
You don't need a wrapper.
ResultSet.getObject(1, UUID.class)
and
PreparedStatement.setObject(1, someUUID);
will work just fine.
Thomas
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2019-07-22 06:22 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-07-21 21:58 (How to) Make SQLData of UUID? Alexander Myodov <amyodov@gmail.com>
2019-07-21 22:44 ` Rob Sargent <robjsargent@gmail.com>
2019-07-21 23:08 ` Basil Bourque <basil.bourque@gmail.com>
2019-07-21 23:43 ` Alexander Myodov <amyodov@gmail.com>
2019-07-22 06:22 ` Thomas Kellerer <spam_eater@gmx.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox