Skip to content

Commit 162706a

Browse files
committed
SQLに識別子を追記できるようにしました
データベース側のログからSQLの発行場所を特定できるようにするためです
1 parent f12fa83 commit 162706a

File tree

70 files changed

+752
-869
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+752
-869
lines changed

src/main/java/org/seasar/doma/internal/RuntimeConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.seasar.doma.jdbc.ClassHelper;
2323
import org.seasar.doma.jdbc.CommandImplementors;
24+
import org.seasar.doma.jdbc.Commenter;
2425
import org.seasar.doma.jdbc.Config;
2526
import org.seasar.doma.jdbc.JdbcLogger;
2627
import org.seasar.doma.jdbc.MapKeyNaming;
@@ -113,6 +114,11 @@ public TransactionManager getTransactionManager() {
113114
return config.getTransactionManager();
114115
}
115116

117+
@Override
118+
public Commenter getCommenter() {
119+
return config.getCommenter();
120+
}
121+
116122
@Override
117123
public MapKeyNaming getMapKeyNaming() {
118124
return config.getMapKeyNaming();

src/main/java/org/seasar/doma/internal/jdbc/dao/AbstractDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ private void validateConfig(Config config, DataSource dataSource) {
163163
throw new ConfigException(config.getClass().getName(),
164164
"getMapKeyNaming");
165165
}
166+
if (config.getCommenter() == null) {
167+
throw new ConfigException(config.getClass().getName(),
168+
"getCommenter");
169+
}
166170
}
167171

168172
@Override
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.seasar.doma.internal.jdbc.sql;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.function.Function;
6+
7+
import org.seasar.doma.DomaNullPointerException;
8+
import org.seasar.doma.jdbc.Sql;
9+
import org.seasar.doma.jdbc.SqlKind;
10+
import org.seasar.doma.jdbc.SqlLogType;
11+
import org.seasar.doma.jdbc.SqlParameter;
12+
13+
public abstract class AbstractSql<P extends SqlParameter> implements Sql<P> {
14+
15+
protected final SqlKind kind;
16+
17+
protected final String rawSql;
18+
19+
protected final String formattedSql;
20+
21+
protected final String sqlFilePath;
22+
23+
protected final List<P> parameters;
24+
25+
protected final SqlLogType sqlLogType;
26+
27+
protected AbstractSql(SqlKind kind, CharSequence rawSql,
28+
CharSequence formattedSql, String sqlFilePath,
29+
List<? extends P> parameters, SqlLogType sqlLogType,
30+
Function<String, String> commenter) {
31+
if (kind == null) {
32+
throw new DomaNullPointerException("kind");
33+
}
34+
if (rawSql == null) {
35+
throw new DomaNullPointerException("rawSql");
36+
}
37+
if (formattedSql == null) {
38+
throw new DomaNullPointerException("formattedSql");
39+
}
40+
if (parameters == null) {
41+
throw new DomaNullPointerException("parameters");
42+
}
43+
if (sqlLogType == null) {
44+
throw new DomaNullPointerException("sqlLogType");
45+
}
46+
if (commenter == null) {
47+
throw new DomaNullPointerException("commenter");
48+
}
49+
this.kind = kind;
50+
this.rawSql = commenter.apply(rawSql.toString().trim());
51+
this.formattedSql = commenter.apply(formattedSql.toString().trim());
52+
this.sqlFilePath = sqlFilePath;
53+
this.parameters = Collections.unmodifiableList(parameters);
54+
this.sqlLogType = sqlLogType;
55+
}
56+
57+
@Override
58+
public SqlKind getKind() {
59+
return kind;
60+
}
61+
62+
@Override
63+
public String getRawSql() {
64+
return rawSql;
65+
}
66+
67+
@Override
68+
public String getFormattedSql() {
69+
return formattedSql;
70+
}
71+
72+
@Override
73+
public String getSqlFilePath() {
74+
return sqlFilePath;
75+
}
76+
77+
@Override
78+
public List<P> getParameters() {
79+
return parameters;
80+
}
81+
82+
@Override
83+
public SqlLogType getSqlLogType() {
84+
return sqlLogType;
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return rawSql;
90+
}
91+
92+
}

src/main/java/org/seasar/doma/internal/jdbc/sql/CallableSql.java

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
package org.seasar.doma.internal.jdbc.sql;
1717

18-
import java.util.Collections;
1918
import java.util.List;
19+
import java.util.function.Function;
2020

21-
import org.seasar.doma.DomaNullPointerException;
22-
import org.seasar.doma.jdbc.Sql;
2321
import org.seasar.doma.jdbc.SqlKind;
2422
import org.seasar.doma.jdbc.SqlLogType;
2523
import org.seasar.doma.jdbc.SqlParameter;
@@ -29,76 +27,13 @@
2927
* @author taedium
3028
*
3129
*/
32-
public class CallableSql implements Sql<SqlParameter> {
33-
34-
protected final SqlKind kind;
35-
36-
protected final String rawSql;
37-
38-
protected final String formattedSql;
39-
40-
protected final List<SqlParameter> parameters;
41-
42-
protected final SqlLogType sqlLogType;
30+
public class CallableSql extends AbstractSql<SqlParameter> {
4331

4432
public CallableSql(SqlKind kind, CharSequence rawSql,
4533
CharSequence formattedSql, List<? extends SqlParameter> parameters,
46-
SqlLogType sqlLogType) {
47-
if (kind == null) {
48-
throw new DomaNullPointerException("kind");
49-
}
50-
if (rawSql == null) {
51-
throw new DomaNullPointerException("rawSql");
52-
}
53-
if (formattedSql == null) {
54-
throw new DomaNullPointerException("formattedSql");
55-
}
56-
if (parameters == null) {
57-
throw new DomaNullPointerException("parameters");
58-
}
59-
if (sqlLogType == null) {
60-
throw new DomaNullPointerException("sqlLogType");
61-
}
62-
this.kind = kind;
63-
this.rawSql = rawSql.toString().trim();
64-
this.formattedSql = formattedSql.toString().trim();
65-
this.parameters = Collections.unmodifiableList(parameters);
66-
this.sqlLogType = sqlLogType;
67-
}
68-
69-
@Override
70-
public SqlKind getKind() {
71-
return kind;
72-
}
73-
74-
@Override
75-
public String getRawSql() {
76-
return rawSql;
77-
}
78-
79-
@Override
80-
public String getFormattedSql() {
81-
return formattedSql;
82-
}
83-
84-
@Override
85-
public String getSqlFilePath() {
86-
return null;
87-
}
88-
89-
@Override
90-
public List<SqlParameter> getParameters() {
91-
return parameters;
92-
}
93-
94-
@Override
95-
public SqlLogType getSqlLogType() {
96-
return sqlLogType;
97-
}
98-
99-
@Override
100-
public String toString() {
101-
return rawSql;
34+
SqlLogType sqlLogType, Function<String, String> converter) {
35+
super(kind, rawSql, formattedSql, null, parameters, sqlLogType,
36+
converter);
10237
}
10338

10439
}

src/main/java/org/seasar/doma/internal/jdbc/sql/CallableSqlBuilder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.LinkedList;
2222
import java.util.List;
23+
import java.util.function.Function;
2324

2425
import org.seasar.doma.jdbc.Config;
2526
import org.seasar.doma.jdbc.SqlKind;
@@ -69,7 +70,8 @@ public CallableSqlBuilder(Config config, SqlKind kind, String moduleName,
6970
this.formattingFunction = new ConvertToLogFormatFunction();
7071
}
7172

72-
public CallableSql build() {
73+
public CallableSql build(Function<String, String> commenter) {
74+
assertNotNull(commenter);
7375
Context context = new Context();
7476
context.append("{");
7577
if (resultParameter != null) {
@@ -90,7 +92,8 @@ public CallableSql build() {
9092
allParameters.addFirst(resultParameter);
9193
}
9294
return new CallableSql(kind, context.getSqlBuf(),
93-
context.getFormattedSqlBuf(), allParameters, sqlLogType);
95+
context.getFormattedSqlBuf(), allParameters, sqlLogType,
96+
commenter);
9497
}
9598

9699
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ public NodePreparedSqlBuilder(Config config, SqlKind kind,
131131
this.columnsExpander = columnsExpander;
132132
}
133133

134-
public PreparedSql build(SqlNode sqlNode) {
135-
assertNotNull(sqlNode);
134+
public PreparedSql build(SqlNode sqlNode, Function<String, String> commenter) {
135+
assertNotNull(sqlNode, commenter);
136136
Context context = new Context(config, evaluator);
137137
sqlNode.accept(this, context);
138138
return new PreparedSql(kind, context.getSqlBuf(),
139139
context.getFormattedSqlBuf(), sqlFilePath,
140-
context.getParameters(), sqlLogType);
140+
context.getParameters(), sqlLogType, commenter);
141141
}
142142

143143
@Override

src/main/java/org/seasar/doma/internal/jdbc/sql/PreparedSql.java

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
package org.seasar.doma.internal.jdbc.sql;
1717

18-
import java.util.Collections;
1918
import java.util.List;
19+
import java.util.function.Function;
2020

21-
import org.seasar.doma.DomaNullPointerException;
22-
import org.seasar.doma.jdbc.Sql;
2321
import org.seasar.doma.jdbc.SqlKind;
2422
import org.seasar.doma.jdbc.SqlLogType;
2523

@@ -28,79 +26,21 @@
2826
* @author taedium
2927
*
3028
*/
31-
public class PreparedSql implements Sql<InParameter<?>> {
32-
33-
protected final SqlKind kind;
34-
35-
protected final String rawSql;
36-
37-
protected final String formattedSql;
38-
39-
protected final String sqlFilePath;
40-
41-
protected final List<InParameter<?>> parameters;
42-
43-
protected final SqlLogType sqlLogType;
29+
public class PreparedSql extends AbstractSql<InParameter<?>> {
4430

4531
public PreparedSql(SqlKind kind, CharSequence rawSql,
4632
CharSequence formattedSql, String sqlFilePath,
4733
List<? extends InParameter<?>> parameters, SqlLogType sqlLogType) {
48-
if (kind == null) {
49-
throw new DomaNullPointerException("kind");
50-
}
51-
if (rawSql == null) {
52-
throw new DomaNullPointerException("rawSql");
53-
}
54-
if (formattedSql == null) {
55-
throw new DomaNullPointerException("formattedSql");
56-
}
57-
if (parameters == null) {
58-
throw new DomaNullPointerException("parameters");
59-
}
60-
if (sqlLogType == null) {
61-
throw new DomaNullPointerException("sqlLogType");
62-
}
63-
this.kind = kind;
64-
this.rawSql = rawSql.toString().trim();
65-
this.formattedSql = formattedSql.toString().trim();
66-
this.sqlFilePath = sqlFilePath;
67-
this.parameters = Collections.unmodifiableList(parameters);
68-
this.sqlLogType = sqlLogType;
69-
}
70-
71-
@Override
72-
public SqlKind getKind() {
73-
return kind;
74-
}
75-
76-
@Override
77-
public String getRawSql() {
78-
return rawSql;
34+
this(kind, rawSql, formattedSql, sqlFilePath, parameters, sqlLogType,
35+
Function.identity());
7936
}
8037

81-
@Override
82-
public String getFormattedSql() {
83-
return formattedSql;
84-
}
85-
86-
@Override
87-
public String getSqlFilePath() {
88-
return sqlFilePath;
89-
}
90-
91-
@Override
92-
public List<InParameter<?>> getParameters() {
93-
return parameters;
94-
}
95-
96-
@Override
97-
public SqlLogType getSqlLogType() {
98-
return sqlLogType;
99-
}
100-
101-
@Override
102-
public String toString() {
103-
return rawSql;
38+
public PreparedSql(SqlKind kind, CharSequence rawSql,
39+
CharSequence formattedSql, String sqlFilePath,
40+
List<? extends InParameter<?>> parameters, SqlLogType sqlLogType,
41+
Function<String, String> commenter) {
42+
super(kind, rawSql, formattedSql, sqlFilePath, parameters, sqlLogType,
43+
commenter);
10444
}
10545

10646
}

src/main/java/org/seasar/doma/internal/jdbc/sql/PreparedSqlBuilder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.function.Function;
2223

2324
import org.seasar.doma.internal.jdbc.command.JdbcMappable;
2425
import org.seasar.doma.jdbc.Config;
@@ -69,8 +70,10 @@ public <BASIC> void appendParameter(JdbcMappable<BASIC> parameter) {
6970
parameters.add(new BasicInParameter<BASIC>(() -> wrapper));
7071
}
7172

72-
public PreparedSql build() {
73+
public PreparedSql build(Function<String, String> commenter) {
74+
assertNotNull(commenter);
7375
return new PreparedSql(kind, rawSql, formattedSql, null, parameters,
74-
sqlLogType);
76+
sqlLogType, commenter);
7577
}
78+
7679
}

0 commit comments

Comments
 (0)