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.


90
91
92
93
94
95
96
97
# File 'lib/postgres-pr/pg-compat.rb', line 90

def initialize(res)
  @res = res
  @fields = @res.fields.map {|f| f.name}
  @result = []
  @res.rows.each do |row|
    @result << REXML::SyncEnumerator.new(fields, row).map {|name, value| [name, value]}
  end
end

Instance Attribute Details

#fieldsObject (readonly)

TODO: status, getlength, cmdstatus


101
102
103
# File 'lib/postgres-pr/pg-compat.rb', line 101

def fields
  @fields
end

#resultObject (readonly)

TODO: status, getlength, cmdstatus


101
102
103
# File 'lib/postgres-pr/pg-compat.rb', line 101

def result
  @result
end

Instance Method Details

#[](index) ⇒ Object


86
87
88
# File 'lib/postgres-pr/pg-compat.rb', line 86

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

#clearObject

free the result set


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

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

#cmdstatusObject


148
149
150
# File 'lib/postgres-pr/pg-compat.rb', line 148

def cmdstatus
  @res.cmd_tag || ''
end

#cmdtuplesObject Also known as: cmd_tuples

Returns the number of rows affected by the SQL command


158
159
160
161
162
163
164
165
166
167
# File 'lib/postgres-pr/pg-compat.rb', line 158

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

#each(&block) ⇒ Object


82
83
84
# File 'lib/postgres-pr/pg-compat.rb', line 82

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

#fieldname(index) ⇒ Object


115
116
117
# File 'lib/postgres-pr/pg-compat.rb', line 115

def fieldname(index)
  @fields[index]
end

#fieldnum(name) ⇒ Object


119
120
121
# File 'lib/postgres-pr/pg-compat.rb', line 119

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

#getvalue(tup_num, field_num) ⇒ Object


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

def getvalue(tup_num, field_num)
  @result[tup_num][field_num]
end

#num_fieldsObject Also known as: nfields


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

def num_fields
  @fields.size
end

#num_tuplesObject Also known as: ntuples


103
104
105
# File 'lib/postgres-pr/pg-compat.rb', line 103

def num_tuples
  @result.size
end

#size(index) ⇒ Object


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

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

#statusObject


140
141
142
143
144
145
146
# File 'lib/postgres-pr/pg-compat.rb', line 140

def status
  if num_tuples > 0
    TUPLES_OK
  else
    COMMAND_OK
  end
end

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


123
124
125
126
# File 'lib/postgres-pr/pg-compat.rb', line 123

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