Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
RecordReader.php
Go to the documentation of this file.
1 <?php
25 class Dataface_RecordReader implements Iterator {
30  private $start = null;
31 
37  private $limit = null;
38 
43  private $query = null;
44 
50  private $bufferSize = 30;
51 
56  private $buffer = null;
57 
64  private $bufferStartPos = null;
65 
70  private $currPos = null;
71 
77  public $previewRecords = true;
78 
79 
89  public function __construct(array $query, $bufferSize=30, $previewRecords = true){
90  $this->start = @$query['-skip'] ? intval($query['-skip']):0;
91  $this->limit = @$query['-limit'] ? intval($query['-limit']):null;
92  $this->query = $query;
93  $this->bufferSize = $bufferSize;
94  $this->buffer = null;
95  $this->bufferStartPos = null;
96  $this->currPos = $this->start;
97  $this->previewRecords = $previewRecords;
98  if ( isset($this->limit) and $this->limit < $this->bufferSize ) $this->bufferSize = $this->limit;
99  }
100 
104  public function __destruct(){
105  $this->buffer = null;
106  }
107 
108 
112  public function rewind(){
113  $this->currPos = $this->start;
114 
115  if ( !isset($this->bufferStartPos) ){
116  // The buffer isn't set yet so we are at the beginning
117  return;
118  } else if ( $this->bufferStartPos > $this->start ){
119  // The buffer is beyond the first buffer set
120  // so we reset the buffer now
121  $this->buffer = null;
122  $this->bufferStartPos = null;
123  } else {
124  // The buffer is not beyond the first set
125  // so the buffer is good.
126  }
127 
128  }
129 
133  private function loadBuffer(){
134  if ( $this->currPos >= $this->bufferStartPos + $this->bufferSize ){
135  $this->bufferStartPos += $this->bufferSize;
136  } else if ( !isset($this->bufferStartPos) ){
137  $this->bufferStartPos = $this->start;
138  } else {
139  return;
140  }
141  $q = $this->query;
142  $q['-skip'] = $this->bufferStartPos;
143  $q['-limit'] = $this->bufferSize;
144  if ( isset($this->limit) ){
145  $q['-limit'] = min($this->bufferSize, $this->start+$this->limit-$this->bufferStartPos);
146  }
147  if ( $q['-limit'] > 0 ){
148  $this->buffer = df_get_records_array($q['-table'], $q, $q['-skip'], $q['-limit'], $this->previewRecords);
149  } else {
150  $this->buffer = array();
151  }
152 
153  }
154 
159  public function current(){
160  $this->loadBuffer();
161  return $this->buffer[$this->currPos-$this->bufferStartPos];
162  }
163 
168  public function key(){
169  return $this->currPos;
170  }
171 
175  public function next(){
176  ++$this->currPos;
177  }
178 
182  public function valid(){
183  $this->loadBuffer();
184  return isset($this->buffer[$this->currPos-$this->bufferStartPos]);
185  }
186 }