Skip to content

Commit

Permalink
[persistence][database][query builder] extract into repository
Browse files Browse the repository at this point in the history
  • Loading branch information
GradedJestRisk committed Jul 4, 2021
1 parent 52ce000 commit 64ad61a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 72 deletions.
8 changes: 4 additions & 4 deletions persistence/database/query-builder/jooq/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
plugins {
// id 'nu.studer.jooq' version '5.2.1'
id 'java'
id 'application'
}

repositories {
Expand All @@ -28,10 +29,9 @@ dependencies {
implementation group: 'org.jooq', name: 'jooq-codegen', version: '3.14.11'
}

//application {
// // Define the main class for the application.
// mainClass = 'hello.jooq.App'
//}
application {
mainClass = 'hello.jooq.Main'
}

tasks.named('test') {
// Use junit platform for unit tests.
Expand Down
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;

}
}
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("");
}

}
}

This file was deleted.

This file was deleted.

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");
}
}

0 comments on commit 64ad61a

Please sign in to comment.