Re: (How to) Make composite PGObject with Text? (Was: (How to) MakeSQLData of UUID?) - Mailing list pgsql-jdbc
From | Alexander Myodov |
---|---|
Subject | Re: (How to) Make composite PGObject with Text? (Was: (How to) MakeSQLData of UUID?) |
Date | |
Msg-id | CAHF95JyVJjCXjovg5FUzmtvDB2YfHM0euafU6uDoGumgzQn9CQ@mail.gmail.com Whole thread Raw |
In response to | Re: (How to) Make composite PGObject with Text? (Was: (How to) MakeSQLData of UUID?) (Dave Cramer <pg@fastcrypt.com>) |
Responses |
Re: (How to) Make composite PGObject with Text? (Was: (How to) MakeSQLData of UUID?)
|
List | pgsql-jdbc |
See my response inlineOn Mon, 22 Jul 2019 at 20:45, Alexander Myodov <amyodov@gmail.com> wrote:Well, seems like was my primary mistake in original email was trying to use the SQLData/typemap approach; seems it is still not supported by org.postgresql driver. And the primary way of using the PostgreSQL's composite types in Java is to make a matching PGObject subclass, and bind it through PGConnection::addDataType.I'd be curious why and how the SQLData/typemap is better ?
Alas, there is rather little documentation on actually doing that; and the existing github code gives some hints, but insufficiently. I actually managed to map the type created like CREATE TYPE MYFUNC_RETURN_TYPE AS (key UUID) to a Java class. But then faced even a more basic issue: how to bind a type containing the text strings (like Text or varchar)?We would love for you to contribute to the documentation
The problems are: 1. how to parse (setValue) the serialized literal with this type properly? 2. And more importantly, how to serialize it (getValue) properly for PostgreSQL?Both questions assume that the string may be as wild as it happens - any Unicode, any single or double quotes, any combinations of backslash characters.are you sure you need to serialize it properly? The driver does encode strings that it sends to the backend.
2. More problem is with the serializing the data in the String getValue().I’ll have to create the string like ("Value of a","Value of b"); if some of them e.g. a is NULL, the string will look something like (,"Value of b").Well, okay; I should add leading/trailing brackets and split them with comma; and if a or b is null, put an empty string instead of it.But how to properly escape the string itself?The best I found is String.format("\"%s\"", Utils.escapeLiteral(null, str, true)) – but it doesn’t seem to handle various combinations of single or double quotes.Any idea how to escape the strings properly then?If you send such a string into setString does it fail ? (I'm genuinely curious)
String preEscaped = str
// replace <"> to <"">
String escaped = String.format("\"%s\"", preEscaped);
if (str == null) {
return "";
// replace <"> to <"">
.replaceAll("\"", "\"\"")
.replaceAll("\\\\", "\\\\\\\\");
return String.format("\"%s\"", escaped);
public static String tokenizeUuid(UUID uuid) {
public static String tokenizeBytea(byte[] bytea) {
return (bytea == null) ?
"" :
String.format("\"\\\\x%s\"", Utils.toHexString(bytea));
}
return String.format("(%s,%s,%s,%s)",
tokenizeString(a), tokenizeString(b)
);
}
The most documentation on this that I could find, is in https://www.postgresql.org/docs/current/rowtypes.html#ROWTYPES-IO-SYNTAX. I wrap the data it with parentheses as needed, and reference the part “Double quotes and backslashes embedded in field values will be doubled” to do my custom “string tokenization”. This documentation article also suggests to use ROW() method but it doesn’t work inside getValue()-provided strings.
Daveпн, 22 июл. 2019 г. в 00:58, Alexander Myodov <amyodov@gmail.com>: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--Alex Myodov
pgsql-jdbc by date: