Skip to content

Commit f1da6fe

Browse files
committed
Deprecate attach() and next() methods in ResultSetAdapter.
1 parent 8d3c18e commit f1da6fe

File tree

7 files changed

+16
-246
lines changed

7 files changed

+16
-246
lines changed

README.md

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -953,97 +953,6 @@ the values of the "first_name" and "last_name" columns would be returned in a ne
953953
]
954954
```
955955

956-
### Nested Queries
957-
`ResultSetAdapter` can also be used to return the results of nested queries. The `attach()` method assigns a subquery to a key in the result map:
958-
959-
```java
960-
public void attach(String key, String subquery) { ... }
961-
public void attach(String key, Parameters subquery) { ... }
962-
```
963-
964-
Each attached query is executed once per row in the result set. The resulting rows are returned in a list that is associated with the corresponding key.
965-
966-
Internally, subqueries are executed as prepared statements using the `Parameters` class. All values in the base row are supplied as parameter values to each subquery.
967-
968-
An example based on the MySQL "employees" sample database is shown below. The base query retreives the employee's number, first name, and last name from the "employees" table. Subqueries to return the employee's salary and title history are optionally attached based on the values provided in the `details` parameter:
969-
970-
```java
971-
@RequestMethod("GET")
972-
@ResourcePath("?:employeeNumber")
973-
public Map<String, ?> getEmployee(List<String> details) throws SQLException {
974-
String employeeNumber = getKey("employeeNumber");
975-
976-
Parameters parameters = Parameters.parse("SELECT emp_no AS employeeNumber, "
977-
+ "first_name AS firstName, "
978-
+ "last_name AS lastName "
979-
+ "FROM employees WHERE emp_no = :employeeNumber");
980-
981-
Map<String, ?> employee;
982-
try (Connection connection = dataSource.getConnection();
983-
PreparedStatement statement = connection.prepareStatement(parameters.getSQL())) {
984-
parameters.apply(statement, mapOf(
985-
entry("employeeNumber", employeeNumber))
986-
);
987-
988-
try (ResultSet resultSet = statement.executeQuery()) {
989-
ResultSetAdapter resultSetAdapter = new ResultSetAdapter(resultSet);
990-
991-
for (String detail : details) {
992-
switch (detail) {
993-
case "titles": {
994-
resultSetAdapter.attach("titles", "SELECT title, "
995-
+ "from_date AS fromDate, "
996-
+ "to_date AS toDate "
997-
+ "FROM titles WHERE emp_no = :employeeNumber");
998-
999-
break;
1000-
}
1001-
1002-
case "salaries": {
1003-
resultSetAdapter.attach("salaries", "SELECT salary, "
1004-
+ "from_date AS fromDate, "
1005-
+ "to_date AS toDate "
1006-
+ "FROM salaries WHERE emp_no = :employeeNumber");
1007-
1008-
break;
1009-
}
1010-
}
1011-
}
1012-
1013-
employee = resultSetAdapter.next();
1014-
}
1015-
}
1016-
1017-
return employee;
1018-
}
1019-
```
1020-
1021-
A sample response including both titles and salaries is shown below:
1022-
1023-
```json
1024-
{
1025-
"employeeNumber": 10004,
1026-
"firstName": "Chirstian",
1027-
"lastName": "Koblick",
1028-
"titles": [
1029-
{
1030-
"title": "Senior Engineer",
1031-
"fromDate": 817794000000,
1032-
"toDate": 253370782800000
1033-
},
1034-
...
1035-
],
1036-
"salaries": [
1037-
{
1038-
"salary": 74057,
1039-
"fromDate": 1006837200000,
1040-
"toDate": 253370782800000
1041-
},
1042-
...
1043-
]
1044-
}
1045-
```
1046-
1047956
## StreamAdapter
1048957
The `StreamAdapter` class presents the contents of a stream as an iterable sequence. This can be used to serialize the result of a stream operation without needing to first collect the results, which may be expensive if the stream is large:
1049958

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
subprojects {
1616
group = 'org.httprpc'
17-
version = '7.0.2'
17+
version = '7.0.3'
1818

1919
apply plugin: 'java-library'
2020
apply plugin: 'maven-publish'

httprpc-client/src/main/java/org/httprpc/sql/ResultSetAdapter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class ResultSetAdapter implements Iterable<Map<String, Object>> {
3535
private ResultSet resultSet;
3636
private ResultSetMetaData resultSetMetaData;
3737

38+
// TODO Remove this when attach() method is dropped
3839
private LinkedHashMap<String, Parameters> subqueries = new LinkedHashMap<>();
3940

4041
private Iterator<Map<String, Object>> iterator = new Iterator<Map<String, Object>>() {
@@ -149,7 +150,10 @@ public ResultSetAdapter(ResultSet resultSet) {
149150
*
150151
* @param subquery
151152
* The subquery to attach.
153+
*
154+
* @deprecated Use {@link Stream} operations instead.
152155
*/
156+
@Deprecated
153157
public void attach(String key, String subquery) {
154158
attach(key, Parameters.parse(subquery));
155159
}
@@ -162,7 +166,10 @@ public void attach(String key, String subquery) {
162166
*
163167
* @param subquery
164168
* The subquery to attach.
169+
*
170+
* @deprecated Use {@link Stream} operations instead.
165171
*/
172+
@Deprecated
166173
public void attach(String key, Parameters subquery) {
167174
if (key == null) {
168175
throw new IllegalArgumentException();
@@ -180,7 +187,10 @@ public void attach(String key, Parameters subquery) {
180187
*
181188
* @return
182189
* The next result, or <tt>null</tt> if there are no more results.
190+
*
191+
* @deprecated Use {@link Stream} operations instead.
183192
*/
193+
@Deprecated
184194
public Map<String, Object> next() {
185195
return iterator.hasNext() ? iterator.next() : null;
186196
}

httprpc-server/src/main/java/org/httprpc/WebService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,11 @@ private void describeResource(String path, Resource resource, TreeMap<Class<?>,
653653
xmlStreamWriter.writeEndElement();
654654

655655
for (Map.Entry<String, LinkedList<Handler>> entry : resource.handlerMap.entrySet()) {
656-
for (Handler handler : entry.getValue()) {
656+
List<Handler> handlers = entry.getValue();
657+
658+
handlers.sort(Comparator.comparing(handler -> handler.method.getName()));
659+
660+
for (Handler handler : handlers) {
657661
Parameter[] parameters = handler.method.getParameters();
658662

659663
xmlStreamWriter.writeStartElement("pre");

httprpc-test/src/main/java/org/httprpc/test/mysql/EmployeeService.java

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

httprpc-test/src/main/webapp/index.jsp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,5 @@
207207
<br/>
208208
<a href="${pageContext.request.contextPath}/pets/average-age">Average Age</a><br/>
209209

210-
<hr/>
211-
212-
<a href="${pageContext.request.contextPath}/employees?api">Employees (API)</a><br/>
213-
<br/>
214-
<a href="${pageContext.request.contextPath}/employees">Employees</a><br/>
215-
<a href="${pageContext.request.contextPath}/employees?name=bal*">Employees ("bal*")</a><br/>
216-
<br/>
217-
<a href="${pageContext.request.contextPath}/employees/10001">Employee 10001</a><br/>
218-
<a href="${pageContext.request.contextPath}/employees/10001?details=titles&details=salaries">Employee 10001 (details)</a><br/>
219-
220210
</body>
221211
</html>

settings.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
rootProject.name = 'HTTP-RPC'
22

33
include 'httprpc', 'httprpc-client', 'httprpc-server', 'httprpc-test'
4-
5-
enableFeaturePreview('STABLE_PUBLISHING')

0 commit comments

Comments
 (0)