On Jul 23, 12:06 am, kensystem <
kensys...@gmail.com> wrote:
> does anyone have suggestion on how to do
> handle bytea data from pg dumps?
So, looked at the info for PostgreSQL here:
http://www.postgresql.org/docs/8.0/static/datatype-binary.html
.. and, I've got to say that I feel for you. This could be messy, but
I think H2's functions can make it easier.
Here's what I suggest: make a copy of the script for experimentation,
and open it in a text editor.
Then use search-and-replace to convert PG's escaping for single
apostrophes (slash or unicode) to H2's escaping (doubled
apostrophes).
I *think* H2 will accept the BYTEA items as valid strings then, and
you can use string functions to convert it.
Try STRINGDECODE(STRINGDECODE(input)) and see if this converts it to
the proper string representation (it should).
Now you've got the BYTEA as a string corresponding to the ASCII, but
in Unicode format.
To convert the Unicode string to bytes in US-ASCII (I think that's
what PG is using?), you'll need to use java or a user-defined
function.
Copy the following to a file called H2Utils.java, and compile it to
a .class file:
public class H2Utils {
public static byte[] encodeString(String input, String encoding)
throws UnsupportedEncodingException {
return input.getBytes(encoding);
}
public static String decodeString(byte[] input, String encoding)
throws UnsupportedEncodingException {
return new String(input,encoding);
}
}
Set up the classpath for your H2 application so it includes the
H2Utils.class file.
Create user-defined functions for these methods with:
CREATE ALIAS IF NOT EXISTS string_to_bytes FOR 'H2Utils.decodeString';
CREATE ALIAS IF NOT EXISTS bytes_to_string FOR 'H2Utils.encodeString';
The final expression to produce your BINARY output from PG BYTEA input
(read by H2 as a CLOB or VARCHAR)
will be:
string_to_bytes(STRINGDECODE(STRINGDECODE(pg_bytea_string)),'US-
ASCII').
With some finagling and the above, you should be able to do a 2-stage
build from script, first reading BYTEA as
VARCHAR/CLOB, then using that function to convert to BINARY/BLOB.
Messy, but should work. Proviso:
not tested -- do let me know how well it works.
> tracking the values and aligning to the DDL-type and ordinal position is another
> thing :-)
You might find the functions in org.h2.values.DataType and
java.sql.Types useful here.
They assist with type conversions & type mapping.
(browse source online:
http://code.google.com/p/h2database/source/browse/trunk/h2/src/main/org/h2/value/DataType.java
)
Cheers,
Bob McGee