1: <?php
2:
3: namespace PHPixie\Database\Driver\PDO;
4:
5: class Result extends \PHPixie\Database\Result
6: {
7: protected $statement;
8: protected $fetched = false;
9: protected $current;
10: protected $position;
11:
12: public function __construct($statement)
13: {
14: $this->statement = $statement;
15: }
16:
17: public function current()
18: {
19: $this->checkFetched();
20:
21: return $this->current;
22: }
23:
24: public function key()
25: {
26: $this->checkFetched();
27:
28: if (!$this->valid())
29: return null;
30:
31: return $this->position;
32: }
33:
34: public function valid()
35: {
36: $this->checkFetched();
37:
38: return $this->current !== null;
39: }
40:
41: public function next()
42: {
43: $this->checkFetched();
44: $this->fetchNext();
45: }
46:
47: public function rewind()
48: {
49: if ($this->position > 0)
50: throw new \PHPixie\Database\Exception("PDO statement cannot be rewound for unbuffered queries");
51: }
52:
53: public function statement()
54: {
55: return $this->statement;
56: }
57:
58: protected function checkFetched()
59: {
60: if (!$this->fetched) {
61: $this->fetchNext();
62: $this->fetched = true;
63: }
64: }
65:
66: protected function fetchNext()
67: {
68: $this->current = $this->statement->fetchObject();
69: $this->fetched = true;
70: if ($this->current !== false) {
71: if ($this->position === null) {
72: $this->position = 0;
73: } else {
74: $this->position++;
75: }
76: } else {
77: $this->current = null;
78: $this->statement->closeCursor();
79: }
80: }
81:
82: public function get($field = null)
83: {
84: return parent::get($field);
85: }
86:
87: public function getItemField($item, $field = null)
88: {
89: if($field === null)
90: $field = $this->getFirstFieldName($item);
91:
92: return parent::getItemField($item, $field);
93: }
94:
95: public function getField($field = null, $skipNulls = false)
96: {
97: if ($field === null) {
98: $this->rewind();
99: $field = $this->getFirstFieldName($this->current());
100: }
101:
102: return parent::getField($field, $skipNulls);
103: }
104:
105: protected function getFirstFieldName($item)
106: {
107: return key(get_object_vars($item));
108: }
109: }
110: