Skip to content

Commit d54c24d

Browse files
authored
Merge pull request #421 from den-crane/Feature/bugfix/result-without-columns
fixes #208 - Result without columns by adding a new line before format
2 parents 8f87ef5 + 4574863 commit d54c24d

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/main/java/ru/yandex/clickhouse/ClickHouseStatementImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ private static String addFormatIfAbsent(final String sql, ClickHouseFormat forma
464464
int idx = cleanSQL.endsWith(";")
465465
? cleanSQL.length() - 1
466466
: cleanSQL.length();
467-
sb.append(cleanSQL.substring(0, idx))
468-
.append(" FORMAT ")
467+
sb.append(cleanSQL, 0, idx)
468+
.append("\nFORMAT ")
469469
.append(format.name())
470470
.append(';');
471471
return sb.toString();

src/test/java/ru/yandex/clickhouse/ClickHouseStatementTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,41 @@ public class ClickHouseStatementTest {
2323
@Test
2424
public void testClickhousify() throws Exception {
2525
String sql = "SELECT ololo FROM ololoed;";
26-
assertEquals("SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql));
26+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql), "SELECT ololo FROM ololoed\nFORMAT TabSeparatedWithNamesAndTypes;");
2727

2828
String sql2 = "SELECT ololo FROM ololoed";
29-
assertEquals("SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql2));
29+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql2), "SELECT ololo FROM ololoed\nFORMAT TabSeparatedWithNamesAndTypes;");
3030

3131
String sql3 = "SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes";
32-
assertEquals("SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes", ClickHouseStatementImpl.clickhousifySql(sql3));
32+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql3), "SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes");
3333

3434
String sql4 = "SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;";
35-
assertEquals("SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql4));
35+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql4), "SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;");
3636

3737
String sql5 = "SHOW ololo FROM ololoed;";
38-
assertEquals("SHOW ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql5));
38+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql5), "SHOW ololo FROM ololoed\nFORMAT TabSeparatedWithNamesAndTypes;");
3939

4040
String sql6 = " show ololo FROM ololoed;";
41-
assertEquals("show ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql6));
41+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql6), "show ololo FROM ololoed\nFORMAT TabSeparatedWithNamesAndTypes;");
42+
43+
String sql7 = "SELECT ololo FROM ololoed \nFORMAT TabSeparatedWithNamesAndTypes";
44+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql7), "SELECT ololo FROM ololoed \nFORMAT TabSeparatedWithNamesAndTypes");
45+
46+
String sql8 = "SELECT ololo FROM ololoed \n\n FORMAT TabSeparatedWithNamesAndTypes";
47+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql8), "SELECT ololo FROM ololoed \n\n FORMAT TabSeparatedWithNamesAndTypes");
48+
49+
String sql9 = "SELECT ololo FROM ololoed\n-- some comments one line";
50+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql9), "SELECT ololo FROM ololoed\n-- some comments one line\nFORMAT TabSeparatedWithNamesAndTypes;");
51+
52+
String sql10 = "SELECT ololo FROM ololoed\n-- some comments\ntwo line";
53+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql10), "SELECT ololo FROM ololoed\n-- some comments\ntwo line\nFORMAT TabSeparatedWithNamesAndTypes;");
54+
55+
String sql11 = "SELECT ololo FROM ololoed/*\nsome comments\ntwo line*/";
56+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql11), "SELECT ololo FROM ololoed/*\nsome comments\ntwo line*/\nFORMAT TabSeparatedWithNamesAndTypes;");
57+
58+
String sql12 = "SELECT ololo FROM ololoed\n// c style some comments one line";
59+
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql12), "SELECT ololo FROM ololoed\n// c style some comments one line\nFORMAT TabSeparatedWithNamesAndTypes;");
60+
4261
}
4362

4463
@Test

src/test/java/ru/yandex/clickhouse/integration/ClickHousePreparedStatementTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,10 @@ public void clickhouseJdbcFailsBecauseOfCommentInStart() throws Exception {
365365
@Test
366366
public void testTrailingParameter() throws Exception {
367367
String sqlStatement =
368-
"SELECT 42 AS foo, 23 AS bar "
368+
// "SELECT 42 AS foo, 23 AS bar "
369+
"SELECT 42 AS foo, 23 AS bar from numbers(100) "
369370
+ "ORDER BY foo DESC LIMIT ?, ?";
371+
370372
PreparedStatement stmt = connection.prepareStatement(sqlStatement);
371373
stmt.setInt(1, 42);
372374
stmt.setInt(2, 23);

0 commit comments

Comments
 (0)