|
21 | 21 | * code: create, read, optional read, list, update, a missing-row read, a |
22 | 22 | * committed transaction, a rolled back transaction, a case-insensitive search, |
23 | 23 | * a count, a page, a book insert, a left-joined projection, a cataloged book |
24 | | - * insert, a read by enum value, and delete. |
| 24 | + * insert, a read by enum value, an optional filter by name, a read by id list, |
| 25 | + * a grouped book count, a rename, an upsert, and delete. |
25 | 26 | * |
26 | | - * <p>All thirteen named queries are methods of one generated |
| 27 | + * <p>All eighteen named queries are methods of one generated |
27 | 28 | * {@link AuthorRepository}. The repository is constructed once per execution |
28 | 29 | * context: once from the {@code DataSource}-backed executor, and once more per |
29 | 30 | * transaction from a caller-owned connection. |
30 | 31 | * |
31 | | - * <p>Create, read, optional read, list, search, and page each return one |
32 | | - * complete {@code authors} row, so all six share the single top-level |
33 | | - * {@link AuthorsRow} record of the generated package, and the two queries that |
34 | | - * return a complete {@code books} row share the top-level {@link BooksRow} |
35 | | - * record. A query with its own result shape generates its own nested record: |
36 | | - * {@link AuthorRepository.CountAuthorsResult} carries the non-null |
37 | | - * {@code Long} count, and {@link AuthorRepository.ListAuthorBooksResult} |
38 | | - * carries an author name beside the title of the left-joined {@code books} row, |
39 | | - * which is {@code null} for an author without a book. |
| 32 | + * <p>Create, read, optional read, list, search, page, filter, upsert, and the |
| 33 | + * read by id list each return one complete {@code authors} row, so all nine |
| 34 | + * share the single top-level {@link AuthorsRow} record of the generated |
| 35 | + * package, and the two queries that return a complete {@code books} row share |
| 36 | + * the top-level {@link BooksRow} record. A query with its own result shape |
| 37 | + * generates its own nested record: {@link AuthorRepository.CountAuthorsResult} |
| 38 | + * carries the non-null {@code Long} count, |
| 39 | + * {@link AuthorRepository.ListAuthorBooksResult} carries an author name beside |
| 40 | + * the title of the left-joined {@code books} row, which is {@code null} for an |
| 41 | + * author without a book, and |
| 42 | + * {@link AuthorRepository.CountBooksByAuthorResult} carries an author id beside |
| 43 | + * the non-null {@code Long} number of that author's books. |
40 | 44 | * |
41 | 45 | * <p>Row absence is expressed by the {@code :optional} {@code FindAuthor} |
42 | 46 | * query, which returns an empty {@link Optional}, while the {@code :one} |
|
49 | 53 | * {@code JSONB} document as JSON text, which PostgreSQL returns normalized. |
50 | 54 | * The enum is also bound as a query parameter to select books by format. |
51 | 55 | * |
| 56 | + * <p>The fourth migration adds the nullable {@code updated_at} column to |
| 57 | + * {@code authors}, so {@link AuthorsRow} carries it as a |
| 58 | + * {@link java.time.LocalDateTime}. The rename and the upsert set it with |
| 59 | + * {@code now()} without binding a parameter for it, and the upsert targets the |
| 60 | + * existing author of the conflicting id, so the {@code BIGSERIAL} key is never |
| 61 | + * given an explicit new value. The filter binds its one {@code :name} |
| 62 | + * parameter to both of its occurrences, so a {@code null} argument returns |
| 63 | + * every author and a name returns only that author, and the read by id list |
| 64 | + * binds one {@code List} as one server array, so an empty list matches no row. |
| 65 | + * |
52 | 66 | * <p>Every step is checked, so the process exits non-zero as soon as one |
53 | 67 | * generated operation returns an unexpected result. |
54 | 68 | * |
@@ -218,6 +232,71 @@ public static void main(String[] args) throws SQLException { |
218 | 232 | + " " + cataloged.tags() + " " + cataloged.details() |
219 | 233 | ); |
220 | 234 |
|
| 235 | + List<AuthorsRow> unfiltered = authors.filterAuthors(null); |
| 236 | + |
| 237 | + checkEquals(2, unfiltered.size(), "FilterAuthors row count without a name"); |
| 238 | + checkEquals(created.id(), unfiltered.get(0).id(), "FilterAuthors first unfiltered id"); |
| 239 | + checkEquals(committedId, unfiltered.get(1).id(), "FilterAuthors second unfiltered id"); |
| 240 | + |
| 241 | + List<AuthorsRow> filtered = authors.filterAuthors("Ada Lovelace"); |
| 242 | + |
| 243 | + checkEquals(1, filtered.size(), "FilterAuthors row count for one name"); |
| 244 | + checkEquals(created.id(), filtered.get(0).id(), "FilterAuthors filtered id"); |
| 245 | + checkEquals("Ada Lovelace", filtered.get(0).name(), "FilterAuthors filtered name"); |
| 246 | + |
| 247 | + System.out.println( |
| 248 | + "filtered: " + unfiltered.size() + " without a name, " |
| 249 | + + filtered.get(0).name() + " by name" |
| 250 | + ); |
| 251 | + |
| 252 | + List<AuthorsRow> byIds = authors.listAuthorsByIds(List.of(created.id(), committedId)); |
| 253 | + |
| 254 | + checkEquals(2, byIds.size(), "ListAuthorsByIds row count for two ids"); |
| 255 | + checkEquals(created.id(), byIds.get(0).id(), "ListAuthorsByIds first id"); |
| 256 | + checkEquals(committedId, byIds.get(1).id(), "ListAuthorsByIds second id"); |
| 257 | + checkEquals( |
| 258 | + 0, |
| 259 | + authors.listAuthorsByIds(List.of()).size(), |
| 260 | + "ListAuthorsByIds row count for an empty list" |
| 261 | + ); |
| 262 | + |
| 263 | + System.out.println("by ids: " + byIds.get(0).id() + " " + byIds.get(1).id()); |
| 264 | + |
| 265 | + List<AuthorRepository.CountBooksByAuthorResult> bookCounts = authors.countBooksByAuthor(); |
| 266 | + |
| 267 | + checkEquals(1, bookCounts.size(), "CountBooksByAuthor row count"); |
| 268 | + checkEquals(committedId, bookCounts.get(0).authorId(), "CountBooksByAuthor author id"); |
| 269 | + checkEquals(2L, bookCounts.get(0).books(), "CountBooksByAuthor count"); |
| 270 | + |
| 271 | + System.out.println( |
| 272 | + "book counts: " + bookCounts.get(0).authorId() + " / " + bookCounts.get(0).books() |
| 273 | + ); |
| 274 | + |
| 275 | + int renamedRows = authors.renameAuthor("Ada Byron", created.id()); |
| 276 | + |
| 277 | + checkEquals(1, renamedRows, "RenameAuthor affected rows"); |
| 278 | + |
| 279 | + AuthorsRow renamed = authors.getAuthor(created.id()); |
| 280 | + |
| 281 | + checkEquals("Ada Byron", renamed.name(), "name after rename"); |
| 282 | + check(renamed.updatedAt() != null, "RenameAuthor left updatedAt null"); |
| 283 | + |
| 284 | + System.out.println("renamed: " + renamed.name() + " at " + renamed.updatedAt()); |
| 285 | + |
| 286 | + AuthorsRow upserted = authors.upsertAuthor( |
| 287 | + committedId, |
| 288 | + "Grace Murray Hopper", |
| 289 | + "Rear admiral" |
| 290 | + ); |
| 291 | + |
| 292 | + check(upserted != null, "UpsertAuthor returned no row"); |
| 293 | + checkEquals(committedId, upserted.id(), "UpsertAuthor id"); |
| 294 | + checkEquals("Grace Murray Hopper", upserted.name(), "UpsertAuthor name"); |
| 295 | + checkEquals("Rear admiral", upserted.bio(), "UpsertAuthor bio"); |
| 296 | + check(upserted.updatedAt() != null, "UpsertAuthor left updatedAt null"); |
| 297 | + |
| 298 | + System.out.println("upserted: " + upserted.name() + " / " + upserted.bio()); |
| 299 | + |
221 | 300 | int deletedRows = authors.deleteAuthor(created.id()); |
222 | 301 |
|
223 | 302 | checkEquals(1, deletedRows, "DeleteAuthor affected rows"); |
|
0 commit comments