Class: PG::Tuple
- Inherits:
-
Object
- Object
- PG::Tuple
- Includes:
- Enumerable
- Defined in:
- ext/pg_tuple.c,
lib/pg/tuple.rb,
ext/pg_tuple.c
Overview
******************************************************************
The class to represent one query result tuple (row).
An instance of this class can be created by PG::Result#tuple .
All field values of the tuple are retrieved on demand from the underlying PGresult object and converted to a Ruby object.
Subsequent access to the same field returns the same object, since they are cached when materialized.
Each PG::Tuple holds a reference to the related PG::Result object, but gets detached, when all fields are materialized.
Example:
require 'pg'
conn = PG.connect(:dbname => 'test')
res = conn.exec('VALUES(1,2), (3,4)')
t0 = res.tuple(0) # => #<PG::Tuple column1: "1", column2: "2">
t1 = res.tuple(1) # => #<PG::Tuple column1: "3", column2: "4">
t1[0] # => "3"
t1["column2"] # => "4"
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns a field value by either column index or column name.
-
#each {|key, value| ... } ⇒ Object
Invokes block for each field name and value in the tuple.
- #each_key(&block) ⇒ Object
-
#each_value {|value| ... } ⇒ Object
Invokes block for each field value in the tuple.
-
#fetch(*args) ⇒ Object
Returns a field value by either column index or column name.
- #has_key?(key) ⇒ Boolean (also: #key?)
-
#index(key) ⇒ Object
Returns the field number which matches the given column name.
-
#inspect ⇒ Object
Return a String representation of the object suitable for debugging.
- #keys ⇒ Object
-
#length ⇒ Object
(also: #size)
Returns number of fields of this tuple.
-
#values ⇒ Array
Returns the values of this tuple as Array.
Instance Method Details
#[](key) ⇒ Object
Returns a field value by either column index or column name.
An integer key
is interpreted as column index. Negative values of index count from the end of the array.
Depending on Result#field_name_type= a string or symbol key
is interpreted as column name.
If the key can’t be found, it returns nil
.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'ext/pg_tuple.c', line 309
static VALUE
pg_tuple_aref(VALUE self, VALUE key)
{
VALUE index;
int field_num;
t_pg_tuple *this = pg_tuple_get_this(self);
switch(rb_type(key)){
case T_FIXNUM:
case T_BIGNUM:
field_num = NUM2INT(key);
if ( field_num < 0 )
field_num = this->num_fields + field_num;
if ( field_num < 0 || field_num >= this->num_fields )
return Qnil;
break;
default:
index = rb_hash_aref(this->field_map, key);
if( index == Qnil ) return Qnil;
field_num = NUM2INT(index);
}
return pg_tuple_materialize_field(self, field_num);
}
|
#each {|key, value| ... } ⇒ Object
Invokes block for each field name and value in the tuple.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'ext/pg_tuple.c', line 355
static VALUE
pg_tuple_each(VALUE self)
{
t_pg_tuple *this = pg_tuple_get_this(self);
VALUE field_names;
RETURN_SIZED_ENUMERATOR(self, 0, NULL, pg_tuple_num_fields_for_enum);
field_names = pg_tuple_get_field_names(this);
if( field_names == Qfalse ){
rb_hash_foreach(this->field_map, pg_tuple_yield_key_value, self);
} else {
int i;
for( i = 0; i < this->num_fields; i++ ){
VALUE value = pg_tuple_materialize_field(self, i);
rb_yield_values(2, RARRAY_AREF(field_names, i), value);
}
}
pg_tuple_detach(self);
return self;
}
|
#each_key(&block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/pg/tuple.rb', line 23 def each_key(&block) if fn=field_names fn.each(&block) else field_map.each_key(&block) end end |
#each_value {|value| ... } ⇒ Object
Invokes block for each field value in the tuple.
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'ext/pg_tuple.c', line 385
static VALUE
pg_tuple_each_value(VALUE self)
{
t_pg_tuple *this = pg_tuple_get_this(self);
int field_num;
RETURN_SIZED_ENUMERATOR(self, 0, NULL, pg_tuple_num_fields_for_enum);
for(field_num = 0; field_num < this->num_fields; field_num++) {
VALUE value = pg_tuple_materialize_field(self, field_num);
rb_yield(value);
}
pg_tuple_detach(self);
return self;
}
|
#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key) ⇒ Object
Returns a field value by either column index or column name.
An integer key
is interpreted as column index. Negative values of index count from the end of the array.
Depending on Result#field_name_type= a string or symbol key
is interpreted as column name.
If the key can’t be found, there are several options: With no other arguments, it will raise a IndexError exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'ext/pg_tuple.c', line 252
static VALUE
pg_tuple_fetch(int argc, VALUE *argv, VALUE self)
{
VALUE key;
long block_given;
VALUE index;
int field_num;
t_pg_tuple *this = pg_tuple_get_this(self);
rb_check_arity(argc, 1, 2);
key = argv[0];
block_given = rb_block_given_p();
if (block_given && argc == 2) {
rb_warn("block supersedes default value argument");
}
switch(rb_type(key)){
case T_FIXNUM:
case T_BIGNUM:
field_num = NUM2INT(key);
if ( field_num < 0 )
field_num = this->num_fields + field_num;
if ( field_num < 0 || field_num >= this->num_fields ){
if (block_given) return rb_yield(key);
if (argc == 1) rb_raise( rb_eIndexError, "Index %d is out of range", field_num );
return argv[1];
}
break;
default:
index = rb_hash_aref(this->field_map, key);
if (index == Qnil) {
if (block_given) return rb_yield(key);
if (argc == 1) rb_raise( rb_eKeyError, "column not found" );
return argv[1];
}
field_num = NUM2INT(index);
}
return pg_tuple_materialize_field(self, field_num);
}
|
#has_key?(key) ⇒ Boolean Also known as: key?
14 15 16 |
# File 'lib/pg/tuple.rb', line 14 def has_key?(key) field_map.has_key?(key) end |
#index(key) ⇒ Object
Returns the field number which matches the given column name.
452 453 454 455 456 457 |
# File 'ext/pg_tuple.c', line 452
static VALUE
pg_tuple_index(VALUE self, VALUE key)
{
t_pg_tuple *this = pg_tuple_get_this(self);
return rb_hash_aref(this->field_map, key);
}
|
#inspect ⇒ Object
Return a String representation of the object suitable for debugging.
10 11 12 |
# File 'lib/pg/tuple.rb', line 10 def inspect "#<#{self.class} #{self.map{|k,v| "#{k}: #{v.inspect}" }.join(", ") }>" end |
#keys ⇒ Object
19 20 21 |
# File 'lib/pg/tuple.rb', line 19 def keys field_names || field_map.keys.freeze end |
#length ⇒ Object Also known as: size
Returns number of fields of this tuple.
439 440 441 442 443 444 |
# File 'ext/pg_tuple.c', line 439
static VALUE
pg_tuple_length(VALUE self)
{
t_pg_tuple *this = pg_tuple_get_this(self);
return INT2NUM(this->num_fields);
}
|
#values ⇒ Array
Returns the values of this tuple as Array. res.tuple(i).values is equal to res.tuple_values(i) .
410 411 412 413 414 415 416 417 |
# File 'ext/pg_tuple.c', line 410
static VALUE
pg_tuple_values(VALUE self)
{
t_pg_tuple *this = pg_tuple_get_this(self);
pg_tuple_materialize(self);
return rb_ary_new4(this->num_fields, &this->values[0]);
}
|