2013-09-19 10:05:00 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class SqlResultProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-03-06 15:26:59 +04:00
|
|
|
private $result;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
* @desc my be changed in assoc method
|
|
|
|
*/
|
2013-09-19 10:05:00 +04:00
|
|
|
private $result_items;
|
|
|
|
|
2014-03-06 15:26:59 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-09-19 10:05:00 +04:00
|
|
|
private $result_items_base;
|
|
|
|
|
|
|
|
/**
|
2014-03-06 15:26:59 +04:00
|
|
|
* @param $result DbStatement
|
2013-09-19 10:05:00 +04:00
|
|
|
*/
|
2014-03-06 15:26:59 +04:00
|
|
|
public function __construct($result)
|
2013-09-19 10:05:00 +04:00
|
|
|
{
|
2014-03-06 15:26:59 +04:00
|
|
|
$this->result = $result;
|
2013-09-19 10:05:00 +04:00
|
|
|
}
|
|
|
|
|
2014-03-06 15:26:59 +04:00
|
|
|
private function defineResultItems()
|
2013-09-19 10:05:00 +04:00
|
|
|
{
|
|
|
|
if (is_null($this->result_items_base)) {
|
2014-03-06 15:26:59 +04:00
|
|
|
$this->result_items_base = $this->result->fetchAll();
|
|
|
|
$this->result_items = $this->result_items_base;
|
2013-09-19 10:05:00 +04:00
|
|
|
}
|
2014-03-06 15:26:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $field string
|
|
|
|
* @param bool $assoc_as_array
|
|
|
|
* @return $this SqlResultProvider
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
|
|
|
public function assoc($field, $assoc_as_array = false)
|
|
|
|
{
|
|
|
|
$this->defineResultItems();
|
2013-09-19 10:05:00 +04:00
|
|
|
$result_items_assoc = array();
|
|
|
|
foreach ($this->result_items_base as $item) {
|
|
|
|
if (!isset($item->{$field})) {
|
|
|
|
throw new ErrorException('Undefined field. ' . $field);
|
|
|
|
}
|
|
|
|
if ($assoc_as_array) {
|
|
|
|
if (!isset($result_items_assoc[$item->{$field}])) {
|
|
|
|
$result_items_assoc[$item->{$field}] = array();
|
|
|
|
}
|
|
|
|
$result_items_assoc[$item->{$field}][] = $item;
|
|
|
|
} else {
|
|
|
|
if (isset($result_items_assoc[$item->{$field}])) {
|
|
|
|
throw new ErrorException('Field not unique. May be use assoc_as_array. ' . $field);
|
|
|
|
}
|
|
|
|
$result_items_assoc[$item->{$field}] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Ассоциирование внутри каждого элемента массива
|
2013-09-19 12:54:10 +04:00
|
|
|
if ($assoc_as_array) {
|
2013-09-19 10:05:00 +04:00
|
|
|
foreach ($result_items_assoc as &$value) {
|
2014-03-06 15:26:59 +04:00
|
|
|
$value = new SqlResultCollection($value);
|
2013-09-19 10:05:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->result_items = $result_items_assoc;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getKeys()
|
|
|
|
{
|
2014-03-11 15:44:20 +04:00
|
|
|
$this->defineResultItems();
|
|
|
|
return array_keys($this->result_items);
|
2013-09-19 10:05:00 +04:00
|
|
|
}
|
|
|
|
|
2014-03-06 15:26:59 +04:00
|
|
|
/**
|
|
|
|
* @return DbStatement[]|SqlResultCollection[]
|
|
|
|
*/
|
2013-09-19 10:05:00 +04:00
|
|
|
public function fetchAll()
|
|
|
|
{
|
2014-03-06 15:26:59 +04:00
|
|
|
$this->defineResultItems();
|
2013-09-19 10:05:00 +04:00
|
|
|
return $this->result_items;
|
|
|
|
}
|
|
|
|
|
2013-09-20 22:13:02 +04:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @return mixed
|
2014-03-06 15:26:59 +04:00
|
|
|
* метод актуален после вызова assoc
|
2013-09-20 22:13:02 +04:00
|
|
|
*/
|
2013-09-19 10:05:00 +04:00
|
|
|
public function fetchKey($key)
|
|
|
|
{
|
|
|
|
return $this->result_items[$key];
|
|
|
|
}
|
|
|
|
|
2013-09-20 22:13:02 +04:00
|
|
|
/**
|
|
|
|
* @param $field
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-09-20 22:11:54 +04:00
|
|
|
public function fetchField($field)
|
|
|
|
{
|
2014-03-06 15:26:59 +04:00
|
|
|
return $this->result->fetchField($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetch($style = Db::FETCH_OBJ)
|
|
|
|
{
|
|
|
|
return $this->result->fetch($style);
|
2013-09-20 22:11:54 +04:00
|
|
|
}
|
|
|
|
|
2014-03-06 15:26:59 +04:00
|
|
|
public function affectedRows()
|
2013-09-19 10:05:00 +04:00
|
|
|
{
|
2014-03-06 15:26:59 +04:00
|
|
|
return $this->result->affectedRows();
|
2013-09-19 10:05:00 +04:00
|
|
|
}
|
2014-03-11 15:44:20 +04:00
|
|
|
}
|