Sha256: fd27ec497ab62c8ce732f262f73ae86fedb4fc6605bfaf99968a01407b8a5b88

Contents?: true

Size: 1 KB

Versions: 15

Compression:

Stored size: 1 KB

Contents

module RR
  # Provides functionality to cast a query result value into the correct ruby type.
  # Requires originating table and column to be known.
  class TypeCastingCursor

    # Delegate the uninteresting methods to the original cursor
    def next?; org_cursor.next? end
    def clear; org_cursor.clear end
    
    # The original cursor object
    attr_accessor :org_cursor
    
    # A column_name => Column cache
    attr_accessor :columns
    
    # Creates a new TypeCastingCursor based on provided database connection and table name
    # for the provided database query cursor
    def initialize(connection, table, cursor)
      self.org_cursor = cursor
      self.columns = {}
      connection.columns(table).each {|c| columns[c.name] = c}
    end
    
    # Reads the next row from the original cursor and returns the row with the type casted row values.
    def next_row
      row = org_cursor.next_row
      row.each {|column, value| row[column] = columns[column].type_cast value}
      row
    end    
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
andyjeffries-rubyrep-1.2.1 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.2.0 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.1.2 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.1.1 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.1.0 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.9 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.8 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.3 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.4 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.5 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.6 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.7 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.1 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.0 lib/rubyrep/type_casting_cursor.rb
rubyrep-1.0.2 lib/rubyrep/type_casting_cursor.rb