Class: Libcouchbase::N1QL

Inherits:
Object
  • Object
show all
Defined in:
lib/libcouchbase/n1ql.rb

Constant Summary collapse

Ordering =
[
    :build_index, :create_index, :drop_index, :create_primary_index,
    :drop_primary_index, :grant, :on, :to, :infer, :select, :insert_into,
    :delete_from, :update, :from, :with, :use_keys, :unnest, :join, :where,
    :group_by, :order_by, :limit, :offset, :upsert_into, :merge_into
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, explain: false, **options) ⇒ N1QL

Returns a new instance of N1QL.



12
13
14
15
16
17
18
19
20
21
# File 'lib/libcouchbase/n1ql.rb', line 12

def initialize(bucket, explain: false, **options)
    @bucket = bucket
    @connection = bucket.connection
    @explain = !!explain
    options.each do |key, value|
        if self.respond_to? key
            self.public_send key, value
        end
    end
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



25
26
27
# File 'lib/libcouchbase/n1ql.rb', line 25

def bucket
  @bucket
end

#connectionObject (readonly)

Returns the value of attribute connection.



25
26
27
# File 'lib/libcouchbase/n1ql.rb', line 25

def connection
  @connection
end

#explain(val = nil) ⇒ Object

Returns the value of attribute explain.



24
25
26
# File 'lib/libcouchbase/n1ql.rb', line 24

def explain
  @explain
end

Instance Method Details

#results(&row_modifier) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/libcouchbase/n1ql.rb', line 65

def results(&row_modifier)
    n1ql_view = @connection.n1ql_query(self)

    current = ::Libuv::Reactor.current
    if current && current.running?
        ResultsLibuv.new(n1ql_view, current, &row_modifier)
    elsif Object.const_defined?(:EventMachine) && EM.reactor_thread?
        ResultsEM.new(n1ql_view, &row_modifier)
    else
        ResultsNative.new(n1ql_view, &row_modifier)
    end
end

#to_sObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/libcouchbase/n1ql.rb', line 45

def to_s
    res = String.new
    res << "EXPLAIN\n" if @explain
    Ordering.each do |statement|
        val = public_send statement
        unless val.nil?
            res << "#{statement.to_s.gsub('_', ' ').upcase} "

            if val.is_a? Array
                res << val.collect { |obj| obj.to_s }.join(', ')
            else
                res << val.to_s
            end

            res << "\n"
        end
    end
    res
end