Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Allow QueryResult object to lazy-load more results
Browse files Browse the repository at this point in the history
  • Loading branch information
MidnightLightning committed May 24, 2012
1 parent 0c3deb6 commit 2b867de
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions soapclient/SforceBaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,12 @@ public function getUpdated($type, $startDate, $endDate) {
*/
public function query($query) {
$this->setHeaders("query");
$QueryResult = $this->sforce->query(array (
$raw = $this->sforce->query(array (
'queryString' => $query
))->result;
return new QueryResult($QueryResult);
$QueryResult = new QueryResult($raw);
$QueryResult->setSf($this); // Dependency Injection
return $QueryResult;
}

/**
Expand All @@ -802,8 +804,10 @@ public function queryMore($queryLocator) {
$this->setHeaders("queryMore");
$arg = new stdClass();
$arg->queryLocator = $queryLocator;
$QueryResult = $this->sforce->queryMore($arg)->result;
return new QueryResult($QueryResult);
$raw = $this->sforce->queryMore($arg)->result;
$QueryResult = new QueryResult($raw);
$QueryResult->setSf($this); // Dependency Injection
return $QueryResult;
}

/**
Expand All @@ -815,10 +819,12 @@ public function queryMore($queryLocator) {
*/
public function queryAll($query, $queryOptions = NULL) {
$this->setHeaders("queryAll");
$QueryResult = $this->sforce->queryAll(array (
$raw = $this->sforce->queryAll(array (
'queryString' => $query
))->result;
return new QueryResult($QueryResult);
$QueryResult = new QueryResult($raw);
$QueryResult->setSf($this); // Dependency Injection
return $QueryResult;
}


Expand Down Expand Up @@ -919,16 +925,22 @@ public function __construct($response) {
}
}

class QueryResult {
class QueryResult implements Iterator{
public $queryLocator;
public $done;
public $records;
public $size;

public $pointer; // Current iterator location
private $sf; // SOAP Client

public function __construct($response) {
$this->queryLocator = $response->queryLocator;
$this->done = $response->done;
$this->size = $response->size;

$this->pointer = 0;
$this->sf = false;

if($response instanceof QueryResult) {
$this->records = $response->records;
Expand All @@ -947,6 +959,32 @@ public function __construct($response) {
}
}
}

public function setSf(SforceBaseClient $sf) { $this->sf = $sf; } // Dependency Injection

// Basic Iterator implementation functions
public function rewind() { $this->pointer = 0; }
public function next() { ++$this->pointer; }
public function key() { return $this->pointer; }
public function current() { return $this->records[$this->pointer]; }

public function valid() {
while ($this->pointer >= count($this->records)) {
// Pointer is larger than (current) result set; see if we can fetch more
if ($this->done === false) {
if ($this->sf === false) throw new Exception("Dependency not met!");
$response = $this->sf->queryMore($this->queryLocator);
$this->records = array_merge($this->records, $response->records); // Append more results
$this->done = $response->done;
$this->queryLocator = $response->queryLocator;
} else {
return false; // No more records to fetch
}
}
if (isset($this->records[$this->pointer])) return true;

throw new Exception("QueryResult has gaps in the record data?");
}
}

class SObject {
Expand Down

0 comments on commit 2b867de

Please sign in to comment.