vertical-blank / sql-formatter

SQL formatter written with only Java Standard Library, without dependencies.
MIT License
227 stars 47 forks source link

"Between And" with convert function are not formatted correctly #73

Open yisiliang opened 1 month ago

yisiliang commented 1 month ago

Hi

If "between and" go with convert function, the tools are not formatted correctly.

SELECT * FROM table WHERE 1=1 and date BETWEEN str_to_date('2001-01-01', '%Y-%M-%D') AND str_to_date('2001-12-01', '%Y-%M-%D') and 2=2
SELECT
  *
FROM
  table
WHERE
  1 = 1
  and date BETWEEN str_to_date('2001-01-01', '%Y-%M-%D')
  AND str_to_date('2001-12-01', '%Y-%M-%D')
  and 2 = 2
SELECT * FROM table WHERE date BETWEEN '2001-01-01' AND '2001-12-01'
SELECT
  *
FROM
  table
WHERE
  date BETWEEN '2001-01-01' AND '2001-12-01'

The source code is only look behind 2.

  private String formatNewlineReservedWord(Token token, String query) {
    if (Token.isAnd(token) && Token.isBetween(this.tokenLookBehind(2))) {
      return this.formatWithSpaces(token, query);
    }
    return this.addNewline(query) + this.equalizeWhitespace(this.show(token)) + " ";
  }