Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Response implements Frame{
protected $_header;

/**
* @var
* @var StreamReader
*/
protected $_stream;

Expand Down
10 changes: 9 additions & 1 deletion src/Response/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Cassandra\Protocol\Frame;
use Cassandra\Type;

class Result extends Response {
class Result extends Response implements \IteratorAggregate {
const VOID = 0x0001;
const ROWS = 0x0002;
const SET_KEYSPACE = 0x0003;
Expand Down Expand Up @@ -311,4 +311,12 @@ public function fetchOne(){

return null;
}

/**
* @return
*/
public function getIterator() {
return new ResultIterator($this->_stream, $this->getMetadata(), $this->_rowClass);
}

}
107 changes: 107 additions & 0 deletions src/Response/ResultIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Cassandra\Response;

/**
* @author Dennis Birkholz <[email protected]>
*/
class ResultIterator implements \Iterator {
/**
* Stream containing the raw result data
*
* @var StreamReader
*/
private $stream;

/**
* Offset to start reading data in this stream
*
* @var int
*/
private $offset;

/**
* Metadata generated by the creating Result
*
* @var array
*/
private $metadata;

/**
* Class to use for each row of data
*
* @var string
*/
private $rowClass;

/**
* Number of available rows in the resultset
*
* @var int
*/
private $count;

/**
* Current row
*
* @var int
*/
private $row = 0;


public function __construct(StreamReader $stream, array $metadata, $rowClass) {
$this->stream = clone $stream;
$this->metadata = $metadata;
$this->count = $this->stream->readInt();
$this->offset = $this->stream->pos();
$this->rewind();
}

public function current() {
$data = [];

foreach ($this->metadata['columns'] as $column) {
$data[$column['name']] = $this->stream->readValue($column['type']);
}

$rowClass = $this->rowClass;
return ($rowClass === null ? $data : new $rowClass($data));
}

/**
* The current position in this result set
*
* @return int
*/
public function key() {
return $this->row;
}

/**
* Move forward to next element
*
* @return void
*/
public function next() {
$this->row++;
}

/**
* Reset the result set
*
* @return void
*/
public function rewind() {
$this->row = 0;
$this->stream->offset($this->offset);
}

/**
* Checks if current position is valid
*
* @return boolean
*/
public function valid() {
return (($this->row >= 0) && ($this->row < $this->count));
}
}
9 changes: 9 additions & 0 deletions src/Response/StreamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public function offset($offset){
public function reset(){
$this->offset = 0;
}

/**
* Get the current position of this stream
*
* @return int
*/
public function pos() {
return $this->offset;
}

/**
* Read single character.
Expand Down