A set of OpenRewrite recipes for formatting SQL/HQL code.
Below is a detailed description of each Recipe.
The io.github.mhagnumdw.FormatSqlBlockRecipe recipe automatically formats SQL or HQL embedded in Text Blocks present in the following annotations:
org.hibernate.annotations.processing.HQLorg.hibernate.annotations.processing.SQLjakarta.data.repository.Query
Future enhancements may allow configuration of custom annotations. Please open an issue.
The io.github.mhagnumdw.FormatSqlFileRecipe recipe automatically formats the content of SQL files.
The following options are applicable to both FormatSqlBlockRecipe and FormatSqlFileRecipe:
| Type | Name | Description | Example | Default Value |
|---|---|---|---|---|
| String | filePath |
Optional. The path to the files that the Recipe should process. Accepts a glob expression; multiple patterns can be specified, separated by a semicolon ;. If omitted, processes all matching files. |
**/*DAO.java **/*.sql |
FormatSqlBlockRecipe: **/*.java FormatSqlFileRecipe: **/*.sql |
| String | sqlDialect |
Optional. The SQL dialect to be used for formatting. Valid options: sql (StandardSql), mysql, postgresql, db2, plsql (Oracle PL/SQL), n1ql (Couchbase N1QL), redshift, spark, tsql (SQL Server Transact-SQL). Details here. |
plsql |
sql |
| String | indent |
Optional. The string to be used for indentation. | " " for 2 spaces "\t" for a tab |
4 spaces " " |
| Integer | maxColumnLength |
Optional. The maximum length of a line before the formatter tries to break it. | 100 |
120 |
| Boolean | uppercase |
Optional. Whether to convert SQL keywords to uppercase (not safe to use when the SQL dialect has case-sensitive identifiers). | true |
false |
Before
package com.mycompany;
import jakarta.data.repository.Query;
public interface HolidayRepository {
@Query("""
select h.*, c.name as country_name from Holiday h inner join Country c on h.country_id = c.id where h.year = :year and h.name != 'Christmas' order by h.name""")
void findByYear(int year);
}After
package com.mycompany;
import jakarta.data.repository.Query;
public interface HolidayRepository {
@Query("""
select
h. *,
c .name as country_name
from
Holiday h
inner join Country c on h.country_id = c .id
where
h.year = :year
and h.name != 'Christmas'
order by
h.name""")
void findByYear(int year);
}Consider the following example.sql file:
select * from users where id = 1;
--
select d.name, c.name from City c inner join Department d on c.id = d.city_id;After running the recipe, the example.sql file will be:
select
*
from
users
where
id = 1;
--
select
d.name,
c .name
from
City c
inner join Department d on c .id = d.city_id;There is an open issue regarding the space in some aliases: vertical-blank/sql-formatter#77
The usage modes are described below. It is possible to apply one or both Recipes.
If you have a project and will run the recipe regularly, this is the recommended way.
Inside the plugins section, add:
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>6.11.0</version>
<configuration>
<activeRecipes>
<!-- Add the recipes you want to use here -->
<recipe>io.github.mhagnumdw.FormatSqlBlockRecipe</recipe>
<recipe>io.github.mhagnumdw.FormatSqlFileRecipe</recipe>
</activeRecipes>
<failOnDryRunResults>false</failOnDryRunResults>
</configuration>
<dependencies>
<dependency>
<groupId>io.github.mhagnumdw</groupId>
<artifactId>rewrite-format-sql</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>Then run:
./mvnw rewrite:runTo customize the recipe configuration, you need to have a rewrite.yml file in the project root. Example:
---
type: specs.openrewrite.org/v1beta/recipe
name: io.github.mhagnumdw.FormatSqlCustomConfig
recipeList:
# Add the Recipes you want to use here
- io.github.mhagnumdw.FormatSqlBlockRecipe:
sqlDialect: "plsql"
- io.github.mhagnumdw.FormatSqlFileRecipe:
sqlDialect: "mysql"
- This file is a way to create your own custom recipe from other recipes.
- The
nameattribute is arbitrary, but it's good if it relates to its purpose. It's the name of the custom recipe.- The
rewrite.ymlfile should be versioned.- For more details about
rewrite.yml, see here.
And change the <recipe> tag in pom.xml to:
<recipe>io.github.mhagnumdw.FormatSqlCustomConfig</recipe>As in this example the
FormatSqlCustomConfigrecipe includes bothFormatSqlBlockRecipeandFormatSqlFileReciperecipes, inpom.xmlit is only necessary to define theFormatSqlCustomConfigrecipe.
Then run:
./mvnw rewrite:runFor more details on configuring OpenRewrite with Maven, see here.
This mode is indicated if your intention is to run the recipe only once.
./mvnw org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.activeRecipes=io.github.mhagnumdw.FormatSqlBlockRecipe,io.github.mhagnumdw.FormatSqlFileRecipe \
-Drewrite.recipeArtifactCoordinates=io.github.mhagnumdw:rewrite-format-sql:1.0.0To customize the recipe configuration, you need to have the rewrite.yml file in the project root, as in the previous example.
Then run:
./mvnw org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.activeRecipes=io.github.mhagnumdw.FormatSqlCustomConfig \
-Drewrite.recipeArtifactCoordinates=io.github.mhagnumdw:rewrite-format-sql:1.0.0
io.github.mhagnumdw.FormatSqlCustomConfigis thenamedefined in therewrite.ymlfile.- For a single recipe, you don't even need to have the
rewrite.ymlfile to customize the configuration, see here.
For details on how to contribute, set up the development environment, run tests, and other development-related information, please see our CONTRIBUTING.md file.