diff --git a/hex_repo/examples/hex/repo/examples/HexRepoExamples.java b/hex_repo/examples/hex/repo/examples/HexRepoExamples.java index 39e9790..50a6493 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.test.Person; import hex.repo.test.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 de99e58..c04a5ce 100644 --- a/hex_repo/src/hex/repo/AbstractRepository.java +++ b/hex_repo/src/hex/repo/AbstractRepository.java @@ -1,21 +1,22 @@ package hex.repo; +import hex.ql.Query; +import hex.ql.Queryable; import hex.ql.ast.InvalidAstException; +import hex.ql.ast.predicates.EqualityPredicate; import hex.repo.metadata.DataMappingException; import hex.repo.metadata.Metadata; import hex.repo.sql.PreparedSqlQuery; import hex.repo.streams.RepositoryStream; -import hex.ql.Queryable; -import hex.ql.ast.predicates.EqualityPredicate; import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.function.Consumer; +import java.util.function.Function; /** * Created by jason on 14-11-05. @@ -59,6 +60,23 @@ public List executeQuery(PreparedSqlQuery query) { return executeQuery(query, ResultSetMapper.toList(get_metadata()::mapRecord)); } + @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) stream().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/streams/RepositoryStream.java b/hex_repo/src/hex/repo/streams/RepositoryStream.java index 5f2172f..43f4ba5 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 @@ -440,4 +440,23 @@ public Query unordered() { dup.orderBy = null; 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) + } }