Class: FluentQuery::Drivers::Shared::Results::DBI

Inherits:
Result
  • Object
show all
Defined in:
lib/fluent-query/drivers/shared/results/dbi.rb

Overview

PostgreSQL query native token.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ DBI

Returns a new instance of DBI.



43
44
45
46
47
48
49
50
51
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 43

def initialize(source)
   super()
   
   @_source = source
   @_columns = nil

   ObjectSpace::define_finalizer(self, self.class.method(:finalize).to_proc)
   @@_sources[self.object_id] = @_source
end

Class Method Details

.finalize(id) ⇒ Object



58
59
60
61
62
63
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 58

def self.finalize(id)
    if @@_sources[id]
        @@_sources[id].finish
        @@_sources.delete(id)
    end
end

Instance Method Details

#allObject



70
71
72
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 70

def all
    self.to_enum.map { |i| i }
end

#column_namesObject



142
143
144
145
146
147
148
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 142

def column_names
    if @@column_names.nil?
        @@column_names = @_source.column_names.map { |i| i.to_sym }
    end
    
    @@column_names
end

#countObject



165
166
167
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 165

def count
    @_source.all.count
end

#each(&block) ⇒ Object



133
134
135
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 133

def each(&block)
    self.hash &block
end

#free!Object



174
175
176
177
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 174

def free!
    @_source.finish
    @@_sources.delete(self.object_id)
end

#hashObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 114

def hash
    result = nil
    first = true
    
    while result or first
        first = false
        result = self.one

        if result
            yield result
        end
    end
end

#oneObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 79

def one
    row = @_source.fetch_hash
    
    if row
        row = row.map_keys { |i| i.to_sym }
        result = FluentQuery::Data::new(row)
    else
        result = nil
    end

    return result
end

#repeat!Object



155
156
157
158
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 155

def repeat!
    @_source.execute()
    self
end

#singleObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fluent-query/drivers/shared/results/dbi.rb', line 97

def single
    result = @_source.fetch

    if not result.nil?
        result = result.first
    else
        result = nil
    end

    return result
end