@Test public void testRemoveCommentInSql() { String originSql = "select count(*) from test_kylin_fact where price > 10.0"; String originSql2 = "select count(*) from test_kylin_fact where TEST_COLUMN != 'not--a comment'"; { String sqlWithComment = "-- comment \n" + originSql; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = "-- comment \n -- comment\n" + originSql; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = "-- \n -- comment \n" + originSql; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = originSql + "-- \n -- comment \n"; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = "-- \n -- comment \n" + originSql + "-- \n -- comment \n"; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = " " + originSql + "-- \n -- comment \n"; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = " " + originSql; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = " " + originSql; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = " -- comment 5\n" + originSql; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = " -- comment 5\n" + originSql + ""; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment = " -- comment 3\n" + originSql + "-- comment 5\n"; Assert.assertEquals(originSql, QueryUtil.removeCommentInSql(sqlWithComment)); } { String sqlWithComment2 = " -- comment 5\n" + originSql2 + ""; Assert.assertEquals(originSql2, QueryUtil.removeCommentInSql(sqlWithComment2)); } { String sqlWithComment2 = " -- comment 5\n" + originSql2; Assert.assertEquals(originSql2, QueryUtil.removeCommentInSql(sqlWithComment2)); } { String sqlWithComment2 = " " + originSql2; Assert.assertEquals(originSql2, QueryUtil.removeCommentInSql(sqlWithComment2)); } { String sqlWithComment2 = " " + originSql2; Assert.assertEquals(originSql2, QueryUtil.removeCommentInSql(sqlWithComment2)); } { String sqlWithComment2 = "-- \n -- comment \n" + originSql2; Assert.assertEquals(originSql2, QueryUtil.removeCommentInSql(sqlWithComment2)); } { String sqlWithComment2 = "-- comment \n" + originSql2; Assert.assertEquals(originSql2, QueryUtil.removeCommentInSql(sqlWithComment2)); } { String sqlWithComment2 = "-- comment \n -- comment\n" + originSql2; Assert.assertEquals(originSql2, QueryUtil.removeCommentInSql(sqlWithComment2)); } String content = " -- One-line comment and \n" + "select price as " + "\n" + "revenue from v_lineitem;"; String expectedContent = "select price as revenue from v_lineitem;"; String trimmedContent = QueryUtil.removeCommentInSql(content).replaceAll("\n", "").trim(); Assert.assertEquals(trimmedContent, expectedContent); }
public static String removeCommentInSql(String sql1) { final String[] commentPatterns = new String[] { "--(?!.*\\*/).*?[\r\n]", "/\\*(.|\r|\n)*?\\*/" }; for (int i = 0; i < commentPatterns.length; i++) { sql1 = sql1.replaceAll(commentPatterns[i], ""); } sql1 = sql1.trim(); return sql1; }
QueryUtil { public static String removeCommentInSql(String sql1) { final String[] commentPatterns = new String[] { "--(?!.*\\*/).*?[\r\n]", "/\\*(.|\r|\n)*?\\*/" }; for (int i = 0; i < commentPatterns.length; i++) { sql1 = sql1.replaceAll(commentPatterns[i], ""); } sql1 = sql1.trim(); return sql1; } }
QueryUtil { public static String removeCommentInSql(String sql1) { final String[] commentPatterns = new String[] { "--(?!.*\\*/).*?[\r\n]", "/\\*(.|\r|\n)*?\\*/" }; for (int i = 0; i < commentPatterns.length; i++) { sql1 = sql1.replaceAll(commentPatterns[i], ""); } sql1 = sql1.trim(); return sql1; } private  QueryUtil(); }
QueryUtil { public static String removeCommentInSql(String sql1) { final String[] commentPatterns = new String[] { "--(?!.*\\*/).*?[\r\n]", "/\\*(.|\r|\n)*?\\*/" }; for (int i = 0; i < commentPatterns.length; i++) { sql1 = sql1.replaceAll(commentPatterns[i], ""); } sql1 = sql1.trim(); return sql1; } private  QueryUtil(); static String appendLimitOffsetToSql(String sql, int limit, int offset); @Deprecated static String massageSql(String sql, String project, int limit, int offset, String defaultSchema); static String massageSql(String sql, String project, int limit, int offset, String defaultSchema,
            String catalog); static String makeErrorMsgUserFriendly(Throwable e); static String makeErrorMsgUserFriendly(String errorMsg); static boolean isSelectStatement(String sql); static String removeCommentInSql(String sql1); }
QueryUtil { public static String removeCommentInSql(String sql1) { final String[] commentPatterns = new String[] { "--(?!.*\\*/).*?[\r\n]", "/\\*(.|\r|\n)*?\\*/" }; for (int i = 0; i < commentPatterns.length; i++) { sql1 = sql1.replaceAll(commentPatterns[i], ""); } sql1 = sql1.trim(); return sql1; } private  QueryUtil(); static String appendLimitOffsetToSql(String sql, int limit, int offset); @Deprecated static String massageSql(String sql, String project, int limit, int offset, String defaultSchema); static String massageSql(String sql, String project, int limit, int offset, String defaultSchema,
            String catalog); static String makeErrorMsgUserFriendly(Throwable e); static String makeErrorMsgUserFriendly(String errorMsg); static boolean isSelectStatement(String sql); static String removeCommentInSql(String sql1);  }