Skip to content

Commit 418d1a8

Browse files
committed
SQLファイルでUPDATE文のSET句を自動生成
UPDATE文のWHERE句を手書きしたいがSET句は自動設定したいという要望に応える機能です。 次のように記述できます。 ```java @entity class Person { @id Integer id; String name; Integer age; @Version Integer version; } ``` ```java @dao interface PersonDao { @update(sqlFile = true) int update(Person person, int age); } ``` ```sql // SQLファイル PersonDao/update.sql update person set /*%populate */ id = id where age < /* age */0 ``` ```sql // 上記SQLファイルに対応するPreparedStatement update person set name = ?, age = ?, version = ? + 1 where age < ? ``` この機能のポイントは次のものです。 - SQLファイル内の `/*%populate */` からWHERE句までが自動で置き換えられる - `pupulate` ではなく `values` というキーワードを使う案もあったが、式コメント内ではSQLの文脈と異なるキーワードを使ったほうがわかりやすいと判断 - SET句の生成結果は `@Update(sqlFile = false)` のときと全く同じ - IDは含めない - VERSIONはインクリメントする - `@Update` の `ignoreVersion` や `suppressOptimisticLockException` 等の要素も有効 - Daoメソッドの第一引数はエンティティクラスでなければいけない - エンティティクラスのプロパティがSET句に展開される - 第二引数以降は任意の型を指定でき、その引数はSQLファイルの式コメントから参照できる - `/*%populate */` は `@Update(sqlFile = true)` のときだけでなく `@BatchUpdate(sqlFile = true)` のときも使える
1 parent ced9108 commit 418d1a8

File tree

53 files changed

+1871
-154
lines changed

Some content is hidden

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

53 files changed

+1871
-154
lines changed

src/main/java/org/seasar/doma/internal/apt/BatchSqlValidator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public class BatchSqlValidator extends SqlValidator {
4545
public BatchSqlValidator(ProcessingEnvironment env,
4646
ExecutableElement methodElement,
4747
Map<String, TypeMirror> parameterTypeMap, String path,
48-
boolean expandable) {
49-
super(env, methodElement, parameterTypeMap, path, expandable);
48+
boolean expandable, boolean populatable) {
49+
super(env, methodElement, parameterTypeMap, path, expandable,
50+
populatable);
5051
suppress = methodElement.getAnnotation(Suppress.class);
5152
}
5253

src/main/java/org/seasar/doma/internal/apt/DaoGenerator.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,34 @@ public Void visitSqlFileModifyQueryMeta(SqlFileModifyQueryMeta m,
721721
m.getEntityCtType().getBoxedMetaTypeName());
722722
}
723723

724+
Boolean excludeNull = m.getExcludeNull();
725+
if (excludeNull != null) {
726+
iprint("__query.setNullExcluded(%1$s);%n", excludeNull);
727+
}
728+
724729
Boolean ignoreVersion = m.getIgnoreVersion();
725730
if (ignoreVersion != null) {
726731
iprint("__query.setVersionIgnored(%1$s);%n", ignoreVersion);
727732
}
728733

734+
List<String> include = m.getInclude();
735+
if (include != null) {
736+
iprint("__query.setIncludedPropertyNames(%1$s);%n",
737+
toCSVFormat(include));
738+
}
739+
740+
List<String> exclude = m.getExclude();
741+
if (exclude != null) {
742+
iprint("__query.setExcludedPropertyNames(%1$s);%n",
743+
toCSVFormat(m.getExclude()));
744+
}
745+
746+
Boolean includeUnchanged = m.getIncludeUnchanged();
747+
if (includeUnchanged != null) {
748+
iprint("__query.setUnchangedPropertyIncluded(%1$s);%n",
749+
includeUnchanged);
750+
}
751+
729752
Boolean suppressOptimisticLockException = m
730753
.getSuppressOptimisticLockException();
731754
if (suppressOptimisticLockException != null) {
@@ -869,6 +892,18 @@ public Void visitSqlFileBatchModifyQueryMeta(
869892
iprint("__query.setVersionIgnored(%1$s);%n", ignoreVersion);
870893
}
871894

895+
List<String> include = m.getInclude();
896+
if (include != null) {
897+
iprint("__query.setIncludedPropertyNames(%1$s);%n",
898+
toCSVFormat(include));
899+
}
900+
901+
List<String> exclude = m.getExclude();
902+
if (exclude != null) {
903+
iprint("__query.setExcludedPropertyNames(%1$s);%n",
904+
toCSVFormat(exclude));
905+
}
906+
872907
Boolean suppressOptimisticLockException = m
873908
.getSuppressOptimisticLockException();
874909
if (suppressOptimisticLockException != null) {

src/main/java/org/seasar/doma/internal/apt/SqlValidator.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.seasar.doma.internal.jdbc.sql.node.ForBlockNode;
4646
import org.seasar.doma.internal.jdbc.sql.node.ForNode;
4747
import org.seasar.doma.internal.jdbc.sql.node.IfNode;
48+
import org.seasar.doma.internal.jdbc.sql.node.PopulateNode;
4849
import org.seasar.doma.internal.jdbc.sql.node.SqlLocation;
4950
import org.seasar.doma.jdbc.SqlNode;
5051
import org.seasar.doma.message.Message;
@@ -67,18 +68,21 @@ public class SqlValidator extends SimpleSqlNodeVisitor<Void, Void> {
6768

6869
protected final boolean expandable;
6970

71+
protected final boolean populatable;
72+
7073
protected final ExpressionValidator expressionValidator;
7174

7275
public SqlValidator(ProcessingEnvironment env,
7376
ExecutableElement methodElement,
7477
Map<String, TypeMirror> parameterTypeMap, String path,
75-
boolean expandable) {
78+
boolean expandable, boolean populatable) {
7679
assertNotNull(env, methodElement, parameterTypeMap, path);
7780
this.env = env;
7881
this.methodElement = methodElement;
7982
this.parameterTypeMap = parameterTypeMap;
8083
this.path = path;
8184
this.expandable = expandable;
85+
this.populatable = populatable;
8286
expressionValidator = new ExpressionValidator(env, methodElement,
8387
parameterTypeMap);
8488
}
@@ -279,6 +283,17 @@ public Void visitExpandNode(ExpandNode node, Void p) {
279283
return visitNode(node, p);
280284
}
281285

286+
@Override
287+
public Void visitPopulateNode(PopulateNode node, Void p) {
288+
if (!populatable) {
289+
SqlLocation location = node.getLocation();
290+
String sql = getSql(location);
291+
throw new AptException(Message.DOMA4270, env, methodElement, path,
292+
sql, location.getLineNumber(), location.getPosition());
293+
}
294+
return visitNode(node, p);
295+
}
296+
282297
@Override
283298
protected Void defaultAction(SqlNode node, Void p) {
284299
return visitNode(node, p);

src/main/java/org/seasar/doma/internal/apt/meta/AbstractSqlFileQueryMetaFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected AbstractSqlFileQueryMetaFactory(ProcessingEnvironment env) {
5353
}
5454

5555
protected void doSqlFiles(M queryMeta, ExecutableElement method,
56-
DaoMeta daoMeta, boolean expandable) {
56+
DaoMeta daoMeta, boolean expandable, boolean populatable) {
5757
if (!Options.getSqlValidation(env)) {
5858
return;
5959
}
@@ -77,7 +77,7 @@ protected void doSqlFiles(M queryMeta, ExecutableElement method,
7777
sqlFilePath, sql);
7878
SqlValidator validator = createSqlValidator(method,
7979
queryMeta.getBindableParameterTypeMap(), sqlFilePath,
80-
expandable);
80+
expandable, populatable);
8181
validator.validate(sqlNode);
8282
queryMeta.addFileName(fileName);
8383
}
@@ -153,8 +153,8 @@ protected SqlNode createSqlNode(M queryMeta, ExecutableElement method,
153153

154154
protected SqlValidator createSqlValidator(ExecutableElement method,
155155
Map<String, TypeMirror> parameterTypeMap, String sqlFilePath,
156-
boolean expandable) {
156+
boolean expandable, boolean populatable) {
157157
return new SqlValidator(env, method, parameterTypeMap, sqlFilePath,
158-
expandable);
158+
expandable, populatable);
159159
}
160160
}

src/main/java/org/seasar/doma/internal/apt/meta/SqlFileBatchModifyQueryMeta.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.seasar.doma.internal.apt.meta;
1717

18+
import java.util.List;
19+
1820
import javax.lang.model.element.ExecutableElement;
1921

2022
import org.seasar.doma.internal.apt.cttype.CtType;
@@ -89,10 +91,23 @@ public Boolean getSuppressOptimisticLockException() {
8991
return batchModifyMirror.getSuppressOptimisticLockExceptionValue();
9092
}
9193

94+
public List<String> getInclude() {
95+
return batchModifyMirror.getIncludeValue();
96+
}
97+
98+
public List<String> getExclude() {
99+
return batchModifyMirror.getExcludeValue();
100+
}
101+
92102
public SqlLogType getSqlLogType() {
93103
return batchModifyMirror.getSqlLogValue();
94104
}
95105

106+
public boolean isPopulatable() {
107+
return entityCtType != null
108+
&& queryKind == QueryKind.SQLFILE_BATCH_UPDATE;
109+
}
110+
96111
@Override
97112
public void addBindableParameterCtType(final String parameterName,
98113
CtType bindableParameterCtType) {

src/main/java/org/seasar/doma/internal/apt/meta/SqlFileBatchModifyQueryMetaFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public QueryMeta createQueryMeta(ExecutableElement method, DaoMeta daoMeta) {
6161
doParameters(queryMeta, method, daoMeta);
6262
doReturnType(queryMeta, method, daoMeta);
6363
doThrowTypes(queryMeta, method, daoMeta);
64-
doSqlFiles(queryMeta, method, daoMeta, false);
64+
doSqlFiles(queryMeta, method, daoMeta, false, queryMeta.isPopulatable());
6565
return queryMeta;
6666
}
6767

@@ -162,9 +162,9 @@ public Void visitEntityCtType(EntityCtType ctType, Void p)
162162
@Override
163163
protected SqlValidator createSqlValidator(ExecutableElement method,
164164
Map<String, TypeMirror> parameterTypeMap, String sqlFilePath,
165-
boolean expandable) {
165+
boolean expandable, boolean populatable) {
166166
return new BatchSqlValidator(env, method, parameterTypeMap,
167-
sqlFilePath, expandable);
167+
sqlFilePath, expandable, populatable);
168168
}
169169

170170
}

src/main/java/org/seasar/doma/internal/apt/meta/SqlFileModifyQueryMeta.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.seasar.doma.internal.apt.meta;
1717

18+
import java.util.List;
19+
1820
import javax.lang.model.element.ExecutableElement;
1921

2022
import org.seasar.doma.internal.apt.cttype.EntityCtType;
@@ -69,14 +71,34 @@ public Boolean getIgnoreVersion() {
6971
return modifyMirror.getIgnoreVersionValue();
7072
}
7173

74+
public Boolean getExcludeNull() {
75+
return modifyMirror.getExcludeNullValue();
76+
}
77+
7278
public Boolean getSuppressOptimisticLockException() {
7379
return modifyMirror.getSuppressOptimisticLockExceptionValue();
7480
}
7581

82+
public Boolean getIncludeUnchanged() {
83+
return modifyMirror.getIncludeUnchangedValue();
84+
}
85+
86+
public List<String> getInclude() {
87+
return modifyMirror.getIncludeValue();
88+
}
89+
90+
public List<String> getExclude() {
91+
return modifyMirror.getExcludeValue();
92+
}
93+
7694
public SqlLogType getSqlLogType() {
7795
return modifyMirror.getSqlLogValue();
7896
}
7997

98+
public boolean isPopulatable() {
99+
return entityCtType != null && queryKind == QueryKind.SQLFILE_UPDATE;
100+
}
101+
80102
@Override
81103
public <R, P> R accept(QueryMetaVisitor<R, P> visitor, P p) {
82104
return visitor.visitSqlFileModifyQueryMeta(this, p);

src/main/java/org/seasar/doma/internal/apt/meta/SqlFileModifyQueryMetaFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public QueryMeta createQueryMeta(ExecutableElement method, DaoMeta daoMeta) {
5353
doParameters(queryMeta, method, daoMeta);
5454
doReturnType(queryMeta, method, daoMeta);
5555
doThrowTypes(queryMeta, method, daoMeta);
56-
doSqlFiles(queryMeta, method, daoMeta, false);
56+
doSqlFiles(queryMeta, method, daoMeta, false, queryMeta.isPopulatable());
5757
return queryMeta;
5858
}
5959

src/main/java/org/seasar/doma/internal/apt/meta/SqlFileScriptQueryMetaFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public QueryMeta createQueryMeta(ExecutableElement method, DaoMeta daoMeta) {
5050
doReturnType(queryMeta, method, daoMeta);
5151
doParameters(queryMeta, method, daoMeta);
5252
doThrowTypes(queryMeta, method, daoMeta);
53-
doSqlFiles(queryMeta, method, daoMeta, false);
53+
doSqlFiles(queryMeta, method, daoMeta, false, false);
5454
return queryMeta;
5555
}
5656

@@ -87,7 +87,8 @@ protected void doParameters(SqlFileScriptQueryMeta queryMeta,
8787

8888
@Override
8989
protected void doSqlFiles(SqlFileScriptQueryMeta queryMeta,
90-
ExecutableElement method, DaoMeta daoMeta, boolean expandable) {
90+
ExecutableElement method, DaoMeta daoMeta, boolean expandable,
91+
boolean populatable) {
9192
String filePath = ScriptFileUtil.buildPath(daoMeta.getDaoElement()
9293
.getQualifiedName().toString(), queryMeta.getName());
9394
File file = getFile(queryMeta, method, filePath);

src/main/java/org/seasar/doma/internal/apt/meta/SqlFileSelectQueryMeta.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ public SqlLogType getSqlLogType() {
153153
return selectMirror.getSqlLogValue();
154154
}
155155

156+
public boolean isExpandable() {
157+
return entityCtType != null;
158+
}
159+
156160
@Override
157161
public <R, P> R accept(QueryMetaVisitor<R, P> visitor, P p) {
158162
return visitor.visitSqlFileSelectQueryMeta(this, p);

0 commit comments

Comments
 (0)