Class: Couchbase::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, bucket_name, scope_name) ⇒ Scope

Returns a new instance of Scope.

Parameters:

  • backend (Couchbase::Backend)
  • bucket_name (String)

    name of the bucket

  • scope_name (String, :_default)

    name of the scope



28
29
30
31
32
# File 'lib/couchbase/scope.rb', line 28

def initialize(backend, bucket_name, scope_name)
  @backend = backend
  @bucket_name = bucket_name
  @name = scope_name
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



20
21
22
# File 'lib/couchbase/scope.rb', line 20

def bucket_name
  @bucket_name
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/couchbase/scope.rb', line 21

def name
  @name
end

Instance Method Details

#collection(collection_name) ⇒ Collection

Opens the default collection for this scope

Parameters:

  • collection_name (String)

    name of the collection

Returns:



39
40
41
# File 'lib/couchbase/scope.rb', line 39

def collection(collection_name)
  Collection.new(@backend, @bucket_name, @name, collection_name)
end

#query(statement, options = Cluster::QueryOptions.new) ⇒ QueryResult

Performs a query against the query (N1QL) services.

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

Parameters:

  • statement (String)

    the N1QL query statement

  • options (QueryOptions) (defaults to: Cluster::QueryOptions.new)

    the custom options for this query

Returns:

  • (QueryResult)

See Also:



54
55
56
57
58
59
60
61
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
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/couchbase/scope.rb', line 54

def query(statement, options = Cluster::QueryOptions.new)
  resp = @backend.document_query(statement, {
    timeout: options.timeout,
    adhoc: options.adhoc,
    client_context_id: options.client_context_id,
    max_parallelism: options.max_parallelism,
    readonly: options.readonly,
    scan_wait: options.scan_wait,
    scan_cap: options.scan_cap,
    pipeline_batch: options.pipeline_batch,
    pipeline_cap: options.pipeline_cap,
    metrics: options.metrics,
    profile: options.profile,
    positional_parameters: options.export_positional_parameters,
    named_parameters: options.export_named_parameters,
    scope_name: @name,
    bucket_name: @bucket_name,
    scope_qualifier: options.scope_qualifier,
    raw_parameters: options.raw_parameters,
    scan_consistency: options.scan_consistency,
    mutation_state: options.mutation_state&.to_a,
  })

  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
      res[:warnings] = resp[:warnings].map { |warn| QueryWarning.new(warn[:code], warn[:message]) } if resp[:warnings]
    end
    res.instance_variable_set("@rows", resp[:rows])
  end
end