2424
2525package com .nmalygin .superb .jdbc .real ;
2626
27- import com .nmalygin .superb .jdbc .api .Rdbms ;
27+ import com .nmalygin .superb .jdbc .api .*;
28+ import com .nmalygin .superb .jdbc .api .arguments .IntArgument ;
29+ import com .nmalygin .superb .jdbc .api .arguments .ObjectArgument ;
30+ import com .nmalygin .superb .jdbc .api .arguments .StringArgument ;
31+ import com .nmalygin .superb .jdbc .api .handlers .ColumnToListRsh ;
32+ import com .nmalygin .superb .jdbc .api .handlers .columns .StringColumn ;
2833import com .nmalygin .superb .jdbc .real .testdb .BooksTable ;
2934import com .nmalygin .superb .jdbc .real .testdb .DataSourceBooksTable ;
3035import com .nmalygin .superb .jdbc .real .testdb .H2DataSource ;
3338
3439import javax .sql .DataSource ;
3540import java .sql .SQLException ;
41+ import java .util .List ;
42+ import java .util .UUID ;
3643
3744import static org .junit .jupiter .api .Assertions .assertEquals ;
3845
@@ -49,4 +56,143 @@ void quickStart() throws SQLException {
4956
5057 assertEquals (1 , booksTable .books ().size ());
5158 }
59+
60+ @ Test
61+ void simpleSelect () throws SQLException {
62+ final DataSource dataSource = new H2DataSource ();
63+ new LibraryDB (dataSource ).init ();
64+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
65+ booksTable .insert (UUID .randomUUID (), "Clean Code" );
66+
67+ Queries queries = new RealRdbms (dataSource );
68+ List <String > titles = queries
69+ .query ("SELECT title FROM books" )
70+ .executeWith (new ColumnToListRsh <>(new StringColumn ("title" )));
71+
72+ assertEquals (1 , titles .size ());
73+ assertEquals ("Clean Code" , titles .get (0 ));
74+ }
75+
76+ @ Test
77+ void selectWithParams () throws SQLException {
78+ final DataSource dataSource = new H2DataSource ();
79+ new LibraryDB (dataSource ).init ();
80+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
81+ booksTable .insert (UUID .randomUUID (), "Clean Code" );
82+
83+ Queries queries = new RealRdbms (dataSource );
84+ List <String > titles = queries
85+ .query (
86+ "SELECT title FROM books WHERE title LIKE ?" ,
87+ new StringArgument ("Clean%" )
88+ )
89+ .executeWith (new ColumnToListRsh <>(new StringColumn ("title" )));
90+
91+ assertEquals (1 , titles .size ());
92+ assertEquals ("Clean Code" , titles .get (0 ));
93+ }
94+
95+ @ Test
96+ void buildingSelect () throws SQLException {
97+ final DataSource dataSource = new H2DataSource ();
98+ new LibraryDB (dataSource ).init ();
99+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
100+ booksTable .insert (UUID .randomUUID (), "Clean Code" );
101+
102+ Queries queries = new RealRdbms (dataSource );
103+
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 ));
107+ List <String > titles = titlesQuery .executeWith (new ColumnToListRsh <>(new StringColumn ("title" )));
108+
109+ assertEquals (1 , titles .size ());
110+ assertEquals ("Clean Code" , titles .get (0 ));
111+ }
112+
113+ @ Test
114+ void changeSimpleExample () throws SQLException {
115+ final DataSource dataSource = new H2DataSource ();
116+ new LibraryDB (dataSource ).init ();
117+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
118+
119+ Changes changes = new RealRdbms (dataSource );
120+
121+ changes
122+ .change ("INSERT INTO books(title) VALUES ('Clean Code')" )
123+ .apply ();
124+
125+ assertEquals (1 , booksTable .books ().size ());
126+ }
127+
128+ @ Test
129+ void changeWithParams () throws SQLException {
130+ final DataSource dataSource = new H2DataSource ();
131+ new LibraryDB (dataSource ).init ();
132+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
133+
134+ Changes changes = new RealRdbms (dataSource );
135+
136+ changes
137+ .change ("INSERT INTO books(title) VALUES (?)" , new StringArgument ("Clean Code" ))
138+ .apply ();
139+
140+ assertEquals (1 , booksTable .books ().size ());
141+ }
142+
143+ @ Test
144+ void batchSimpleExample () throws SQLException {
145+ final DataSource dataSource = new H2DataSource ();
146+ new LibraryDB (dataSource ).init ();
147+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
148+
149+ Batches batches = new RealRdbms (dataSource );
150+
151+ try (Batch batch = batches .batch ("INSERT INTO books(title) VALUES (?)" )) {
152+ batch .put (new StringArgument ("Clean Code" ));
153+ batch .put (new StringArgument ("Code Complete" ));
154+ batch .put (new StringArgument ("Effective Java" ));
155+ batch .apply ();
156+ }
157+
158+ assertEquals (3 , booksTable .books ().size ());
159+ }
160+
161+ @ Test
162+ void transactionSimpleExample () throws SQLException {
163+ final DataSource dataSource = new H2DataSource ();
164+ new LibraryDB (dataSource ).init ();
165+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
166+
167+ Transactions transactions = new RealRdbms (dataSource );
168+
169+ try (Transaction transaction = transactions .transaction ()) {
170+ transaction .change ("INSERT INTO books(title) VALUES ('Clean Code')" ).apply ();
171+ transaction .change ("INSERT INTO books(title) VALUES ('Code Complete')" ).apply ();
172+ transaction .change ("INSERT INTO books(title) VALUES ('Effective Java')" ).apply ();
173+ transaction .commit ();
174+ }
175+
176+ assertEquals (3 , booksTable .books ().size ());
177+ }
178+
179+ @ Test
180+ void transactionUsingSavepoints () throws SQLException {
181+ final DataSource dataSource = new H2DataSource ();
182+ new LibraryDB (dataSource ).init ();
183+ final BooksTable booksTable = new DataSourceBooksTable (dataSource );
184+
185+ Transactions transactions = new RealRdbms (dataSource );
186+
187+ try (Transaction transaction = transactions .transaction ()) {
188+ transaction .change ("INSERT INTO books(title) VALUES ('Clean Code')" ).apply ();
189+ transaction .setSavepoint ("MySavepoint" );
190+ transaction .change ("INSERT INTO books(title) VALUES ('Code Complete')" ).apply ();
191+ transaction .rollbackTo ("MySavepoint" );
192+ transaction .change ("INSERT INTO books(title) VALUES ('Effective Java')" ).apply ();
193+ transaction .commit ();
194+ }
195+
196+ assertEquals (3 , booksTable .books ().size ());
197+ }
52198}
0 commit comments