Skip to content

Commit 1bcd131

Browse files
authored
Enable removal of comments from SQL templates (#1134)
* Enable removal of comments from SQL templates * Determine removal based on comment content
1 parent 93649b7 commit 1bcd131

File tree

9 files changed

+168
-7
lines changed

9 files changed

+168
-7
lines changed

doma-core/src/main/java/org/seasar/doma/internal/jdbc/sql/SqlParser.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.seasar.doma.internal.util.StringUtil;
4949
import org.seasar.doma.jdbc.JdbcException;
5050
import org.seasar.doma.jdbc.SqlNode;
51+
import org.seasar.doma.jdbc.SqlParserConfig;
5152
import org.seasar.doma.message.Message;
5253

5354
public class SqlParser {
@@ -59,6 +60,8 @@ public class SqlParser {
5960

6061
protected final String sql;
6162

63+
protected final SqlParserConfig config;
64+
6265
protected final SqlTokenizer tokenizer;
6366

6467
protected final AnonymousNode rootNode;
@@ -68,8 +71,13 @@ public class SqlParser {
6871
protected String token;
6972

7073
public SqlParser(String sql) {
71-
assertNotNull(sql);
74+
this(sql, SqlParserConfig.DEFAULT);
75+
}
76+
77+
public SqlParser(String sql, SqlParserConfig config) {
78+
assertNotNull(sql, config);
7279
this.sql = sql;
80+
this.config = config;
7381
tokenizer = new SqlTokenizer(sql);
7482
rootNode = new AnonymousNode();
7583
nodeStack.push(rootNode);
@@ -227,9 +235,13 @@ public SqlNode parse() {
227235
break;
228236
}
229237
case BLOCK_COMMENT:
238+
{
239+
parseBlockComment();
240+
break;
241+
}
230242
case LINE_COMMENT:
231243
{
232-
parseComment();
244+
parseLineComment();
233245
break;
234246
}
235247
case OTHER:
@@ -411,9 +423,18 @@ protected void parseDistinctWord() {
411423
appendNode(node);
412424
}
413425

414-
protected void parseComment() {
415-
CommentNode node = new CommentNode(token);
416-
appendNode(node);
426+
protected void parseBlockComment() {
427+
if (!config.shouldRemoveBlockComment(token)) {
428+
CommentNode node = new CommentNode(token);
429+
appendNode(node);
430+
}
431+
}
432+
433+
protected void parseLineComment() {
434+
if (!config.shouldRemoveLineComment(token)) {
435+
CommentNode node = new CommentNode(token);
436+
appendNode(node);
437+
}
417438
}
418439

419440
protected void parseOpenedParens() {

doma-core/src/main/java/org/seasar/doma/jdbc/AbstractSqlFileRepository.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
/** A skeletal implementation of the {@link SqlFileRepository} interface. */
1717
public abstract class AbstractSqlFileRepository implements SqlFileRepository {
1818

19+
protected final SqlParserConfig sqlParserConfig;
20+
21+
protected AbstractSqlFileRepository() {
22+
this(SqlParserConfig.DEFAULT);
23+
}
24+
25+
protected AbstractSqlFileRepository(SqlParserConfig sqlParserConfig) {
26+
this.sqlParserConfig = Objects.requireNonNull(sqlParserConfig);
27+
}
28+
1929
@Override
2030
public final SqlFile getSqlFile(Method method, String path, Dialect dialect) {
2131
if (method == null) {
@@ -100,7 +110,7 @@ protected final String getPrimaryPath(String path, Dialect dialect) {
100110
* @return the SQL node
101111
*/
102112
protected final SqlNode parse(String sql) {
103-
SqlParser parser = new SqlParser(sql);
113+
SqlParser parser = new SqlParser(sql, sqlParserConfig);
104114
return parser.parse();
105115
}
106116

doma-core/src/main/java/org/seasar/doma/jdbc/GreedyCacheSqlFileRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ public class GreedyCacheSqlFileRepository extends AbstractSqlFileRepository {
1010

1111
protected final ConcurrentMap<CacheKey, SqlFile> sqlFileMap = new ConcurrentHashMap<>(200);
1212

13+
public GreedyCacheSqlFileRepository() {}
14+
15+
public GreedyCacheSqlFileRepository(SqlParserConfig sqlParserConfig) {
16+
super(sqlParserConfig);
17+
}
18+
1319
@Override
1420
protected SqlFile getSqlFileWithCacheControl(Method method, String path, Dialect dialect) {
1521
CacheKey key = new CacheKey(method, path);

doma-core/src/main/java/org/seasar/doma/jdbc/NoCacheSqlFileRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
/** An SQL file repository that does not cache the results of SQL parsing. */
77
public class NoCacheSqlFileRepository extends AbstractSqlFileRepository {
88

9+
public NoCacheSqlFileRepository() {}
10+
11+
public NoCacheSqlFileRepository(SqlParserConfig sqlParserConfig) {
12+
super(sqlParserConfig);
13+
}
14+
915
@Override
1016
protected SqlFile getSqlFileWithCacheControl(Method method, String path, Dialect dialect) {
1117
return createSqlFile(method, path, dialect);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.seasar.doma.jdbc;
2+
3+
/** A configuration for SQL parser. */
4+
public interface SqlParserConfig {
5+
6+
/** The default implementation. */
7+
SqlParserConfig DEFAULT =
8+
new SqlParserConfig() {
9+
@Override
10+
public boolean shouldRemoveBlockComment(String comment) {
11+
return false;
12+
}
13+
14+
@Override
15+
public boolean shouldRemoveLineComment(String comment) {
16+
return false;
17+
}
18+
};
19+
20+
/**
21+
* Returns whether block comments should be removed.
22+
*
23+
* @param comment the block comment
24+
* @return whether block comments should be removed
25+
*/
26+
boolean shouldRemoveBlockComment(String comment);
27+
28+
/**
29+
* Returns whether line comments should be removed.
30+
*
31+
* @param comment the line comment
32+
* @return whether line comments should be removed
33+
*/
34+
boolean shouldRemoveLineComment(String comment);
35+
}

doma-core/src/test/java/org/seasar/doma/internal/jdbc/sql/SqlParserTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.seasar.doma.jdbc.SqlKind;
2323
import org.seasar.doma.jdbc.SqlLogType;
2424
import org.seasar.doma.jdbc.SqlNode;
25+
import org.seasar.doma.jdbc.SqlParserConfig;
2526
import org.seasar.doma.message.Message;
2627

2728
public class SqlParserTest {
@@ -967,6 +968,54 @@ public void testDelimiter() {
967968
assertEquals("select 1;", sql.getRawSql());
968969
}
969970

971+
@Test
972+
public void testSqlParserConfig_default() {
973+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
974+
evaluator.add("name", new Value(String.class, "hoge"));
975+
evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(10000)));
976+
String testSql =
977+
"select * from aaa where ename = /** block comment */bbb -- line comment 1\norder by ccc-- line comment 2";
978+
SqlParser parser = new SqlParser(testSql, SqlParserConfig.DEFAULT);
979+
SqlNode sqlNode = parser.parse();
980+
PreparedSql sql =
981+
new NodePreparedSqlBuilder(
982+
config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED)
983+
.build(sqlNode, Function.identity());
984+
assertEquals(
985+
"select * from aaa where ename = /** block comment */bbb -- line comment 1\norder by ccc-- line comment 2",
986+
sql.getRawSql());
987+
}
988+
989+
@Test
990+
public void testSqlParserConfig_removeAllComments() {
991+
SqlParserConfig sqlParserConfig =
992+
new SqlParserConfig() {
993+
994+
@Override
995+
public boolean shouldRemoveBlockComment(String comment) {
996+
return true;
997+
}
998+
999+
@Override
1000+
public boolean shouldRemoveLineComment(String comment) {
1001+
return true;
1002+
}
1003+
};
1004+
1005+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
1006+
evaluator.add("name", new Value(String.class, "hoge"));
1007+
evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(10000)));
1008+
String testSql =
1009+
"select * from aaa where ename = /** block comment */bbb -- line comment 1\norder by ccc-- line comment 2";
1010+
SqlParser parser = new SqlParser(testSql, sqlParserConfig);
1011+
SqlNode sqlNode = parser.parse();
1012+
PreparedSql sql =
1013+
new NodePreparedSqlBuilder(
1014+
config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED)
1015+
.build(sqlNode, Function.identity());
1016+
assertEquals("select * from aaa where ename = bbb \norder by ccc", sql.getRawSql());
1017+
}
1018+
9701019
public enum MyEnum {
9711020
AAA,
9721021
BBB,

integration-test-common/src/main/java/org/seasar/doma/it/AppConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import java.util.Objects;
44
import javax.sql.DataSource;
55
import org.seasar.doma.jdbc.Config;
6+
import org.seasar.doma.jdbc.GreedyCacheSqlFileRepository;
67
import org.seasar.doma.jdbc.JdbcLogger;
78
import org.seasar.doma.jdbc.Naming;
89
import org.seasar.doma.jdbc.RequiresNewController;
910
import org.seasar.doma.jdbc.SimpleDataSource;
11+
import org.seasar.doma.jdbc.SqlFileRepository;
1012
import org.seasar.doma.jdbc.dialect.Dialect;
1113
import org.seasar.doma.jdbc.tx.LocalTransaction;
1214
import org.seasar.doma.jdbc.tx.LocalTransactionDataSource;
@@ -28,6 +30,9 @@ public class AppConfig implements Config {
2830

2931
private final LocalTransactionManager transactionManager;
3032

33+
private final SqlFileRepository sqlFileRepository =
34+
new GreedyCacheSqlFileRepository(new LineCommentRemovalSqlParserConfig());
35+
3136
public AppConfig(Dialect dialect, Dbms dbms, String url, String user, String password) {
3237
Objects.requireNonNull(dialect);
3338
Objects.requireNonNull(dbms);
@@ -101,4 +106,9 @@ public Naming getNaming() {
101106
public JdbcLogger getJdbcLogger() {
102107
return jdbcLogger;
103108
}
109+
110+
@Override
111+
public SqlFileRepository getSqlFileRepository() {
112+
return sqlFileRepository;
113+
}
104114
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.seasar.doma.it;
2+
3+
import org.seasar.doma.jdbc.SqlParserConfig;
4+
5+
public class LineCommentRemovalSqlParserConfig implements SqlParserConfig {
6+
7+
@Override
8+
public boolean shouldRemoveBlockComment(String comment) {
9+
return false;
10+
}
11+
12+
@Override
13+
public boolean shouldRemoveLineComment(String comment) {
14+
return true;
15+
}
16+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
select * from EMPLOYEE where EMPLOYEE_ID = /*employeeId*/0
1+
/**
2+
* This comment will not be removed
3+
*/
4+
select
5+
*
6+
from
7+
EMPLOYEE -- this comment will be removed
8+
where
9+
EMPLOYEE_ID = /*employeeId*/0 -- this comment will be also removed

0 commit comments

Comments
 (0)