domaframework / doma

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

Reimplement "Enable removal of comments from SQL templates" #1140

Closed nakamura-to closed 2 months ago

nakamura-to commented 2 months ago

This PR reverts #1134 and reimplements the removal of comments.

In this PR, settings related to SQL building are consolidated into SqlBuilderSettings.

public class ExampleSqlBuilderSettings implements SqlBuilderSettings {

  @Override
  public boolean shouldRemoveBlockComment(String comment) {
    return false;
  }

  @Override
  public boolean shouldRemoveLineComment(String comment) {
    return true;
  }
}

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 following SQL template:

/**
 * This comment will not be removed
 */
select
  * 
from
  EMPLOYEE -- this comment will be removed
where  
  EMPLOYEE_ID = /*employeeId*/0 -- this comment will be also removed

The result of the application is as follows:

/**
 * This comment will not be removed
 */
select
  * 
from
  EMPLOYEE 
where  
  EMPLOYEE_ID = ?