Skip to content

Commit

Permalink
Change StructuredQuery builder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Jan 26, 2016
1 parent c4e01cd commit b5249ea
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,109 @@

package com.google.gcloud.datastore;

import com.google.api.services.datastore.DatastoreV1;

/**
* An implementation of a Google Cloud Datastore entity query that can be constructed by providing
* all the specific query elements.
*
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
* queries</a>
*/
public class EntityQuery extends StructuredQuery<Entity> {
public final class EntityQuery extends StructuredQuery<Entity> {

private static final long serialVersionUID = 2990565454831019471L;

/**
* A StructuredQuery builder for queries that return Entity results.
* A {@code EntityQuery} builder for queries that return Entity results.
*/
public static final class Builder extends StructuredQuery.BaseBuilder<Entity, Builder> {
public static final class Builder extends StructuredQuery.BaseBuilder<Entity> {

private StructuredQuery.BuilderImpl<Entity, Builder> builder;

Builder() {
super(ResultType.ENTITY);
builder = new StructuredQuery.BuilderImpl<>(ResultType.ENTITY);
}

Builder(EntityQuery query) {
super(query);
builder = new StructuredQuery.BuilderImpl<>(query);
}

@Override
Builder mergeFrom(DatastoreV1.Query queryPb) {
builder.mergeFrom(queryPb);
builder.clearProjection();
builder.clearGroupBy();
return this;
}

@Override
public Builder namespace(String namespace) {
builder.namespace(namespace);
return this;
}

@Override
public Builder kind(String kind) {
builder.kind(kind);
return this;
}

@Override
public Builder startCursor(Cursor startCursor) {
builder.startCursor(startCursor);
return this;
}

@Override
public Builder endCursor(Cursor endCursor) {
builder.endCursor(endCursor);
return this;
}

@Override
public Builder offset(int offset) {
builder.offset(offset);
return this;
}

@Override
public Builder limit(Integer limit) {
builder.limit(limit);
return this;
}

@Override
public Builder filter(Filter filter) {
builder.filter(filter);
return this;
}

@Override
public Builder clearOrderBy() {
builder.clearOrderBy();
return this;
}

@Override
public Builder orderBy(OrderBy orderBy, OrderBy... others) {
builder.orderBy(orderBy, others);
return this;
}

@Override
public Builder addOrderBy(OrderBy orderBy, OrderBy... others) {
builder.addOrderBy(orderBy, others);
return this;
}

@Override
public EntityQuery build() {
return new EntityQuery(this);
return new EntityQuery(builder);
}
}

EntityQuery(Builder builder) {
EntityQuery(BuilderImpl<Entity, Builder> builder) {
super(builder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,101 @@
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
* queries</a>
*/
public class KeyQuery extends StructuredQuery<Key> {
public final class KeyQuery extends StructuredQuery<Key> {

private static final long serialVersionUID = -746768461459070045L;

/**
* A StructuredQuery builder for queries that return Key results.
* A {@code KeyQuery} builder for queries that return Key results.
*/
public static final class Builder extends StructuredQuery.BaseBuilder<Key, Builder> {
public static final class Builder extends StructuredQuery.BaseBuilder<Key> {

private StructuredQuery.BuilderImpl<Key, Builder> builder;

Builder() {
super(ResultType.KEY);
projection(Projection.property(KEY_PROPERTY_NAME));
builder = new StructuredQuery.BuilderImpl<>(ResultType.KEY);
builder.projection(Projection.property(KEY_PROPERTY_NAME));
}

Builder(KeyQuery query) {
super(query);
builder = new StructuredQuery.BuilderImpl<>(query);
}

@Override
Builder mergeFrom(DatastoreV1.Query queryPb) {
super.mergeFrom(queryPb);
projection(Projection.property(KEY_PROPERTY_NAME));
clearGroupBy();
builder.mergeFrom(queryPb);
builder.projection(Projection.property(KEY_PROPERTY_NAME));
builder.clearGroupBy();
return this;
}

@Override
public Builder namespace(String namespace) {
builder.namespace(namespace);
return this;
}

@Override
public Builder kind(String kind) {
builder.kind(kind);
return this;
}

@Override
public Builder startCursor(Cursor startCursor) {
builder.startCursor(startCursor);
return this;
}

@Override
public Builder endCursor(Cursor endCursor) {
builder.endCursor(endCursor);
return this;
}

@Override
public Builder offset(int offset) {
builder.offset(offset);
return this;
}

@Override
public Builder limit(Integer limit) {
builder.limit(limit);
return this;
}

@Override
public Builder filter(Filter filter) {
builder.filter(filter);
return this;
}

@Override
public Builder clearOrderBy() {
builder.clearOrderBy();
return this;
}

@Override
public Builder orderBy(OrderBy orderBy, OrderBy... others) {
builder.orderBy(orderBy, others);
return this;
}

@Override
public Builder addOrderBy(OrderBy orderBy, OrderBy... others) {
builder.addOrderBy(orderBy, others);
return this;
}

@Override
public KeyQuery build() {
return new KeyQuery(this);
return new KeyQuery(builder);
}
}

KeyQuery(Builder builder) {
KeyQuery(BuilderImpl<Key, Builder> builder) {
super(builder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,77 +16,149 @@

package com.google.gcloud.datastore;

import com.google.api.services.datastore.DatastoreV1.Query;

/**
* An implementation of a Google Cloud Datastore projection entity query that can be constructed by
* providing all the specific query elements.
*
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
* queries</a>
*/
public class ProjectionEntityQuery extends StructuredQuery<ProjectionEntity> {
public final class ProjectionEntityQuery extends StructuredQuery<ProjectionEntity> {

private static final long serialVersionUID = 5488451194542425391L;

/**
* A StructuredQuery builder for queries that return Key results.
* A {@code ProjectionEntityQuery} builder for queries that return Key results.
*/
public static final class Builder extends StructuredQuery.BaseBuilder<ProjectionEntity, Builder> {
public static final class Builder extends StructuredQuery.BaseBuilder<ProjectionEntity> {

private StructuredQuery.BuilderImpl<ProjectionEntity, Builder> builder;

Builder() {
super(ResultType.PROJECTION_ENTITY);
builder = new StructuredQuery.BuilderImpl<>(ResultType.PROJECTION_ENTITY);
}

Builder(ProjectionEntityQuery query) {
super(query);
builder = new StructuredQuery.BuilderImpl<>(query);
}

@Override
Builder mergeFrom(Query queryPb) {
builder.mergeFrom(queryPb);
return this;
}

@Override
public Builder namespace(String namespace) {
builder.namespace(namespace);
return this;
}

@Override
public Builder kind(String kind) {
builder.kind(kind);
return this;
}

@Override
public Builder startCursor(Cursor startCursor) {
builder.startCursor(startCursor);
return this;
}

@Override
public Builder endCursor(Cursor endCursor) {
builder.endCursor(endCursor);
return this;
}

@Override
public Builder offset(int offset) {
builder.offset(offset);
return this;
}

@Override
public Builder limit(Integer limit) {
builder.limit(limit);
return this;
}

@Override
public Builder filter(Filter filter) {
builder.filter(filter);
return this;
}

@Override
public Builder clearOrderBy() {
builder.clearOrderBy();
return this;
}

@Override
public Builder orderBy(OrderBy orderBy, OrderBy... others) {
builder.orderBy(orderBy, others);
return this;
}

@Override
public Builder addOrderBy(OrderBy orderBy, OrderBy... others) {
builder.addOrderBy(orderBy, others);
return this;
}

public Builder clearProjection() {
return super.clearProjection();
builder.clearProjection();
return this;
}

/**
* Sets the query's projection clause (clearing any previously specified Projection settings).
*/
@Override
public Builder projection(Projection projection, Projection... others) {
return super.projection(projection, others);
builder.projection(projection, others);
return this;
}

/**
* Adds one or more projections to the existing projection clause.
*/
@Override
public Builder addProjection(Projection projection, Projection... others) {
return super.addProjection(projection, others);
builder.addProjection(projection, others);
return this;
}

@Override
public Builder clearGroupBy() {
return super.clearGroupBy();
builder.clearGroupBy();
return this;
}

/**
* Sets the query's group by clause (clearing any previously specified GroupBy settings).
*/
@Override
public Builder groupBy(String property, String... others) {
return super.groupBy(property, others);
builder.groupBy(property, others);
return this;
}

/**
* Adds one or more properties to the existing group by clause.
*/
@Override
public Builder addGroupBy(String property, String... others) {
return super.addGroupBy(property, others);
builder.addGroupBy(property, others);
return this;
}

@Override
public ProjectionEntityQuery build() {
return new ProjectionEntityQuery(this);
return new ProjectionEntityQuery(builder);
}
}

ProjectionEntityQuery(BaseBuilder<ProjectionEntity, ?> builder) {
ProjectionEntityQuery(BuilderImpl<ProjectionEntity, Builder> builder) {
super(builder);
}

Expand Down
Loading

0 comments on commit b5249ea

Please sign in to comment.