Skip to content

Commit 4bfa83c

Browse files
authored
Feature/storm 19 (#20)
* Support a list-based findAll method alongside stream-based methods. * Find and register Jackson modules.
1 parent c97db97 commit 4bfa83c

File tree

17 files changed

+170
-73
lines changed

17 files changed

+170
-73
lines changed

README.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ Hybrid::
717717
Map<Ref<City>, Long> counts = ORM(dataSource).entity(User.class)
718718
.select(GroupedByCity.class, RAW."\{select(City.class, SelectMode.PK)}, COUNT(*)")
719719
.groupBy(RAW."\{User_.city}")
720-
.getResultList()
720+
.getResultList().stream()
721721
.collect(toMap(GroupedByCity::city, GroupedByCity::count));
722722
----
723723
====
@@ -776,7 +776,7 @@ ORM::
776776
+
777777
[source,java]
778778
----
779-
try (Stream<User> users = userRepository.findAll()) {
779+
try (Stream<User> users = userRepository.selectAll()) {
780780
List<Integer> userIds = users.map(User::id).toList();
781781
...
782782
}

storm-json/src/main/java/st/orm/json/spi/JsonORMConverterImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public JsonORMConverterImpl(@Nonnull RecordComponent component, @Nonnull TypeRef
5959
.orElse(null);
6060
this.mapper = OBJECT_MAPPER.computeIfAbsent(new CacheKey(type, json), key -> {
6161
var mapper = new ObjectMapper();
62+
mapper.findAndRegisterModules();
6263
if (!json.failOnUnknown()) {
6364
mapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
6465
}

storm-kotlin/src/main/java/st/orm/kotlin/repository/KEntityRepository.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,19 @@ public interface KEntityRepository<E extends Record & Entity<ID>, ID> extends KR
412412

413413
// List based methods.
414414

415+
/**
416+
* Returns a list of all entities of the type supported by this repository. Each element in the list represents
417+
* an entity in the database, encapsulating all relevant data as mapped by the entity model.
418+
*
419+
* <p><strong>Please note:</strong> loading all entities into memory at once can be very memory-intensive if your
420+
* table is large.</p>
421+
*
422+
* @return a stream of all entities of the type supported by this repository.
423+
* @throws PersistenceException if the selection operation fails due to underlying database issues, such as
424+
* connectivity.
425+
*/
426+
List<E> findAll();
427+
415428
/**
416429
* Retrieves a list of entities based on their primary keys.
417430
*
@@ -613,7 +626,7 @@ public interface KEntityRepository<E extends Record & Entity<ID>, ID> extends KR
613626
* @return the result produced by the callback's processing of the entity sequence.
614627
* @throws PersistenceException if the operation fails due to underlying database issues, such as connectivity.
615628
*/
616-
<R> R findAll(@Nonnull KResultCallback<E, R> callback);
629+
<R> R selectAll(@Nonnull KResultCallback<E, R> callback);
617630

618631
/**
619632
* Processes a sequence of entities corresponding to the provided IDs using the specified callback.
@@ -630,7 +643,7 @@ public interface KEntityRepository<E extends Record & Entity<ID>, ID> extends KR
630643
* @return the result produced by the callback's processing of the entity sequence.
631644
* @throws PersistenceException if the operation fails due to underlying database issues, such as connectivity.
632645
*/
633-
<R> R findAll(@Nonnull Sequence<ID> ids, @Nonnull KResultCallback<E, R> callback);
646+
<R> R selectAll(@Nonnull Sequence<ID> ids, @Nonnull KResultCallback<E, R> callback);
634647

635648
/**
636649
* Retrieves a sequence of entities based on their primary keys.
@@ -655,7 +668,7 @@ public interface KEntityRepository<E extends Record & Entity<ID>, ID> extends KR
655668
* @throws PersistenceException if the selection operation fails due to underlying database issues, such as
656669
* connectivity.
657670
*/
658-
<R> R findAllById(@Nonnull Sequence<ID> ids, int batchSize, @Nonnull KResultCallback<E, R> callback);
671+
<R> R selectAllById(@Nonnull Sequence<ID> ids, int batchSize, @Nonnull KResultCallback<E, R> callback);
659672

660673
/**
661674
* Retrieves a sequence of entities based on their primary keys.
@@ -680,7 +693,7 @@ public interface KEntityRepository<E extends Record & Entity<ID>, ID> extends KR
680693
* @throws PersistenceException if the selection operation fails due to underlying database issues, such as
681694
* connectivity.
682695
*/
683-
<R> R findAllByRef(@Nonnull Sequence<Ref<E>> refs, int batchSize, @Nonnull KResultCallback<E, R> callback);
696+
<R> R selectAllByRef(@Nonnull Sequence<Ref<E>> refs, int batchSize, @Nonnull KResultCallback<E, R> callback);
684697

685698
/**
686699
* Counts the number of entities identified by the provided sequence of IDs using the default batch size.

storm-kotlin/src/main/java/st/orm/kotlin/repository/KProjectionRepository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ public interface KProjectionRepository<P extends Record & Projection<ID>, ID> ex
190190

191191
// List based methods.
192192

193+
/**
194+
* Returns a list of all projections of the type supported by this repository. Each element in the list represents
195+
* a projection in the database, encapsulating all relevant data as mapped by the projection model.
196+
*
197+
* <p><strong>Please note:</strong> loading all projections into memory at once can be very memory-intensive if
198+
* your table is large.</p>
199+
*
200+
* @return a stream of all entities of the type supported by this repository.
201+
* @throws PersistenceException if the selection operation fails due to underlying database issues, such as
202+
* connectivity.
203+
*/
204+
List<P> findAll();
205+
193206
/**
194207
* Retrieves a list of projections based on their primary keys.
195208
*

storm-kotlin/src/main/java/st/orm/kotlin/spi/KEntityRepositoryImpl.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,21 @@ public E getByRef(@Nonnull Ref<E> ref) {
559559

560560
// List based methods.
561561

562+
/**
563+
* Returns a list of all entities of the type supported by this repository. Each element in the list represents
564+
* an entity in the database, encapsulating all relevant data as mapped by the entity model.
565+
*
566+
* <p><strong>Please note:</strong> loading all entities into memory at once can be very memory-intensive if your
567+
* table is large.</p>
568+
*
569+
* @return a stream of all entities of the type supported by this repository.
570+
* @throws PersistenceException if the selection operation fails due to underlying database issues, such as
571+
* connectivity.
572+
*/
573+
public List<E> findAll() {
574+
return entityRepository.findAll();
575+
}
576+
562577
/**
563578
* Retrieves a list of entities based on their primary keys.
564579
*
@@ -771,8 +786,8 @@ public void delete(@Nonnull Iterable<E> entities) {
771786
* @throws PersistenceException if the operation fails due to underlying database issues, such as connectivity.
772787
*/
773788
@Override
774-
public <R> R findAll(@Nonnull KResultCallback<E, R> callback) {
775-
return entityRepository.findAll(stream -> callback.process(toSequence(stream)));
789+
public <R> R selectAll(@Nonnull KResultCallback<E, R> callback) {
790+
return entityRepository.selectAll(stream -> callback.process(toSequence(stream)));
776791
}
777792

778793
/**
@@ -791,8 +806,8 @@ public <R> R findAll(@Nonnull KResultCallback<E, R> callback) {
791806
* @throws PersistenceException if the operation fails due to underlying database issues, such as connectivity.
792807
*/
793808
@Override
794-
public <R> R findAll(@Nonnull Sequence<ID> ids, @Nonnull KResultCallback<E, R> callback) {
795-
return entityRepository.findAllById(toStream(ids), stream -> callback.process(toSequence(stream)));
809+
public <R> R selectAll(@Nonnull Sequence<ID> ids, @Nonnull KResultCallback<E, R> callback) {
810+
return entityRepository.selectAllById(toStream(ids), stream -> callback.process(toSequence(stream)));
796811
}
797812

798813
/**
@@ -819,8 +834,8 @@ public <R> R findAll(@Nonnull Sequence<ID> ids, @Nonnull KResultCallback<E, R> c
819834
* connectivity.
820835
*/
821836
@Override
822-
public <R> R findAllById(@Nonnull Sequence<ID> ids, int batchSize, @Nonnull KResultCallback<E, R> callback) {
823-
return entityRepository.findAllById(toStream(ids), batchSize, stream -> callback.process(toSequence(stream)));
837+
public <R> R selectAllById(@Nonnull Sequence<ID> ids, int batchSize, @Nonnull KResultCallback<E, R> callback) {
838+
return entityRepository.selectAllById(toStream(ids), batchSize, stream -> callback.process(toSequence(stream)));
824839
}
825840

826841
/**
@@ -847,8 +862,8 @@ public <R> R findAllById(@Nonnull Sequence<ID> ids, int batchSize, @Nonnull KRes
847862
* connectivity.
848863
*/
849864
@Override
850-
public <R> R findAllByRef(@Nonnull Sequence<Ref<E>> refs, int batchSize, @Nonnull KResultCallback<E, R> callback) {
851-
return entityRepository.findAllByRef(toStream(refs), batchSize, stream -> callback.process(toSequence(stream)));
865+
public <R> R selectAllByRef(@Nonnull Sequence<Ref<E>> refs, int batchSize, @Nonnull KResultCallback<E, R> callback) {
866+
return entityRepository.selectAllByRef(toStream(refs), batchSize, stream -> callback.process(toSequence(stream)));
852867
}
853868

854869
/**

storm-kotlin/src/main/java/st/orm/kotlin/spi/KProjectionRepositoryImpl.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
import static java.util.Objects.requireNonNull;
4444
import static java.util.Spliterators.spliteratorUnknownSize;
45-
import static kotlin.sequences.SequencesKt.sequenceOf;
4645

4746
/**
4847
*/
@@ -266,6 +265,21 @@ public P getByRef(@Nonnull Ref<P> ref) {
266265

267266
// List based methods.
268267

268+
/**
269+
* Returns a list of all projections of the type supported by this repository. Each element in the list represents
270+
* a projection in the database, encapsulating all relevant data as mapped by the projection model.
271+
*
272+
* <p><strong>Please note:</strong> loading all projections into memory at once can be very memory-intensive if
273+
* your table is large.</p>
274+
*
275+
* @return a stream of all entities of the type supported by this repository.
276+
* @throws PersistenceException if the selection operation fails due to underlying database issues, such as
277+
* connectivity.
278+
*/
279+
public List<P> findAll() {
280+
return projectionRepository.findAll();
281+
}
282+
269283
/**
270284
* Retrieves a list of projections based on their primary keys.
271285
*
@@ -324,7 +338,7 @@ public List<P> findAllByRef(@Nonnull Iterable<Ref<P>> refs) {
324338
*/
325339
@Override
326340
public <R> R findAll(@Nonnull KResultCallback<P, R> callback) {
327-
return projectionRepository.findAll(stream -> callback.process(toSequence(stream)));
341+
return projectionRepository.selectAll(stream -> callback.process(toSequence(stream)));
328342
}
329343

330344
/**
@@ -344,7 +358,7 @@ public <R> R findAll(@Nonnull KResultCallback<P, R> callback) {
344358
*/
345359
@Override
346360
public <R> R findAllById(@Nonnull Sequence<ID> ids, @Nonnull KResultCallback<P, R> callback) {
347-
return projectionRepository.findAllById(toStream(ids), stream -> callback.process(toSequence(stream)));
361+
return projectionRepository.selectAllById(toStream(ids), stream -> callback.process(toSequence(stream)));
348362
}
349363

350364
/**
@@ -372,7 +386,7 @@ public <R> R findAllById(@Nonnull Sequence<ID> ids, @Nonnull KResultCallback<P,
372386
*/
373387
@Override
374388
public <R> R findAllById(@Nonnull Sequence<ID> ids, int batchSize, @Nonnull KResultCallback<P, R> callback) {
375-
return projectionRepository.findAllById(toStream(ids), batchSize, stream -> callback.process(toSequence(stream)));
389+
return projectionRepository.selectAllById(toStream(ids), batchSize, stream -> callback.process(toSequence(stream)));
376390
}
377391

378392
/**
@@ -392,7 +406,7 @@ public <R> R findAllById(@Nonnull Sequence<ID> ids, int batchSize, @Nonnull KRes
392406
*/
393407
@Override
394408
public <R> R findAllByRef(@Nonnull Sequence<Ref<P>> refs, @Nonnull KResultCallback<P, R> callback) {
395-
return projectionRepository.findAllByRef(toStream(refs), stream -> callback.process(toSequence(stream)));
409+
return projectionRepository.selectAllByRef(toStream(refs), stream -> callback.process(toSequence(stream)));
396410
}
397411

398412
/**
@@ -420,7 +434,7 @@ public <R> R findAllByRef(@Nonnull Sequence<Ref<P>> refs, @Nonnull KResultCallba
420434
*/
421435
@Override
422436
public <R> R findAllByRef(@Nonnull Sequence<Ref<P>> refs, int batchSize, @Nonnull KResultCallback<P, R> callback) {
423-
return projectionRepository.findAllByRef(toStream(refs), batchSize, stream -> callback.process(toSequence(stream)));
437+
return projectionRepository.selectAllByRef(toStream(refs), batchSize, stream -> callback.process(toSequence(stream)));
424438
}
425439

426440
/**

storm-kotlin/src/test/java/st/orm/kotlin/KotlinRepositoryPreparedStatementIntegrationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ open class KotlinRepositoryPreparedStatementIntegrationTest {
7474
@Test
7575
fun testFindAll() {
7676
val repository = ORM(dataSource).repository(KotlinPetRepository::class)
77-
repository.findAll().let {
77+
repository.getAll().let {
7878
assertEquals(1, it.first().id())
7979
assertEquals(13, it.size)
8080
}

storm-kotlin/src/test/java/st/orm/kotlin/repository/KotlinPetRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import st.orm.kotlin.model.KotlinPet
44

55
interface KotlinPetRepository : KEntityRepository<KotlinPet, Int> {
66

7-
fun findAll(): List<KotlinPet> = orm().query {
7+
fun getAll(): List<KotlinPet> = orm().query {
88
"""
99
SELECT ${it(KotlinPet::class)}
1010
FROM ${it(KotlinPet::class)}

storm-mssqlserver/src/main/java/st/orm/spi/mssqlserver/MSSQLServerEntityRepositoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ protected void insertAndFetch(@Nonnull List<E> batch,
448448
@Nonnull Supplier<PreparedQuery> querySupplier,
449449
@Nullable BatchCallback<E> callback) {
450450
insertAndFetchIds(batch, querySupplier, callback == null ? null : ids -> {
451-
try (var stream = findAllById(ids)) {
451+
try (var stream = selectAllById(ids)) {
452452
callback.process(stream);
453453
}
454454
});
@@ -458,7 +458,7 @@ protected void upsertAndFetch(@Nonnull List<E> batch,
458458
@Nonnull Supplier<PreparedQuery> querySupplier,
459459
@Nullable BatchCallback<E> callback) {
460460
upsertAndFetchIds(batch, querySupplier, callback == null ? null : ids -> {
461-
try (var stream = findAllById(ids)) {
461+
try (var stream = selectAllById(ids)) {
462462
callback.process(stream);
463463
}
464464
});

storm-mysql/src/main/java/st/orm/spi/mysql/MySQLEntityRepositoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ protected void upsertAndFetchIds(@Nonnull List<E> batch, @Nonnull Supplier<Prepa
484484

485485
protected void upsertAndFetch(@Nonnull List<E> batch, @Nonnull Supplier<PreparedQuery> querySupplier, @Nullable BatchCallback<E> callback) {
486486
upsertAndFetchIds(batch, querySupplier, callback == null ? null : ids -> {
487-
try (var stream = findAllById(ids)) {
487+
try (var stream = selectAllById(ids)) {
488488
callback.process(stream);
489489
}
490490
});

0 commit comments

Comments
 (0)