Skip to content

Commit

Permalink
#1: Add select queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Nov 8, 2019
1 parent 46a9656 commit 1f3db01
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 12 deletions.
29 changes: 27 additions & 2 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/BoundsAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package io.jenetics.jpx.jdbc;

import static io.jenetics.facilejdbc.Dctor.field;
import static io.jenetics.facilejdbc.Param.value;

import java.sql.Connection;
import java.sql.SQLException;
Expand All @@ -28,6 +29,7 @@

import io.jenetics.facilejdbc.Dctor;
import io.jenetics.facilejdbc.Query;
import io.jenetics.facilejdbc.RowParser;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
Expand All @@ -37,23 +39,46 @@
public final class BoundsAccess {
private BoundsAccess() {}

private static final Query INSERT_QUERY = Query.of(
private static final Query SELECT = Query.of(
"SELECT minlat, minlon, maxlat, maxlon " +
"FROM bounds " +
"WHERE id = :id"
);

private static final Query INSERT = Query.of(
"INSERT INTO bounds(minlat, minlon, maxlat, maxlon) " +
"VALUES(:minlat, :minlon, :maxlat, :maxlon)"
);

private static final RowParser<Bounds> PARSER = row -> Bounds.of(
row.getDouble("minlat"),
row.getDouble("minlon"),
row.getDouble("maxlat"),
row.getDouble("maxlon")
);

private static final Dctor<Bounds> DCTOR = Dctor.of(
field("minlat", Bounds::getMinLatitude),
field("minlon", Bounds::getMinLongitude),
field("maxlat", Bounds::getMaxLatitude),
field("maxlon", Bounds::getMaxLongitude)
);

public static Bounds selectById(final Long id, final Connection conn)
throws SQLException
{
return id != null
? SELECT
.on(value("id", id))
.as(PARSER.singleNullable(), conn)
: null;
}

public static Long insert(final Bounds bounds, final Connection conn)
throws SQLException
{
return bounds != null
? INSERT_QUERY
? INSERT
.on(bounds, DCTOR)
.executeInsert(conn)
.orElseThrow()
Expand Down
30 changes: 28 additions & 2 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/CopyrightAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
package io.jenetics.jpx.jdbc;

import static io.jenetics.facilejdbc.Dctor.field;
import static io.jenetics.facilejdbc.Param.value;

import java.net.URI;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.Year;

import io.jenetics.jpx.Copyright;
import io.jenetics.jpx.Person;

import io.jenetics.facilejdbc.Dctor;
import io.jenetics.facilejdbc.Query;
import io.jenetics.facilejdbc.RowParser;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
Expand All @@ -38,22 +42,44 @@
public final class CopyrightAccess {
private CopyrightAccess() {}

private static final Query INSERT_QUERY = Query.of(
private static final Query SELECT = Query.of(
"SELECT id, author, year, license " +
"FROM copyright " +
"WHERE id = :id"
);

private static final Query INSERT = Query.of(
"INSERT INTO copyright(author, year, license) " +
"VALUES(:author, :year, :license)"
);

private static final RowParser<Copyright> PARSER = row -> Copyright.of(
row.getString("author"),
Year.of(row.getInt("year")),
URI.create(row.getString("license"))
);

private static final Dctor<Copyright> DCTOR = Dctor.of(
field("author", Copyright::getAuthor),
field("year", c -> c.getYear().map(Year::getValue)),
field("license", Copyright::getLicense)
);

public static Copyright selectById(final Long id, final Connection conn)
throws SQLException
{
return id != null
? SELECT
.on(value("id", id))
.as(PARSER.singleNullable(), conn)
: null;
}

public static Long insert(final Copyright copyright, final Connection conn)
throws SQLException
{
return copyright != null
? INSERT_QUERY
? INSERT
.on(copyright, DCTOR)
.executeInsert(conn)
.orElseThrow()
Expand Down
5 changes: 2 additions & 3 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class LinkAccess {
private LinkAccess() {}

private static final Query SELECT = Query.of(
"SELECT href, text, type " +
"SELECT id, href, text, type " +
"FROM link " +
"WHERE id = :id;"
);
Expand Down Expand Up @@ -69,8 +69,7 @@ public static Link selectById(final Long id, final Connection conn)
return id != null
? SELECT
.on(value("id", id))
.as(PARSER.singleOpt(), conn)
.orElse(null)
.as(PARSER.singleNullable(), conn)
: null;
}

Expand Down
73 changes: 71 additions & 2 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/MetadataAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,30 @@
package io.jenetics.jpx.jdbc;

import static io.jenetics.facilejdbc.Dctor.field;
import static io.jenetics.facilejdbc.Param.value;

import lombok.Builder;
import lombok.Value;
import lombok.experimental.Accessors;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

import io.jenetics.jpx.Bounds;
import io.jenetics.jpx.Copyright;
import io.jenetics.jpx.Link;
import io.jenetics.jpx.Metadata;
import io.jenetics.jpx.Person;

import io.jenetics.facilejdbc.Batch;
import io.jenetics.facilejdbc.Dctor;
import io.jenetics.facilejdbc.Query;
import io.jenetics.facilejdbc.RowParser;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
Expand All @@ -40,7 +53,26 @@
public final class MetadataAccess {
private MetadataAccess() {}

private static final Query INSERT_QUERY = Query.of(
@Value
@Builder(builderClassName = "Builder", toBuilder = true)
@Accessors(fluent = true)
private static final class MetadataRow {
private final String name;
private final String desc;
private final Timestamp time;
private final String keyword;
private final Long personId;
private final Long copyrightId;
private final Long boundsId;
}

private static final Query SELECT = Query.of(
"SELECT name, dscr, time, keywords, person_id, copyright_id, bounds_id " +
"FROM metadata " +
"WHERE id = :id"
);

private static final Query INSERT = Query.of(
"INSERT INTO metadata(" +
"name, " +
"dscr, " +
Expand All @@ -61,6 +93,17 @@ private MetadataAccess() {}
")"
);

private static final RowParser<MetadataRow> ROW_PARSER = row ->
MetadataRow.builder()
.name(row.getString("name"))
.desc(row.getString("dscr"))
.time(row.getTimestamp("time"))
.keyword(row.getString("keywords"))
.personId(row.getObject("person_id", Long.class))
.copyrightId(row.getObject("copyright_id", Long.class))
.boundsId(row.getObject("bounds_id", Long.class))
.build();

private static final Dctor<Metadata> DCTOR = Dctor.of(
field("name", Metadata::getName),
field("dscr", Metadata::getDescription),
Expand All @@ -80,12 +123,38 @@ private MetadataAccess() {}
)
);

public static Metadata selectById(final Long id, final Connection conn)
throws SQLException
{
if (id == null) return null;

final MetadataRow row = SELECT
.on(value("id", id))
.as(ROW_PARSER.singleNullable(), conn);

if (row == null) return null;

final Person author = PersonAccess.selectById(row.personId(), conn);
final Copyright copyright = CopyrightAccess.selectById(row.copyrightId(), conn);
final Bounds bounds = BoundsAccess.selectById(row.boundsId(), conn);

return Metadata.builder()
.name(row.name())
.desc(row.desc())
.time(ZonedDateTime.ofInstant(row.time().toInstant(), ZoneId.systemDefault()))
.keywords(row.keyword())
.author(author)
.copyright(copyright)
.bounds(bounds)
.build();
}

public static Long insert(final Metadata metadata, final Connection conn)
throws SQLException
{
if (metadata == null || metadata.isEmpty()) return null;

final Long id = INSERT_QUERY
final Long id = INSERT
.on(metadata, DCTOR)
.executeInsert(conn)
.orElseThrow();
Expand Down
5 changes: 2 additions & 3 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/PersonAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class PersonAccess {
private PersonAccess() {}

private static final Query SELECT = Query.of(
"SELECT name, email, link_href, link_text, link_type " +
"SELECT person.id, name, email, link_href, link_text, link_type " +
"FROM person " +
"INNER JOIN link on person.link_id = link.id " +
"WHERE person.id = :id"
Expand Down Expand Up @@ -75,8 +75,7 @@ public static Person selectById(final Long id, final Connection conn)
return id != null
? SELECT
.on(value("id", id))
.as(PARSER.singleOpt(), conn)
.orElse(null)
.as(PARSER.singleNullable(), conn)
: null;
}

Expand Down

0 comments on commit 1f3db01

Please sign in to comment.