Class: Vertica::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vertica/result.rb

Overview

Class that represents a buffered resultset.

This class implements the Enumerable interface for easy iteration through the rows. You can also address specific values in the result using #fetch. To leanr more about the shape of the result #row_description will give you an ordered list of all the Columns in this result.

Examples:

Iterating over the rows

result = connection.query("SELECT name, id")
result.each do |row|
  puts "#{row[:id]}: #{row[:name]}"
end

Fetching specific values in the result

result = connection.query('SELECT id, name FROM people WHERE id = 1')
name = result.fetch(0, 'name')
id = result[0,0]

Retrieving the only value in a result

average_salery = connection.query("SELECT AVG(salery) FROM employees").the_value

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row_description: nil, rows: nil, tag: nil) ⇒ Result

Initializes a new Vertica::Result instance.

The constructor assumes that the row description, and the list of rows match up. If you're unsure, use build which will assert this is the case.

Parameters:

  • row_description (Vertica::RowDescription) (defaults to: nil)

    The description of the rows

  • rows (Array<Vertica::Row>) (defaults to: nil)

    The array of rows

  • tag (String) (defaults to: nil)

    The kind of command that returned this result.

See Also:



40
41
42
# File 'lib/vertica/result.rb', line 40

def initialize(row_description: nil, rows: nil, tag: nil)
  @row_description, @rows, @tag = row_description, rows, tag
end

Instance Attribute Details

#row_descriptionVertica::RowDescription (readonly) Also known as: columns

The columns in the result.

Returns:



26
27
28
# File 'lib/vertica/result.rb', line 26

def row_description
  @row_description
end

#tagString (readonly)

The kind of SQL command that produced this reuslt.

Returns:

  • (String)

    the current value of tag



26
27
28
# File 'lib/vertica/result.rb', line 26

def tag
  @tag
end

Class Method Details

.build(row_description: nil, rows: [], tag: nil) ⇒ Vertica::Result

Builds a Vertica::Result from a row description and a list of compatible rows.

Parameters:

Returns:



107
108
109
110
111
# File 'lib/vertica/result.rb', line 107

def self.build(row_description: nil, rows: [], tag: nil)
  row_description = Vertica::RowDescription.build(row_description)
  rows = rows.map { |values| row_description.build_row(values) }
  new(row_description: row_description, rows: rows, tag: tag)
end

Instance Method Details

#each {|row| ... }

This method returns an undefined value.

Iterates through the resultset. This class also includes the Enumerable interface, which means you can use all the Enumerable methods like map and inject as well.

Yields:

  • The provided block will be called for every row in the resutset.

Yield Parameters:



50
51
52
# File 'lib/vertica/result.rb', line 50

def each(&block)
  @rows.each(&block)
end

#empty?Boolean

Returns true if the result has no rows.

Returns:

  • (Boolean)

    Returns true if the result has no rows.



55
56
57
# File 'lib/vertica/result.rb', line 55

def empty?
  @rows.empty?
end

#fetch(row) ⇒ Vertica::Row #fetch(row, column) ⇒ Object Also known as: []

Retrieves a row or value from the result.

Overloads:

  • #fetch(row) ⇒ Vertica::Row

    Returns a row from the result.

    Parameters:

    • row (Integer)

      The 0-indexed row number.

    Returns:

    Raises:

    • (IndexError)

      if the row index is out of bounds.

  • #fetch(row, column) ⇒ Object

    Returns a singular value from the result.

    Parameters:

    • row (Integer)

      The 0-indexed row number.

    • col (Symbol, String, Integer)

      The name or index of the column.

    Returns:

    • The value at the given row and column in the result.

    Raises:

    • (IndexError)

      if the row index is out of bounds.

    • (KeyError)

      if the requested column is not part of the result

Returns:



82
83
84
85
86
# File 'lib/vertica/result.rb', line 82

def fetch(row, col = nil)
  row = @rows.fetch(row)
  return row if col.nil?
  row.fetch(col)
end

#sizeInteger Also known as: count, length

Returns The number of rows in this result.

Returns:

  • (Integer)

    The number of rows in this result



60
61
62
# File 'lib/vertica/result.rb', line 60

def size
  @rows.length
end

#valueObject Also known as: the_value

Shorthand to return the value of a query that only returns a single value.

Returns:

  • The first value of the first row, i.e. fetch(0, 0).



92
93
94
# File 'lib/vertica/result.rb', line 92

def value
  fetch(0, 0)
end