java - Is it possible to write a jOOQ Converter to apply to an array? -



java - Is it possible to write a jOOQ Converter to apply to an array? -

i have postgres database columns of type varchar[]. jooq , pgjdbc-ng aren't playing along nicely; jooq's defaultbindcontext has along lines of:

protected final bindcontext bindvalue0(object value, field<?> field) throws sqlexception { sqldialect dialect = configuration.dialect(); // [#650] [#3108] utilize field's converter before binding value converter<?, ?> converter = field.getconverter(); class<?> type = converter.fromtype(); value = ((converter) converter).to(value); //... else if (type.isarray()) { switch (dialect) { case postgres: { stmt.setstring(nextindex(), topgarraystring((object[]) value)); break; }

which sets statement variable "{\"value1\", \"value2\", \"etc\"}", how you'd specify array in query. later on, pgjdbc-ng has:

public static object coercetoarray(format format, object val, type type, class<?> targettype, map<string, class<?>> typemap, pgconnectionimpl connection) throws sqlexception { if (val == null) { homecoming null; } else if (val instanceof pgarray) { homecoming coercetoarray(format, ((pgarray) val).getvalue(), type, targettype, typemap, connection); } else if (val.getclass().isarray()) { homecoming coercetoarray(format, val, 0, array.getlength(val), type, targettype, typemap, connection); } throw createcoercionexception(val.getclass(), targettype); }

which expects value on statement of either type pgarray or actual array; fails coerce string representation of array string representation of array. :(

i trying write jooq converter convert between string[] , pgarray; ideally, mean jooq's defaultbindcontext leave converted value plenty alone, , pgjdbc-ng able handle correctly.

however, have been unable write jooq schema configuration allows me this. i've tried variations on:

<customtype> <customtype> <name>stringarray</name> <type>java.lang.string[]</type> <converter>my.package.pgstringarrayconverter</converter> </customtype> </customtypes> <forcedtypes> <forcedtype> <name>stringarray</name> <types>varchar\[\]</types> </forcedtype> </forcedtypes>

without having luck; generated table objects refer string[][], , varchar[] doesn't match on anything. if break down, forcedtype matches on type <expression> matches column, , converter's type java.lang.string, end java compiler complaining beingness unable cast object[] string[].

is there lite @ end of tunnel, or should start looking migrate database?

so, turns out reply is, "kinda."

i creating pgstringarrayconverter class backwards; had created

public abstract class pgarrayconverter implements converter<string[], pgarray>

what ended working was:

public abstract class pgarrayconverter implements converter<object, string[]> { @override public string[] from(final object databaseobject) { if (databaseobject == null) { homecoming null; } if (databaseobject.getclass().isarray()) { homecoming (string[]) databaseobject; } homecoming (string[]) ((pgarray)databaseobject).getvalue(); } @override public object to(final string[] userobject) { if (userobject == null) { homecoming null; } homecoming new pgarray(null, null, userobject); } @override public class<object> fromtype() { homecoming object.class; } @override public class<string[]> totype() { homecoming string[].class; } }

from() turned out kind of odd; getting arrays of strings, rather pgarrays. don't think database aware "supposed to" serialize them pgarrays.

i had modify generated code, obnoxious. schema.xml file contained:

<customtype> <name>stringarray</name> <type>java.lang.string</type> <converter>my.package.pgstringarrayconverter</converter> </customtype> <forcedtype> <name>stringarray</name> <expression>.*tabular_answer_entry.text.*</expression> <types>.*</types> </forcedtype>

and had alter generated table files remove .getarraydatatype() phone call on createfield's info type:

public final org.jooq.tablefield<my.package.gen.tables.records.tabularanswerentryrecord, java.lang.string[]> text = createfield("text", org.jooq.impl.defaultdatatype.getdefaultdatatype("java.lang.string").getarraydatatype(), this, "", new my.package.pgstringarrayconverter());

to:

public final org.jooq.tablefield<my.package.gen.tables.records.tabularanswerentryrecord, java.lang.string[]> text = createfield("text", org.jooq.impl.defaultdatatype.getdefaultdatatype("java.lang.string"), this, "", new my.package.pgstringarrayconverter());

the whole solution feels kind of hacky, , going have write monster of unit test create sure doesn't break when update of relevant packages, @ to the lowest degree able read , write database.

java postgresql jooq pg-jdbc

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -