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:

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:

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


45
46
47
# File 'lib/google/cloud/datastore/gql_query.rb', line 45

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



92
93
94
# File 'lib/google/cloud/datastore/gql_query.rb', line 92

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:

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



110
111
112
# File 'lib/google/cloud/datastore/gql_query.rb', line 110

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



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/google/cloud/datastore/gql_query.rb', line 121

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:

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/google/cloud/datastore/gql_query.rb', line 147

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



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/google/cloud/datastore/gql_query.rb', line 169

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:

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/google/cloud/datastore/gql_query.rb', line 194

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



57
58
59
60
61
# File 'lib/google/cloud/datastore/gql_query.rb', line 57

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



81
82
83
# File 'lib/google/cloud/datastore/gql_query.rb', line 81

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