import java.util.regex.Pattern;
import javax.annotation.Nullable;
-import org.apache.commons.lang.StringUtils;
import static org.apache.commons.lang.StringUtils.abbreviate;
}
public static String formatSql(String sql) {
- return StringUtils.replaceChars(sql, '\n', ' ');
+ char[] chars = sql.toCharArray();
+ StringBuilder result = new StringBuilder(chars.length);
+
+ for (int i = 0; i < chars.length; i++) {
+ char c = chars[i];
+ if (c == '\n' || c == '\t') {
+ c = ' ';
+ }
+ if (Character.isWhitespace(c) && i > 0 && Character.isWhitespace(chars[i - 1])) {
+ continue;
+ }
+ result.append(c);
+ }
+ return result.toString();
}
public static String formatParam(@Nullable Object param) {
@Test
public void formatSql() {
+ assertThat(SqlLogFormatter.formatSql("")).isEqualTo("");
assertThat(SqlLogFormatter.formatSql("select *")).isEqualTo("select *");
- }
-
- @Test
- public void formatSql_removes_newlines() {
assertThat(SqlLogFormatter.formatSql("select *\nfrom issues")).isEqualTo("select * from issues");
+ assertThat(SqlLogFormatter.formatSql("select *\n from issues")).isEqualTo("select * from issues");
+ assertThat(SqlLogFormatter.formatSql("select *\n from issues")).isEqualTo("select * from issues");
+ assertThat(SqlLogFormatter.formatSql("select *\n from issues")).isEqualTo("select * from issues");
+ assertThat(SqlLogFormatter.formatSql("select *\n\t\t from \tissues")).isEqualTo("select * from issues");
}
@Test