-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[persistence][database][query builder] extract into repository
- Loading branch information
1 parent
52ce000
commit 64ad61a
Showing
6 changed files
with
121 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
persistence/database/query-builder/jooq/app/src/main/java/hello/jooq/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package hello.jooq; | ||
|
||
import org.jooq.DSLContext; | ||
import org.jooq.Query; | ||
import org.jooq.Record; | ||
import org.jooq.Result; | ||
import org.jooq.SQLDialect; | ||
import org.jooq.impl.DSL; | ||
|
||
import java.sql.*; | ||
import java.util.List; | ||
|
||
import static org.jooq.impl.DSL.field; | ||
import static org.jooq.impl.DSL.table; | ||
|
||
public class BookRepository { | ||
|
||
static String userName = "user"; | ||
static String password = "password"; | ||
static String url = "jdbc:postgresql://localhost/database"; | ||
|
||
// https://www.jooq.org/doc/3.15/manual/getting-started/use-cases/jooq-as-a-sql-builder-without-codegeneration/ | ||
public ResultSet getBooksPublishedIn(int year) { | ||
|
||
ResultSet resultSet = null; | ||
// Connection is the only JDBC resource that we need | ||
// PreparedStatement and ResultSet are handled by jOOQ, internally | ||
try (Connection conn = DriverManager.getConnection(url, userName, password)) { | ||
DSLContext create = DSL.using(conn, SQLDialect.POSTGRES); | ||
Query query = create.select(field("book.title"), field("author.first_name"), field("author.last_name")) | ||
.from(table("book")) | ||
.join(table("author")) | ||
.on(field("book.author_id").eq(field("author.id"))) | ||
.where(field("book.published_in").eq(year)); | ||
String sql = query.getSQL(); | ||
List<Object> bindValues = query.getBindValues(); | ||
|
||
PreparedStatement pstmt = conn.prepareStatement(sql); | ||
pstmt.setInt(1, (Integer) bindValues.get(0)); | ||
resultSet = pstmt.executeQuery(); | ||
} | ||
|
||
// For the sake of this tutorial, let's keep exception handling simple | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return resultSet; | ||
} | ||
|
||
public Result<Record> getAuthors() { | ||
|
||
Result<Record> result = null; | ||
|
||
try (Connection conn = DriverManager.getConnection(url, userName, password)) { | ||
DSLContext create = DSL.using(conn, SQLDialect.POSTGRES); | ||
result = create.select().from(table("author")).fetch(); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return result; | ||
|
||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
persistence/database/query-builder/jooq/app/src/main/java/hello/jooq/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package hello.jooq; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws SQLException { | ||
System.getProperties().setProperty("org.jooq.no-logo", "true"); | ||
BookRepository bookRepository = new BookRepository(); | ||
ResultSet resultSet = bookRepository.getBooksPublishedIn(1948); | ||
|
||
ResultSetMetaData rsmd = resultSet.getMetaData(); | ||
int columnsNumber = rsmd.getColumnCount(); | ||
while (resultSet.next()) { | ||
for (int i = 1; i <= columnsNumber; i++) { | ||
if (i > 1) System.out.print(", "); | ||
String columnValue = resultSet.getString(i); | ||
System.out.print(rsmd.getColumnName(i) + ": " + columnValue); | ||
} | ||
System.out.println(""); | ||
} | ||
|
||
} | ||
} |
54 changes: 0 additions & 54 deletions
54
persistence/database/query-builder/jooq/app/src/main/java/test/Main.java
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
persistence/database/query-builder/jooq/app/src/test/java/hello/jooq/AppTest.java
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
persistence/database/query-builder/jooq/app/src/test/java/hello/jooq/BookRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package hello.jooq; | ||
|
||
import org.jooq.Record; | ||
import org.jooq.Result; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.sql.ResultSet; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
|
||
class BookRepositoryTest { | ||
@Test void getBooksPublishedInShouldReturnSomething() { | ||
System.getProperties().setProperty("org.jooq.no-logo", "true"); | ||
BookRepository bookRepository = new BookRepository(); | ||
ResultSet resultSet = bookRepository.getBooksPublishedIn(1948); | ||
assertNotNull(resultSet, "getBooksPublishedIn should return an object"); | ||
} | ||
|
||
@Test void getAuthorsShouldReturnSomething() { | ||
System.getProperties().setProperty("org.jooq.no-logo", "true"); | ||
BookRepository bookRepository = new BookRepository(); | ||
Result<Record> result = bookRepository.getAuthors(); | ||
assertNotNull(result, "getAuthors should return an object"); | ||
} | ||
} |