Skip to content

Commit c804ac0

Browse files
authored
Merge pull request #361 from domaframework/doc/sql-format-update-readme
Update README to clarify SQL formatter features and limitations
2 parents 9d60234 + edfa667 commit c804ac0

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

README.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Checks that bind variables are used appropriately for DAO and SQL associations.
3434
The plugin also provides quick fixes for DAO methods where the required SQL files do not exist.
3535

3636
- Quick fix to generate SQL template file.
37+
3738
![quickfix.png](images/quickfix.png)
3839
- Checks for unused DAO method arguments.
3940
![inspection.png](images/inspection.png)
@@ -69,17 +70,78 @@ Along with DAO changes, the plugin will refactor the SQL file directory and file
6970
- After refactoring the DAO package, the SQL directory will also be updated.
7071
![RenameDao.gif](images/gif/RenameDao.gif)
7172

72-
## Formatter (Preview)
73+
## Formatter
7374
Provides code formatting for SQL syntax.
7475
This feature is in preview. You cannot customize the indentation or keywords to be broken down!
7576

77+
The formatting applies to SQL written in SQL files and in value fields (text blocks) within org.seasar.doma.Sql annotations.
78+
79+
![Format.gif](images/gif/Format.gif)
80+
7681
Automatic indentation on newlines provided by the SQL formatting feature is disabled by default.
7782

7883
To enable auto-indentation, toggle the corresponding flag in the settings screen below.
7984

8085
`Settings > Other Settings > Doma Tools > Enable auto-indent for SQL`
8186

82-
![Format.gif](images/gif/Format.gif)
87+
### Limitations
88+
The current formatter has the following limitations:
89+
90+
- **No customizable indentation**: The number of spaces for indentation is fixed and cannot be changed
91+
- **No customizable line breaks**: You cannot configure which keywords trigger line breaks
92+
- **No custom function registration**: User-defined functions cannot be registered for formatting rules
93+
- **No formatting style options**: Cannot choose between different formatting styles or conventions
94+
95+
### Formatting Features
96+
The SQL formatter supports the following formatting capabilities:
97+
98+
- **SQL Keywords**: Converts keywords to uppercase (SELECT, FROM, WHERE, INSERT, UPDATE, DELETE, etc.)
99+
- **Statement Structure**: Properly formats different SQL statement types:
100+
- SELECT statements with proper column and clause alignment
101+
- INSERT statements with formatted column lists and VALUES clauses
102+
- UPDATE statements with aligned SET clauses
103+
- DELETE statements with formatted conditions
104+
- WITH clauses (CTE) with proper indentation
105+
- **Joins**: Formats JOIN operations with appropriate indentation and alignment
106+
- **Subqueries**: Properly indents nested queries and subselects
107+
- **Functions**: Formats function calls with proper parameter alignment
108+
- **Comments**: Preserves single-line (--) and multi-line (/* */) comments
109+
- **Doma Directives**: Maintains proper formatting for Doma-specific directives:
110+
- Bind variables: `/* paramName */` with proper spacing
111+
- Conditional directives: `/*%if condition */` ... `/*%end*/`
112+
- Loop directives: `/*%for item : collection */` ... `/*%end*/`
113+
- Expand directive: `/*%expand */`
114+
- Populate directive: `/*%populate */`
115+
- Static property calls: `/* @ClassName@property */`
116+
- Literal values: `/*^ literalValue */`
117+
- Embedded variables: `/*# variable */`
118+
119+
### Examples
120+
121+
**Before formatting:**
122+
```sql
123+
SELECT COUNT(DISTINCT x) AS count_x, o.*, COALESCE(nbor.nearest, 999)
124+
AS nearest FROM ( SELECT p.objid, p.psfmag_g - p.extinction_g AS rpm
125+
FROM phototag p JOIN usno u ON p.objid = u.objid
126+
WHERE p.TYPE = 'Star' /*%if status == 2 */ and u.propermotion > 2.0 /*%end*/ ) as o
127+
```
128+
129+
**After formatting:**
130+
```sql
131+
SELECT COUNT(DISTINCT x) AS count_x
132+
, o.*
133+
, COALESCE(nbor.nearest
134+
, 999) AS nearest
135+
FROM ( SELECT p.objid
136+
, p.psfmag_g - p.extinction_g AS rpm
137+
FROM phototag p
138+
JOIN usno u
139+
ON p.objid = u.objid
140+
WHERE p.TYPE = 'Star'
141+
/*%if status == 2 */
142+
AND u.propermotion > 2.0
143+
/*%end*/ ) AS o
144+
```
83145

84146
## Reference Contributor
85147
Ctrl+Click on a bind variable in an SQL file to jump to its source symbol.

0 commit comments

Comments
 (0)