1: <?php
2:
3: namespace Alchemy\engine;
4: use PDO;
5: use PDOStatement;
6: use Iterator;
7: use Exception;
8: use PDOException;
9:
10:
11: /**
12: * Implementation of IResultSet for PDO results
13: */
14: class ResultSet implements IResultSet {
15: protected $connector;
16: protected $statement;
17: protected $index = 0;
18: protected $current;
19:
20:
21: /**
22: * Object constructor.
23: *
24: * @param PDO $connector
25: * @param PDOStatement $statement
26: */
27: public function __construct(PDO $connector, PDOStatement $statement) {
28: $this->connector = $connector;
29: $this->statement = $statement;
30:
31: try {
32: $this->fetch();
33: } catch (PDOException $e) {}
34: }
35:
36:
37: /**
38: * See {@see Iterator::current()}
39: */
40: public function current() {
41: return $this->current;
42: }
43:
44:
45: /**
46: * Fetch the next result from the cursor
47: */
48: protected function fetch() {
49: $this->current = $this->statement->fetch(PDO::FETCH_ASSOC);
50: }
51:
52:
53: /**
54: * Return the last inserted ID form the database
55: *
56: * @return integer
57: */
58: public function lastInsertID() {
59: return $this->connector->lastInsertId();
60: }
61:
62:
63: /**
64: * See {@see Iterator::key()}
65: */
66: public function key() {
67: return $this->index;
68: }
69:
70:
71: /**
72: * See {@see Iterator::next()}
73: */
74: public function next() {
75: $this->index++;
76: $this->fetch();
77: }
78:
79:
80: /**
81: * See {@see Iterator::rewind()}
82: */
83: public function rewind() {} // this is a forward-only iterator
84:
85:
86: /**
87: * Return the row count of the result set
88: *
89: * @return integer
90: */
91: public function rowCount() {
92: return $this->statement->rowCount();
93: }
94:
95:
96: /**
97: * See {@see Iterator::valid()}
98: */
99: public function valid() {
100: return !empty($this->current);
101: }
102: }
103: