Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions jdbc/src/main/java/tech/ydb/jdbc/common/TypeDescription.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package tech.ydb.jdbc.common;

import java.sql.SQLDataException;
import java.sql.SQLException;
import java.util.Objects;

import tech.ydb.table.values.OptionalType;
import tech.ydb.table.values.OptionalValue;
import tech.ydb.table.values.PrimitiveType;
import tech.ydb.table.values.Type;
import tech.ydb.table.values.Value;

public class TypeDescription {
private final Type type;
Expand Down Expand Up @@ -74,8 +77,12 @@ public MappingGetters.Getters getters() {
return getters;
}

public MappingSetters.Setters setters() {
return setters;
public Value<?> toYdbValue(Object obj) throws SQLException {
try {
return setters.toValue(obj);
} catch (RuntimeException ex) {
throw new SQLDataException(ex.getMessage(), ex);
}
}

public Type ydbType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void setValue(Object obj, int sqlType) throws SQLException {
return;
}

value = type.setters().toValue(obj);
value = type.toYdbValue(obj);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void setValue(Object obj, int sqlType) throws SQLException {
return;
}

value = type.setters().toValue(obj);
value = type.toYdbValue(obj);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ public void setValue(Object obj, int sqlType) throws SQLException {
return;
}

value = types.find(type).setters().toValue(obj);
value = types.find(type).toYdbValue(obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public TypeDescription getType() {

@Override
public void setValue(Object obj, int sqlType) throws SQLException {
value = desc.setters().toValue(obj);
value = desc.toYdbValue(obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static Value<?> readValue(String name, Object value, TypeDescription type
}
}
} else {
Value<?> targetValue = type.setters().toValue(value);
Value<?> targetValue = type.toYdbValue(value);
if (type.isOptional()) {
return targetValue.makeOptional();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,18 @@ public void timestampTest(SqlQueries.JdbcQuery query) throws SQLException {
ps.setObject(2, "2011-12-03T10:15:30.456789123Z", ydbSqlType);
ps.execute();
}

// Wrong values
ps.setInt(1, 8);
ExceptionAssert.sqlDataException(
"Instant value is before minimum timestamp(1970-01-01 00:00:00.000000): 1969-12-31T23:59:59.999Z",
() -> ps.setTimestamp(2, Timestamp.from(Instant.EPOCH.minusMillis(1)))
);

ExceptionAssert.sqlDataException(
"Instant value is after maximum timestamp(2105-12-31 23:59:59.999999): 2106-01-01T00:00:00Z",
() -> ps.setTimestamp(2, Timestamp.from(Instant.ofEpochSecond(4291747200l)))
);
}

try (Statement statement = jdbc.connection().createStatement()) {
Expand Down