Class: NusaDB::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nusadb/connection.rb

Overview

The result of a statement: column names, rows (each an array of String/nil), and the command tag.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns, rows, command, column_types = nil) ⇒ Result

Returns a new instance of Result.



26
27
28
29
30
31
32
33
# File 'lib/nusadb/connection.rb', line 26

def initialize(columns, rows, command, column_types = nil)
  @columns = columns
  @rows = rows
  @command = command
  # Per-column type name (protocol 1.1, R42-B.03), parallel to +columns+; each entry is a
  # canonical name such as 'INT'/'TEXT' or nil if the server did not report it.
  @column_types = column_types || Array.new(columns.size)
end

Instance Attribute Details

#column_typesObject (readonly)

Returns the value of attribute column_types.



24
25
26
# File 'lib/nusadb/connection.rb', line 24

def column_types
  @column_types
end

#columnsObject (readonly)

Returns the value of attribute columns.



24
25
26
# File 'lib/nusadb/connection.rb', line 24

def columns
  @columns
end

#commandObject (readonly)

Returns the value of attribute command.



24
25
26
# File 'lib/nusadb/connection.rb', line 24

def command
  @command
end

#rowsObject (readonly)

Returns the value of attribute rows.



24
25
26
# File 'lib/nusadb/connection.rb', line 24

def rows
  @rows
end

Instance Method Details

#affectedObject

The affected-row count parsed from the trailing number of the command tag.



49
50
51
52
53
54
# File 'lib/nusadb/connection.rb', line 49

def affected
  return 0 if @command.nil?

  last = @command.split(' ').last
  last =~ /\A\d+\z/ ? last.to_i : 0
end

#eachObject

Iterate rows as hashes keyed by column name.



36
37
38
39
40
41
42
# File 'lib/nusadb/connection.rb', line 36

def each
  return enum_for(:each) unless block_given?

  @rows.each do |row|
    yield @columns.each_with_index.to_h { |name, i| [name, row[i]] }
  end
end

#to_aObject



44
45
46
# File 'lib/nusadb/connection.rb', line 44

def to_a
  each.to_a
end