Skip to content

Commit dc222a7

Browse files
committed
Fix old-style positional parameters
1 parent d4fe2fd commit dc222a7

File tree

3 files changed

+112
-68
lines changed

3 files changed

+112
-68
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public boolean hasFreeParameters() {
5050
}
5151

5252
public String getParameterName(int parameterIndex) {
53-
if (extraParams == null) {
53+
if (!hasFreeParameters()) {
5454
return YdbConst.INDEXED_PARAMETER_PREFIX + parameterIndex;
5555
}
5656

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package tech.ydb.jdbc;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.PreparedStatement;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.RegisterExtension;
12+
13+
import tech.ydb.test.junit5.YdbHelperExtention;
14+
15+
/**
16+
*
17+
* @author Aleksandr Gorshenin
18+
*/
19+
public class YdbDriverExampleTest {
20+
@RegisterExtension
21+
private static final YdbHelperExtention ydb = new YdbHelperExtention();
22+
23+
private static String jdbcURL() {
24+
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
25+
.append(ydb.useTls() ? "grpcs://" : "grpc://")
26+
.append(ydb.endpoint())
27+
.append(ydb.database())
28+
.append("?");
29+
30+
if (ydb.authToken() != null) {
31+
jdbc.append("token=").append(ydb.authToken()).append("&");
32+
}
33+
34+
return jdbc.toString();
35+
}
36+
37+
@Test
38+
public void testYdb() throws SQLException {
39+
String url = jdbcURL(); // "jdbc:ydb:localhost:2135/local"
40+
try (Connection connection = DriverManager.getConnection(url)) {
41+
try {
42+
connection.createStatement()
43+
.execute("--jdbc:SCHEME\n" +
44+
"drop table table_sample");
45+
} catch (SQLException e) {
46+
//
47+
}
48+
connection.createStatement()
49+
.execute("--jdbc:SCHEME\n" +
50+
"create table table_sample(id Int32, value Text, primary key (id))");
51+
52+
try (PreparedStatement ps = connection.prepareStatement("" +
53+
"upsert into table_sample (id, value) values (?, ?)")) {
54+
55+
ps.setInt(1, 1);
56+
ps.setString(2, "value-1");
57+
ps.executeUpdate();
58+
59+
ps.setInt(1, 2);
60+
ps.setString(2, "value-2");
61+
ps.executeUpdate();
62+
}
63+
64+
try (PreparedStatement ps = connection.prepareStatement("" +
65+
"declare $p1 as Int32;\n" +
66+
"declare $p2 as Text;\n" +
67+
"upsert into table_sample (id, value) values ($p1, $p2)")) {
68+
69+
ps.setInt(1, 3);
70+
ps.setString(2, "value-3");
71+
ps.executeUpdate();
72+
73+
ps.setInt(1, 4);
74+
ps.setString(2, "value-4");
75+
ps.executeUpdate();
76+
}
77+
78+
try (PreparedStatement select = connection
79+
.prepareStatement("select count(1) as cnt from table_sample")) {
80+
ResultSet rs = select.executeQuery();
81+
rs.next();
82+
Assertions.assertEquals(4, rs.getLong("cnt"));
83+
}
84+
85+
try (YdbPreparedStatement psBatch = connection
86+
.prepareStatement("" +
87+
"declare $values as List<Struct<id:Int32,value:Text>>;\n" +
88+
"upsert into table_sample select * from as_table($values)")
89+
.unwrap(YdbPreparedStatement.class)) {
90+
91+
psBatch.setInt("id", 5);
92+
psBatch.setString("value", "value-5");
93+
psBatch.addBatch();
94+
95+
psBatch.setInt("id", 6);
96+
psBatch.setString("value", "value-6");
97+
psBatch.addBatch();
98+
99+
psBatch.executeBatch();
100+
}
101+
102+
try (PreparedStatement select = connection
103+
.prepareStatement("select count(1) as cnt from table_sample")) {
104+
ResultSet rs = select.executeQuery();
105+
rs.next();
106+
Assertions.assertEquals(6, rs.getLong("cnt"));
107+
}
108+
}
109+
}
110+
}

src/test/java/tech/ydb/jdbc/YdbDriverIntegrationTest.java

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
import java.sql.Connection;
44
import java.sql.DriverManager;
5-
import java.sql.PreparedStatement;
6-
import java.sql.ResultSet;
75
import java.sql.SQLException;
86
import java.util.Properties;
97

10-
import org.junit.jupiter.api.AfterAll;
118
import org.junit.jupiter.api.Assertions;
129
import org.junit.jupiter.api.Test;
1310
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -26,6 +23,7 @@ public class YdbDriverIntegrationTest {
2623

2724
private static String jdbcURL(String... extras) {
2825
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
26+
.append(ydb.useTls() ? "grpcs://" : "grpc://")
2927
.append(ydb.endpoint())
3028
.append(ydb.database())
3129
.append("?");
@@ -41,13 +39,6 @@ private static String jdbcURL(String... extras) {
4139
return jdbc.toString();
4240
}
4341

44-
@AfterAll
45-
public static void afterAll() throws SQLException {
46-
if (YdbDriver.isRegistered()) {
47-
YdbDriver.deregister();
48-
}
49-
}
50-
5142
@Test
5243
public void connect() throws SQLException {
5344
try (Connection connection = DriverManager.getConnection(jdbcURL())) {
@@ -119,61 +110,4 @@ public void testContextCache() throws SQLException {
119110
Assertions.assertNotSame(ctx, unwrapped.getCtx());
120111
}
121112
}
122-
123-
@Test
124-
public void testYdb() throws SQLException {
125-
try (Connection connection = DriverManager.getConnection(jdbcURL())) {
126-
try {
127-
connection.createStatement().execute(
128-
"--jdbc:SCHEME\n"
129-
+ "drop table jdbc_table_sample");
130-
} catch (SQLException e) {
131-
//
132-
}
133-
connection.createStatement()
134-
.execute("--jdbc:SCHEME\n"
135-
+ "create table jdbc_table_sample(id Int32, value Text, primary key (id))");
136-
137-
PreparedStatement ps = connection
138-
.prepareStatement("" +
139-
"upsert into jdbc_table_sample (id, value) values (?, ?)");
140-
ps.setInt(1, 1);
141-
ps.setString(2, "value-1");
142-
ps.executeUpdate();
143-
144-
ps.setInt(1, 2);
145-
ps.setString(2, "value-2");
146-
ps.executeUpdate();
147-
148-
connection.commit();
149-
150-
151-
PreparedStatement select = connection
152-
.prepareStatement("select count(1) as cnt from jdbc_table_sample");
153-
ResultSet rs = select.executeQuery();
154-
rs.next();
155-
Assertions.assertEquals(2, rs.getLong("cnt"));
156-
157-
YdbPreparedStatement psBatch = connection
158-
.prepareStatement("" +
159-
"declare $values as List<Struct<id:Int32,value:Text>>;\n" +
160-
"upsert into jdbc_table_sample select * from as_table($values)")
161-
.unwrap(YdbPreparedStatement.class);
162-
psBatch.setInt("id", 3);
163-
psBatch.setString("value", "value-3");
164-
psBatch.addBatch();
165-
166-
psBatch.setInt("id", 4);
167-
psBatch.setString("value", "value-4");
168-
psBatch.addBatch();
169-
170-
psBatch.executeBatch();
171-
172-
connection.commit();
173-
174-
rs = select.executeQuery();
175-
rs.next();
176-
Assertions.assertEquals(4, rs.getLong("cnt"));
177-
}
178-
}
179113
}

0 commit comments

Comments
 (0)