All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.datastore.Query Maven / Gradle / Ivy

There is a newer version: 2.24.2
Show newest version
/*
 * Copyright 2015 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.datastore;

import com.google.api.core.InternalApi;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.Maps;
import java.io.Serializable;
import java.util.Map;

/**
 * A Google Cloud Datastore query. For usage examples see {@link GqlQuery}, {@link StructuredQuery}
 * and {@link AggregationQuery}.
 *
 * 

Note that queries require proper indexing. See Cloud Datastore Index * Configuration for help configuring indexes. * * @param the type of the values returned by this query. * @see Datastore Queries */ public abstract class Query implements Serializable { private static final long serialVersionUID = 7967659059395653941L; private final String namespace; /** * This class represents the expected type of the result. ENTITY: A full entity represented by * {@link Entity}. PROJECTION_ENTITY: A projection entity, represented by {@link * ProjectionEntity}. KEY: An entity's {@link Key}. */ public abstract static class ResultType implements Serializable { private static final long serialVersionUID = 2104157695425806623L; private static final Map> PB_TO_INSTANCE = Maps.newEnumMap(com.google.datastore.v1.EntityResult.ResultType.class); static final ResultType UNKNOWN = new ResultType(null, Object.class) { private static final long serialVersionUID = 1602329532153860907L; @Override Object convert(com.google.datastore.v1.Entity entityPb) { if (entityPb.getPropertiesMap().isEmpty()) { if (!entityPb.hasKey()) { return null; } return Key.fromPb(entityPb.getKey()); } return ProjectionEntity.fromPb(entityPb); } }; public static final ResultType ENTITY = new ResultType(com.google.datastore.v1.EntityResult.ResultType.FULL, Entity.class) { private static final long serialVersionUID = 7712959777507168274L; @Override Entity convert(com.google.datastore.v1.Entity entityPb) { return Entity.fromPb(entityPb); } }; public static final ResultType KEY = new ResultType(com.google.datastore.v1.EntityResult.ResultType.KEY_ONLY, Key.class) { private static final long serialVersionUID = -8514289244104446252L; @Override Key convert(com.google.datastore.v1.Entity entityPb) { return Key.fromPb(entityPb.getKey()); } }; public static final ResultType PROJECTION_ENTITY = new ResultType( com.google.datastore.v1.EntityResult.ResultType.PROJECTION, ProjectionEntity.class) { private static final long serialVersionUID = -7591409419690650246L; @Override ProjectionEntity convert(com.google.datastore.v1.Entity entityPb) { return ProjectionEntity.fromPb(entityPb); } }; private final Class resultClass; private final com.google.datastore.v1.EntityResult.ResultType queryType; private ResultType( com.google.datastore.v1.EntityResult.ResultType queryType, Class resultClass) { this.queryType = queryType; this.resultClass = resultClass; if (queryType != null) { PB_TO_INSTANCE.put(queryType, this); } } public Class resultClass() { return resultClass; } @InternalApi public com.google.datastore.v1.EntityResult.ResultType getQueryType() { return this.queryType; } @Override public int hashCode() { return resultClass.hashCode(); } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof ResultType)) { return false; } ResultType other = (ResultType) obj; return resultClass.equals(other.resultClass); } @Override public String toString() { ToStringHelper toStringHelper = MoreObjects.toStringHelper(this); toStringHelper.add("queryType", queryType); toStringHelper.add("resultClass", resultClass); return toStringHelper.toString(); } boolean isAssignableFrom(ResultType otherResultType) { return resultClass.isAssignableFrom(otherResultType.resultClass); } abstract V convert(com.google.datastore.v1.Entity entityPb); static ResultType fromPb(com.google.datastore.v1.EntityResult.ResultType typePb) { return MoreObjects.firstNonNull(PB_TO_INSTANCE.get(typePb), UNKNOWN); } } Query(String namespace) { this.namespace = namespace; } public String getNamespace() { return namespace; } ToStringHelper toStringHelper() { return MoreObjects.toStringHelper(this).add("namespace", namespace); } /** * Returns a new {@link GqlQuery} builder. * *

Example of creating and running a GQL query. * *

{@code
   * String kind = "my_kind";
   * String gqlQuery = "select * from " + kind;
   * Query query = Query.newGqlQueryBuilder(gqlQuery).build();
   * QueryResults results = datastore.run(query);
   * // Use results
   * }
* * @see GQL Reference */ public static GqlQuery.Builder newGqlQueryBuilder(String gql) { return newGqlQueryBuilder(ResultType.UNKNOWN, gql); } /** * Returns a new {@link GqlQuery} builder. * *

Example of creating and running a typed GQL query. * *

{@code
   * String kind = "my_kind";
   * String gqlQuery = "select * from " + kind;
   * Query query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, gqlQuery).build();
   * QueryResults results = datastore.run(query);
   * // Use results
   * }
* * @see GQL Reference */ public static GqlQuery.Builder newGqlQueryBuilder(ResultType resultType, String gql) { return new GqlQuery.Builder<>(resultType, gql); } /** * Returns a new {@link StructuredQuery} builder for full (complete entities) queries. * *

Example of creating and running an entity query. * *

{@code
   * String kind = "my_kind";
   * Query query = Query.newEntityQueryBuilder().setKind(kind).build();
   * QueryResults results = datastore.run(query);
   * // Use results
   * }
*/ public static EntityQuery.Builder newEntityQueryBuilder() { return new EntityQuery.Builder(); } /** * Returns a new {@link StructuredQuery} builder for key only queries. * *

Example of creating and running a key query. * *

{@code
   * String kind = "my_kind";
   * Query query = Query.newKeyQueryBuilder().setKind(kind).build();
   * QueryResults results = datastore.run(query);
   * // Use results
   * }
*/ public static KeyQuery.Builder newKeyQueryBuilder() { return new KeyQuery.Builder(); } /** * Returns a new {@link StructuredQuery} builder for projection queries. * *

Example of creating and running a projection entity query. * *

{@code
   * String kind = "my_kind";
   * String property = "my_property";
   * Query query = Query.newProjectionEntityQueryBuilder()
   *     .setKind(kind)
   *     .addProjection(property)
   *     .build();
   * QueryResults results = datastore.run(query);
   * // Use results
   * }
*/ public static ProjectionEntityQuery.Builder newProjectionEntityQueryBuilder() { return new ProjectionEntityQuery.Builder(); } /** * Returns a new {@link AggregationQuery} builder. * *

Example of creating and running an {@link AggregationQuery}. * *

{@link StructuredQuery} example: * *

{@code
   * EntityQuery selectAllQuery = Query.newEntityQueryBuilder()
   *    .setKind("Task")
   *    .build();
   * AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()
   *    .addAggregation(count().as("total_count"))
   *    .over(selectAllQuery)
   *    .build();
   * AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);
   * // Use aggregationResults
   * }
* *

{@link GqlQuery} example: * *

{@code
   * GqlQuery selectAllGqlQuery = Query.newGqlQueryBuilder(
   *         "AGGREGATE COUNT(*) AS total_count OVER(SELECT * FROM Task)"
   *     )
   *     .setAllowLiteral(true)
   *     .build();
   * AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()
   *     .over(selectAllGqlQuery)
   *     .build();
   * AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);
   * // Use aggregationResults
   * }
*/ public static AggregationQuery.Builder newAggregationQueryBuilder() { return new AggregationQuery.Builder(); } }