Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions hex_repo/examples/hex/repo/examples/HexRepoExamples.java
Original file line number Diff line number Diff line change
@@ -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<Person> people = new PersonRepository();

from(people).where(Person::getLastName, is("Newton"))
.forEach((p) -> System.out.println(p.getFullName()));
}

private static void bulkUpdate() {
Repository<Book> repo = new RepositoryBase<>(Book.class);

Query<Book> books = from(repo).where(Book::getPublishedYear, is(2114));
repo.update(books, b -> b.setPublishedYear(2014));
}
}
26 changes: 22 additions & 4 deletions hex_repo/src/hex/repo/AbstractRepository.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -59,6 +60,23 @@ public List<T> executeQuery(PreparedSqlQuery query) {
return executeQuery(query, ResultSetMapper.toList(get_metadata()::mapRecord));
}

@Override
public void update(Query<T> queryToUpdate, Consumer<T> updateDescriptor) {
if(queryToUpdate instanceof RepositoryStream) {
((RepositoryStream<T>) queryToUpdate).update(updateDescriptor);
} else {
// TODO: Log INFO about performance of this
queryToUpdate.forEach(updateDescriptor.andThen(this::update));
}
}

@Override
public void update(T t) {
Function<T, Integer> primaryKey = get_metadata().getPrimaryKey();
RepositoryStream<T> stream = (RepositoryStream<T>) stream().where(primaryKey, new EqualityPredicate<>(primaryKey.apply(t)));
stream.update(t);
}

@Override
public Iterator<T> iterator() {
return stream().iterator();
Expand Down
6 changes: 6 additions & 0 deletions hex_repo/src/hex/repo/Repository.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -12,4 +14,8 @@ public interface Repository<T> extends Queryable<T> {
Optional<T> find(int id);

RepositoryStream<T> stream();

void update(Query<T> queryToUpdate, Consumer<T> updateDescriptor);

void update(T t);
}
21 changes: 20 additions & 1 deletion hex_repo/src/hex/repo/streams/RepositoryStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public <R> Query<R> map(Function<? super T, ? extends R> 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.
* <p>
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
Expand Down Expand Up @@ -440,4 +440,23 @@ public Query<T> 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<T> 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)
}
}