Class: Rubyfb::Row

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/src.rb,
lib/row.rb

Overview

This class models a row of data fetched as part of a SQL query.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results, data, number) ⇒ Row

This is the constructor for the Row class. This method shouldn’t really be used as Row objects are automatically created by ResultSets.

Parameters

results

The ResultSet object that the row relates to.

data

An array containing the row data values.

number

The row number for the new row.



938
939
940
941
942
# File 'lib/src.rb', line 938

def initialize(, data, row_number)
  @number = row_number
  @metadata = 
  @data = data
end

Instance Attribute Details

#numberObject (readonly)

This is the accessor for the row number attribute. This will generally reflect the order the row was fetched from the result set in, with 1 being the first row retrieved.



946
947
948
# File 'lib/src.rb', line 946

def number
  @number
end

Instance Method Details

#[](index) ⇒ Object

This method fetches the value associated with a column within a Row object.

Parameters

index

Either the offset of the column to retrieve the value of or the alias of the column to retrieve the value of (column alias comparisons are case sensitive).



1003
1004
1005
# File 'lib/src.rb', line 1003

def [](idx)
  @data[get_index(idx)]
end

#aliasesObject

This method retrieves an array of column aliases for a Row object.



1109
1110
1111
# File 'lib/src.rb', line 1109

def aliases
  @metadata.collect{ |col| col.alias }
end

#column_alias(index) ⇒ Object

This method fetches the alias of a column within a row of data.

Parameters

index

The index of the column to fetch the alias for. The first column in the row is at offset zero.



977
978
979
# File 'lib/src.rb', line 977

def column_alias(idx)
  @metadata[get_index(idx)].alias
end

#column_countObject

This method fetches a count of the number of columns of data that are available from a row.



954
955
956
# File 'lib/src.rb', line 954

def column_count
  @metadata.size
end

#column_name(index) ⇒ Object

This method fetches the name of a column within a row of data.

Parameters

index

The index of the column to fetch the name for. The first column in the row is at offset zero.



966
967
968
# File 'lib/src.rb', line 966

def column_name(idx)
  @metadata[get_index(idx)].name
end

#column_scale(column) ⇒ Object

This method fetches the scale associated with a specified column within a row of data. See the documentation of ResultSet#column_scale for details.

Parameters

column

A reference to the column number to fetch the details for. Column numbers start at zero.



990
991
992
# File 'lib/src.rb', line 990

def column_scale(idx)
  @metadata[get_index(idx)].scale
end

#each {|column, vlaue| ... } ⇒ Object

This method iterates over the contents of a Row object. The block specified for the method should accept two parameters; one for the column alias and one for the column value.

Yields:

  • (column, vlaue)


1012
1013
1014
1015
1016
# File 'lib/src.rb', line 1012

def each
  @metadata.each_with_index do |c, i|
    yield c.key, @data[i]
  end if block_given?
end

#each_key {|column| ... } ⇒ Object

This method iterates over the column names for a Row class.

Yields:

  • (column)


1020
1021
1022
1023
1024
# File 'lib/src.rb', line 1020

def each_key
  @metadata.each do |c|
    yield c.key
  end if block_given?
end

#each_pairObject



122
123
124
125
126
# File 'lib/row.rb', line 122

def each
  @metadata.each_with_index do |c, i|
    yield c.key, @data[i]
  end if block_given?
end

#each_value {|value| ... } ⇒ Object

This method iterators over the column values for a Row class.

Yields:

  • (value)


1028
1029
1030
1031
1032
# File 'lib/src.rb', line 1028

def each_value
  @data.each do |v|
    yield v
  end if block_given?
end

#fetch(key, alternative = nil) {|key| ... } ⇒ Object

An implementation of the Hash#fetch method for the Row class. The method accepts a block but this should not be specified if a value for the alternative parameter is specified.

Parameters

key

A string containing the column alias to retrieve.

alternative

A reference to the alternative value to be returned if the keyed value is not found. Defaults to nil.

Yields:

  • (key)


1043
1044
1045
1046
1047
1048
1049
1050
1051
# File 'lib/src.rb', line 1043

def fetch(index, default=nil)
  soft_value(index) || default || (
    if block_given?
      yield index
    else
      raise IndexError.new("Column identifier #{index} not found in row.")
    end
  )
end

#get_base_type(index) ⇒ Object

This method retrieves the base SQL type for a column of data within a Row object. The method returns one of the base types defined in the SQLType class but does not return an actual SQLType object.

Parameters

index

The offset from the Row first column of the column to return the type information for.



1166
1167
1168
# File 'lib/src.rb', line 1166

def get_base_type(idx)
  @metadata[get_index(idx)].type
end

#has_alias?(name) ⇒ Boolean

This method is used to determine whether a Row object contains a given column alias.

Parameters

name

A String containing the column alias to check for.

Returns:

  • (Boolean)


1077
1078
1079
# File 'lib/src.rb', line 1077

def has_alias?(column_alias)
  !!@metadata.find{ |col| col.alias == column_alias }
end

#has_column?(name) ⇒ Boolean

This method is used to determine whether a Row object contains a given column name.

Parameters

name

A String containing the column name to check for.

Returns:

  • (Boolean)


1066
1067
1068
# File 'lib/src.rb', line 1066

def has_column?(name)
  !!@metadata.find{ |col| col.name == name }
end

#has_key?(name) ⇒ Boolean

This method is used to determine whether a Row object contains a given column alias.

Parameters

name

A String containing the column name to check for.

Returns:

  • (Boolean)


1055
1056
1057
# File 'lib/src.rb', line 1055

def has_key?(key)
  !!@metadata.find{ |col| col.key == key }
end

#has_value?(value) ⇒ Boolean

This method is used to determine whether a Row object contains a given column value.

Parameters

value

A reference to an object value to be checked for.

Returns:

  • (Boolean)


1088
1089
1090
# File 'lib/src.rb', line 1088

def has_value?(value)
  !!@data.find{ |v| v == value }
end

#include?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/row.rb', line 123

def has_key?(key)
  !!@metadata.find{ |col| col.key == key }
end

#key?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/row.rb', line 124

def has_key?(key)
  !!@metadata.find{ |col| col.key == key }
end

#keysObject

This method retrieves an array of column aliases for a Row object.



1095
1096
1097
# File 'lib/src.rb', line 1095

def keys
  @metadata.collect{ |col| col.key }
end

#lengthObject



127
128
129
# File 'lib/row.rb', line 127

def column_count
  @metadata.size
end

#member?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/row.rb', line 125

def has_key?(key)
  !!@metadata.find{ |col| col.key == key }
end

#namesObject

This method retrieves an array of column names for a Row object.



1102
1103
1104
# File 'lib/src.rb', line 1102

def names
  @metadata.collect{ |col| col.name }
end

#select {|column, value| ... } ⇒ Object

This method returns an array of the Row elements for which the specified block returns true.

Yields:

  • (column, value)


1124
1125
1126
1127
1128
1129
1130
1131
1132
# File 'lib/src.rb', line 1124

def select
  block_given? || (raise StandardError.new("No block specified in call to Row#select."))
  
  [].tap do |a|
    @metadata.each_with_index do |c, i|
      a << [c.key, @data[i]] if yield c.key, @data[i]
    end
  end 
end

#sizeObject



128
129
130
# File 'lib/row.rb', line 128

def column_count
  @metadata.size
end

#to_aObject

This method retrieves an Array containing the values from a Row object. Each value is represented as an Array containing a column name and the associated column value.



1134
1135
1136
# File 'lib/src.rb', line 1134

def to_a
  select{ true }
end

#to_hashObject

This method retrieves a Hash created from a Row objects values. The Row objects column names will be used as a key on to the column values.



1142
1143
1144
1145
1146
1147
1148
# File 'lib/src.rb', line 1142

def to_hash
  {}.tap do |map|
    @metadata.each_with_index do |c, i|
      map[c.key] = @data[i]
    end
  end
end

#value?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/row.rb', line 126

def has_value?(value)
  !!@data.find{ |v| v == value }
end

#valuesObject

This method retrieves an array of column values for a Row object.



1116
1117
1118
# File 'lib/src.rb', line 1116

def values
  @data
end

#values_at(*names) ⇒ Object

This method returns an array of column values based on a list of column aliases.

Parameters

names

One or more Strings containing the names of the columns to retrieve values for.



1154
1155
1156
# File 'lib/src.rb', line 1154

def values_at(*columns)
  columns.collect{ |key| soft_value(key) }
end