2013-11-27 23:55:25 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\Connect;
|
|
|
|
|
|
|
|
use Iterator;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2013-11-27 23:55:25 +01:00
|
|
|
/**
|
|
|
|
* Abstract query-result class.
|
2013-06-21 00:32:08 +02:00
|
|
|
* Once again, this should be subclassed by an actual database implementation. It will only
|
|
|
|
* ever be constructed by a subclass of SS_Database. The result of a database query - an iteratable object
|
|
|
|
* that's returned by DB::SS_Query
|
2013-11-27 23:55:25 +01:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* Primarily, the SS_Query class takes care of the iterator plumbing, letting the subclasses focusing
|
|
|
|
* on providing the specific data-access methods that are required: {@link nextRecord()}, {@link numRecords()}
|
|
|
|
* and {@link seek()}
|
2013-11-27 23:55:25 +01:00
|
|
|
*/
|
|
|
|
abstract class SS_Query implements Iterator {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-27 23:55:25 +01:00
|
|
|
/**
|
|
|
|
* The current record in the interator.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
protected $currentRecord = null;
|
|
|
|
|
2013-11-27 23:55:25 +01:00
|
|
|
/**
|
|
|
|
* The number of the current row in the interator.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @var int
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
protected $rowNum = -1;
|
|
|
|
|
2013-11-27 23:55:25 +01:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Flag to keep track of whether iteration has begun, to prevent unnecessary seeks
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @var bool
|
2013-11-27 23:55:25 +01:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
protected $queryHasBegun = false;
|
2013-11-27 23:55:25 +01:00
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Return an array containing all the values from a specific column. If no column is set, then the first will be
|
|
|
|
* returned
|
2013-11-27 23:55:25 +01:00
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function column($column = null) {
|
|
|
|
$result = array();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
while ($record = $this->next()) {
|
|
|
|
if ($column) {
|
2013-11-27 23:55:25 +01:00
|
|
|
$result[] = $record[$column];
|
|
|
|
} else {
|
|
|
|
$result[] = $record[key($record)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Return an array containing all values in the leftmost column, where the keys are the
|
|
|
|
* same as the values.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function keyedColumn() {
|
|
|
|
$column = array();
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach ($this as $record) {
|
2013-11-27 23:55:25 +01:00
|
|
|
$val = $record[key($record)];
|
|
|
|
$column[$val] = $val;
|
|
|
|
}
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a map from the first column to the second column.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function map() {
|
|
|
|
$column = array();
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach ($this as $record) {
|
2013-11-27 23:55:25 +01:00
|
|
|
$key = reset($record);
|
|
|
|
$val = next($record);
|
|
|
|
$column[$key] = $val;
|
|
|
|
}
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next record in the iterator.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function record() {
|
|
|
|
return $this->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first column of the first record.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function value() {
|
|
|
|
$record = $this->next();
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($record) {
|
|
|
|
return $record[key($record)];
|
|
|
|
}
|
|
|
|
return null;
|
2013-11-27 23:55:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Return an HTML table containing the full result-set
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return string
|
2013-11-27 23:55:25 +01:00
|
|
|
*/
|
|
|
|
public function table() {
|
|
|
|
$first = true;
|
|
|
|
$result = "<table>\n";
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
foreach ($this as $record) {
|
|
|
|
if ($first) {
|
2013-11-27 23:55:25 +01:00
|
|
|
$result .= "<tr>";
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach ($record as $k => $v) {
|
2013-11-27 23:55:25 +01:00
|
|
|
$result .= "<th>" . Convert::raw2xml($k) . "</th> ";
|
|
|
|
}
|
|
|
|
$result .= "</tr> \n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$result .= "<tr>";
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach ($record as $k => $v) {
|
2013-11-27 23:55:25 +01:00
|
|
|
$result .= "<td>" . Convert::raw2xml($v) . "</td> ";
|
|
|
|
}
|
|
|
|
$result .= "</tr> \n";
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-11-27 23:55:25 +01:00
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
$result .= "</table>\n";
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
if ($first) return "No records found";
|
2013-11-27 23:55:25 +01:00
|
|
|
return $result;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-11-27 23:55:25 +01:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Iterator function implementation. Rewind the iterator to the first item and return it.
|
|
|
|
* Makes use of {@link seek()} and {@link numRecords()}, takes care of the plumbing.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rewind() {
|
2013-06-21 00:32:08 +02:00
|
|
|
if ($this->queryHasBegun && $this->numRecords() > 0) {
|
2013-11-27 23:55:25 +01:00
|
|
|
$this->queryHasBegun = false;
|
2015-06-16 05:04:20 +02:00
|
|
|
$this->currentRecord = null;
|
2013-11-27 23:55:25 +01:00
|
|
|
return $this->seek(0);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2013-11-27 23:55:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Iterator function implementation. Return the current item of the iterator.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function current() {
|
2013-06-21 00:32:08 +02:00
|
|
|
if (!$this->currentRecord) {
|
2013-11-27 23:55:25 +01:00
|
|
|
return $this->next();
|
|
|
|
} else {
|
|
|
|
return $this->currentRecord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator function implementation. Return the first item of this iterator.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function first() {
|
|
|
|
$this->rewind();
|
|
|
|
return $this->current();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Iterator function implementation. Return the row number of the current item.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function key() {
|
|
|
|
return $this->rowNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator function implementation. Return the next record in the iterator.
|
|
|
|
* Makes use of {@link nextRecord()}, takes care of the plumbing.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function next() {
|
|
|
|
$this->queryHasBegun = true;
|
|
|
|
$this->currentRecord = $this->nextRecord();
|
|
|
|
$this->rowNum++;
|
|
|
|
return $this->currentRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Iterator function implementation. Check if the iterator is pointing to a valid item.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return bool
|
2013-11-27 23:55:25 +01:00
|
|
|
*/
|
|
|
|
public function valid() {
|
2013-06-21 00:32:08 +02:00
|
|
|
if (!$this->queryHasBegun) $this->next();
|
2013-11-27 23:55:25 +01:00
|
|
|
return $this->currentRecord !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the next record in the query result.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function nextRecord();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the total number of items in the query result.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
abstract public function numRecords();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go to a specific row number in the query result and return the record.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param int $rowNum Row number to go to.
|
2013-11-27 23:55:25 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function seek($rowNum);
|
2014-02-10 01:32:39 +01:00
|
|
|
}
|