Skip to content

Commit db4fcc6

Browse files
committed
Update ParameterMetaData tests
1 parent d88c74a commit db4fcc6

File tree

4 files changed

+87
-68
lines changed

4 files changed

+87
-68
lines changed

jdbc/src/main/java/tech/ydb/jdbc/impl/params/PreparedParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public String getNameByIndex(int index) throws SQLException {
129129
if (index <= 0 || index > paramNames.length) {
130130
throw new SQLException(YdbConst.PARAMETER_NUMBER_NOT_FOUND + index);
131131
}
132-
return paramNames[index - 1];
132+
return paramNames[index - 1].substring(YdbConst.VARIABLE_PARAMETER_PREFIX.length());
133133
}
134134

135135
@Override

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

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ private String scanSelectByKey(String column) throws SQLException {
128128
return QueryType.SCAN_QUERY.getPrefix() + "\n" + sql;
129129
}
130130

131-
private YdbPreparedStatement prepareUpsertValues() throws SQLException {
132-
return jdbc.connection().prepareStatement(SqlQueries.namedUpsertSQL(TEST_TABLE_NAME))
133-
.unwrap(YdbPreparedStatement.class);
134-
}
135-
136131
private YdbPreparedStatement prepareSelectAll() throws SQLException {
137132
return jdbc.connection().prepareStatement(TEST_TABLE.selectSQL())
138133
.unwrap(YdbPreparedStatement.class);
@@ -487,9 +482,11 @@ public void executeExplainQueryExplicitly(SqlQueries.YqlQuery mode) throws SQLEx
487482
}
488483
}
489484

490-
@Test
491-
public void testSetNull() throws SQLException {
492-
try (YdbPreparedStatement ps = prepareUpsertValues()) {
485+
@ParameterizedTest(name = "with {0}")
486+
@EnumSource(value = SqlQueries.YqlQuery.class)
487+
public void testSetNull(SqlQueries.YqlQuery mode) throws SQLException {
488+
String yql = TEST_TABLE.namedUpsertAll(mode);
489+
try (YdbPreparedStatement ps = jdbc.connection().unwrap(YdbConnection.class).prepareStatement(yql)) {
493490
ps.setInt("key", 1);
494491
YdbTypes types = ps.getConnection().getYdbTypes();
495492
ps.setNull("c_Bool", types.wrapYdbJdbcType(PrimitiveType.Bool));
@@ -517,7 +514,7 @@ public void testSetNull() throws SQLException {
517514
ps.executeUpdate();
518515
}
519516

520-
try (YdbPreparedStatement ps = prepareUpsertValues()) {
517+
try (YdbPreparedStatement ps = jdbc.connection().unwrap(YdbConnection.class).prepareStatement(yql)) {
521518
ps.setInt("key", 2);
522519
ps.setNull("c_Bool", -1, "Bool");
523520
ps.setNull("c_Int8", -1, "Int8");
@@ -544,7 +541,7 @@ public void testSetNull() throws SQLException {
544541
ps.executeUpdate();
545542
}
546543

547-
try (YdbPreparedStatement ps = prepareUpsertValues()) {
544+
try (YdbPreparedStatement ps = jdbc.connection().unwrap(YdbConnection.class).prepareStatement(yql)) {
548545
ps.setInt("key", 3);
549546
ps.setNull("c_Bool", -1);
550547
ps.setNull("c_Int8", -1);
@@ -590,9 +587,11 @@ public void testSetNull() throws SQLException {
590587
}
591588
}
592589

593-
@Test
594-
public void testParametersMeta() throws SQLException {
595-
try (YdbPreparedStatement ps = prepareUpsertValues()) {
590+
@ParameterizedTest(name = "with {0}")
591+
@EnumSource(value = SqlQueries.YqlQuery.class)
592+
public void testParametersMeta(SqlQueries.YqlQuery mode) throws SQLException {
593+
String yql = TEST_TABLE.namedUpsertAll(mode);
594+
try (YdbPreparedStatement ps = jdbc.connection().unwrap(YdbConnection.class).prepareStatement(yql)) {
596595
final ParameterMetaData meta = ps.getParameterMetaData();
597596
final YdbParameterMetaData ydbMeta = meta.unwrap(YdbParameterMetaData.class);
598597

@@ -603,6 +602,7 @@ public void testParametersMeta() throws SQLException {
603602
Assertions.assertEquals(22, meta.getParameterCount());
604603
for (int param = 1; param <= meta.getParameterCount(); param++) {
605604
String name = ydbMeta.getParameterName(param);
605+
boolean isKey = "key".equals(name);
606606

607607
Assertions.assertFalse(meta.isSigned(param), "All params are not isSigned");
608608
Assertions.assertEquals(0, meta.getPrecision(param), "No precision available");
@@ -613,14 +613,15 @@ public void testParametersMeta() throws SQLException {
613613
int type = meta.getParameterType(param);
614614
Assertions.assertTrue(type != 0, "All params have sql type, including " + name);
615615

616-
if (name.equals("$key")) {
617-
continue;
616+
if (isKey) {
617+
Assertions.assertEquals(ParameterMetaData.parameterNoNulls, meta.isNullable(param),
618+
"Primary key defined as not nullable");
619+
} else {
620+
Assertions.assertEquals(ParameterMetaData.parameterNullable, meta.isNullable(param),
621+
"All parameters expect primary key defined as nullable, including " + name);
618622
}
619623

620-
Assertions.assertEquals(ParameterMetaData.parameterNullable, meta.isNullable(param),
621-
"All parameters expect primary key defined as nullable, including " + name);
622-
623-
String expectType = name.substring("$c_".length()).toLowerCase();
624+
String expectType = isKey ? "int32" : name.substring("c_".length()).toLowerCase();
624625
if (expectType.equals("decimal")) {
625626
expectType += "(22, 9)";
626627
}
@@ -632,54 +633,54 @@ public void testParametersMeta() throws SQLException {
632633

633634
String expectClassName;
634635
switch (name) {
635-
case "$key":
636-
case "$c_Bool":
636+
case "c_Bool":
637637
expectClassName = Boolean.class.getName();
638638
break;
639-
case "$c_Int8":
639+
case "c_Int8":
640640
expectClassName = Byte.class.getName();
641641
break;
642-
case "$c_Int16":
642+
case "c_Int16":
643643
expectClassName = Short.class.getName();
644644
break;
645-
case "$c_Int32":
646-
case "$c_Uint8":
647-
case "$c_Uint16":
645+
case "key":
646+
case "c_Int32":
647+
case "c_Uint8":
648+
case "c_Uint16":
648649
expectClassName = Integer.class.getName();
649650
break;
650-
case "$c_Int64":
651-
case "$c_Uint64":
652-
case "$c_Uint32":
651+
case "c_Int64":
652+
case "c_Uint64":
653+
case "c_Uint32":
653654
expectClassName = Long.class.getName();
654655
break;
655-
case "$c_Float":
656+
case "c_Float":
656657
expectClassName = Float.class.getName();
657658
break;
658-
case "$c_Double":
659+
case "c_Double":
659660
expectClassName = Double.class.getName();
660661
break;
661-
case "$c_Text":
662-
case "$c_Json":
663-
case "$c_JsonDocument":
662+
case "c_Text":
663+
case "c_Json":
664+
case "c_JsonDocument":
664665
expectClassName = String.class.getName();
665666
break;
666-
case "$c_Bytes":
667-
case "$c_Yson":
667+
case "c_Bytes":
668+
case "c_Yson":
668669
expectClassName = byte[].class.getName();
669670
break;
670-
case "$c_Date":
671+
case "c_Date":
671672
expectClassName = LocalDate.class.getName();
672673
break;
673-
case "$c_Datetime":
674+
case "c_Datetime":
674675
expectClassName = LocalDateTime.class.getName();
675676
break;
676-
case "$c_Timestamp":
677+
case "c_Timestamp":
677678
expectClassName = Instant.class.getName();
678679
break;
679-
case "$c_Interval":
680+
case "c_Interval":
680681
expectClassName = Duration.class.getName();
681682
break;
682-
case "$c_Decimal":
683+
case "c_Decimal":
683684
expectClassName = DecimalValue.class.getName();
684685
break;
685686
default:

jdbc/src/test/java/tech/ydb/jdbc/impl/helper/SqlQueries.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public enum YqlQuery {
2828

2929
private static final String SELECT = YdbLookup.stringFileReference("classpath:sql/select.sql");
3030

31-
private static final String NAMED_UPSERT = YdbLookup.stringFileReference("classpath:sql/upsert/named.sql");
31+
private static final String SIMPLE_UPSERT_ALL = YdbLookup.stringFileReference("classpath:sql/upsert/simple.sql");
32+
private static final String NAMED_UPSERT_ALL = YdbLookup.stringFileReference("classpath:sql/upsert/named.sql");
33+
private static final String BATCHED_UPSERT_ALL = YdbLookup.stringFileReference("classpath:sql/upsert/batched.sql");
34+
private static final String INDEXED_UPSERT_ALL = YdbLookup.stringFileReference("classpath:sql/upsert/types.sql");
3235

3336
private static final String SELECT_ALL = "select * from #tableName";
3437
private static final String DELETE_ALL = "delete from #tableName";
@@ -103,6 +106,25 @@ public String deleteAllSQL() {
103106
return withTableName(DELETE_ALL);
104107
}
105108

109+
public String namedUpsertAll(YqlQuery mode) {
110+
switch (mode) {
111+
case BATCHED:
112+
return withTableName(BATCHED_UPSERT_ALL, tableName);
113+
case SIMPLE:
114+
default:
115+
return withTableName(NAMED_UPSERT_ALL, tableName);
116+
117+
}
118+
}
119+
120+
public String simpleUpsertAllSQL() {
121+
return withTableName(SIMPLE_UPSERT_ALL, tableName);
122+
}
123+
124+
public String indexesUpsertAllSQL() {
125+
return withTableName(INDEXED_UPSERT_ALL, tableName);
126+
}
127+
106128
/**
107129
* @param column name of column
108130
* @return select key, #column from #tableName */
@@ -137,8 +159,4 @@ public static String selectAllValuesSql(String tableName) {
137159
public static String upsertAllValuesSql(String tableName) {
138160
return withTableName(INIT_TABLE, tableName);
139161
}
140-
141-
public static String namedUpsertSQL(String tableName) {
142-
return withTableName(NAMED_UPSERT, tableName);
143-
}
144162
}
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
declare $list as List<Struct<
22
key:Int32,
33

4-
c_Bool:Bool?,
4+
c_Bool:Optional<Bool>,
55

6-
c_Int8:Int8?,
7-
c_Int16:Int16?,
8-
c_Int32:Int32?,
9-
c_Int64:Int64?,
6+
c_Int8:Optional<Int8>,
7+
c_Int16:Optional<Int16>,
8+
c_Int32:Optional<Int32>,
9+
c_Int64:Optional<Int64>,
1010

11-
c_Uint8:Uint8?,
12-
c_Uint16:Uint16?,
13-
c_Uint32:Uint32?,
14-
c_Uint64:Uint64?,
11+
c_Uint8:Optional<Uint8>,
12+
c_Uint16:Optional<Uint16>,
13+
c_Uint32:Optional<Uint32>,
14+
c_Uint64:Optional<Uint64>,
1515

16-
c_Float:Float?,
17-
c_Double:Double?,
16+
c_Float:Optional<Float>,
17+
c_Double:Optional<Double>,
1818

19-
c_Bytes:Bytes?,
20-
c_Text:Text?,
21-
c_Json:Json?,
22-
c_JsonDocument:JsonDocument?,
23-
c_Yson:Yson?,
19+
c_Bytes:Optional<Bytes>,
20+
c_Text:Optional<Text>,
21+
c_Json:Optional<Json>,
22+
c_JsonDocument:Optional<JsonDocument>,
23+
c_Yson:Optional<Yson>,
2424

25-
c_Date:Date?,
26-
c_Datetime:Datetime?,
27-
c_Timestamp:Timestamp?,
28-
c_Interval:Interval?,
25+
c_Date:Optional<Date>,
26+
c_Datetime:Optional<Datetime>,
27+
c_Timestamp:Optional<Timestamp>,
28+
c_Interval:Optional<Interval>,
2929

30-
c_Decimal:Decimal(22,9)?
30+
c_Decimal:Optional<Decimal(22,9)>
3131
>>;
3232

3333
upsert into #tableName select * from as_table($list)

0 commit comments

Comments
 (0)