Method: Couchbase::Scope#query

Defined in:
lib/couchbase/scope.rb

#query(statement, options = Options::Query::DEFAULT) ⇒ QueryResult

Performs a query against the query (N1QL) services.

The query will be implicitly scoped using current bucket and scope names.

Examples:

Select first ten hotels from travel sample dataset

scope.query("SELECT * FROM `travel-sample` WHERE type = $type LIMIT 10",
             Options::Query(named_parameters: {type: "hotel"}, metrics: true))

Parameters:

  • statement (String)

    the N1QL query statement

  • options (Options::Query) (defaults to: Options::Query::DEFAULT)

    the custom options for this query

Returns:

  • (QueryResult)

See Also:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/couchbase/scope.rb', line 62

def query(statement, options = Options::Query::DEFAULT)
  resp = @backend.document_query(statement, options.to_backend(scope_name: @name, bucket_name: @bucket_name))

  Cluster::QueryResult.new do |res|
    res. = Cluster::QueryMetaData.new do |meta|
      meta.status = resp[:meta][:status]
      meta.request_id = resp[:meta][:request_id]
      meta.client_context_id = resp[:meta][:client_context_id]
      meta.signature = JSON.parse(resp[:meta][:signature]) if resp[:meta][:signature]
      meta.profile = JSON.parse(resp[:meta][:profile]) if resp[:meta][:profile]
      meta.metrics = Cluster::QueryMetrics.new do |metrics|
        if resp[:meta][:metrics]
          metrics.elapsed_time = resp[:meta][:metrics][:elapsed_time]
          metrics.execution_time = resp[:meta][:metrics][:execution_time]
          metrics.sort_count = resp[:meta][:metrics][:sort_count]
          metrics.result_count = resp[:meta][:metrics][:result_count]
          metrics.result_size = resp[:meta][:metrics][:result_size]
          metrics.mutation_count = resp[:meta][:metrics][:mutation_count]
          metrics.error_count = resp[:meta][:metrics][:error_count]
          metrics.warning_count = resp[:meta][:metrics][:warning_count]
        end
      end
      meta.warnings = resp[:warnings].map { |warn| Cluster::QueryWarning.new(warn[:code], warn[:message]) } if resp[:warnings]
    end
    res.instance_variable_set(:@rows, resp[:rows])
  end
end