vertical-blank / sql-formatter

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

Fix wrong order of indexed placeholders #51

Closed clausnagel closed 2 years ago

clausnagel commented 2 years ago

Indexed placeholders are not inserted in the provided order.

Code to reproduce the issue:

 System.out.println(SqlFormatter.format(
    "select * from test where a = ? and b = ? and c = ?",
     Arrays.asList("3", "2", "1")));

Generated but wrong output is:

select
  *
from
  test
where
  a = 1
  and b = 2
  and c = 3

In a correct output, a must be assigned 3 and c must be assigned 1 instead.

The issue is that IndexedParams uses a PriorityQueue that stores the placeholder values based on their natural ordering instead of the order of the provided list. This PR proposes to replace PriorityQueue with an ArrayDeque to fix this issue.

codecov[bot] commented 2 years ago

Codecov Report

Merging #51 (05fe403) into master (84a26b4) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #51   +/-   ##
=========================================
  Coverage     89.32%   89.32%           
  Complexity      271      271           
=========================================
  Files            25       25           
  Lines           899      899           
  Branches         46       46           
=========================================
  Hits            803      803           
  Misses           83       83           
  Partials         13       13           
Impacted Files Coverage Δ
...ithub/vertical_blank/sqlformatter/core/Params.java 70.37% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 84a26b4...05fe403. Read the comment docs.

vertical-blank commented 2 years ago

@clausnagel Thank you very much!

clausnagel commented 2 years ago

Hi @vertical-blank, are you planning a new release soon that will contain this fix?

vertical-blank commented 2 years ago

@clausnagel I've released 2.0.3 to Maven Central just now. https://repo1.maven.org/maven2/com/github/vertical-blank/sql-formatter/2.0.3/