Class: Extralite::Iterator
- Inherits:
-
Object
- Object
- Extralite::Iterator
- 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
-
#each ⇒ Extralite::Iterator
Iterates through the associated query's result set using the iteration mode set when initialized.
-
#initialize(query) ⇒ void
constructor
Initializes an iterator using the given query object and iteration mode.
-
#inspect ⇒ String
Returns a short string representation of the iterator instance, including the SQL string.
-
#next(*args) ⇒ Object
Returns the next 1 or more rows from the associated query's result set according to the iteration mode, i.e.
-
#to_a ⇒ Array
Returns all rows from the associated query's result set according to the iteration mode, i.e.
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
...
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
#each ⇒ Extralite::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.
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;
}
|
#inspect ⇒ String
Returns a short string representation of the iterator instance, including the SQL string.
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);
}
|
#next ⇒ Hash, ... #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.
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_a ⇒ Array
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.
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);
}
|