Class: PGresult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/postgres-pr/pg-compat.rb,
lib/postgres-pr/postgres-compat.rb

Constant Summary collapse

EMPTY_QUERY =
0
COMMAND_OK =
1
TUPLES_OK =
2
COPY_OUT =
3
COPY_IN =
4
BAD_RESPONSE =
5
NONFATAL_ERROR =
6
FATAL_ERROR =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res) ⇒ PGresult

Returns a new instance of PGresult.



117
118
119
120
121
122
123
124
125
126
# File 'lib/postgres-pr/pg-compat.rb', line 117

def initialize(res)
  @res = res
  @fields = @res.fields.map {|f| f.name}
  @result = []
  @res.rows.each do |row|
    h = {}
    @fields.zip(row){|field, value| h[field] = value}
    @result << h
  end
end

Instance Attribute Details

#fieldsObject (readonly)

TODO: status, getlength, cmdstatus



97
98
99
# File 'lib/postgres-pr/postgres-compat.rb', line 97

def fields
  @fields
end

#resultObject (readonly)

TODO: status, getlength, cmdstatus



97
98
99
# File 'lib/postgres-pr/postgres-compat.rb', line 97

def result
  @result
end

Instance Method Details

#[](index) ⇒ Object



113
114
115
# File 'lib/postgres-pr/pg-compat.rb', line 113

def [](index)
  @result[index]
end

#clearObject

free the result set



204
205
206
# File 'lib/postgres-pr/pg-compat.rb', line 204

def clear
  @res = @fields = @result = nil
end

#cmdstatusObject



199
200
201
# File 'lib/postgres-pr/pg-compat.rb', line 199

def cmdstatus
  @res.cmd_tag || ''
end

#cmdtuplesObject Also known as: cmd_tuples

Returns the number of rows affected by the SQL command



209
210
211
212
213
214
215
216
217
218
# File 'lib/postgres-pr/pg-compat.rb', line 209

def cmdtuples
  case @res.cmd_tag
  when nil 
    return nil
  when /^INSERT\s+(\d+)\s+(\d+)$/, /^(DELETE|UPDATE|MOVE|FETCH)\s+(\d+)$/
    $2.to_i
  else
    nil
  end
end

#column_values(i) ⇒ Object

Raises:

  • (IndexError)


134
135
136
137
# File 'lib/postgres-pr/pg-compat.rb', line 134

def column_values(i)
  raise IndexError, "no column #{i} in result"  unless i < @fields.size
  @res.rows.map{|row| row[i]}
end

#each(&block) ⇒ Object



109
110
111
# File 'lib/postgres-pr/pg-compat.rb', line 109

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

#field_values(field) ⇒ Object

Raises:

  • (IndexError)


139
140
141
142
# File 'lib/postgres-pr/pg-compat.rb', line 139

def field_values(field)
  raise IndexError, "no such field '#{field}' in result"  unless @fields.include?(field)
  @result.map{|row| row[field]}
end

#fname(index) ⇒ Object Also known as: fieldname



158
159
160
# File 'lib/postgres-pr/pg-compat.rb', line 158

def fname(index)
  @fields[index]
end

#fnum(name) ⇒ Object Also known as: fieldnum



164
165
166
# File 'lib/postgres-pr/pg-compat.rb', line 164

def fnum(name)
  @fields.index(name)
end

#getlength(tup_num, field_num) ⇒ Object



187
188
189
# File 'lib/postgres-pr/pg-compat.rb', line 187

def getlength(tup_num, field_num)
  @res.rows[typ_num][field_num].length
end

#getvalue(tup_num, field_num) ⇒ Object



183
184
185
# File 'lib/postgres-pr/pg-compat.rb', line 183

def getvalue(tup_num, field_num)
  @res.rows[tup_num][field_num]
end

#num_fieldsObject Also known as: nfields



152
153
154
# File 'lib/postgres-pr/pg-compat.rb', line 152

def num_fields
  @fields.size
end

#num_tuplesObject Also known as: ntuples



146
147
148
# File 'lib/postgres-pr/pg-compat.rb', line 146

def num_tuples
  @result.size
end

#size(index) ⇒ Object

Raises:



177
178
179
180
181
# File 'lib/postgres-pr/pg-compat.rb', line 177

def size(index)
  raise
  # TODO: correct?
  @res.fields[index].typlen
end

#statusObject



191
192
193
194
195
196
197
# File 'lib/postgres-pr/pg-compat.rb', line 191

def status
  if num_tuples > 0
    TUPLES_OK
  else
    COMMAND_OK
  end
end

#type(index) ⇒ Object Also known as: ftype



170
171
172
173
# File 'lib/postgres-pr/pg-compat.rb', line 170

def type(index)
  # TODO: correct?
  @res.fields[index].type_oid
end

#valuesObject

TODO: status, cmdstatus



130
131
132
# File 'lib/postgres-pr/pg-compat.rb', line 130

def values
  @res.rows
end