Class: Google::Cloud::Datastore::GqlQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/datastore/gql_query.rb

Overview

GqlQuery

Represents a GQL query.

GQL is a SQL-like language for retrieving entities or keys from Datastore.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

gql_query = Google::Cloud::Datastore::GqlQuery.new
gql_query.query_string = "SELECT * FROM Task ORDER BY created ASC"
tasks = datastore.run gql_query

See Also:

Instance Method Summary collapse

Constructor Details

#initializeGqlQuery

Returns a new GqlQuery instance.

Examples:

require "google/cloud/datastore"

gql_query = Google::Cloud::Datastore::GqlQuery.new


51
52
53
# File 'lib/google/cloud/datastore/gql_query.rb', line 51

def initialize
  @grpc = Google::Datastore::V1::GqlQuery.new
end

Instance Method Details

#allow_literalsBoolean

Whether the query may contain literal values. When false, the query string must not contain any literals and instead must bind all values using #named_bindings= or #positional_bindings=.

Returns:

  • (Boolean)

    true if the query may contain literal values



98
99
100
# File 'lib/google/cloud/datastore/gql_query.rb', line 98

def allow_literals
  @grpc.allow_literals
end

#allow_literals=(new_allow_literals) ⇒ Object

Sets whether the query may contain literal values. When false, the query string must not contain any literals and instead must bind all values using #named_bindings= or #positional_bindings=.

Examples:

require "google/cloud/datastore"

gql_query = Google::Cloud::Datastore::GqlQuery.new
gql_query.query_string = "SELECT * FROM Task " \
                         "WHERE completed = false AND priority = 4"
gql_query.allow_literals = true

Parameters:

  • new_allow_literals (Boolean)

    true if the query may contain literal values



118
119
120
# File 'lib/google/cloud/datastore/gql_query.rb', line 118

def allow_literals= new_allow_literals
  @grpc.allow_literals = new_allow_literals
end

#named_bindingsHash

The named binding values for a query that contains named argument binding sites that start with @.

Returns:

  • (Hash)

    a frozen hash that maps the binding site names in the query string to valid GQL arguments



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/google/cloud/datastore/gql_query.rb', line 129

def named_bindings
  bindings = Hash[@grpc.named_bindings.map do |name, gql_query_param|
    if gql_query_param.parameter_type == :cursor
      [name, Cursor.from_grpc(gql_query_param.cursor)]
    else
      [name, Core::GRPCUtils.from_value(gql_query_param.value)]
    end
  end]
  bindings.freeze
  bindings
end

#named_bindings=(new_named_bindings) ⇒ Object

Sets named binding values for a query that contains named argument binding sites that start with @.

Examples:

require "google/cloud/datastore"

gql_query = Google::Cloud::Datastore::GqlQuery.new
gql_query.query_string = "SELECT * FROM Task " \
                         "WHERE done = @done " \
                         "AND priority = @priority"
gql_query.named_bindings = {done: false, priority: 4}

Parameters:

  • new_named_bindings (Hash)

    a hash that maps the binding site names in the query string to valid GQL arguments



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/google/cloud/datastore/gql_query.rb', line 157

def named_bindings= new_named_bindings
  @grpc.named_bindings.clear
  new_named_bindings.map do |name, value|
    if value.is_a? Google::Cloud::Datastore::Cursor
      @grpc.named_bindings[name.to_s] = \
        Google::Datastore::V1::GqlQueryParameter.new(
          cursor: value.to_grpc)
    else
      @grpc.named_bindings[name.to_s] = \
        Google::Datastore::V1::GqlQueryParameter.new(
          value: Core::GRPCUtils.to_value(value))
    end
  end
end

#positional_bindingsArray

The binding values for a query that contains numbered argument binding sites that start with @.

Returns:

  • (Array)

    a frozen array containing the query arguments in the order of the numbered binding sites in the query string



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/google/cloud/datastore/gql_query.rb', line 179

def positional_bindings
  bindings = @grpc.positional_bindings.map do |gql_query_param|
    if gql_query_param.parameter_type == :cursor
      Cursor.from_grpc gql_query_param.cursor
    else
      Core::GRPCUtils.from_value gql_query_param.value
    end
  end
  bindings.freeze
  bindings
end

#positional_bindings=(new_positional_bindings) ⇒ Object

Sets the binding values for a query that contains numbered argument binding sites that start with @.

Examples:

require "google/cloud/datastore"

gql_query = Google::Cloud::Datastore::GqlQuery.new
gql_query.query_string = "SELECT * FROM Task" \
                         "WHERE completed = @1 AND priority = @2"
gql_query.positional_bindings = [false, 4]

Parameters:

  • new_positional_bindings (Array)

    query arguments in the order of the numbered binding sites in the query string



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/google/cloud/datastore/gql_query.rb', line 206

def positional_bindings= new_positional_bindings
  @grpc.positional_bindings.clear
  new_positional_bindings.map do |value|
    if value.is_a? Google::Cloud::Datastore::Cursor
      @grpc.positional_bindings << \
        Google::Datastore::V1::GqlQueryParameter.new(
          cursor: value.to_grpc)
    else
      @grpc.positional_bindings << \
        Google::Datastore::V1::GqlQueryParameter.new(
          value: Core::GRPCUtils.to_value(value))
    end
  end
end

#query_stringString

The GQL query string for the query. The string may contain named or positional argument binding sites that start with @. Corresponding binding values should be set with #named_bindings= or #positional_bindings=.

Returns:

  • (String)

    a GQL statement



63
64
65
66
67
# File 'lib/google/cloud/datastore/gql_query.rb', line 63

def query_string
  gql = @grpc.query_string.dup
  gql.freeze
  gql
end

#query_string=(new_query_string) ⇒ Object

Sets the GQL query string for the query. The string may contain named or positional argument binding sites that start with @. Corresponding binding values should be set with #named_bindings= or #positional_bindings=.

See the GQL Reference.

Examples:

gql_query = Google::Cloud::Datastore::GqlQuery.new
gql_query.query_string = "SELECT * FROM Task " \
                         "WHERE done = @done " \
                         "AND priority = @priority"
gql_query.named_bindings = {done: false, priority: 4}

Parameters:

  • new_query_string (String)

    a valid GQL statement



87
88
89
# File 'lib/google/cloud/datastore/gql_query.rb', line 87

def query_string= new_query_string
  @grpc.query_string = new_query_string.to_s
end