-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Class Structure Overview
- A SubGroup class acts as the parent,
- The SubqueryGroup class is implemented as a child under the SubGroup.
This structure is the basis for the following space and indentation control rules.
Space Adjustment Rules
Universal Rule for Subquery Groups:
- Always insert a space before the closing parenthesis
).
SELECT *
FROM ( SELECT id
, name
FROM users ) -- insert a space before the closing parenthesis When to insert a space after the opening parenthesis () in a sub-group:
-
If the sub-group is one of the following:
- A SubqueryGroup
SELECT *
FROM ( SELECT id -- insert a space after the opening parenthesis
, name
FROM users ) -- insert a space before the closing parenthesis - A ValueGroup
INSERT INTO /*# tableName */
(x1 -- ColumnGroup:not spacing after the opening parenthesis
, x2)
VALUES ( /* reportId */1 --ValueGroup:insert a space after the opening parenthesis
, /* @maxDateTime() */'9999-12-31' ) -- insert a space before the closing parenthesis Cases where a space should be inserted before the closing parenthesis ):
-
The sub-group is a column definition group in a
CREATE TABLEstatement -
The sub-group is a column update group or value group in an
UPDATEstatement -
The sub-group is a list-style test data group, and:
- The last element in the group is not a keyword group
- (i.e., test data is not set by subquery)
-
The sub-group is not a function argument group
- (e.g., excludes
CALL,MAX, etc.)
- (e.g., excludes
-
The sub-group is not an insert column group
-
The sub-group is a SubqueryGroup
-
The grandparent of the sub-group is:
- a query keyword group such as
SELECT,FROM, orWHERE - and that group’s parent is not an
INSERTquery group
- a query keyword group such as
-
The grandparent of the sub-group is a
JOINgroup
Formatting Rules by Query Type: Column/Value Group Indentation & Line Breaks
INSERT INTO
-
Column Group:
- No line breaks
- No spaces before or after
(and)
-
Value Group:
- No line breaks after
VALUESor before the final) - Insert 1 space after
(and before) - Value rows should be indented with 1 space after
(
- No line breaks after
Bulk UPDATE
-
No line breaks
-
Insert 1 space before and after
(and) -
Follow the rules defined in:
- “Rules for bulk column update syntax in
UPDATE”
- “Rules for bulk column update syntax in
CREATE TABLE
- Column names should be right-aligned
- Group start: 2-space indent
- Each column row: indented to align with the keyword following
CREATE
By following these formatting rules, the SQL formatter ensures consistency and improves the readability and maintainability of SQL code. It is also recommended to reflect these rules in documentation and unit tests to maintain implementation accuracy.