Search in sources :

Example 1 with JdbcArray

use of org.h2.jdbc.JdbcArray in project h2database by h2database.

the class JdbcConnection method createArrayOf.

/**
 * Create a new Array object.
 *
 * @param typeName the type name
 * @param elements the values
 * @return the array
 */
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
    try {
        int id = getNextId(TraceObject.ARRAY);
        debugCodeAssign("Array", TraceObject.ARRAY, id, "createArrayOf()");
        checkClosed();
        Value value = DataType.convertToValue(session, elements, Value.ARRAY);
        return new JdbcArray(this, value, id);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Value(org.h2.value.Value) Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 2 with JdbcArray

use of org.h2.jdbc.JdbcArray in project sqlg by pietermartin.

the class H2Dialect method sqlArrayTypeNameToPropertyType.

/**
 * All this is because H2 does not return the TYPE_NAME for column meta data.
 * The strategy is to actualy query the table get the column's value and interrogate it to get its type.
 * If the column has no data then we are stuffed and an exception is thrown.
 * @param typeName
 * @param sqlgGraph
 * @param schema
 * @param table
 * @param columnName
 * @param metaDataIter
 * @return
 */
@Override
public PropertyType sqlArrayTypeNameToPropertyType(String typeName, SqlgGraph sqlgGraph, String schema, String table, String columnName, ListIterator<Triple<String, Integer, String>> metaDataIter) {
    Connection connection = sqlgGraph.tx().getConnection();
    try (Statement statement = connection.createStatement()) {
        String sql = "SELECT \"" + columnName + "\" FROM \"" + schema + "\".\"" + table + "\" WHERE \"" + columnName + "\" IS NOT NULL LIMIT 1";
        ResultSet rs = statement.executeQuery(sql);
        if (!rs.next()) {
            throw new IllegalStateException("The sqlg_schema can not be created because the column " + schema + "." + table + "." + columnName + " has no data in it. For arrays on H2 there must be data in the column to determine the type.");
        } else {
            java.sql.Array o = rs.getArray(1);
            JdbcArray jdbcArray = (JdbcArray) o;
            Object[] array = (Object[]) jdbcArray.getArray();
            for (Object o1 : array) {
                if (o1 instanceof Byte) {
                    return PropertyType.BYTE_ARRAY;
                } else if (o1 instanceof Short) {
                    return PropertyType.SHORT_ARRAY;
                } else if (o1 instanceof Integer) {
                    return PropertyType.INTEGER_ARRAY;
                } else if (o1 instanceof Long) {
                    return PropertyType.LONG_ARRAY;
                } else if (o1 instanceof Float) {
                    return PropertyType.FLOAT_ARRAY;
                } else if (o1 instanceof Double) {
                    return PropertyType.DOUBLE_ARRAY;
                } else if (o1 instanceof String) {
                    return PropertyType.STRING_ARRAY;
                } else if (o1 instanceof Timestamp) {
                    // ja well this sucks but I know of no other way to distinguish between LocalDateTime and LocalDate
                    Timestamp timestamp = (Timestamp) o1;
                    LocalDateTime localDateTime = timestamp.toLocalDateTime();
                    if (localDateTime.getHour() == 0 && localDateTime.getMinute() == 0 && localDateTime.getSecond() == 0 && localDateTime.getNano() == 0) {
                        return PropertyType.LOCALDATE_ARRAY;
                    } else {
                        return PropertyType.LOCALDATETIME_ARRAY;
                    }
                } else if (o1 instanceof Time) {
                    return PropertyType.LOCALTIME_ARRAY;
                } else {
                    throw new UnsupportedOperationException("H2 does not support typeName on arrays");
                }
            }
        }
        rs.close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    throw new UnsupportedOperationException("H2 does not support typeName on arrays");
}
Also used : JdbcArray(org.h2.jdbc.JdbcArray) java.sql(java.sql)

Example 3 with JdbcArray

use of org.h2.jdbc.JdbcArray in project h2database by h2database.

the class JdbcResultSet method extractObjectOfType.

private <T> T extractObjectOfType(Class<T> type, Value value) throws SQLException {
    if (value == ValueNull.INSTANCE) {
        return null;
    }
    if (type == BigDecimal.class) {
        return type.cast(value.getBigDecimal());
    } else if (type == BigInteger.class) {
        return type.cast(value.getBigDecimal().toBigInteger());
    } else if (type == String.class) {
        return type.cast(value.getString());
    } else if (type == Boolean.class) {
        return type.cast(value.getBoolean());
    } else if (type == Byte.class) {
        return type.cast(value.getByte());
    } else if (type == Short.class) {
        return type.cast(value.getShort());
    } else if (type == Integer.class) {
        return type.cast(value.getInt());
    } else if (type == Long.class) {
        return type.cast(value.getLong());
    } else if (type == Float.class) {
        return type.cast(value.getFloat());
    } else if (type == Double.class) {
        return type.cast(value.getDouble());
    } else if (type == Date.class) {
        return type.cast(value.getDate());
    } else if (type == Time.class) {
        return type.cast(value.getTime());
    } else if (type == Timestamp.class) {
        return type.cast(value.getTimestamp());
    } else if (type == java.util.Date.class) {
        return type.cast(new java.util.Date(value.getTimestamp().getTime()));
    } else if (type == Calendar.class) {
        Calendar calendar = DateTimeUtils.createGregorianCalendar();
        calendar.setTime(value.getTimestamp());
        return type.cast(calendar);
    } else if (type == UUID.class) {
        return type.cast(value.getObject());
    } else if (type == byte[].class) {
        return type.cast(value.getBytes());
    } else if (type == java.sql.Array.class) {
        int id = getNextId(TraceObject.ARRAY);
        return type.cast(value == ValueNull.INSTANCE ? null : new JdbcArray(conn, value, id));
    } else if (type == Blob.class) {
        int id = getNextId(TraceObject.BLOB);
        return type.cast(value == ValueNull.INSTANCE ? null : new JdbcBlob(conn, value, id));
    } else if (type == Clob.class) {
        int id = getNextId(TraceObject.CLOB);
        return type.cast(value == ValueNull.INSTANCE ? null : new JdbcClob(conn, value, id));
    } else if (type == TimestampWithTimeZone.class) {
        return type.cast(value.getObject());
    } else if (DataType.isGeometryClass(type)) {
        return type.cast(value.getObject());
    } else if (type == LocalDateTimeUtils.LOCAL_DATE) {
        return type.cast(LocalDateTimeUtils.valueToLocalDate(value));
    } else if (type == LocalDateTimeUtils.LOCAL_TIME) {
        return type.cast(LocalDateTimeUtils.valueToLocalTime(value));
    } else if (type == LocalDateTimeUtils.LOCAL_DATE_TIME) {
        return type.cast(LocalDateTimeUtils.valueToLocalDateTime(value));
    } else if (type == LocalDateTimeUtils.INSTANT) {
        return type.cast(LocalDateTimeUtils.valueToInstant(value));
    } else if (type == LocalDateTimeUtils.OFFSET_DATE_TIME) {
        return type.cast(LocalDateTimeUtils.valueToOffsetDateTime(value));
    } else {
        throw unsupported(type.getName());
    }
}
Also used : Calendar(java.util.Calendar) Time(java.sql.Time) ValueTime(org.h2.value.ValueTime) ValueDouble(org.h2.value.ValueDouble) ValueDate(org.h2.value.ValueDate) Date(java.sql.Date) Array(java.sql.Array) ValueLong(org.h2.value.ValueLong) BigInteger(java.math.BigInteger) ValueBoolean(org.h2.value.ValueBoolean) UUID(java.util.UUID) NClob(java.sql.NClob) Clob(java.sql.Clob) ValueShort(org.h2.value.ValueShort)

Example 4 with JdbcArray

use of org.h2.jdbc.JdbcArray in project h2database by h2database.

the class JdbcResultSet method getArray.

/**
 * Returns the value of the specified column as an Array.
 *
 * @param columnLabel the column label
 * @return the value
 * @throws SQLException if the column is not found or if the result set is
 *             closed
 */
@Override
public Array getArray(String columnLabel) throws SQLException {
    try {
        int id = getNextId(TraceObject.ARRAY);
        if (isDebugEnabled()) {
            debugCodeAssign("Array", TraceObject.ARRAY, id, "getArray(" + quote(columnLabel) + ")");
        }
        Value v = get(columnLabel);
        return v == ValueNull.INSTANCE ? null : new JdbcArray(conn, v, id);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 5 with JdbcArray

use of org.h2.jdbc.JdbcArray in project h2database by h2database.

the class JdbcResultSet method getArray.

/**
 * Returns the value of the specified column as an Array.
 *
 * @param columnIndex (1,2,...)
 * @return the value
 * @throws SQLException if the column is not found or if the result set is
 *             closed
 */
@Override
public Array getArray(int columnIndex) throws SQLException {
    try {
        int id = getNextId(TraceObject.ARRAY);
        if (isDebugEnabled()) {
            debugCodeAssign("Array", TraceObject.ARRAY, id, "getArray(" + columnIndex + ")");
        }
        Value v = get(columnIndex);
        return v == ValueNull.INSTANCE ? null : new JdbcArray(conn, v, id);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Aggregations

SQLException (java.sql.SQLException)3 DbException (org.h2.message.DbException)3 Value (org.h2.value.Value)3 BigInteger (java.math.BigInteger)1 java.sql (java.sql)1 Array (java.sql.Array)1 Clob (java.sql.Clob)1 Date (java.sql.Date)1 NClob (java.sql.NClob)1 SQLClientInfoException (java.sql.SQLClientInfoException)1 Savepoint (java.sql.Savepoint)1 Time (java.sql.Time)1 Calendar (java.util.Calendar)1 UUID (java.util.UUID)1 JdbcArray (org.h2.jdbc.JdbcArray)1 ValueBoolean (org.h2.value.ValueBoolean)1 ValueDate (org.h2.value.ValueDate)1 ValueDouble (org.h2.value.ValueDouble)1 ValueLong (org.h2.value.ValueLong)1 ValueShort (org.h2.value.ValueShort)1