Skip to content

Commit 2e3585a

Browse files
committed
Fixed argument count in the statement's metadata
1 parent 1c2a7df commit 2e3585a

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.clickhouse.client.api.DataTypeUtils;
44
import com.clickhouse.client.api.metadata.TableSchema;
55
import com.clickhouse.client.api.sql.SQLUtils;
6+
import com.clickhouse.data.ClickHouseColumn;
67
import com.clickhouse.data.ClickHouseDataType;
78
import com.clickhouse.data.Tuple;
89
import com.clickhouse.jdbc.internal.ExceptionUtils;
@@ -60,6 +61,8 @@
6061
import java.util.UUID;
6162
import java.util.regex.Matcher;
6263
import java.util.regex.Pattern;
64+
import java.util.stream.Collectors;
65+
import java.util.stream.IntStream;
6366

6467
public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper {
6568
private static final Logger LOG = LoggerFactory.getLogger(PreparedStatementImpl.class);
@@ -251,7 +254,7 @@ public void clearParameters() throws SQLException {
251254
}
252255

253256
int getParametersCount() {
254-
return values.length;
257+
return argCount;
255258
}
256259

257260
@Override
@@ -409,7 +412,10 @@ public ResultSetMetaData getMetaData() throws SQLException {
409412
}
410413

411414
if (resultSetMetaData == null) {
412-
resultSetMetaData = new ResultSetMetaDataImpl(Collections.emptyList(),
415+
List<ClickHouseColumn> columns = IntStream.range(0, argCount)
416+
.mapToObj(value -> ClickHouseColumn.of("v_" + value, "Nothing"))
417+
.collect(Collectors.toList());
418+
resultSetMetaData = new ResultSetMetaDataImpl(columns,
413419
connection.getSchema(), connection.getCatalog(),
414420
"", JdbcUtils.DATA_TYPE_CLASS_MAP);
415421
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,4 +1404,16 @@ public void testCheckOfParametersAreSet() throws Exception {
14041404
}
14051405
}
14061406
}
1407+
1408+
@Test
1409+
public void testParameterCount() throws Exception {
1410+
try (Connection conn = getJdbcConnection();) {
1411+
try (PreparedStatement stmt = conn.prepareStatement("select ?, ? as v1, ? as v2")) {
1412+
Assert.assertEquals(stmt.getMetaData().getColumnCount(), 3);
1413+
}
1414+
try (PreparedStatement stmt = conn.prepareStatement("WITH toDateTime(?) AS target_time SELECT * FROM table")) {
1415+
Assert.assertEquals(stmt.getMetaData().getColumnCount(), 1);
1416+
}
1417+
}
1418+
}
14071419
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@Test(groups = {"integration"})
1414
public class WriterStatementImplTest extends JdbcIntegrationTest {
15-
15+
1616

1717
@Test(groups = {"integration"})
1818
public void testTargetTypeMethodThrowException() throws SQLException {

jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/SqlParserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ public static Object[][] testCTEStmtsDP() {
247247
{"(with a as (select ?) select * from a);", 1},
248248
{"with a as (select 1) select * from a; ", 0},
249249
{"(with ? as a select a);", 1},
250-
{"select * from ( with x as ( select 9 ) select * from x );", 0}
251-
250+
{"select * from ( with x as ( select 9 ) select * from x );", 0},
251+
{"WITH toDateTime(?) AS target_time SELECT * FROM table", 1}
252252
};
253253
}
254254

0 commit comments

Comments
 (0)