// Copyright (c) 2015, Google Inc.
//
// 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.

syntax = "proto3";

package google.datastore.v1beta3;

import "google/api/annotations.proto";
import "google/datastore/v1beta3/entity.proto";
import "google/protobuf/wrappers.proto";

option java_multiple_files = true;
option java_outer_classname = "QueryProto";
option java_package = "com.google.datastore.v1beta3";


// The result of fetching an entity from the datastore.
message EntityResult {
  // Specifies what data the 'entity' field contains.
  // A `ResultType` is either implied (for example, in `LookupResponse.found`
  // from `datastore.proto`, it is always `FULL`) or specified by context (for
  // example, in message `QueryResultBatch`, field `entity_result_type`
  // specifies a `ResultType` for all the values in field `entity_results`).
  enum ResultType {
    // Unspecified.
    RESULT_TYPE_UNSPECIFIED = 0;

    // The entire entity.
    FULL = 1;

    // A projected subset of properties. The entity may have no key. A property
    // value may have meaning 18.
    PROJECTION = 2;

    // Only the key.
    KEY_ONLY = 3;
  }

  // The resulting entity.
  Entity entity = 1;

  // A cursor that points to the position after the result entity.
  // Set only when the `EntityResult` is part of a `QueryResultBatch` message.
  bytes cursor = 3;
}

// A query.
message Query {
  // The projection to return. Defaults to returning all properties.
  repeated Projection projection = 2;

  // The kinds to query (if empty, returns entities of all kinds).
  // Currently at most 1 kind may be specified.
  repeated KindExpression kind = 3;

  // The filter to apply.
  Filter filter = 4;

  // The order to apply to the query results (if empty, order is unspecified).
  repeated PropertyOrder order = 5;

  // The properties to make distinct. The query results will contain the first
  // result for each distinct combination of values for the given properties
  // (if empty, all results are returned).
  repeated PropertyReference distinct_on = 6;

  // A starting point for the query results. Query cursors are
  // returned in query result batches.
  bytes start_cursor = 7;

  // An ending point for the query results. Query cursors are
  // returned in query result batches.
  bytes end_cursor = 8;

  // The number of results to skip. Applies before limit, but after all other
  // constraints.
  // Must be >= 0.
  int32 offset = 10;

  // The maximum number of results to return. Applies after all other
  // constraints.
  // Unspecified is interpreted as no limit.
  // Must be >= 0.
  google.protobuf.Int32Value limit = 12;
}

// A representation of a kind.
message KindExpression {
  // The name of the kind.
  string name = 1;
}

// A reference to a property relative to the kind expressions.
message PropertyReference {
  // The name of the property.
  string name = 2;
}

// A representation of a property in a projection.
message Projection {
  // The property to project.
  PropertyReference property = 1;
}

// The desired order for a specific property.
message PropertyOrder {
  // Direction.
  enum Direction {
    // Unspecified.
    DIRECTION_UNSPECIFIED = 0;

    // Ascending.
    ASCENDING = 1;

    // Descending.
    DESCENDING = 2;
  }

  // The property to order by.
  PropertyReference property = 1;

  // The direction to order by. Defaults to `ASCENDING`.
  Direction direction = 2;
}

// A holder for any type of filter.
message Filter {
  // The type of filter.
  oneof filter_type {
    // A composite filter.
    CompositeFilter composite_filter = 1;

    // A filter on a property.
    PropertyFilter property_filter = 2;
  }
}

// A filter that merges the multiple other filters using the given operator.
message CompositeFilter {
  // Composite filter operator.
  enum Operator {
    // Unspecified. This value must not be used.
    OPERATOR_UNSPECIFIED = 0;

    // And.
    AND = 1;
  }

  // The operator for combining multiple filters.
  Operator op = 1;

  // The list of filters to combine.
  // Must contain at least one filter.
  repeated Filter filters = 2;
}

// A filter on a specific property.
message PropertyFilter {
  // Property filter operator.
  enum Operator {
    // Unspecified. This value must not be used.
    OPERATOR_UNSPECIFIED = 0;

    // Less than.
    LESS_THAN = 1;

    // Less than or equal.
    LESS_THAN_OR_EQUAL = 2;

    // Greater than.
    GREATER_THAN = 3;

    // Greater than or equal.
    GREATER_THAN_OR_EQUAL = 4;

    // Equal.
    EQUAL = 5;

    // Has ancestor.
    HAS_ANCESTOR = 11;
  }

  // The property to filter by.
  PropertyReference property = 1;

  // The operator to filter by.
  Operator op = 2;

  // The value to compare the property to.
  Value value = 3;
}

// A GQL query.
message GqlQuery {
  // A string of the format described
  // [here](https://developers.google.com/datastore/docs/concepts/gql).
  string query_string = 1;

  // When false, the query string must not contain any literals and instead
  // must bind all values. For example,
  // `SELECT * FROM Kind WHERE a = 'string literal'` is not allowed, while
  // `SELECT * FROM Kind WHERE a = @value` is.
  bool allow_literals = 2;

  // For each non-reserved named binding site in the query string,
  // there must be a named parameter with that name,
  // but not necessarily the inverse.
  // Key must match regex `[A-Za-z_$][A-Za-z_$0-9]*`, must not match regex
  // `__.*__`, and must not be `""`.
  map<string, GqlQueryParameter> named_bindings = 5;

  // Numbered binding site @1 references the first numbered parameter,
  // effectively using 1-based indexing, rather than the usual 0.
  // For each binding site numbered i in `query_string`,
  // there must be an i-th numbered parameter.
  // The inverse must also be true.
  repeated GqlQueryParameter positional_bindings = 4;
}

// A binding parameter for a GQL query.
message GqlQueryParameter {
  // The type of parameter.
  oneof parameter_type {
    // Value.
    Value value = 2;

    // Cursor.
    bytes cursor = 3;
  }
}

// A batch of results produced by a query.
message QueryResultBatch {
  // The possible values for the `more_results` field.
  enum MoreResultsType {
    // Unspecified. This value is never used.
    MORE_RESULTS_TYPE_UNSPECIFIED = 0;

    // There may be additional batches to fetch from this query.
    NOT_FINISHED = 1;

    // The query is finished, but there may be more results after the limit.
    MORE_RESULTS_AFTER_LIMIT = 2;

    // The query is finished, but there may be more results after the end cursor.
    MORE_RESULTS_AFTER_CURSOR = 4;

    // The query has been exhausted.
    NO_MORE_RESULTS = 3;
  }

  // The number of results skipped, typically because of an offset.
  int32 skipped_results = 6;

  // A cursor that points to the position after the last skipped result.
  // Will be set when `skipped_results` != 0.
  bytes skipped_cursor = 3;

  // The result type for every entity in `entity_results`.
  EntityResult.ResultType entity_result_type = 1;

  // The results for this batch.
  repeated EntityResult entity_results = 2;

  // A cursor that points to the position after the last result in the batch.
  bytes end_cursor = 4;

  // The state of the query after the current batch.
  MoreResultsType more_results = 5;
}
