Class: Extralite::Iterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/extralite/iterator.c,
ext/extralite/iterator.c

Overview

This class implements an iterator used to iterate through query results.

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ void

Initializes an iterator using the given query object and iteration mode. The iteration mode is one of: :hash, :ary, or :single_column. An iterator is normally returned from one of the methods Query#each/Query#each_hash, Query#each_ary or Query#each_single_column when called without a block:

iterator = query.each
...

Parameters:



55
56
57
58
59
# File 'ext/extralite/iterator.c', line 55

VALUE Iterator_initialize(VALUE self, VALUE query) {
  Iterator_t *iterator = self_to_iterator(self);
  iterator->query = query;
  return Qnil;
}

Instance Method Details

#eachExtralite::Iterator

Iterates through the associated query's result set using the iteration mode set when initialized. Each row would be passed to the given block according to the iteration mode, i.e. as a hash, an array, or a single value. In :single column mode an error will be raised if the result sets contains more than one columns.

Returns:



69
70
71
72
73
74
75
76
# File 'ext/extralite/iterator.c', line 69

VALUE Iterator_each(VALUE self) {
  if (rb_block_given_p()) {
    Iterator_t *iterator = self_to_iterator(self);
    Query_each(iterator->query);
  }

  return self;
}

#inspectString

Returns a short string representation of the iterator instance, including the SQL string.

Returns:

  • (String)

    string representation



116
117
118
119
120
# File 'ext/extralite/iterator.c', line 116

VALUE Iterator_inspect(VALUE self) {
  VALUE cname = rb_class_name(CLASS_OF(self));

  return rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)self);
}

#nextHash, ... #next(row_count) ⇒ Array, Extralite::Iterator

Returns the next 1 or more rows from the associated query's result set according to the iteration mode, i.e. as a hash, an array or a single value.

If no row count is given, a single row is returned. If a row count is given, an array containing up to the row_count rows is returned. If row_count is -1, all rows are returned. If the end of the result set has been reached, nil is returned.

If a block is given, rows are passed to the block and self is returned.

Overloads:

  • #nextHash, ...

    Returns next row or self if block is given.

    Returns:

  • #next(row_count) ⇒ Array, Extralite::Iterator

    Returns next rows or self if block is given.

    Parameters:

    • row_count (Integer)

      maximum row count or -1 for all rows

    Returns:



94
95
96
97
98
99
# File 'ext/extralite/iterator.c', line 94

VALUE Iterator_next(int argc, VALUE *argv, VALUE self) {
  Iterator_t *iterator = self_to_iterator(self);
  VALUE result = Query_next(argc, argv, iterator->query);

  return rb_block_given_p() ? self : result;
}

#to_aArray

Returns all rows from the associated query's result set according to the iteration mode, i.e. as a hash, an array or a single value.

Returns:

  • (Array)

    array of query result set rows



106
107
108
109
# File 'ext/extralite/iterator.c', line 106

VALUE Iterator_to_a(VALUE self) {
  Iterator_t *iterator = self_to_iterator(self);
  return Query_to_a(iterator->query);
}