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 comments from SQL templates #1134

Closed nakamura-to closed 1 month ago

nakamura-to commented 1 month ago

We offer an option to remove block comments and line comments from SQL templates.

Which comments to delete will be indicated in the implementation class of SqlParserConfig:

public class LineCommentRemovalSqlParserConfig implements SqlParserConfig {

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

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

Pass an instance of the above class to the constructor of the implementation class of SqlFileRepository:

public class AppConfig implements Config {

  ...

  private final SqlFileRepository sqlFileRepository =
      new GreedyCacheSqlFileRepository(new LineCommentRemovalSqlParserConfig());

  @Override
  public SqlFileRepository getSqlFileRepository() {
    return sqlFileRepository;
  }
}

Let's apply the above configuration 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 = ?