From e03fe1a32abc5ae7bc3e9d09d9a349080e4e0642 Mon Sep 17 00:00:00 2001 From: Jason Wall Date: Mon, 24 Nov 2014 00:31:34 -0700 Subject: [PATCH 1/3] chisel out the structure of the update methods --- .../hex/repo/examples/HexRepoExamples.java | 16 ++++++++++++++ hex_repo/src/hex/repo/AbstractRepository.java | 21 +++++++++++++++++++ hex_repo/src/hex/repo/Repository.java | 6 ++++++ hex_repo/src/hex/repo/metadata/Metadata.java | 2 ++ .../hex/repo/streams/RepositoryStream.java | 8 +++++++ 5 files changed, 53 insertions(+) diff --git a/hex_repo/examples/hex/repo/examples/HexRepoExamples.java b/hex_repo/examples/hex/repo/examples/HexRepoExamples.java index 2a8f55d..98e0996 100644 --- a/hex_repo/examples/hex/repo/examples/HexRepoExamples.java +++ b/hex_repo/examples/hex/repo/examples/HexRepoExamples.java @@ -1,16 +1,32 @@ package hex.repo.examples; +import hex.ql.Query; import hex.repo.Person; import hex.repo.PersonRepository; import hex.repo.Repository; +import hex.repo.RepositoryBase; +import hex.repo.sql.test.Book; import static hex.ql.QueryLanguage.*; public class HexRepoExamples { public static void main(String[] args) { + basicQuery(); + + bulkUpdate(); + } + + private static void basicQuery() { Repository people = new PersonRepository(); from(people).where(Person::getLastName, is("Newton")) .forEach((p) -> System.out.println(p.getFullName())); } + + private static void bulkUpdate() { + Repository repo = new RepositoryBase<>(Book.class); + + Query books = from(repo).where(Book::getPublishedYear, is(2114)); + repo.update(books, b -> b.setPublishedYear(2014)); + } } diff --git a/hex_repo/src/hex/repo/AbstractRepository.java b/hex_repo/src/hex/repo/AbstractRepository.java index 3dc2d5e..555a574 100644 --- a/hex_repo/src/hex/repo/AbstractRepository.java +++ b/hex_repo/src/hex/repo/AbstractRepository.java @@ -1,5 +1,6 @@ package hex.repo; +import hex.ql.Query; import hex.repo.metadata.DataMappingException; import hex.repo.metadata.Metadata; import hex.repo.streams.RepositoryStream; @@ -11,6 +12,9 @@ import java.sql.Statement; import java.util.Iterator; import java.util.Optional; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; /** * Created by jason on 14-11-05. @@ -36,6 +40,23 @@ public R executeQuery(String sql, ResultSetMapper mapper) { } } + @Override + public void update(Query queryToUpdate, Consumer updateDescriptor) { + if(queryToUpdate instanceof RepositoryStream) { + ((RepositoryStream) queryToUpdate).update(updateDescriptor); + } else { + // TODO: Log INFO about performance of this + queryToUpdate.forEach(updateDescriptor.andThen(this::update)); + } + } + + @Override + public void update(T t) { + Function primaryKey = get_metadata().getPrimaryKey(); + RepositoryStream stream = (RepositoryStream) where(primaryKey, new EqualityPredicate<>(primaryKey.apply(t))); + stream.update(t); + } + @Override public Iterator iterator() { return stream().iterator(); diff --git a/hex_repo/src/hex/repo/Repository.java b/hex_repo/src/hex/repo/Repository.java index 93f6663..e904c78 100644 --- a/hex_repo/src/hex/repo/Repository.java +++ b/hex_repo/src/hex/repo/Repository.java @@ -1,9 +1,11 @@ package hex.repo; +import hex.ql.Query; import hex.ql.Queryable; import hex.repo.streams.RepositoryStream; import java.util.Optional; +import java.util.function.Consumer; /** * Created by jason on 14-11-01. @@ -12,4 +14,8 @@ public interface Repository extends Queryable { Optional find(int id); RepositoryStream stream(); + + void update(Query queryToUpdate, Consumer updateDescriptor); + + void update(T t); } diff --git a/hex_repo/src/hex/repo/metadata/Metadata.java b/hex_repo/src/hex/repo/metadata/Metadata.java index d42082e..ae6fd25 100644 --- a/hex_repo/src/hex/repo/metadata/Metadata.java +++ b/hex_repo/src/hex/repo/metadata/Metadata.java @@ -1,5 +1,6 @@ package hex.repo.metadata; +import hex.ql.ast.Node; import hex.repo.RepositoryException; import hex.repo.ResultSetWrapper; import hex.repo.streams.RepositoryStream; @@ -11,6 +12,7 @@ import java.sql.SQLException; import java.util.HashMap; import java.util.Map; +import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Stream; diff --git a/hex_repo/src/hex/repo/streams/RepositoryStream.java b/hex_repo/src/hex/repo/streams/RepositoryStream.java index 04cce7c..923a43f 100644 --- a/hex_repo/src/hex/repo/streams/RepositoryStream.java +++ b/hex_repo/src/hex/repo/streams/RepositoryStream.java @@ -435,4 +435,12 @@ public Query unordered() { dup.orderBy = null; return dup; } + + public void update(Consumer updateDescriptor) { + + } + + public void update(T t) { + + } } From 7f3c00fbd25235ef648c98e123ceb933a9d12262 Mon Sep 17 00:00:00 2001 From: Jason Wall Date: Sun, 21 Dec 2014 15:43:31 -0700 Subject: [PATCH 2/3] correct grammar in javadoc --- hex_repo/src/hex/repo/streams/RepositoryStream.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hex_repo/src/hex/repo/streams/RepositoryStream.java b/hex_repo/src/hex/repo/streams/RepositoryStream.java index 2393e5c..554d8a4 100644 --- a/hex_repo/src/hex/repo/streams/RepositoryStream.java +++ b/hex_repo/src/hex/repo/streams/RepositoryStream.java @@ -170,7 +170,7 @@ public Query map(Function mapper) { * this stream with the contents of a mapped stream produced by applying * the provided mapping function to each element. * - * This is effectively this is an inner join to a one-to-many relationship in a + * This is effectively an inner join to a one-to-many relationship in a * relational database. *

*

This is an intermediate From de3469ced804feeb3329fe791cc1786813818f84 Mon Sep 17 00:00:00 2001 From: Jason Wall Date: Sun, 21 Dec 2014 15:43:42 -0700 Subject: [PATCH 3/3] add speccing comments to update methods --- hex_repo/src/hex/repo/streams/RepositoryStream.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/hex_repo/src/hex/repo/streams/RepositoryStream.java b/hex_repo/src/hex/repo/streams/RepositoryStream.java index 554d8a4..43f4ba5 100644 --- a/hex_repo/src/hex/repo/streams/RepositoryStream.java +++ b/hex_repo/src/hex/repo/streams/RepositoryStream.java @@ -441,11 +441,22 @@ public Query unordered() { return dup; } + /** + * Converts this stream into an update statement that makes the changes caused by + * {@code updateDescriptor} to the records currently described by this {@link RepositoryStream}. + * @param updateDescriptor A consumer that describes the changes to be made + */ public void update(Consumer updateDescriptor) { } + /** + * Update the record {@code t} if it exists with all the values found in {@code t}. + * @param t Record values to save to the database + * @throws hex.repo.RepositoryException If the record does not exist or an error occurred during update + */ public void update(T t) { - + // use the metadata to generate the update statement + // create a query generating class similar to SqlQuery. Use dialects wherever possible (i.e.: everywhere) } }