Skip to content

Commit

Permalink
Merge pull request #421 from den-crane/Feature/bugfix/result-without-…
Browse files Browse the repository at this point in the history
…columns

fixes #208 - Result without columns by adding a new line before format
  • Loading branch information
alex-krash authored Feb 3, 2020
2 parents 8f87ef5 + 4574863 commit d54c24d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ private static String addFormatIfAbsent(final String sql, ClickHouseFormat forma
int idx = cleanSQL.endsWith(";")
? cleanSQL.length() - 1
: cleanSQL.length();
sb.append(cleanSQL.substring(0, idx))
.append(" FORMAT ")
sb.append(cleanSQL, 0, idx)
.append("\nFORMAT ")
.append(format.name())
.append(';');
return sb.toString();
Expand Down
31 changes: 25 additions & 6 deletions src/test/java/ru/yandex/clickhouse/ClickHouseStatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,41 @@ public class ClickHouseStatementTest {
@Test
public void testClickhousify() throws Exception {
String sql = "SELECT ololo FROM ololoed;";
assertEquals("SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql));
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql), "SELECT ololo FROM ololoed\nFORMAT TabSeparatedWithNamesAndTypes;");

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

String sql3 = "SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes";
assertEquals("SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes", ClickHouseStatementImpl.clickhousifySql(sql3));
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql3), "SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes");

String sql4 = "SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;";
assertEquals("SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql4));
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql4), "SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;");

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

String sql6 = " show ololo FROM ololoed;";
assertEquals("show ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql6));
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql6), "show ololo FROM ololoed\nFORMAT TabSeparatedWithNamesAndTypes;");

String sql7 = "SELECT ololo FROM ololoed \nFORMAT TabSeparatedWithNamesAndTypes";
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql7), "SELECT ololo FROM ololoed \nFORMAT TabSeparatedWithNamesAndTypes");

String sql8 = "SELECT ololo FROM ololoed \n\n FORMAT TabSeparatedWithNamesAndTypes";
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql8), "SELECT ololo FROM ololoed \n\n FORMAT TabSeparatedWithNamesAndTypes");

String sql9 = "SELECT ololo FROM ololoed\n-- some comments one line";
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql9), "SELECT ololo FROM ololoed\n-- some comments one line\nFORMAT TabSeparatedWithNamesAndTypes;");

String sql10 = "SELECT ololo FROM ololoed\n-- some comments\ntwo line";
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql10), "SELECT ololo FROM ololoed\n-- some comments\ntwo line\nFORMAT TabSeparatedWithNamesAndTypes;");

String sql11 = "SELECT ololo FROM ololoed/*\nsome comments\ntwo line*/";
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql11), "SELECT ololo FROM ololoed/*\nsome comments\ntwo line*/\nFORMAT TabSeparatedWithNamesAndTypes;");

String sql12 = "SELECT ololo FROM ololoed\n// c style some comments one line";
assertEquals(ClickHouseStatementImpl.clickhousifySql(sql12), "SELECT ololo FROM ololoed\n// c style some comments one line\nFORMAT TabSeparatedWithNamesAndTypes;");

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,10 @@ public void clickhouseJdbcFailsBecauseOfCommentInStart() throws Exception {
@Test
public void testTrailingParameter() throws Exception {
String sqlStatement =
"SELECT 42 AS foo, 23 AS bar "
// "SELECT 42 AS foo, 23 AS bar "
"SELECT 42 AS foo, 23 AS bar from numbers(100) "
+ "ORDER BY foo DESC LIMIT ?, ?";

PreparedStatement stmt = connection.prepareStatement(sqlStatement);
stmt.setInt(1, 42);
stmt.setInt(2, 23);
Expand Down

0 comments on commit d54c24d

Please sign in to comment.