3737import org .junit .jupiter .api .Test ;
3838
3939import javax .sql .DataSource ;
40+ import java .sql .ResultSet ;
4041import java .sql .SQLException ;
42+ import java .util .ArrayList ;
4143import java .util .List ;
4244import java .util .UUID ;
4345
@@ -58,7 +60,7 @@ void quickStart() throws SQLException {
5860 }
5961
6062 @ Test
61- void simpleSelect () throws SQLException {
63+ void querySimpleExample () throws SQLException {
6264 final DataSource dataSource = new H2DataSource ();
6365 new LibraryDB (dataSource ).init ();
6466 final BooksTable booksTable = new DataSourceBooksTable (dataSource );
@@ -74,7 +76,7 @@ void simpleSelect() throws SQLException {
7476 }
7577
7678 @ Test
77- void selectWithParams () throws SQLException {
79+ void queryUsingParams () throws SQLException {
7880 final DataSource dataSource = new H2DataSource ();
7981 new LibraryDB (dataSource ).init ();
8082 final BooksTable booksTable = new DataSourceBooksTable (dataSource );
@@ -93,23 +95,58 @@ void selectWithParams() throws SQLException {
9395 }
9496
9597 @ Test
96- void buildingSelect () throws SQLException {
98+ void queryBuilding () throws SQLException {
9799 final DataSource dataSource = new H2DataSource ();
98100 new LibraryDB (dataSource ).init ();
99101 final BooksTable booksTable = new DataSourceBooksTable (dataSource );
100102 booksTable .insert (UUID .randomUUID (), "Clean Code" );
101103
102104 Queries queries = new RealRdbms (dataSource );
103105
104- Query titlesQuery = queries .query ("SELECT title FROM books" );
105- titlesQuery .append (" WHERE title LIKE ?" , new StringArgument ("Clean%" ));
106- titlesQuery .append (" LIMIT ?" , new IntArgument (10 ));
106+ Query titlesQuery = queries .query ("SELECT title FROM books " );
107+ titlesQuery .append ("WHERE title LIKE ? " , new StringArgument ("Clean%" ));
108+ titlesQuery .append ("LIMIT ?" , new IntArgument (10 ));
107109 List <String > titles = titlesQuery .executeWith (new ColumnToListRsh <>(new StringColumn ("title" )));
108110
109111 assertEquals (1 , titles .size ());
110112 assertEquals ("Clean Code" , titles .get (0 ));
111113 }
112114
115+ @ Test
116+ void queryCustomRsh () throws SQLException {
117+ final DataSource dataSource = new H2DataSource ();
118+ new LibraryDB (dataSource ).init ();
119+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
120+ booksTable .insert (UUID .randomUUID (), "Clean Code" );
121+
122+ class Book {
123+ private final UUID id ;
124+ private final Queries queries ;
125+
126+ public Book (ResultSet resultSet , Queries queries ) throws SQLException {
127+ this .id = resultSet .getObject ("id" , UUID .class );
128+ this .queries = queries ;
129+ }
130+
131+ // methods: title, changeTitle, etc.
132+ }
133+
134+ Queries queries = new RealRdbms (dataSource );
135+ List <Book > titles = queries
136+ .query ("SELECT id FROM books" )
137+ .executeWith (resultSet -> {
138+ final List <Book > books = new ArrayList <>();
139+ while (resultSet .next ()) {
140+ books .add (new Book (resultSet , queries ));
141+ }
142+
143+ return books ;
144+ }
145+ );
146+
147+ assertEquals (1 , titles .size ());
148+ }
149+
113150 @ Test
114151 void changeSimpleExample () throws SQLException {
115152 final DataSource dataSource = new H2DataSource ();
0 commit comments