Skip to content

Commit a9737c1

Browse files
authored
Merge pull request #1140 from domaframework/feat/reimplement-removal-of-sql-comments
Reimplement "Enable removal of comments from SQL templates"
2 parents f20632c + 3a974bf commit a9737c1

File tree

17 files changed

+264
-238
lines changed

17 files changed

+264
-238
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.seasar.doma.internal.jdbc.sql.node.BindVariableNode;
3030
import org.seasar.doma.internal.jdbc.sql.node.BlankNode;
3131
import org.seasar.doma.internal.jdbc.sql.node.CommentNode;
32+
import org.seasar.doma.internal.jdbc.sql.node.CommentType;
3233
import org.seasar.doma.internal.jdbc.sql.node.DistinctNode;
3334
import org.seasar.doma.internal.jdbc.sql.node.ElseNode;
3435
import org.seasar.doma.internal.jdbc.sql.node.ElseifNode;
@@ -69,6 +70,7 @@
6970
import org.seasar.doma.jdbc.InParameter;
7071
import org.seasar.doma.jdbc.JdbcException;
7172
import org.seasar.doma.jdbc.PreparedSql;
73+
import org.seasar.doma.jdbc.SqlBuilderSettings;
7274
import org.seasar.doma.jdbc.SqlKind;
7375
import org.seasar.doma.jdbc.SqlLogFormattingFunction;
7476
import org.seasar.doma.jdbc.SqlLogType;
@@ -86,7 +88,7 @@ public class NodePreparedSqlBuilder
8688

8789
protected final Config config;
8890

89-
protected final boolean shouldRemoveBlankLines;
91+
protected final SqlBuilderSettings sqlBuilderSettings;
9092

9193
protected final SqlKind kind;
9294

@@ -156,7 +158,7 @@ public NodePreparedSqlBuilder(
156158
BiConsumer<PopulateNode, SqlContext> valuesPopulater) {
157159
assertNotNull(config, kind, evaluator, columnsExpander, valuesPopulater);
158160
this.config = config;
159-
this.shouldRemoveBlankLines = config.getSqlBuilderSettings().shouldRemoveBlankLines();
161+
this.sqlBuilderSettings = config.getSqlBuilderSettings();
160162
this.kind = kind;
161163
this.sqlFilePath = sqlFilePath;
162164
this.evaluator = evaluator;
@@ -205,8 +207,21 @@ public Void visitWhitespaceNode(WhitespaceNode node, Context p) {
205207
@Override
206208
public Void visitCommentNode(CommentNode node, Context p) {
207209
String comment = node.getComment();
208-
p.appendRawSql(comment);
209-
p.appendFormattedSql(comment);
210+
CommentType commentType = node.getCommentType();
211+
switch (commentType) {
212+
case BLOCK:
213+
if (!sqlBuilderSettings.shouldRemoveBlockComment(comment)) {
214+
p.appendRawSql(comment);
215+
p.appendFormattedSql(comment);
216+
}
217+
break;
218+
case LINE:
219+
if (!sqlBuilderSettings.shouldRemoveLineComment(comment)) {
220+
p.appendRawSql(comment);
221+
p.appendFormattedSql(comment);
222+
}
223+
break;
224+
}
210225
return null;
211226
}
212227

@@ -769,14 +784,14 @@ public Void visitExpandNode(ExpandNode node, Context p) {
769784
}
770785

771786
private Context createContext(Context context) {
772-
if (shouldRemoveBlankLines) {
787+
if (sqlBuilderSettings.shouldRemoveBlankLines()) {
773788
return new BlankLineRemovalContext(context);
774789
}
775790
return new DefaultContext(context);
776791
}
777792

778793
private Context createContext(Config config, ExpressionEvaluator evaluator) {
779-
if (shouldRemoveBlankLines) {
794+
if (sqlBuilderSettings.shouldRemoveBlankLines()) {
780795
return new BlankLineRemovalContext(config, evaluator);
781796
}
782797
return new DefaultContext(config, evaluator);
@@ -827,9 +842,6 @@ interface Context {
827842
Value removeValue(String variableName);
828843

829844
EvaluationResult evaluate(SqlLocation location, String expression);
830-
831-
@Override
832-
String toString();
833845
}
834846

835847
protected static class DefaultContext implements Context {
@@ -1093,7 +1105,7 @@ private void flushBlankNodes() {
10931105
}
10941106

10951107
private static String toString(List<BlankNode> nodes, int eolNodeCount) {
1096-
if (eolNodeCount > 1) {
1108+
if (eolNodeCount > 0) {
10971109
int seenEolNodeCount = 0;
10981110
ListIterator<BlankNode> iterator = nodes.listIterator();
10991111
while (iterator.hasNext()) {
@@ -1189,7 +1201,6 @@ public EvaluationResult evaluate(SqlLocation location, String expression) {
11891201

11901202
@Override
11911203
public String toString() {
1192-
flushBlankNodes();
11931204
return context.toString();
11941205
}
11951206
}

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.seasar.doma.internal.jdbc.sql.node.BindVariableNode;
1313
import org.seasar.doma.internal.jdbc.sql.node.BlockNode;
1414
import org.seasar.doma.internal.jdbc.sql.node.CommentNode;
15+
import org.seasar.doma.internal.jdbc.sql.node.CommentType;
1516
import org.seasar.doma.internal.jdbc.sql.node.DistinctNode;
1617
import org.seasar.doma.internal.jdbc.sql.node.ElseNode;
1718
import org.seasar.doma.internal.jdbc.sql.node.ElseifNode;
@@ -48,7 +49,6 @@
4849
import org.seasar.doma.internal.util.StringUtil;
4950
import org.seasar.doma.jdbc.JdbcException;
5051
import org.seasar.doma.jdbc.SqlNode;
51-
import org.seasar.doma.jdbc.SqlParserConfig;
5252
import org.seasar.doma.message.Message;
5353

5454
public class SqlParser {
@@ -60,8 +60,6 @@ public class SqlParser {
6060

6161
protected final String sql;
6262

63-
protected final SqlParserConfig config;
64-
6563
protected final SqlTokenizer tokenizer;
6664

6765
protected final AnonymousNode rootNode;
@@ -71,13 +69,8 @@ public class SqlParser {
7169
protected String token;
7270

7371
public SqlParser(String sql) {
74-
this(sql, SqlParserConfig.DEFAULT);
75-
}
76-
77-
public SqlParser(String sql, SqlParserConfig config) {
78-
assertNotNull(sql, config);
72+
assertNotNull(sql);
7973
this.sql = sql;
80-
this.config = config;
8174
tokenizer = new SqlTokenizer(sql);
8275
rootNode = new AnonymousNode();
8376
nodeStack.push(rootNode);
@@ -239,6 +232,7 @@ public SqlNode parse() {
239232
parseBlockComment();
240233
break;
241234
}
235+
242236
case LINE_COMMENT:
243237
{
244238
parseLineComment();
@@ -424,17 +418,13 @@ protected void parseDistinctWord() {
424418
}
425419

426420
protected void parseBlockComment() {
427-
if (!config.shouldRemoveBlockComment(token)) {
428-
CommentNode node = new CommentNode(token);
429-
appendNode(node);
430-
}
421+
CommentNode node = new CommentNode(token, CommentType.BLOCK);
422+
appendNode(node);
431423
}
432424

433425
protected void parseLineComment() {
434-
if (!config.shouldRemoveLineComment(token)) {
435-
CommentNode node = new CommentNode(token);
436-
appendNode(node);
437-
}
426+
CommentNode node = new CommentNode(token, CommentType.LINE);
427+
appendNode(node);
438428
}
439429

440430
protected void parseOpenedParens() {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ public class CommentNode extends AbstractSqlNode {
1111

1212
protected final String comment;
1313

14-
public CommentNode(String comment) {
15-
assertNotNull(comment);
14+
protected final CommentType commentType;
15+
16+
public CommentNode(String comment, CommentType commentType) {
17+
assertNotNull(comment, commentType);
1618
this.comment = comment;
19+
this.commentType = commentType;
1720
}
1821

1922
public String getComment() {
2023
return comment;
2124
}
2225

26+
public CommentType getCommentType() {
27+
return commentType;
28+
}
29+
2330
@Override
2431
public void appendNode(SqlNode child) {
2532
throw new JdbcUnsupportedOperationException(getClass().getName(), "addNode");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.seasar.doma.internal.jdbc.sql.node;
2+
3+
public enum CommentType {
4+
BLOCK,
5+
LINE
6+
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
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-
2919
@Override
3020
public final SqlFile getSqlFile(Method method, String path, Dialect dialect) {
3121
if (method == null) {
@@ -110,7 +100,7 @@ protected final String getPrimaryPath(String path, Dialect dialect) {
110100
* @return the SQL node
111101
*/
112102
protected final SqlNode parse(String sql) {
113-
SqlParser parser = new SqlParser(sql, sqlParserConfig);
103+
SqlParser parser = new SqlParser(sql);
114104
return parser.parse();
115105
}
116106

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ default EntityListenerProvider getEntityListenerProvider() {
226226
return ConfigSupport.defaultEntityListenerProvider;
227227
}
228228

229+
/**
230+
* Returns the context for SQL builder settings.
231+
*
232+
* @return the context for SQL builder settings
233+
*/
229234
default SqlBuilderSettings getSqlBuilderSettings() {
230235
return ConfigSupport.defaultSqlBuilderSettings;
231236
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ 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-
1913
@Override
2014
protected SqlFile getSqlFileWithCacheControl(Method method, String path, Dialect dialect) {
2115
CacheKey key = new CacheKey(method, path);

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
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-
159
@Override
1610
protected SqlFile getSqlFileWithCacheControl(Method method, String path, Dialect dialect) {
1711
return createSqlFile(method, path, dialect);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
package org.seasar.doma.jdbc;
22

3+
/** A context for SQL builder settings. */
34
public interface SqlBuilderSettings {
5+
6+
/**
7+
* Returns whether the block comment should be removed. The beginning of the {@code comment}
8+
* parameter string must always start with "/*".
9+
*
10+
* @param comment the block comment, never null
11+
* @return true if the block comment should be removed
12+
*/
13+
default boolean shouldRemoveBlockComment(String comment) {
14+
return false;
15+
}
16+
17+
/**
18+
* Returns whether the line comment should be removed. The beginning of the {@code comment}
19+
* parameter string must always start with "--".
20+
*
21+
* @param comment the line comment, never null
22+
* @return true if the line comment should be removed
23+
*/
24+
default boolean shouldRemoveLineComment(String comment) {
25+
return false;
26+
}
27+
28+
/**
29+
* Returns whether the blank lines should be removed.
30+
*
31+
* @return true if the blank lines should be removed
32+
*/
433
default boolean shouldRemoveBlankLines() {
534
return false;
635
}

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)