Skip to content

Commit efe5535

Browse files
authored
Merge pull request #410 from domaframework/rename
Rename the "getResultList" and the "getSingleResult" methods
2 parents 9f81650 + 2b19c2f commit efe5535

File tree

10 files changed

+99
-57
lines changed

10 files changed

+99
-57
lines changed

docs/criteria-api.rst

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ For example, to query ``Employee`` and ``Department`` entities and associate the
103103
department.getEmployeeList().add(employee);
104104
});
105105
106-
List<Employee> list = stmt.getResultList();
106+
List<Employee> list = stmt.fetch();
107107
108108
The above query issues the following SQL statement:
109109

@@ -157,7 +157,7 @@ For example, to query two columns with GROUP BY and HAVING clauses, write as fol
157157
return new Tuple2<>(first, second);
158158
});
159159
160-
List<Tuple2<Long, String>> list = stmt.getResultList();
160+
List<Tuple2<Long, String>> list = stmt.fetch();
161161
162162
The above query issues the following SQL statement:
163163

@@ -214,7 +214,7 @@ We also support the following logical operators:
214214
});
215215
});
216216
217-
List<Employee> list = stmt.getResultList();
217+
List<Employee> list = stmt.fetch();
218218
219219
The above query issues the following SQL statement:
220220

@@ -238,7 +238,7 @@ You can write a subquery as follows:
238238
.where(c -> c.in(e.employeeId, c.from(e2).select(e2.managerId)))
239239
.orderBy(c -> c.asc(e.employeeId));
240240
241-
List<Employee> list = stmt.getResultList();
241+
List<Employee> list = stmt.fetch();
242242
243243
The above query issues the following SQL statement:
244244

@@ -269,7 +269,7 @@ innerJoin (Entityql, NativeSql)
269269
Listable<Employee> stmt =
270270
entityql.from(e).innerJoin(d, on -> on.eq(e.departmentId, d.departmentId));
271271
272-
List<Employee> list = stmt.getResultList();
272+
List<Employee> list = stmt.fetch();
273273
274274
The above query issues the following SQL statement:
275275

@@ -291,7 +291,7 @@ leftJoin (Entityql, NativeSql)
291291
Listable<Employee> stmt =
292292
entityql.from(e).leftJoin(d, on -> on.eq(e.departmentId, d.departmentId));
293293
294-
List<Employee> list = stmt.getResultList();
294+
List<Employee> list = stmt.fetch();
295295
296296
The above query issues the following SQL statement:
297297

@@ -328,7 +328,7 @@ You have to use the ``associate`` operation with join expression.
328328
department.getEmployeeList().add(employee);
329329
});
330330
331-
List<Employee> list = stmt.getResultList();
331+
List<Employee> list = stmt.fetch();
332332
333333
The above query issues the following SQL statement:
334334

@@ -363,7 +363,7 @@ You can associate many entities:
363363
})
364364
.associate(e, a, (employee, address) -> employee.setAddress(address));
365365
366-
List<Employee> list = stmt.getResultList();
366+
List<Employee> list = stmt.fetch();
367367
368368
Aggregate Functions (NativeSql)
369369
===============================
@@ -389,7 +389,7 @@ For example, you can pass the ``sum`` function to the select method:
389389
Listable<Salary> stmt =
390390
nativeSql.from(e).<Salary>select(sum(e.salary)).map(row -> row.get(sum(e.salary)));
391391
392-
List<Salary> list = stmt.getResultList();
392+
List<Salary> list = stmt.fetch();
393393
394394
Note that you have to specify a type argument to the select method.
395395

@@ -418,7 +418,7 @@ Group by expression (NativeSql)
418418
return new Tuple2<>(id, count);
419419
});
420420
421-
List<Tuple2<Integer, Long>> list = stmt.getResultList();
421+
List<Tuple2<Integer, Long>> list = stmt.fetch();
422422
423423
The above query issues the following SQL statement:
424424

@@ -464,7 +464,7 @@ We also support the following logical operators:
464464
return new Tuple2<>(first, second);
465465
});
466466
467-
List<Tuple2<Long, String>> list = stmt.getResultList();
467+
List<Tuple2<Long, String>> list = stmt.fetch();
468468
469469
The above query issues the following SQL statement:
470470

@@ -497,7 +497,7 @@ We support the following order operations:
497497
c.desc(e.salary);
498498
});
499499
500-
List<Employee> list = stmt.getResultList();
500+
List<Employee> list = stmt.fetch();
501501
502502
The above query issues the following SQL statement:
503503

@@ -518,7 +518,7 @@ Limit and Offset expression (Entityql, NativeSql)
518518
Listable<Employee> stmt =
519519
nativeSql.from(e).limit(5).offset(3).orderBy(c -> c.asc(e.employeeNo));
520520
521-
List<Employee> list = stmt.getResultList();
521+
List<Employee> list = stmt.fetch();
522522
523523
The above query issues the following SQL statement:
524524

@@ -539,7 +539,7 @@ For Update expression (Entityql, NativeSql)
539539
540540
Listable<Employee> stmt = nativeSql.from(e).where(c -> c.eq(e.employeeId, 1)).forUpdate();
541541
542-
List<Employee> list = stmt.getResultList();
542+
List<Employee> list = stmt.fetch();
543543
544544
The above query issues the following SQL statement:
545545

@@ -578,7 +578,7 @@ We support the following expressions:
578578
return new Tuple2<>(id, name);
579579
});
580580
581-
List<Tuple2<Integer, String>> list = stmt3.getResultList();
581+
List<Tuple2<Integer, String>> list = stmt3.fetch();
582582
583583
The above query issues the following SQL statement:
584584

@@ -599,7 +599,7 @@ Delete statement (Entityql)
599599
Employee_ e = new Employee_();
600600
601601
Listable<Employee> select = entityql.from(e).where(c -> c.eq(e.employeeId, 5));
602-
Employee employee = select.getSingleResult().orElseThrow(AssertionError::new);
602+
Employee employee = select.fetchOptional().orElseThrow(AssertionError::new);
603603
604604
Statement<Employee> delete = entityql.delete(e, employee);
605605
Employee result = delete.execute();
@@ -618,7 +618,7 @@ Batch Delete is also supported:
618618
619619
Listable<Employee> select =
620620
entityql.from(e).where(c -> c.in(e.employeeId, Arrays.asList(5, 6)));
621-
List<Employee> employees = select.getResultList();
621+
List<Employee> employees = select.fetch();
622622
623623
Statement<List<Employee>> delete = entityql.delete(e, employees);
624624
List<Employee> results = delete.execute();
@@ -751,7 +751,7 @@ Batch Update is also supported:
751751
752752
Listable<Employee> select =
753753
entityql.from(e).where(c -> c.in(e.employeeId, Arrays.asList(5, 6)));
754-
List<Employee> employees = select.getResultList();
754+
List<Employee> employees = select.fetch();
755755
employees.forEach(it -> it.setEmployeeName("aaa"));
756756
757757
Statement<List<Employee>> update = entityql.update(e, employees);
@@ -802,7 +802,7 @@ To get a ``config`` object, call ``Config.get(this)`` in the default method as f
802802
803803
Employee_ e = new Employee_();
804804
Listable<Employee> stmt = entityql.from(e).where(c -> c.eq(e.employeeId, id));
805-
return stmt.getSingleResult();
805+
return stmt.fetchOptional();
806806
}
807807
}
808808
@@ -821,7 +821,7 @@ Be careful of the following points when you use the ``select`` method:
821821
Listable<String> stmt =
822822
nativeSql.from(e).<String>select(e.employeeName).map(row -> row.get(e.employeeName));
823823
824-
List<Salary> list = stmt.getResultList();
824+
List<Salary> list = stmt.fetch();
825825
826826
Debugging (Entityql, NativeSql)
827827
-------------------------------
@@ -862,7 +862,7 @@ You can also get the ``Sql`` object by calling the ``peek`` method.
862862
.peek(System.out::println)
863863
.orderBy(c -> c.asc(d.location))
864864
.peek(sql -> System.out.println(sql.getFormattedSql()))
865-
.getResultList();
865+
.fetch();
866866
867867
The above code prints as follows:
868868

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/statement/Listable.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88

99
public interface Listable<ELEMENT> extends Statement<List<ELEMENT>> {
1010

11-
default List<ELEMENT> getResultList() {
11+
default List<ELEMENT> fetch() {
1212
return execute();
1313
}
1414

15-
default Optional<ELEMENT> getSingleResult() {
16-
return execute().stream().findFirst();
17-
}
18-
1915
default Stream<ELEMENT> stream() {
2016
return execute().stream();
2117
}
2218

19+
default Optional<ELEMENT> fetchOptional() {
20+
return stream().findFirst();
21+
}
22+
23+
default ELEMENT fetchOne() {
24+
return fetchOptional().orElse(null);
25+
}
26+
2327
default Listable<ELEMENT> peek(Consumer<Sql<?>> consumer) {
2428
consumer.accept(asSql());
2529
return this;

test-criteria/src/test/java/example/EntityqlBatchDeleteTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ void test() {
2828

2929
Listable<Employee> select =
3030
entityql.from(e).where(c -> c.in(e.employeeId, Arrays.asList(5, 6)));
31-
List<Employee> employees = select.getResultList();
31+
List<Employee> employees = select.fetch();
3232

3333
Statement<List<Employee>> delete = entityql.delete(e, employees);
3434
List<Employee> results = delete.execute();
3535
assertEquals(employees, results);
3636

37-
List<Employee> employees2 = select.getResultList();
37+
List<Employee> employees2 = select.fetch();
3838
assertTrue(employees2.isEmpty());
3939
}
4040

test-criteria/src/test/java/example/EntityqlBatchInsertTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void test() {
4949

5050
Listable<Department> select =
5151
entityql.from(d).where(c -> c.in(d.departmentId, ids)).orderBy(c -> c.asc(d.departmentId));
52-
List<Department> departments2 = select.getResultList();
52+
List<Department> departments2 = select.fetch();
5353
assertEquals(2, departments2.size());
5454
}
5555

test-criteria/src/test/java/example/EntityqlBatchUpdateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void test() {
2929
Listable<Employee> select =
3030
entityql.from(e).where(c -> c.in(e.employeeId, Arrays.asList(5, 6)));
3131

32-
List<Employee> employees = select.getResultList();
32+
List<Employee> employees = select.fetch();
3333
employees.forEach(it -> it.setEmployeeName("aaa"));
3434

3535
Statement<List<Employee>> update = entityql.update(e, employees);

test-criteria/src/test/java/example/EntityqlDeleteTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ void test() {
2525
Employee_ e = new Employee_();
2626
Listable<Employee> select = entityql.from(e).where(c -> c.eq(e.employeeId, 5));
2727

28-
Employee employee = select.getSingleResult().orElseThrow(AssertionError::new);
28+
Employee employee = select.fetchOptional().orElseThrow(AssertionError::new);
2929

3030
Statement<Employee> delete = entityql.delete(e, employee);
3131
Employee result = delete.execute();
3232
assertEquals(employee, result);
3333

34-
List<Employee> employees = select.getResultList();
34+
List<Employee> employees = select.fetch();
3535
assertTrue(employees.isEmpty());
3636
}
3737
}

test-criteria/src/test/java/example/EntityqlInsertTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void test() {
3535

3636
Listable<Department> select =
3737
entityql.from(d).where(c -> c.eq(d.departmentId, department.getDepartmentId()));
38-
Department department2 = select.getSingleResult().orElseThrow(AssertionError::new);
38+
Department department2 = select.fetchOptional().orElseThrow(AssertionError::new);
3939
assertEquals("aaa", department2.getDepartmentName());
4040
}
4141
}

0 commit comments

Comments
 (0)