Skip to content

Commit 268cd2a

Browse files
authored
Merge pull request #5 from nmalygin-com/ci-test
improve README.md
2 parents e380e86 + c715a42 commit 268cd2a

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

README.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ Rdbms rdbms = new RealRdbms(dataSource);
2828

2929
3. Use the rdbms object
3030
```java
31-
rdbms.change("INSERT INTO books(title) VALUES ('Clean Code')").apply();
31+
rdbms
32+
.change("INSERT INTO books(title) VALUES ('Clean Code')")
33+
.apply();
3234
```
3335

3436
## Abstractions
@@ -38,7 +40,7 @@ rdbms.change("INSERT INTO books(title) VALUES ('Clean Code')").apply();
3840
Interface `Rdbms` combines interfaces: `Queries`, `Changes`, `Batches`, `Transactions` (factories of objects `Query`,
3941
`Change`, `Batch` and `Transaction`).
4042

41-
This separation of interfaces allows us to not violate the _Liskov's Substitution Principle_ in places where we
43+
This separation of interfaces allows us to not violate the _Interface Segregation Principle (ISP)_ in places where we
4244
want to provide partial functionality.
4345

4446
Note that extracting interfaces also allows us to use doubles (Fake, Stub, etc.) to write tests.
@@ -80,16 +82,48 @@ define the query and then set the parameter values).
8082
#### Building a query
8183

8284
```java
83-
Query titlesQuery = queries.query("SELECT title FROM books");
84-
titlesQuery.append(" WHERE title LIKE ?", new StringArgument("Clean%"));
85-
titlesQuery.append(" LIMIT ?", new IntArgument(10));
85+
Query titlesQuery = queries.query("SELECT title FROM books ");
86+
titlesQuery.append("WHERE title LIKE ? ", new StringArgument("Clean%"));
87+
titlesQuery.append("LIMIT ?", new IntArgument(10));
8688
List<String> titles = titlesQuery
8789
.executeWith(new ColumnToListRsh<>(new StringColumn("title")));
8890
```
8991

9092
This approach can be useful, for example, when we have a web form and, depending on the fields filled in it, we want to
9193
be able to change the structure of our query (include or exclude some parts of the query).
9294

95+
#### Custom ResultSetHandler
96+
97+
```java
98+
class Book {
99+
private final UUID id;
100+
private final Queries queries;
101+
102+
public Book(ResultSet resultSet, Queries queries) throws SQLException {
103+
this.id = resultSet.getObject("id", UUID.class);
104+
this.queries = queries;
105+
}
106+
107+
// methods: title(), changeTitle(String newTitle), etc.
108+
}
109+
110+
List<Book> titles = queries
111+
.query("SELECT id FROM books")
112+
.executeWith(resultSet -> {
113+
final List<Book> books = new ArrayList<>();
114+
while (resultSet.next()) {
115+
books.add(new Book(resultSet, queries));
116+
}
117+
118+
return books;
119+
}
120+
);
121+
```
122+
123+
Method `executeWith` accepts an object implementing interface `ResultSetHandler`. In turn, the interface
124+
`ResultSetHandler` has a single method `handle`, which must process the result set obtained as a result of the SQL
125+
query and return some object.
126+
93127
### Change
94128

95129
The `Change` interface is a request to change the database (_DDL or DML_).
@@ -158,4 +192,10 @@ try (Transaction transaction = transactions.transaction()) {
158192
transaction.change("INSERT INTO books(title) VALUES ('Effective Java')").apply();
159193
transaction.commit();
160194
}
161-
```
195+
```
196+
197+
## Contributing
198+
199+
If you find the **Superb JDBC** useful and want to help, you can:
200+
1. Create an issue.
201+
2. Resolve a created issue.

src/test/java/com/nmalygin/superb/jdbc/real/ReadmeTest.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import org.junit.jupiter.api.Test;
3838

3939
import javax.sql.DataSource;
40+
import java.sql.ResultSet;
4041
import java.sql.SQLException;
42+
import java.util.ArrayList;
4143
import java.util.List;
4244
import 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

Comments
 (0)