Class: DataObject::Mysql::Reader

Inherits:
Reader
  • Object
show all
Defined in:
lib/do_mysql.rb

Instance Method Summary collapse

Constructor Details

#initialize(db, reader) ⇒ Reader

Returns a new instance of Reader.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/do_mysql.rb', line 66

def initialize(db, reader)        
  @reader = reader
  unless @reader
    if Mysql_c.mysql_field_count(db) == 0
      @records_affected = Mysql_c.mysql_affected_rows(db)
      close
    else
      raise UnknownError, "An unknown error has occured while trying to process a MySQL query.\n#{Mysql_c.mysql_error(db)}"
    end
  else
    @field_count = @reader.field_count
    @state = STATE_OPEN
    
    @native_fields, @fields = Mysql_c.mysql_c_fetch_field_types(@reader, @field_count), Mysql_c.mysql_c_fetch_field_names(@reader, @field_count)

    raise UnknownError, "An unknown error has occured while trying to process a MySQL query. There were no fields in the resultset\n#{Mysql_c.mysql_error(db)}" if @native_fields.empty?
    
    @has_rows = !(@row = Mysql_c.mysql_c_fetch_row(@reader)).nil?
  end
end

Instance Method Details

#closeObject



87
88
89
90
91
92
93
94
95
# File 'lib/do_mysql.rb', line 87

def close
  if @state == STATE_OPEN
    Mysql_c.mysql_free_result(@reader)
    @state = STATE_CLOSED
    true
  else
    false
  end
end

#current_rowObject



112
113
114
# File 'lib/do_mysql.rb', line 112

def current_row
  @row
end

#eachObject



128
129
130
131
132
133
134
135
# File 'lib/do_mysql.rb', line 128

def each
  return unless has_rows?
  
  while(true) do
    yield
    break unless self.next
  end
end

#get_index(name) ⇒ Object



102
103
104
105
# File 'lib/do_mysql.rb', line 102

def get_index(name)
  super
  @fields.index(name)
end

#item(idx) ⇒ Object



116
117
118
119
# File 'lib/do_mysql.rb', line 116

def item(idx)
  super
  typecast(@row[idx], idx)
end

#name(col) ⇒ Object



97
98
99
100
# File 'lib/do_mysql.rb', line 97

def name(col)
  super
  @fields[col]
end

#nextObject



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

def next
  super
  @row = Mysql_c.mysql_c_fetch_row(@reader)
  close if @row.nil?
  @row ? true : nil
end

#null?(idx) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/do_mysql.rb', line 107

def null?(idx)
  super
  @row[idx] == nil
end