Skip to content

Commit f6a0d54

Browse files
authored
Merge pull request #499 from Okapist/getColumnNames
Restore public getColumnNames method
2 parents 8e68aa4 + 079dfaa commit f6a0d54

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/main/java/ru/yandex/clickhouse/response/ClickHouseResultSet.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ private int asColNum(String column) {
646646
}
647647
}
648648
// TODO Java8
649-
throw new RuntimeException("no column " + column + " in columns list " + getColumnNames());
649+
throw new RuntimeException("no column " + column + " in columns list " + getColumnNamesString());
650650
}
651651

652652
private ByteFragment getValue(int colNum) {
@@ -700,6 +700,14 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) {
700700
return result.setScale(scale, RoundingMode.HALF_UP);
701701
}
702702

703+
public String[] getColumnNames() {
704+
String[] columnNames = new String[columns.size()];
705+
for (int i = 0; i < columns.size(); ++i) {
706+
columnNames[i] = columns.get(i).getColumnName();
707+
}
708+
return columnNames;
709+
}
710+
703711
@Override
704712
public void setFetchDirection(int direction) throws SQLException {
705713
// ignore perfomance hint
@@ -719,7 +727,7 @@ public String toString() {
719727
", bis=" + bis +
720728
", db='" + db + '\'' +
721729
", table='" + table + '\'' +
722-
", columns=" + getColumnNames() +
730+
", columns=" + getColumnNamesString() +
723731
", maxRows=" + maxRows +
724732
", values=" + Arrays.toString(values) +
725733
", lastReadColumn=" + lastReadColumn +
@@ -729,7 +737,7 @@ public String toString() {
729737
'}';
730738
}
731739

732-
private String getColumnNames() {
740+
private String getColumnNamesString() {
733741
StringBuilder sb = new StringBuilder();
734742
for (ClickHouseColumnInfo info : columns) {
735743
sb.append(info.getColumnName()).append(' ');

src/test/java/ru/yandex/clickhouse/response/ClickHouseResultSetTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,25 @@ public void testClassNamesObjects() throws Exception {
448448
}
449449
}
450450

451+
@Test
452+
public void testGetColumnNames() throws Exception {
453+
String response = "SiteName\tCountry\n" +
454+
"String\tString\n" +
455+
"hello.com\tPoland\n" +
456+
"there.com\tUSA\n" +
457+
"\t\n" +
458+
"other.com\t\n" +
459+
"\n" +
460+
"\t\n";
461+
462+
ByteArrayInputStream is = new ByteArrayInputStream(response.getBytes("UTF-8"));
463+
464+
ClickHouseResultSet rs = buildResultSet(is, 1024, "db", "table", false, null, null, props);
465+
String[] columnNames = rs.getColumnNames();
466+
assertEquals(2, columnNames.length);
467+
assertEquals("SiteName", columnNames[0]);
468+
assertEquals("Country", columnNames[1]);
469+
}
451470

452471
/**
453472
* By jdbc specification

0 commit comments

Comments
 (0)