domaframework / doma

DAO oriented database mapping framework for Java
https://doma.readthedocs.io/
Apache License 2.0
438 stars 69 forks source link

Enable removal of blank lines from SQL templates #1135

Closed nakamura-to closed 1 month ago

nakamura-to commented 1 month ago

To Doma Users,

If you have some time, could you please add common use cases to the BlankLineRemovalContextTest class and run it? It would be helpful if you could report any cases that do not work as expected.

nakamura-to commented 1 month ago

We will merge this pull request, but please continue to provide information on any cases where it does not work properly.

nakamura-to commented 1 month ago

Consider the following SQL template:

select
  *
from
  EMPLOYEE
where
  /*%if false */
  EMPLOYEE_ID = 0
  /*%end */
  /*%if false */
  EMPLOYEE_NAME = 'aaa'
  /*%end */
  /*%if true */
  EMPLOYEE_NO = 10
  /*%end */

By default, this SQL template is converted to the following SQL string:

select
  *
from
  EMPLOYEE
where

  EMPLOYEE_NO = 10

To remove blank lines from the SQL string, you can create a class like the following:

public class ExampleSqlBuilderSettings implements SqlBuilderSettings {

  @Override
  public boolean shouldRemoveBlankLines() {
    return false;
  }
}

Return the instance of the above class from your Config implementation:

public class AppConfig implements Config {

  ...

  private final SqlBuilderSettings sqlBuilderSettings = new ExampleSqlBuilderSettings();

  @Override
  public SqlBuilderSettings get SqlBuilderSettings() {
    return sqlBuilderSettings;
  }
}

Let's apply the above settings to the SQL template. The result of the application is as follows:

select
  *
from
  EMPLOYEE
where
  EMPLOYEE_NO = 10