Skip to content

Commit

Permalink
#1: Implement PersonDAO.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Jan 6, 2017
1 parent d71cab7 commit 49dddfe
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 88 deletions.
24 changes: 2 additions & 22 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/DAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Abstract DAO class
Expand Down Expand Up @@ -90,7 +87,7 @@ static <T, K> List<Stored<T>> put(
{
final Map<K, Stored<T>> existing = select.apply(values).stream()
.collect(toMap(
value -> key.apply(value.getValue().orElse(null)),
value -> key.apply(value.optional().orElse(null)),
value -> value,
(a, b) -> b));

Expand All @@ -100,7 +97,7 @@ static <T, K> List<Stored<T>> put(
final Diff<K, Stored<T>, T> diff = Diff.of(existing, actual);
final List<T> missing = diff.missing();
final List<Stored<T>> updated = diff
.updated((e, a) -> Objects.equals(e.getValue().orElse(null), a))
.updated((e, a) -> Objects.equals(e.optional().orElse(null), a))
.entrySet().stream()
.map(entry -> entry.getKey().map(m -> entry.getValue()))
.collect(toList());
Expand All @@ -113,23 +110,6 @@ static <T, K> List<Stored<T>> put(
return result;
}

/*
public static <T> Stored<T> put(
final T value,
final SQL.Function<T, SQL.Option<Stored<T>>> select,
final SQL.Function<T, Stored<T>> insert,
final SQL.Consumer<Stored<T>> update
)
throws SQLException
{
return select.apply(value)
.map(stored -> {
update.accept(stored);
return stored.copy(value); })
.orElseGet(() -> insert.apply(value));
}
*/

/**
* Reads the auto increment id from the previously inserted record.
*
Expand Down
7 changes: 0 additions & 7 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/Diff.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ Map<E, A> updated(final BiPredicate<E, A> equals) {
.collect(toMap(_existing::get, _actual::get, (a, b) -> b));
}

/*
- def updated( doesEqual: (E,A) => Boolean ): Iterable[A] = {
- intersection.filter{ key: K => !doesEqual( existing(key), actual(key) ) }
- .map{ key:K => actual(key) }
- }
*/

/**
* Create a new {@code Diff} object with the given existing and actual values.
*
Expand Down
46 changes: 23 additions & 23 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.net.URI;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -58,6 +56,7 @@ public LinkDAO(final Connection connection) {
)
);


/* *************************************************************************
* INSERT queries
**************************************************************************/
Expand Down Expand Up @@ -96,6 +95,7 @@ public Stored<Link> insert(final Link link)
return insert(singletonList(link)).get(0);
}


/* *************************************************************************
* UPDATE queries
**************************************************************************/
Expand All @@ -115,9 +115,9 @@ public List<Stored<Link>> update(final List<Stored<Link>> links)
"WHERE id = {id}";

Batch(query).update(links, link -> asList(
Param.value("id", link.getID()),
Param.value("text", link.getValue().map(Link::getText)),
Param.value("type", link.getValue().map(Link::getType))
Param.value("id", link.id()),
Param.value("text", link.value().getText()),
Param.value("type", link.value().getType())
));

return links;
Expand All @@ -134,12 +134,20 @@ public Stored<Link> update(final Stored<Link> link) throws SQLException {
return update(singletonList(link)).get(0);
}

/**
* Inserts the given links into the DB. If the DB already contains the given
* link, the link is updated.
*
* @param links the links to insert or update
* @return the inserted or updated links
* @throws SQLException if the insert/update fails
*/
public List<Stored<Link>> put(final List<Link> links) throws SQLException {
return DAO.put(
links,
Link::getHref,
ll -> selectByHrefs(
ll.stream()
list -> selectByHrefs(
list.stream()
.map(Link::getHref)
.collect(Collectors.toList())
),
Expand All @@ -148,6 +156,7 @@ public List<Stored<Link>> put(final List<Link> links) throws SQLException {
);
}


/* *************************************************************************
* SELECT queries
**************************************************************************/
Expand Down Expand Up @@ -190,28 +199,19 @@ public SQL.Option<Stored<Link>> selectByID(final long id)
.as(RowParser.singleOpt());
}


public List<Stored<Link>> selectByHrefs(final List<URI> hrefs)
throws SQLException
{
return SQL("SELECT id, href, text, type FROM link WHERE href IN ({hrefs})")
.on(Param.values("hrefs", hrefs))
.as(RowParser.list());
}

/**
* Select the links by the HREF.
* Selects the links by its hrefs.
*
* @param href the href to select
* @return the selected links
* @param hrefs the hrefs
* @return the link with the given hrefs currently in the DB
* @throws SQLException if the select fails
*/
public SQL.Option<Stored<Link>> selectByHref(final URI href)
public List<Stored<Link>> selectByHrefs(final List<URI> hrefs)
throws SQLException
{
return SQL("SELECT id, href, text, type FROM link WHERE href = {href}")
.on("href", href)
.as(RowParser.singleOpt());
return SQL("SELECT id, href, text, type FROM link WHERE href IN ({hrefs})")
.on(Param.values("hrefs", hrefs))
.as(RowParser.list());
}

/**
Expand Down
24 changes: 8 additions & 16 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/Param.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
package io.jenetics.jpx.jdbc;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

Expand All @@ -42,9 +39,9 @@
final class Param {

private final String _name;
private final List<? extends Object> _values;
private final Collection<? extends Object> _values;

private Param(final String name, final List<? extends Object> values) {
private Param(final String name, final Collection<? extends Object> values) {
_name = requireNonNull(name);
_values = requireNonNull(values);
}
Expand All @@ -58,7 +55,7 @@ String name() {
return _name;
}

List<? extends Object> values() {
Collection<? extends Object> values() {
return _values;
}

Expand All @@ -81,7 +78,10 @@ public static Param value(final String name, final Object value) {
return new Param(name, singletonList(value));
}

public static Param values(final String name, final List<? extends Object> values) {
public static Param values(
final String name,
final Collection<? extends Object> values
) {
return new Param(name, values);
}

Expand All @@ -94,12 +94,4 @@ public static Param values(final String name, final long... values) {
);
}

/*
public static <T> Param insert(
final String name,
final SQL.Supplier<T> value
) {
return new Param(name, SQL.Lazy.of(value));
}
*/
}
Loading

0 comments on commit 49dddfe

Please sign in to comment.