Qore Programming Language Reference Manual 1.19.2
Loading...
Searching...
No Matches
Qore::ListHashIterator Class Reference

This class an iterator class for lists of hashes as returned by Qore::SQL::Datasource::selectRows() and Qore::SQL::DatasourcePool::selectRows(), both of which return lists of columns where each list entry is a hash of the current column values. More...

#include <QC_ListHashIterator.dox.h>

Inheritance diagram for Qore::ListHashIterator:
[legend]

Public Member Methods

 constructor (softlist< auto > l)
 Creates the hash list iterator object. More...
 
 copy ()
 Creates a copy of the ListHashIterator object, iterating the same object as the original and in the same position. More...
 
bool empty ()
 returns True if the result list is empty; False if not More...
 
bool first ()
 returns True if on the first element of the list More...
 
auto getKeyValue (string key)
 Returns the current value for the column given as an argument. More...
 
hash< auto > getRow ()
 returns the current row value as a hash or throws an INVALID-ITERATOR exception if the iterator is invalid More...
 
hash< auto > getValue ()
 returns the current row value as a hash or throws an INVALID-ITERATOR exception if the iterator is invalid More...
 
int index ()
 returns the current iterator position in the list or -1 if not pointing at a valid element More...
 
bool last ()
 returns True if on the last element of the list More...
 
int max ()
 returns the number of elements in the list More...
 
auto memberGate (string key)
 This method allows the iterator to be dereferenced directly as a hash for the current row being iterated, as memberGate methods are called implicitly when an unknown member is accessed from outside the class. More...
 
bool next ()
 Moves the current position to the next element in the result list; returns False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the first element in the list if the list is not empty. More...
 
bool prev ()
 Moves the current position to the previous element in the result list; returns False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the last element in the list if the list is not empty. More...
 
 reset ()
 Reset the iterator instance to its initial state. More...
 
bool set (int pos)
 sets the new position in the result list; if the position is invalid then the method returns False, meaning the iterator is not valid, otherwise it returns True More...
 
bool valid ()
 returns True if the iterator is currently pointing at a valid element, False if not More...
 
abstract bool prev ()
 Moves the current position to the previous element; returns False if there are no more elements. More...
 
abstract auto getValue ()
 returns the current value More...
 
abstract bool next ()
 Moves the current position to the next element; returns False if there are no more elements. More...
 
abstract bool valid ()
 returns True if the iterator is currently pointing at a valid element, False if not More...
 
abstract bool empty ()
 returns True if the object to iterate is empty; False if not More...
 
abstract bool first ()
 returns True if on the first element More...
 
abstract bool last ()
 returns True if on the last element More...
 

Detailed Description

This class an iterator class for lists of hashes as returned by Qore::SQL::Datasource::selectRows() and Qore::SQL::DatasourcePool::selectRows(), both of which return lists of columns where each list entry is a hash of the current column values.

Call ListHashIterator::next() to iterate through the lists of column values; do not use the iterator if ListHashIterator::next() returns False. A result list can be iterated in reverse order by calling ListHashIterator::prev() instead of ListHashIterator::next()

Example: ListHashIterator basic usage
list<auto> data = (
{"column1": 1, "column2": "a"},
{"column1": 2, "column2": "b"},
);
ListHashIterator it(data);
while (it.next()) {
printf("iter %d: %n\n", it.index(), it.getValue());
}
iter 0: hash: (column1: 1, column2: "a")
iter 1: hash: (column1: 2, column2: "b")
Note
  • In general, the ListHashIterator class is not designed to be accessed from multiple threads; it was created without locking for fast and efficient use when used from a single thread. For methods that would be unsafe to use in another thread, any use of such methods in threads other than the thread where the constructor was called will cause an ITERATOR-THREAD-ERROR to be thrown.
See also
ListHashReverseIterator

Member Function Documentation

◆ constructor()

Qore::ListHashIterator::constructor ( softlist< auto >  l)

Creates the hash list iterator object.

Parameters
lthe list of hashes to iterate
Example:
ListHashIterator i(l);
Note
the constructor's argument is softlist so that it can also accept NOTHING

◆ copy()

Qore::ListHashIterator::copy ( )

Creates a copy of the ListHashIterator object, iterating the same object as the original and in the same position.

Example:
ListHashIterator ni = i.copy();

◆ empty()

bool Qore::ListHashIterator::empty ( )
virtual

returns True if the result list is empty; False if not

Returns
True if the result list is empty; False if not
Code Flags:
CONSTANT
Example:
if (i.empty())
printf("the result list is empty\n");

Implements Qore::AbstractQuantifiedIterator.

◆ first()

bool Qore::ListHashIterator::first ( )
virtual

returns True if on the first element of the list

Returns
True if on the first element of the list
Code Flags:
CONSTANT
Example:
while (i.next()) {
if (i.first())
printf("START:\n");
}

Implements Qore::AbstractQuantifiedIterator.

Reimplemented in Qore::ListHashReverseIterator.

◆ getKeyValue()

auto Qore::ListHashIterator::getKeyValue ( string  key)

Returns the current value for the column given as an argument.

Code Flags:
RET_VALUE_ONLY
Parameters
keythe column name for the value to retrieve
Returns
the current column value of the given row
Example:
while (i.next()) {
printf("%d: value: %y", i.index(), i.getKeyValue("value"));
}
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object
INVALID-ITERATORthe iterator is not pointing at a valid element or the list being iterated does not contain a hash element at the current iterator position or the given hash key does not exist
Note
ListHashIterator::memberGate() allows for the iterator to act as if it is a hash with members equal to the current row being iterated

◆ getRow()

hash< auto > Qore::ListHashIterator::getRow ( )

returns the current row value as a hash or throws an INVALID-ITERATOR exception if the iterator is invalid

Returns
the current row value as a hash or throws an INVALID-ITERATOR exception if the iterator is invalid
Code Flags:
RET_VALUE_ONLY
Example:
while (i.next()) {
printf(" + row %d: %y\n", i.index(), i.getValue());
}
Note
equivalent to ListHashIterator::getValue()
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object
INVALID-ITERATORthe iterator is not pointing at a valid element or the list being iterated does not contain a hash element at the current iterator position

◆ getValue()

hash< auto > Qore::ListHashIterator::getValue ( )
virtual

returns the current row value as a hash or throws an INVALID-ITERATOR exception if the iterator is invalid

Returns
the current row value as a hash or throws an INVALID-ITERATOR exception if the iterator is invalid
Code Flags:
RET_VALUE_ONLY
Example:
while (i.next()) {
printf(" + row %d: %y\n", i.index(), i.getValue());
}
Note
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object
INVALID-ITERATORthe iterator is not pointing at a valid element or the list being iterated does not contain a hash element at the current iterator position

Implements Qore::AbstractIterator.

◆ index()

int Qore::ListHashIterator::index ( )

returns the current iterator position in the list or -1 if not pointing at a valid element

Returns
the current iterator position in the list or -1 if not pointing at a valid element
Code Flags:
CONSTANT
Example:
while (i.next()) {
printf("+ %d/%d: %y\n", i.index(), i.max(), i.getValue());
}

◆ last()

bool Qore::ListHashIterator::last ( )
virtual

returns True if on the last element of the list

Returns
True if on the last element of the list
Code Flags:
CONSTANT
Example:
while (i.next()) {
if (i.last())
printf("END.\n");
}

Implements Qore::AbstractQuantifiedIterator.

Reimplemented in Qore::ListHashReverseIterator.

◆ max()

int Qore::ListHashIterator::max ( )

returns the number of elements in the list

Returns
the number of elements in the list
Code Flags:
CONSTANT
Example:
while (i.next()) {
printf("+ %d/%d: %y\n", i.index(), i.max(), i.getValue());
}

◆ memberGate()

auto Qore::ListHashIterator::memberGate ( string  key)

This method allows the iterator to be dereferenced directly as a hash for the current row being iterated, as memberGate methods are called implicitly when an unknown member is accessed from outside the class.

Code Flags:
RET_VALUE_ONLY
Parameters
keythe column name for the value to retrieve
Returns
the current column value of the given row
Example:
while (i.next()) {
printf("%d: value: %y", i.index(), i.value);
}
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object
INVALID-ITERATORthe iterator is not pointing at a valid element or the list being iterated does not contain a hash element at the current iterator position or the given hash key does not exist
Note
equivalent to ListHashIterator::getKeyValue() when called explicitly

◆ next()

bool Qore::ListHashIterator::next ( )
virtual

Moves the current position to the next element in the result list; returns False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the first element in the list if the list is not empty.

This method will return True again after it returns False once if list is not empty, otherwise it will always return False. The iterator object should not be used after this method returns False

Returns
False if there are no more elements in the result list (in which case the iterator object is invalid and should not be used); True if successful (meaning that the iterator object is valid)
Example:
while (i.next()) {
printf(" + row %d: %y\n", i.index(), i.getValue());
}
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

Implements Qore::AbstractIterator.

Reimplemented in Qore::ListHashReverseIterator.

◆ prev()

bool Qore::ListHashIterator::prev ( )
virtual

Moves the current position to the previous element in the result list; returns False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the last element in the list if the list is not empty.

This method will return True again after it returns False once if the list is not empty, otherwise it will always return False. The iterator object should not be used after this method returns False

Returns
False if there are no more elements in the result list (in which case the iterator object is invalid and should not be used); True if successful (meaning that the iterator object is valid)
Example:
while (i.prev()) {
printf(" + row %d: %y\n", i.index(), i.getValue());
}
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

Implements Qore::AbstractBidirectionalIterator.

Reimplemented in Qore::ListHashReverseIterator.

◆ reset()

Qore::ListHashIterator::reset ( )

Reset the iterator instance to its initial state.

Reset the iterator instance to its initial state

Example
i.reset();
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

◆ set()

bool Qore::ListHashIterator::set ( int  pos)

sets the new position in the result list; if the position is invalid then the method returns False, meaning the iterator is not valid, otherwise it returns True

Parameters
posthe new position for the iterator with 0 as the first element
Returns
False, meaning the iterator is not valid, otherwise it returns True
Example:
if (!i.set(pos))
throw "INVALID-POSITION", sprintf("%d is an invalid position", pos);
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

◆ valid()

bool Qore::ListHashIterator::valid ( )
virtual

returns True if the iterator is currently pointing at a valid element, False if not

Returns
True if the iterator is currently pointing at a valid element, False if not
Code Flags:
CONSTANT
Example:
if (i.valid())
printf("current value: %y\n", i.getValue());

Implements Qore::AbstractIterator.