diff --git a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/DAO.java b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/DAO.java index 77a2edc3..2b05c9bc 100644 --- a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/DAO.java +++ b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/DAO.java @@ -28,6 +28,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; @@ -92,32 +93,38 @@ static List> put( ) throws SQLException { - final Map> existing = select.apply(values).stream() - .collect(toMap( - value -> key.apply(value.value()), - value -> value, - (a, b) -> b)); + final List> result; - final Map actual = values.stream() - .collect(toMap(key, value -> value, (a, b) -> b)); + if (!values.isEmpty()) { + final Map> existing = select.apply(values).stream() + .collect(toMap( + value -> key.apply(value.value()), + value -> value, + (a, b) -> b)); - final Diff, T> diff = Diff.of(existing, actual); + final Map actual = values.stream() + .collect(toMap(key, value -> value, (a, b) -> b)); - final List missing = diff.missing(); + final Diff, T> diff = Diff.of(existing, actual); - final List> updated = diff - .updated((e, a) -> Objects.equals(e.value(), a)) - .entrySet().stream() + final List missing = diff.missing(); + + final List> updated = diff + .updated((e, a) -> Objects.equals(e.value(), a)) + .entrySet().stream() .map(entry -> entry.getKey().map(m -> entry.getValue())) .collect(toList()); - final List> unchanged = diff - .unchanged((e, a) -> Objects.equals(e.value(), a)); + final List> unchanged = diff + .unchanged((e, a) -> Objects.equals(e.value(), a)); - final List> result = new ArrayList<>(); - result.addAll(insert.apply(missing)); - result.addAll(update.apply(updated)); - result.addAll(unchanged); + result = new ArrayList<>(); + result.addAll(insert.apply(missing)); + result.addAll(update.apply(updated)); + result.addAll(unchanged); + } else { + result = Collections.emptyList(); + } return result; } diff --git a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkDAO.java b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkDAO.java index 05b1ba88..eb3568f6 100644 --- a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkDAO.java +++ b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkDAO.java @@ -22,13 +22,9 @@ import static java.util.Arrays.asList; import static java.util.Collections.singletonList; -import java.net.URI; import java.sql.Connection; import java.sql.SQLException; -import java.util.Collections; import java.util.List; -import java.util.function.Function; -import java.util.stream.Collectors; import io.jenetics.jpx.Link; @@ -59,6 +55,34 @@ public LinkDAO(final Connection connection) { ); + /* ************************************************************************* + * SELECT queries + **************************************************************************/ + + /** + * Select all available links. + * + * @return all stored links + * @throws SQLException if the select fails + */ + public List> select() throws SQLException { + return SQL("SELECT id, href, text, type FROM link") + .as(RowParser.list()); + } + + public List> select(final List links) + throws SQLException + { + final String query = + "SELECT id, href, text, type " + + "FROM link WHERE href IN ({hrefs})"; + + return SQL(query) + .on(Param.values("hrefs", links, Link::getHref)) + .as(RowParser.list()); + } + + /* ************************************************************************* * INSERT queries **************************************************************************/ @@ -145,73 +169,24 @@ public Stored update(final Stored link) throws SQLException { * @throws SQLException if the insert/update fails */ public List> put(final List links) throws SQLException { - return links.isEmpty() - ? Collections.emptyList() - : DAO.put( - links, - Link::getHref, - //list -> selectByHrefs(map(list, Link::getHref)), - values -> select(values, Link::getHref, this::selectByHrefs), - this::insert, - this::update - ); + return DAO.put( + links, + Link::getHref, + this::select, + this::insert, + this::update + ); } - private List> select( - final List values, - final Function mapper, - final SQL.Function, List>> select - ) - throws SQLException - { - final List keys = values.stream() - .map(mapper) - .collect(Collectors.toList()); - - return select.apply(keys); - } /* ************************************************************************* - * SELECT queries + * DELETE queries **************************************************************************/ - /** - * Select all available links. - * - * @return all stored links - * @throws SQLException if the select fails - */ - public List> select() throws SQLException { - return SQL("SELECT id, href, text, type FROM link") - .as(RowParser.list()); - } - - /** - * Selects the links by its hrefs. - * - * @param hrefs the hrefs - * @return the link with the given hrefs currently in the DB - * @throws SQLException if the select fails - */ - public List> selectByHrefs(final List hrefs) + public List delete(final List> links) throws SQLException { - final String query = - "SELECT id, href, text, type FROM link WHERE href IN ({hrefs})"; - - return SQL(query) - .on(Param.values("hrefs", hrefs)) - .as(RowParser.list()); - } - - /** - * Create a new {@code LinkDAO} for the given connection. - * - * @param conn the DB connection - * @return a new DAO object - */ - public static LinkDAO of(final Connection conn) { - return new LinkDAO(conn); + return null; } } diff --git a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/Param.java b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/Param.java index 585af36b..78a77246 100644 --- a/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/Param.java +++ b/jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/Param.java @@ -24,6 +24,8 @@ import static java.util.Objects.requireNonNull; import java.util.Collection; +import java.util.List; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.LongStream; @@ -89,13 +91,25 @@ public static Param value(final String name, final Object value) { * @param values the parameter values * @return a new parameter object */ - public static Param values( + public static Param values( final String name, - final Collection values + final Collection values ) { return new Param(name, values); } + public static Param values( + final String name, + final Collection values, + final Function mapper + ) { + final List converted = values.stream() + .map(mapper) + .collect(Collectors.toList()); + + return new Param(name, converted); + } + /** * Return a new parameter object with the given name and long values. *