Skip to content

Commit 5738f59

Browse files
authored
Use SQLDataException on invalid values (#125)
2 parents 1228752 + c03cc4f commit 5738f59

File tree

7 files changed

+26
-7
lines changed

7 files changed

+26
-7
lines changed

jdbc/src/main/java/tech/ydb/jdbc/common/TypeDescription.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package tech.ydb.jdbc.common;
22

3+
import java.sql.SQLDataException;
4+
import java.sql.SQLException;
35
import java.util.Objects;
46

57
import tech.ydb.table.values.OptionalType;
68
import tech.ydb.table.values.OptionalValue;
79
import tech.ydb.table.values.PrimitiveType;
810
import tech.ydb.table.values.Type;
11+
import tech.ydb.table.values.Value;
912

1013
public class TypeDescription {
1114
private final Type type;
@@ -74,8 +77,12 @@ public MappingGetters.Getters getters() {
7477
return getters;
7578
}
7679

77-
public MappingSetters.Setters setters() {
78-
return setters;
80+
public Value<?> toYdbValue(Object obj) throws SQLException {
81+
try {
82+
return setters.toValue(obj);
83+
} catch (RuntimeException ex) {
84+
throw new SQLDataException(ex.getMessage(), ex);
85+
}
7986
}
8087

8188
public Type ydbType() {

jdbc/src/main/java/tech/ydb/jdbc/query/params/AsTableJdbcPrm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void setValue(Object obj, int sqlType) throws SQLException {
117117
return;
118118
}
119119

120-
value = type.setters().toValue(obj);
120+
value = type.toYdbValue(obj);
121121
}
122122

123123
@Override

jdbc/src/main/java/tech/ydb/jdbc/query/params/InListJdbcPrm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void setValue(Object obj, int sqlType) throws SQLException {
112112
return;
113113
}
114114

115-
value = type.setters().toValue(obj);
115+
value = type.toYdbValue(obj);
116116
}
117117

118118
@Override

jdbc/src/main/java/tech/ydb/jdbc/query/params/SimpleJdbcPrm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ public void setValue(Object obj, int sqlType) throws SQLException {
6767
return;
6868
}
6969

70-
value = types.find(type).setters().toValue(obj);
70+
value = types.find(type).toYdbValue(obj);
7171
}
7272
}

jdbc/src/main/java/tech/ydb/jdbc/query/params/UInt64JdbcPrm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ public TypeDescription getType() {
4949

5050
@Override
5151
public void setValue(Object obj, int sqlType) throws SQLException {
52-
value = desc.setters().toValue(obj);
52+
value = desc.toYdbValue(obj);
5353
}
5454
}

jdbc/src/main/java/tech/ydb/jdbc/query/params/ValueFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static Value<?> readValue(String name, Object value, TypeDescription type
4949
}
5050
}
5151
} else {
52-
Value<?> targetValue = type.setters().toValue(value);
52+
Value<?> targetValue = type.toYdbValue(value);
5353
if (type.isOptional()) {
5454
return targetValue.makeOptional();
5555
} else {

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,18 @@ public void timestampTest(SqlQueries.JdbcQuery query) throws SQLException {
11221122
ps.setObject(2, "2011-12-03T10:15:30.456789123Z", ydbSqlType);
11231123
ps.execute();
11241124
}
1125+
1126+
// Wrong values
1127+
ps.setInt(1, 8);
1128+
ExceptionAssert.sqlDataException(
1129+
"Instant value is before minimum timestamp(1970-01-01 00:00:00.000000): 1969-12-31T23:59:59.999Z",
1130+
() -> ps.setTimestamp(2, Timestamp.from(Instant.EPOCH.minusMillis(1)))
1131+
);
1132+
1133+
ExceptionAssert.sqlDataException(
1134+
"Instant value is after maximum timestamp(2105-12-31 23:59:59.999999): 2106-01-01T00:00:00Z",
1135+
() -> ps.setTimestamp(2, Timestamp.from(Instant.ofEpochSecond(4291747200l)))
1136+
);
11251137
}
11261138

11271139
try (Statement statement = jdbc.connection().createStatement()) {

0 commit comments

Comments
 (0)