Class: NativeQuery::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/native-query/result.rb

Overview

Represents ORM query result.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Result

Constructor.



23
24
25
# File 'lib/native-query/result.rb', line 23

def initialize(query)
    @query = query
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



17
18
19
# File 'lib/native-query/result.rb', line 17

def query
  @query
end

Instance Method Details

#allObject

Returns all rows.



59
60
61
# File 'lib/native-query/result.rb', line 59

def all
    self.map { |row| row }
end

#assoc(*args, &block) ⇒ Object

Returns data in complex associative level.

According to limitations of equivalent MP::Fluent method. Block is applied to all resultant rows.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/native-query/result.rb', line 70

def assoc(*args, &block)
    @query.assoc(*args) do |row|
        result = Row::new(row)
        
        if block
            result = block.call(result)
        end
        
        result  # returns
    end
end

#countObject

Returns count of the records.



86
87
88
# File 'lib/native-query/result.rb', line 86

def count
    @query.count
end

#eachObject

Iterates through result.



47
48
49
50
51
52
53
# File 'lib/native-query/result.rb', line 47

def each
    @query.each do |row|
        yield Row::new(row)
    end
    
    self.free!
end

#free!Object

Frees result resources.



108
109
110
# File 'lib/native-query/result.rb', line 108

def free!
    @query.free!
end

#map(&block) ⇒ Object

Maps callback to array.



94
95
96
97
98
99
100
101
102
# File 'lib/native-query/result.rb', line 94

def map(&block)
    result = [ ]
    
    self.each do |item|
        result << block.call(item)
    end
    
    return result
end

#oneObject

Returns one result row.



39
40
41
# File 'lib/native-query/result.rb', line 39

def one
    Row::new(@query.one)
end

#singleObject

Returns first field value of first record.



31
32
33
# File 'lib/native-query/result.rb', line 31

def single
    @query.single
end