Class: Velocity::PostgreSQLResult

Inherits:
Object
  • Object
show all
Defined in:
lib/velocity/results/postgresql.rb

Defined Under Namespace

Classes: PostgreSQLMappedRow

Instance Method Summary collapse

Constructor Details

#initialize(sql, adapter) ⇒ PostgreSQLResult

Returns a new instance of PostgreSQLResult.



23
24
25
26
27
# File 'lib/velocity/results/postgresql.rb', line 23

def initialize(sql, adapter)
  @results = adapter.execute(sql)
  @mapping = PostgreSQLMappedRow.new(@results)
  @num_tuples = @results.num_tuples
end

Instance Method Details

#[](index) ⇒ Object



43
44
45
46
47
# File 'lib/velocity/results/postgresql.rb', line 43

def [](index)
  @mapping.tap do |mapping|
    mapping.tuple = index
  end
end

#cleanupObject



69
70
71
# File 'lib/velocity/results/postgresql.rb', line 69

def cleanup
  @results.clear
end

#collect(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/velocity/results/postgresql.rb', line 29

def collect(&block)
  @collected = []    
  @results.num_tuples.times do |tuple|
    @mapping.tuple = tuple
    @collected << yield(@mapping)
  end
  cleanup
  @collected
end

#each(&block) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/velocity/results/postgresql.rb', line 57

def each(&block)
  @results.num_tuples.times do |tuple|
    @mapping.tuple = tuple
    yield(@mapping)
  end
  cleanup
end

#firstObject



39
40
41
# File 'lib/velocity/results/postgresql.rb', line 39

def first
  self.[](0)
end

#inspectObject



49
50
51
# File 'lib/velocity/results/postgresql.rb', line 49

def inspect
  collect(&:attributes).inspect
end

#lengthObject



65
66
67
# File 'lib/velocity/results/postgresql.rb', line 65

def length
  @num_tuples
end

#to_yamlObject



53
54
55
# File 'lib/velocity/results/postgresql.rb', line 53

def to_yaml
  collect(&:attributes).to_yaml
end