title: Is it possible to write a jOOQ Converter to apply to an array?
body: <p>I have a postgres database with some columns that are of type <code>varchar[]</code>. jOOQ and pgjdbc-ng aren't playing along nicely; jOOQ's DefaultBindContext has something along the lines of:</p>
  
  <pre><code>protected final BindContext bindValue0(Object value, Field&lt;?&gt; field) throws SQLException {
      SQLDialect dialect = configuration.dialect();
  
      // [#650] [#3108] Use the Field's Converter before actually binding any value
      Converter&lt;?, ?&gt; converter = field.getConverter();
      Class&lt;?&gt; type = converter.fromType();
      value = ((Converter) converter).to(value);
  
      //...
  
      else if (type.isArray()) {
          switch (dialect) {
              case POSTGRES: {
                  stmt.setString(nextIndex(), toPGArrayString((Object[]) value));
                  break;
              }
  </code></pre>
  
  <p>Which sets the statement variable to <code>"{\"value1\", \"value2\", \"etc\"}"</code>, which is how you'd specify an array in a query. Later on, pgjdbc-ng has:</p>
  
  <pre><code>public static Object coerceToArray(Format format, Object val, Type type, Class&lt;?&gt; targetType, Map&lt;String, Class&lt;?&gt;&gt; typeMap, PGConnectionImpl connection) throws SQLException {
    if (val == null) {
      return null;
    }
    else if (val instanceof PGArray) {
      return coerceToArray(format, ((PGArray) val).getValue(), type, targetType, typeMap, connection);
    }
    else if (val.getClass().isArray()) {
      return coerceToArray(format, val, 0, Array.getLength(val), type, targetType, typeMap, connection);
    }
  
    throw createCoercionException(val.getClass(), targetType);
  }
  </code></pre>
  
  <p>Which expects that the value on the statement will be of either type PGArray or an actual array; it fails to coerce the String representation of an array into a String representation of an array. :(</p>
  
  <p>I am trying to write a jOOQ Converter that will convert between <code>String[]</code> and <code>PGArray</code>; ideally, this would mean that jOOQ's DefaultBindContext would leave the converted value well enough alone, and then pgjdbc-ng would be able to handle it correctly.</p>
  
  <p>However, I have been unable to write a jOOQ schema configuration that allows me to do this. I've tried variations on:</p>
  
  <pre><code>&lt;customType&gt;    
      &lt;customType&gt;
          &lt;name&gt;StringArray&lt;/name&gt;
          &lt;type&gt;java.lang.String[]&lt;/type&gt;
          &lt;converter&gt;my.package.PGStringArrayConverter&lt;/converter&gt;
      &lt;/customType&gt;
  &lt;/customTypes&gt;
  
  &lt;forcedTypes&gt;
      &lt;forcedType&gt;
          &lt;name&gt;StringArray&lt;/name&gt;
          &lt;types&gt;varchar\[\]&lt;/types&gt;
      &lt;/forcedType&gt;
  &lt;/forcedTypes&gt;
  </code></pre>
  
  <p>Without having any luck; the generated table objects refer to a <code>String[][]</code>, and <code>varchar[]</code> doesn't match on anything. Even if I break it down, so that the forcedType matches on any type but with an <code>&lt;expression&gt;</code> that only matches my column, and the Converter's type is <code>java.lang.String</code>, I end up with the java compiler complaining about being unable to cast Object[] to String[].</p>
  
  <p>Is there any light at the end of this tunnel, or should I start looking to migrate my database?</p>