Class: RDO::Postgres::TupleList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/rdo_postgres/tuples.c

Instance Method Summary collapse

Instance Method Details

#eachObject

Allow iteration over all tuples, yielding Hashes into a block



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/rdo_postgres/tuples.c', line 42

static VALUE rdo_postgres_tuple_list_each(VALUE self) {
  if (!rb_block_given_p()) {
    return self;
  }

  RDOPostgresTupleList * list;
  Data_Get_Struct(self, RDOPostgresTupleList, list);

  int i     = 0;
  int ntups = PQntuples(list->res);

  for (; i < ntups; ++i) {
    VALUE hash  = rb_hash_new();
    int j       = 0;
    int nfields = PQnfields(list->res);

    for (; j < nfields; ++j) {
      rb_hash_aset(hash,
          ID2SYM(rb_intern(PQfname(list->res, j))),
          rdo_postgres_cast_value(list->res, i, j, list->encoding));
    }

    rb_yield(hash);
  }

  return self;
}