Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Breaking Changes

- Rename recipe `io.github.mhagnumdw.FormatSqlBlockRecipe` to `io.github.mhagnumdw.recipes.FormatSqlTextBlockByAnnotation`
- Rename recipe `io.github.mhagnumdw.FormatSqlFileRecipe` to `io.github.mhagnumdw.recipes.FormatSqlFile`

### Added

- **FormatSqlTextBlockRecipe**: for SQL Text Blocks marked with `// language=sql` (#20)
Expand Down
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
A set of [OpenRewrite](https://docs.openrewrite.org/) recipes for formatting SQL/HQL code.

- [Recipes](#recipes)
- [FormatSqlBlockRecipe](#formatsqlblockrecipe)
- [FormatSqlTextBlockRecipe](#formatsqltextblockrecipe)
- [FormatSqlFileRecipe](#formatsqlfilerecipe)
- [FormatSqlTextBlockByAnnotation](#formatsqltextblockbyannotation)
- [FormatSqlTextBlockByLanguageInjection](#formatsqltextblockbylanguageinjection)
- [FormatSqlFile](#formatsqlfile)
- [Configurable Options](#configurable-options)
- [Examples](#examples)
- [FormatSqlBlockRecipe Example](#formatsqlblockrecipe-example)
- [FormatSqlTextBlockRecipe Example](#formatsqltextblockrecipe-example)
- [FormatSqlFileRecipe Example](#formatsqlfilerecipe-example)
- [FormatSqlTextBlockByAnnotation example](#formatsqltextblockbyannotation-example)
- [FormatSqlTextBlockByLanguageInjection example](#formatsqltextblockbylanguageinjection-example)
- [FormatSqlFile example](#formatsqlfile-example)
- [Usage](#usage)
- [Configuring in `pom.xml`](#configuring-in-pomxml)
- [Without adding anything to the project](#without-adding-anything-to-the-project)
Expand All @@ -25,41 +25,41 @@ A set of [OpenRewrite](https://docs.openrewrite.org/) recipes for formatting SQL

Below is a detailed description of each Recipe.

### FormatSqlBlockRecipe
### FormatSqlTextBlockByAnnotation

The `io.github.mhagnumdw.FormatSqlBlockRecipe` recipe automatically formats SQL or HQL embedded in [Text Blocks](https://docs.oracle.com/en/java/javase/13/text_blocks/index.html) present in the following annotations:
The `io.github.mhagnumdw.recipes.FormatSqlTextBlockByAnnotation` recipe automatically formats SQL or HQL embedded in [Text Blocks](https://docs.oracle.com/en/java/javase/13/text_blocks/index.html) present in the following annotations:

- `org.hibernate.annotations.processing.HQL`
- `org.hibernate.annotations.processing.SQL`
- `jakarta.data.repository.Query`

> Future enhancements may allow configuration of custom annotations. Please open an issue.

### FormatSqlTextBlockRecipe
### FormatSqlTextBlockByLanguageInjection

The `io.github.mhagnumdw.FormatSqlTextBlockRecipe` recipe formats SQL code in Java [Text Blocks](https://docs.oracle.com/en/java/javase/13/text_blocks/index.html) that are preceded by a `// language=sql` comment (case-insensitive).
The `io.github.mhagnumdw.recipes.FormatSqlTextBlockByLanguageInjection` recipe formats SQL code in Java [Text Blocks](https://docs.oracle.com/en/java/javase/13/text_blocks/index.html) that are preceded by a `// language=sql` comment (case-insensitive).

This is the same [language injection comment](https://www.jetbrains.com/help/idea/language-injections.html) recognized by IntelliJ IDEA for SQL syntax highlighting.

### FormatSqlFileRecipe
### FormatSqlFile

The `io.github.mhagnumdw.FormatSqlFileRecipe` recipe automatically formats the content of SQL files.
The `io.github.mhagnumdw.recipes.FormatSqlFile` recipe automatically formats the content of SQL files.

## Configurable Options

The following options are applicable to `FormatSqlBlockRecipe`, `FormatSqlTextBlockRecipe`, and `FormatSqlFileRecipe`:
The following options are applicable to `FormatSqlTextBlockByAnnotation`, `FormatSqlTextBlockByLanguageInjection`, and `FormatSqlFile`:

| 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` <br> `**/*.sql` | FormatSqlBlockRecipe: `**/*.java` <br> FormatSqlTextBlockRecipe: `**/*.java` <br> FormatSqlFileRecipe: `**/*.sql` |
| 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` <br> `**/*.sql` | FormatSqlTextBlockByAnnotation: `**/*.java` <br> FormatSqlTextBlockByLanguageInjection: `**/*.java` <br> FormatSqlFile: `**/*.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](https://github.com/vertical-blank/sql-formatter). | `plsql` | `sql` |
| String | `indent` | Optional. The string to be used for indentation. | `" "` for 2 spaces <br> `"\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` |

## Examples

### FormatSqlBlockRecipe Example
### FormatSqlTextBlockByAnnotation example

Before

Expand Down Expand Up @@ -99,7 +99,7 @@ public interface HolidayRepository {
}
```

### FormatSqlTextBlockRecipe Example
### FormatSqlTextBlockByLanguageInjection example

Before

Expand All @@ -126,7 +126,7 @@ private static final String QUERY = """
u.name""";
```

### FormatSqlFileRecipe Example
### FormatSqlFile example

Consider the following `example.sql` file:

Expand Down Expand Up @@ -174,9 +174,9 @@ Inside the plugins section, add:
<configuration>
<activeRecipes>
<!-- Add the recipes you want to use here -->
<recipe>io.github.mhagnumdw.FormatSqlBlockRecipe</recipe>
<recipe>io.github.mhagnumdw.FormatSqlTextBlockRecipe</recipe>
<recipe>io.github.mhagnumdw.FormatSqlFileRecipe</recipe>
<recipe>io.github.mhagnumdw.recipes.FormatSqlTextBlockByAnnotation</recipe>
<recipe>io.github.mhagnumdw.recipes.FormatSqlTextBlockByLanguageInjection</recipe>
<recipe>io.github.mhagnumdw.recipes.FormatSqlFile</recipe>
</activeRecipes>
<failOnDryRunResults>false</failOnDryRunResults>
</configuration>
Expand All @@ -201,15 +201,15 @@ To customize the recipe configuration, you need to have a `rewrite.yml` file in
```yml
---
type: specs.openrewrite.org/v1beta/recipe
name: io.github.mhagnumdw.FormatSqlCustomConfig
name: io.github.mhagnumdw.recipes.FormatSqlCustomConfig
recipeList:
# Add the Recipes you want to use here
- io.github.mhagnumdw.FormatSqlBlockRecipe:
- io.github.mhagnumdw.recipes.FormatSqlTextBlockByAnnotation:
sqlDialect: "plsql"
- io.github.mhagnumdw.FormatSqlTextBlockRecipe:
- io.github.mhagnumdw.recipes.FormatSqlTextBlockByLanguageInjection:
sqlDialect: "plsql"
- io.github.mhagnumdw.recipes.FormatSqlFile:
sqlDialect: "plsql"
- io.github.mhagnumdw.FormatSqlFileRecipe:
sqlDialect: "mysql"
```

> - This file is a way to create your own custom recipe from other recipes.
Expand All @@ -220,10 +220,10 @@ recipeList:
And change the `<recipe>` tag in `pom.xml` to:

```xml
<recipe>io.github.mhagnumdw.FormatSqlCustomConfig</recipe>
<recipe>io.github.mhagnumdw.recipes.FormatSqlCustomConfig</recipe>
```

> As in this example the `FormatSqlCustomConfig` recipe includes both `FormatSqlBlockRecipe`, `FormatSqlTextBlockRecipe` and `FormatSqlFileRecipe` recipes, in `pom.xml` it is only necessary to define the `FormatSqlCustomConfig` recipe.
> As in this example the `FormatSqlCustomConfig` recipe includes both `FormatSqlTextBlockByAnnotation`, `FormatSqlTextBlockByLanguageInjection` and `FormatSqlFile` recipes, in `pom.xml` it is only necessary to define the `FormatSqlCustomConfig` recipe.

Then run:

Expand All @@ -239,7 +239,7 @@ This mode is indicated if your intention is to run the recipe only once.

```bash
./mvnw org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.activeRecipes=io.github.mhagnumdw.FormatSqlBlockRecipe,io.github.mhagnumdw.FormatSqlTextBlockRecipe,io.github.mhagnumdw.FormatSqlFileRecipe \
-Drewrite.activeRecipes=io.github.mhagnumdw.recipes.FormatSqlTextBlockByAnnotation,io.github.mhagnumdw.recipes.FormatSqlTextBlockByLanguageInjection,io.github.mhagnumdw.recipes.FormatSqlFile \
-Drewrite.recipeArtifactCoordinates=io.github.mhagnumdw:rewrite-format-sql:1.0.0
```

Expand All @@ -249,11 +249,11 @@ Then run:

```bash
./mvnw org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.activeRecipes=io.github.mhagnumdw.FormatSqlCustomConfig \
-Drewrite.activeRecipes=io.github.mhagnumdw.recipes.FormatSqlCustomConfig \
-Drewrite.recipeArtifactCoordinates=io.github.mhagnumdw:rewrite-format-sql:1.0.0
```

> - `io.github.mhagnumdw.FormatSqlCustomConfig` is the `name` defined in the `rewrite.yml` file.
> - `io.github.mhagnumdw.recipes.FormatSqlCustomConfig` is the `name` defined in the `rewrite.yml` file.
> - For a single recipe, you don't even need to have the `rewrite.yml` file to customize the configuration, see [here](https://docs.openrewrite.org/reference/faq#is-it-possible-to-pass-arguments-to-a-recipe-from-the-command-line).

## For Developers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.github.vertical_blank.sqlformatter.core.FormatConfig;
import com.github.vertical_blank.sqlformatter.languages.Dialect;
import io.github.mhagnumdw.TextBlockUtil;
import io.github.mhagnumdw.utils.TextBlockUtil;
import org.openrewrite.Cursor;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.recipes;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.vertical_blank.sqlformatter.core.FormatConfig;
import com.github.vertical_blank.sqlformatter.languages.Dialect;
import io.github.mhagnumdw.visitors.FormatSqlFileVisitor;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
Expand All @@ -17,12 +18,12 @@
*/
@Value
@EqualsAndHashCode(callSuper = false)
public class FormatSqlFileRecipe extends FormatSqlRecipeAbstract {
public class FormatSqlFile extends FormatSqlRecipeAbstract {

private static final String DEFAULT_FILE_PATH = "**/*.sql";

@JsonCreator
public FormatSqlFileRecipe(@Nullable @JsonProperty("filePath") String filePath,
public FormatSqlFile(@Nullable @JsonProperty("filePath") String filePath,
@Nullable @JsonProperty("sqlDialect") String sqlDialect,
@Nullable @JsonProperty("indent") String indent,
@Nullable @JsonProperty("maxColumnLength") Integer maxColumnLength,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.recipes;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.vertical_blank.sqlformatter.core.FormatConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.recipes;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.vertical_blank.sqlformatter.core.FormatConfig;
import com.github.vertical_blank.sqlformatter.languages.Dialect;
import io.github.mhagnumdw.visitors.FormatSqlBlockVisitor;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
Expand All @@ -18,12 +19,12 @@
*/
@Value
@EqualsAndHashCode(callSuper = false)
public class FormatSqlBlockRecipe extends FormatSqlRecipeAbstract {
public class FormatSqlTextBlockByAnnotation extends FormatSqlRecipeAbstract {

private static final String DEFAULT_FILE_PATH = "**/*.java";

@JsonCreator
public FormatSqlBlockRecipe(@Nullable @JsonProperty("filePath") String filePath,
public FormatSqlTextBlockByAnnotation(@Nullable @JsonProperty("filePath") String filePath,
@Nullable @JsonProperty("sqlDialect") String sqlDialect,
@Nullable @JsonProperty("indent") String indent,
@Nullable @JsonProperty("maxColumnLength") Integer maxColumnLength,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.recipes;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.vertical_blank.sqlformatter.core.FormatConfig;
import com.github.vertical_blank.sqlformatter.languages.Dialect;
import io.github.mhagnumdw.visitors.FormatSqlTextBlockVisitor;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
Expand All @@ -15,12 +16,12 @@

@Value
@EqualsAndHashCode(callSuper = false)
public class FormatSqlTextBlockRecipe extends FormatSqlRecipeAbstract {
public class FormatSqlTextBlockByLanguageInjection extends FormatSqlRecipeAbstract {

private static final String DEFAULT_FILE_PATH = "**/*.java";

@JsonCreator
public FormatSqlTextBlockRecipe(
public FormatSqlTextBlockByLanguageInjection(
@Nullable @JsonProperty("filePath") String filePath,
@Nullable @JsonProperty("sqlDialect") String sqlDialect,
@Nullable @JsonProperty("indent") String indent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.utils;

import static org.openrewrite.Tree.randomId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.visitors;

import com.github.vertical_blank.sqlformatter.core.FormatConfig;
import com.github.vertical_blank.sqlformatter.languages.Dialect;
Expand All @@ -13,7 +13,7 @@ public class FormatSqlBlockVisitor extends JavaIsoVisitor<ExecutionContext> {
private final Dialect dialect;
private final FormatConfig formatConfig;

FormatSqlBlockVisitor(Dialect dialect, FormatConfig formatConfig) {
public FormatSqlBlockVisitor(Dialect dialect, FormatConfig formatConfig) {
this.dialect = dialect;
this.formatConfig = formatConfig;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.visitors;

import com.github.vertical_blank.sqlformatter.SqlFormatter;
import com.github.vertical_blank.sqlformatter.core.FormatConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.visitors;

import static org.openrewrite.java.tree.J.Literal;

import com.github.vertical_blank.sqlformatter.core.FormatConfig;
import com.github.vertical_blank.sqlformatter.languages.Dialect;
import io.github.mhagnumdw.utils.TextBlockUtil;
import org.openrewrite.ExecutionContext;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.Comment;
Expand All @@ -24,7 +25,7 @@ public class FormatSqlTextBlockVisitor extends JavaIsoVisitor<ExecutionContext>
private final Dialect dialect;
private final FormatConfig formatConfig;

FormatSqlTextBlockVisitor(Dialect dialect, FormatConfig formatConfig) {
public FormatSqlTextBlockVisitor(Dialect dialect, FormatConfig formatConfig) {
this.dialect = dialect;
this.formatConfig = formatConfig;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/META-INF/rewrite/examples.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
type: specs.openrewrite.org/v1beta/example
recipeName: io.github.mhagnumdw.FormatSqlBlockRecipe
recipeName: io.github.mhagnumdw.recipes.FormatSqlTextBlockByAnnotation
examples:
- description: '`FormatSqlBlockRecipeTest#shouldFormatSimpleSqlBlock`'
- description: '`FormatSqlTextBlockByAnnotationTest#shouldFormatSimpleSqlBlock`'
parameters:
- io/github/mhagnumdw/fake/holidays/HolidayRepository.java
- plsql
Expand Down Expand Up @@ -45,9 +45,9 @@ examples:
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: io.github.mhagnumdw.FormatSqlTextBlockRecipe
recipeName: io.github.mhagnumdw.recipes.FormatSqlTextBlockByLanguageInjection
examples:
- description: '`FormatSqlTextBlockRecipeTest#shouldFormatFieldWithLanguageSqlComment`'
- description: '`FormatSqlTextBlockByLanguageInjectionTest#shouldFormatFieldWithLanguageSqlComment`'
parameters:
- io/github/mhagnumdw/test/*.java
- sql
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.recipes;

import static org.openrewrite.test.SourceSpecs.other;
import static org.openrewrite.test.SourceSpecs.text;
Expand All @@ -9,12 +9,12 @@
import org.openrewrite.test.RewriteTest;

@SuppressWarnings("java:S2699")
class FormatSqlFileRecipeTest implements RewriteTest {
class FormatSqlFileTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(
new FormatSqlFileRecipe(
new FormatSqlFile(
"file.sql",
"plsql",
null,
Expand Down Expand Up @@ -85,7 +85,7 @@ void shouldNotChangeEmptyFile() {
@Test
void shouldFormatWithCustomOptions() {
rewriteRun(
spec -> spec.recipe(new FormatSqlFileRecipe(null, "plsql", "\t", null, false)),
spec -> spec.recipe(new FormatSqlFile(null, "plsql", "\t", null, false)),
text(
"""
select e.emp_id, e.name, d.dept_name from employees e join departments d on e.dept_id = d.dept_id where e.salary > 50000;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.mhagnumdw;
package io.github.mhagnumdw.recipes;

import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
Expand All @@ -15,12 +15,12 @@
import org.openrewrite.test.RewriteTest;

@SuppressWarnings("java:S2699")
class FormatSqlBlockRecipeTest implements RewriteTest {
class FormatSqlTextBlockByAnnotationTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(
new FormatSqlBlockRecipe(
new FormatSqlTextBlockByAnnotation(
"io/github/mhagnumdw/fake/holidays/HolidayRepository.java",
"plsql",
null,
Expand Down Expand Up @@ -157,7 +157,7 @@ void shouldFormatSqlBlockWithTabs() {

rewriteRun(
spec -> spec.recipe(
new FormatSqlBlockRecipe(
new FormatSqlTextBlockByAnnotation(
"io/github/mhagnumdw/fake/holidays/HolidayRepository.java",
"plsql",
"\t", // Use tab for SQL block indentation
Expand Down
Loading