jdorn / sql-formatter

A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.
http://jdorn.github.com/sql-formatter/
MIT License
3.89k stars 186 forks source link

Please don't indent JOIN-s #89

Open arcadeus opened 8 years ago

arcadeus commented 8 years ago

JOIN clauses are at the same hierarchy level as FROM clause, which is correctly not indented.

F.e. better formatting for

...
FROM 
  {shop} AS s 
  LEFT JOIN {shop_price} AS pr ON pr.good_id = s.id 
  AND pr.trash = '0' 
  AND pr.date_start <= 1469606657 
  AND (
    pr.date_start = 0 
    OR pr.date_finish >= 1469606657
  ) 
  AND pr.currency_id = 0 
  AND pr.role_id = 0 
  AND (pr.person = '0') 
  INNER JOIN {shop_param_element} AS pe3 ON pe3.element_id = s.id 
  AND pe3.param_id = '%d' 
  AND pe3.trash = '0' 
...

.. would be

...
FROM 
  {shop} AS s 
LEFT JOIN {shop_price} AS pr ON pr.good_id = s.id 
  AND pr.trash = '0' 
  AND pr.date_start <= 1469606657 
  AND (
    pr.date_start = 0 
    OR pr.date_finish >= 1469606657
  ) 
  AND pr.currency_id = 0 
  AND pr.role_id = 0 
  AND (pr.person = '0') 
INNER JOIN {shop_param_element} AS pe3 ON pe3.element_id = s.id 
  AND pe3.param_id = '%d' 
  AND pe3.trash = '0' 
...
stof commented 8 years ago

technically, this is not true. Joins are a subpart of the FROM clause in SQL. The indentation of AND in ON clauses is not good though