Search in sources :

Example 1 with DataTypeException

use of org.jooq.exception.DataTypeException in project jOOQ by jOOQ.

the class DefaultBinding method pgNewArray.

/**
     * Create an array from a String
     * <p>
     * Unfortunately, this feature is very poorly documented and true UDT
     * support by the PostGreSQL JDBC driver has been postponed for a long time.
     *
     * @param string A String representation of an array
     * @return The converted array
     */
private static final Object[] pgNewArray(Class<?> type, String string) {
    if (string == null) {
        return null;
    }
    try {
        Class<?> component = type.getComponentType();
        List<String> values = PostgresUtils.toPGArray(string);
        if (values.isEmpty()) {
            return (Object[]) java.lang.reflect.Array.newInstance(component, 0);
        } else {
            Object[] result = (Object[]) java.lang.reflect.Array.newInstance(component, values.size());
            for (int i = 0; i < values.size(); i++) result[i] = pgFromString(type.getComponentType(), values.get(i));
            return result;
        }
    } catch (Exception e) {
        throw new DataTypeException("Error while creating array", e);
    }
}
Also used : DataTypeException(org.jooq.exception.DataTypeException) PostgresUtils.toPGArrayString(org.jooq.util.postgres.PostgresUtils.toPGArrayString) DataTypeException(org.jooq.exception.DataTypeException) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) MappingException(org.jooq.exception.MappingException) SQLException(java.sql.SQLException)

Example 2 with DataTypeException

use of org.jooq.exception.DataTypeException in project jOOQ by jOOQ.

the class PostgresUtils method toBytesFromHexEncoding.

private static byte[] toBytesFromHexEncoding(String string) {
    String hex = string.substring(POSTGRESQL_HEX_STRING_PREFIX.length());
    final StringReader input = new StringReader(hex);
    final ByteArrayOutputStream bytes = new ByteArrayOutputStream(hex.length() / 2);
    int hexDigit;
    int byteValue;
    try {
        while ((hexDigit = input.read()) != -1) {
            byteValue = (hexValue(hexDigit) << 4);
            if ((hexDigit = input.read()) == -1) {
                break;
            }
            byteValue += hexValue(hexDigit);
            bytes.write(byteValue);
        }
    }// should never happen for a string reader
     catch (IOException e) {
        throw new DataTypeException("Error while decoding hex string", e);
    }
    input.close();
    return bytes.toByteArray();
}
Also used : DataTypeException(org.jooq.exception.DataTypeException) StringReader(java.io.StringReader) Integer.toOctalString(java.lang.Integer.toOctalString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with DataTypeException

use of org.jooq.exception.DataTypeException in project jOOQ by jOOQ.

the class PostgresUtils method toBytesFromOctalEncoding.

private static byte[] toBytesFromOctalEncoding(final String string) {
    final Reader reader = new StringReader(string);
    final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    try {
        convertOctalToBytes(reader, bytes);
        return bytes.toByteArray();
    } catch (IOException x) {
        throw new DataTypeException("failed to parse octal hex string: " + x.getMessage(), x);
    }
}
Also used : DataTypeException(org.jooq.exception.DataTypeException) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

DataTypeException (org.jooq.exception.DataTypeException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 Reader (java.io.Reader)1 Integer.toOctalString (java.lang.Integer.toOctalString)1 SQLException (java.sql.SQLException)1 MappingException (org.jooq.exception.MappingException)1 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)1 PostgresUtils.toPGArrayString (org.jooq.util.postgres.PostgresUtils.toPGArrayString)1