Skip to content

Commit 32c5408

Browse files
authored
Introduce RETURNING functionality for DSL and DAO operations (#1321)
2 parents 50fb0db + bf11ecf commit 32c5408

File tree

151 files changed

+6791
-328
lines changed

Some content is hidden

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

151 files changed

+6791
-328
lines changed

doma-core/src/main/java/org/seasar/doma/Delete.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,10 @@
100100
* @return the output format of SQL logs.
101101
*/
102102
SqlLogType sqlLog() default SqlLogType.FORMATTED;
103+
104+
/**
105+
* @return the {@link Returning} annotation configuration, which defines inclusions or exclusions
106+
* for returning values.
107+
*/
108+
Returning returning() default @Returning;
103109
}

doma-core/src/main/java/org/seasar/doma/Insert.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,10 @@
137137
* @return the keys that should be used to determine if a duplicate key exists.
138138
*/
139139
String[] duplicateKeys() default {};
140+
141+
/**
142+
* @return the {@link Returning} annotation configuration, which defines inclusions or exclusions
143+
* for returning values.
144+
*/
145+
Returning returning() default @Returning;
140146
}

doma-core/src/main/java/org/seasar/doma/MultiInsert.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,10 @@
116116
* @return the keys that should be used to determine if a duplicate key exists.
117117
*/
118118
String[] duplicateKeys() default {};
119+
120+
/**
121+
* @return the {@link Returning} annotation configuration, which defines inclusions or exclusions
122+
* for returning values.
123+
*/
124+
Returning returning() default @Returning;
119125
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Doma Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.seasar.doma;
17+
18+
/**
19+
* Indicates that specific properties of an entity should be included or excluded in the context of
20+
* a {@link Dao} operation.
21+
*
22+
* <ul>
23+
* <li>These attributes are optional and can be used together.
24+
* <li>When neither attribute is specified, all properties are included by default.
25+
* </ul>
26+
*/
27+
public @interface Returning {
28+
/**
29+
* Specifies the names of properties to be explicitly included. If specified, only the listed
30+
* properties will be included.
31+
*
32+
* @return an array of property names to include
33+
*/
34+
String[] include() default {};
35+
36+
/**
37+
* Specifies the names of properties to be explicitly excluded. If specified, all properties will
38+
* be included except for those listed.
39+
*
40+
* @return an array of property names to exclude
41+
*/
42+
String[] exclude() default {};
43+
}

doma-core/src/main/java/org/seasar/doma/Update.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,10 @@
138138
* @return the output format of SQL logs.
139139
*/
140140
SqlLogType sqlLog() default SqlLogType.FORMATTED;
141+
142+
/**
143+
* @return the {@link Returning} annotation configuration, which defines inclusions or exclusions
144+
* for returning values.
145+
*/
146+
Returning returning() default @Returning;
141147
}

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.lang.reflect.Method;
1919
import java.util.function.BiFunction;
20+
import java.util.function.Supplier;
2021
import org.seasar.doma.jdbc.aggregate.AggregateCommand;
2122
import org.seasar.doma.jdbc.aggregate.AggregateStrategyType;
2223
import org.seasar.doma.jdbc.aggregate.StreamReducer;
@@ -26,14 +27,17 @@
2627
import org.seasar.doma.jdbc.command.Command;
2728
import org.seasar.doma.jdbc.command.CreateCommand;
2829
import org.seasar.doma.jdbc.command.DeleteCommand;
30+
import org.seasar.doma.jdbc.command.DeleteReturningCommand;
2931
import org.seasar.doma.jdbc.command.FunctionCommand;
3032
import org.seasar.doma.jdbc.command.InsertCommand;
33+
import org.seasar.doma.jdbc.command.InsertReturningCommand;
3134
import org.seasar.doma.jdbc.command.ProcedureCommand;
3235
import org.seasar.doma.jdbc.command.ResultSetHandler;
3336
import org.seasar.doma.jdbc.command.ScriptCommand;
3437
import org.seasar.doma.jdbc.command.SelectCommand;
3538
import org.seasar.doma.jdbc.command.SqlProcessorCommand;
3639
import org.seasar.doma.jdbc.command.UpdateCommand;
40+
import org.seasar.doma.jdbc.command.UpdateReturningCommand;
3741
import org.seasar.doma.jdbc.entity.EntityType;
3842
import org.seasar.doma.jdbc.query.BatchDeleteQuery;
3943
import org.seasar.doma.jdbc.query.BatchInsertQuery;
@@ -65,6 +69,20 @@ default <RESULT> SelectCommand<RESULT> createSelectCommand(
6569
return new SelectCommand<>(query, resultSetHandler);
6670
}
6771

72+
/**
73+
* Creates an {@link AggregateCommand} object for aggregating entities based on a specified query
74+
* and strategy.
75+
*
76+
* @param <RESULT> the result type of the aggregation
77+
* @param <ENTITY> the entity type used in the aggregation
78+
* @param method the DAO method associated with the command
79+
* @param query the select query to be executed
80+
* @param entityType the type of the root entity for aggregation
81+
* @param resultReducer the reducer used to process the stream of aggregated entities into a final
82+
* result
83+
* @param aggregateStrategyType the strategy type used for aggregation logic
84+
* @return a new {@link AggregateCommand} instance configured with the provided parameters
85+
*/
6886
default <RESULT, ENTITY> AggregateCommand<RESULT, ENTITY> createAggregateCommand(
6987
Method method,
7088
SelectQuery query,
@@ -107,6 +125,58 @@ default UpdateCommand createUpdateCommand(Method method, UpdateQuery query) {
107125
return new UpdateCommand(query);
108126
}
109127

128+
/**
129+
* Creates a {@link DeleteReturningCommand} object.
130+
*
131+
* @param <RESULT> the result type of the command
132+
* @param method the DAO method
133+
* @param query the delete query
134+
* @param resultSetHandler the handler for processing the result set
135+
* @param emptyResultSupplier the supplier for providing an empty result
136+
* @return the created {@link DeleteReturningCommand} object
137+
*/
138+
default <RESULT> DeleteReturningCommand<RESULT> createDeleteReturningCommand(
139+
Method method,
140+
DeleteQuery query,
141+
ResultSetHandler<RESULT> resultSetHandler,
142+
Supplier<RESULT> emptyResultSupplier) {
143+
return new DeleteReturningCommand<>(query, resultSetHandler, emptyResultSupplier);
144+
}
145+
146+
/**
147+
* Creates an {@link InsertReturningCommand} object.
148+
*
149+
* @param method the DAO method
150+
* @param query the query
151+
* @param resultSetHandler the result set handler
152+
* @return the command
153+
*/
154+
default <RESULT> InsertReturningCommand<RESULT> createInsertReturningCommand(
155+
Method method,
156+
InsertQuery query,
157+
ResultSetHandler<RESULT> resultSetHandler,
158+
Supplier<RESULT> emptyResultSupplier) {
159+
return new InsertReturningCommand<>(query, resultSetHandler, emptyResultSupplier);
160+
}
161+
162+
/**
163+
* Creates an {@link UpdateReturningCommand} object.
164+
*
165+
* @param <RESULT> the result type of the command
166+
* @param method the DAO method
167+
* @param query the update query
168+
* @param resultSetHandler the handler for processing the result set
169+
* @param emptyResultSupplier the supplier for providing an empty result
170+
* @return the created {@link UpdateReturningCommand} object
171+
*/
172+
default <RESULT> UpdateReturningCommand<RESULT> createUpdateReturningCommand(
173+
Method method,
174+
UpdateQuery query,
175+
ResultSetHandler<RESULT> resultSetHandler,
176+
Supplier<RESULT> emptyResultSupplier) {
177+
return new UpdateReturningCommand<>(query, resultSetHandler, emptyResultSupplier);
178+
}
179+
110180
/**
111181
* Creates a {@link BatchDeleteCommand} object.
112182
*
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Doma Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.seasar.doma.jdbc.command;
17+
18+
import java.sql.PreparedStatement;
19+
import java.sql.SQLException;
20+
import java.util.function.Supplier;
21+
import org.seasar.doma.jdbc.query.DeleteQuery;
22+
import org.seasar.doma.jdbc.statistic.StatisticManager;
23+
24+
public class DeleteReturningCommand<RESULT> extends ModifyReturningCommand<DeleteQuery, RESULT> {
25+
26+
public DeleteReturningCommand(
27+
DeleteQuery query,
28+
ResultSetHandler<RESULT> resultSetHandler,
29+
Supplier<RESULT> emptyResultSupplier) {
30+
super(query, resultSetHandler, emptyResultSupplier);
31+
}
32+
33+
@Override
34+
protected RESULT executeInternal(PreparedStatement preparedStatement) throws SQLException {
35+
StatisticManager statisticManager = query.getConfig().getStatisticManager();
36+
return statisticManager.executeSql(query.getSql(), () -> executeQuery(preparedStatement));
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Doma Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.seasar.doma.jdbc.command;
17+
18+
import java.sql.PreparedStatement;
19+
import java.sql.SQLException;
20+
import java.util.function.Supplier;
21+
import org.seasar.doma.jdbc.query.InsertQuery;
22+
import org.seasar.doma.jdbc.statistic.StatisticManager;
23+
24+
public class InsertReturningCommand<RESULT> extends ModifyReturningCommand<InsertQuery, RESULT> {
25+
26+
public InsertReturningCommand(
27+
InsertQuery query,
28+
ResultSetHandler<RESULT> resultSetHandler,
29+
Supplier<RESULT> emptyResultSupplier) {
30+
super(query, resultSetHandler, emptyResultSupplier);
31+
}
32+
33+
@Override
34+
protected RESULT executeInternal(PreparedStatement preparedStatement) throws SQLException {
35+
StatisticManager statisticManager = query.getConfig().getStatisticManager();
36+
return statisticManager.executeSql(query.getSql(), () -> executeQuery(preparedStatement));
37+
}
38+
}

0 commit comments

Comments
 (0)