Skip to content

Commit

Permalink
updated tests according changes in getObject(). arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
chernser committed Oct 25, 2024
1 parent 8472f9a commit ae8aa27
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private <E> E castOrConvert(T o, Class<E> clazz) {
} else if (o instanceof LocalTime && clazz == java.sql.Time.class) {
return (E) java.sql.Time.valueOf((LocalTime) o);
}

return clazz.cast(o);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.clickhouse.data.ClickHouseChecker;
import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseValue;
import com.clickhouse.data.value.ClickHouseArrayValue;

public class ClickHouseArray implements Array {
private final int columnIndex;
Expand All @@ -25,6 +27,10 @@ protected void ensureValid() throws SQLException {
}
}

/**
* Returns Array base column
* @return
*/
protected ClickHouseColumn getBaseColumn() {
return resultSet.columns.get(columnIndex - 1).getArrayBaseColumn();
}
Expand All @@ -46,15 +52,26 @@ public int getBaseType() throws SQLException {
@Override
public Object getArray() throws SQLException {
ensureValid();

return resultSet.getObject(columnIndex);
return getArray(null);
}

@Override
public Object getArray(Map<String, Class<?>> map) throws SQLException {
ensureValid();

return resultSet.getObject(columnIndex, map);
ClickHouseValue v = resultSet.getValue(columnIndex);
Class<?> targetClass = map != null ? map.get(getBaseTypeName()) : null;
switch (getBaseColumn().getDataType()) {
case Date:
case Date32:
return ((ClickHouseArrayValue)v).asArray(targetClass == null ? java.sql.Date.class : targetClass);
case DateTime:
case DateTime32:
case DateTime64:
return ((ClickHouseArrayValue)v).asArray(targetClass == null ? java.sql.Timestamp.class : targetClass);
default:
return targetClass == null ? v.asArray() : v.asArray(targetClass);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.clickhouse.data.value.ClickHouseArrayValue;
import com.clickhouse.data.value.ClickHouseDateTimeValue;
import com.clickhouse.data.value.ClickHouseDateValue;
import com.clickhouse.data.value.ClickHouseOffsetDateTimeValue;

public class ClickHouseResultSet extends AbstractResultSet {
private ClickHouseRecord currentRow;
Expand Down Expand Up @@ -496,7 +497,7 @@ public Object getObject(int columnIndex, Map<String, Class<?>> map) throws SQLEx
}

Object value;
if (!wrapObject) {
if (!wrapObject && !c.isArray()) {
if (javaType == null) {
value = getJDBCDefault( v, c);
} else {
Expand All @@ -518,18 +519,10 @@ private Object getJDBCDefault(ClickHouseValue v, ClickHouseColumn c) {
return getDateFromValue( v, null);
} else if (v instanceof ClickHouseDateTimeValue) {
return getTimestampFromValue(v, c, null);
} else if (v instanceof ClickHouseOffsetDateTimeValue) {
return getTimestampFromValue( v, c, null);
} else if (v instanceof ClickHouseArrayValue<?>) {
switch (c.getArrayBaseColumn().getDataType()) {
case Date:
case Date32:
return ((ClickHouseArrayValue)v).asArray(java.sql.Date.class);
case DateTime:
case DateTime32:
case DateTime64:
return ((ClickHouseArrayValue)v).asArray(java.sql.Timestamp.class);
default:
return v.asObject();
}
throw new RuntimeException("This method does not support array type");
} else {
return v.asObject();
}
Expand All @@ -542,12 +535,17 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ

@Override
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
return getValue(columnIndex).asObject(type);
ClickHouseValue v = getValue(columnIndex);
if (v instanceof ClickHouseArrayValue) {
return (T) ((ClickHouseArrayValue)v).asArray(type.componentType());
} else {
return v.asObject(type);
}
}

@Override
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
return getValue(findColumn(columnLabel)).asObject(type);
return getObject(findColumn(columnLabel), type);
}

@Override
Expand Down Expand Up @@ -673,6 +671,9 @@ public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException
}

private Timestamp getTimestampFromValue(ClickHouseValue value, ClickHouseColumn column, Calendar cal) {
if (value.isNullOrEmpty()) {
return null;
}
TimeZone tz = column.getTimeZone();
LocalDateTime dt = tz == null ? value.asDateTime(column.getScale())
: value.asOffsetDateTime(column.getScale()).toLocalDateTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public void testCreateArray() throws SQLException {
array = conn.createArrayOf("Int8", new Byte[] { -1, 0, 1 });
Assert.assertEquals(array.getArray(), new byte[] { -1, 0, 1 });
array = conn.createArrayOf("UInt8", new Byte[] { -1, 0, 1 });
Assert.assertEquals(array.getArray(), new byte[] { -1, 0, 1 });
Assert.assertEquals(array.getArray(), new UnsignedByte[] { UnsignedByte.valueOf((byte) -1), UnsignedByte.ZERO,
UnsignedByte.ONE});

array = conn.createArrayOf("Nullable(Int8)", new Byte[] { -1, null, 1 });
Assert.assertEquals(array.getArray(), new Byte[] { -1, null, 1 });
Expand Down
Loading

0 comments on commit ae8aa27

Please sign in to comment.