[TYPO3-dev] DB query iterator; worth a try?

Jigal van Hemert jigal at xs4all.nl
Wed Apr 21 14:08:36 CEST 2010


I've been experimenting a bit with an iterator for queries (comments 
removed to save space):

class databaseIterator implements Iterator {

	private $position = 0;
	private $res;

	public function  __construct($res = NULL) {
		$this->res = $res;
	}

	public function rewind() {
		mysql_data_seek($this->res, 0);
		$this->position = 0;
	}

	public function current() {
		return mysql_fetch_assoc($this->res);
	}

	public function key() {
		return $this->position;
	}

	public function next() {
		$this->position++;
	}

	public function valid() {
		return mysql_data_seek($this->res, $this->position);
	}
}

A function such as:

function exec_SELECTiterator ($select_fields, $from_table, 
$where_clause, $groupBy='', $orderBy='', $limit='') {
	$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($select_fields, 
$from_table, $where_clause, $groupBy, $orderBy, $limit);
	return new databaseIterator($res);
}

Allows you to simply use a foreach directly on the iterator:

$res = $this->exec_SELECTiterator( ....parameters.... );
foreach ($res as $row) {
	// do what you want with the row
}

Initial test seem to indicate that an iterator is slightly slower than 
the usual t3lib_db::sql_fetch_assoc(), but this could also be caused by 
the current implementation (mysql_data_seek() calls, transfer of 
resource to iterator object)
I haven't compared it with exec_SELECTgetRows().

Worth further investigating? Any problems to be expected?
-- 
Jigal van Hemert
skype:jigal.van.hemert
msn: jigal at xs4all.nl
http://twitter.com/jigalvh




More information about the TYPO3-dev mailing list