diff --git a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/BoundsAccess.java b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/BoundsAccess.java index f10916ba..445d1147 100644 --- a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/BoundsAccess.java +++ b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/BoundsAccess.java @@ -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; @@ -28,6 +29,7 @@ import io.jenetics.facilejdbc.Dctor; import io.jenetics.facilejdbc.Query; +import io.jenetics.facilejdbc.RowParser; /** * @author Franz Wilhelmstötter @@ -37,11 +39,24 @@ 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 PARSER = row -> Bounds.of( + row.getDouble("minlat"), + row.getDouble("minlon"), + row.getDouble("maxlat"), + row.getDouble("maxlon") + ); + private static final Dctor DCTOR = Dctor.of( field("minlat", Bounds::getMinLatitude), field("minlon", Bounds::getMinLongitude), @@ -49,11 +64,21 @@ private BoundsAccess() {} 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() diff --git a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/CopyrightAccess.java b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/CopyrightAccess.java index f17bd957..1923b2fb 100644 --- a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/CopyrightAccess.java +++ b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/CopyrightAccess.java @@ -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 Franz Wilhelmstötter @@ -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 PARSER = row -> Copyright.of( + row.getString("author"), + Year.of(row.getInt("year")), + URI.create(row.getString("license")) + ); + private static final Dctor 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() diff --git a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkAccess.java b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkAccess.java index 43ca68b3..0a7c464b 100644 --- a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkAccess.java +++ b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkAccess.java @@ -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;" ); @@ -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; } diff --git a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/MetadataAccess.java b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/MetadataAccess.java index b5fe96a2..92f95b32 100644 --- a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/MetadataAccess.java +++ b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/MetadataAccess.java @@ -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 Franz Wilhelmstötter @@ -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, " + @@ -61,6 +93,17 @@ private MetadataAccess() {} ")" ); + private static final RowParser 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 DCTOR = Dctor.of( field("name", Metadata::getName), field("dscr", Metadata::getDescription), @@ -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(); diff --git a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/PersonAccess.java b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/PersonAccess.java index d71a077d..cb5c2e50 100644 --- a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/PersonAccess.java +++ b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/PersonAccess.java @@ -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" @@ -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; }