1: <?php
2:
3: namespace zsql;
4:
5: use \mysqli_result;
6:
7: class Result
8: {
9:
10: /**
11: * @var \mysqli_result
12: */
13: protected $result;
14:
15: /**
16: * @var string
17: */
18: protected $resultClass;
19:
20: /**
21: * @var integer
22: */
23: protected $resultMode;
24:
25: const FETCH_COLUMN = 0;
26: const FETCH_OBJECT = 1;
27: const FETCH_ASSOC = 2;
28: const FETCH_NUM = 3;
29:
30: /**
31: * Constructor
32: *
33: * @param mixed $object
34: */
35: public function __construct($object)
36: {
37: if( $object instanceof \mysqli_result ) {
38: $this->setResult($object);
39: }
40: }
41:
42: /**
43: * Destructor
44: */
45: public function __destruct()
46: {
47: $this->free();
48: }
49:
50: /**
51: * Frees the local mysqli_result object, and unsetting it.
52: *
53: * @return void
54: */
55: public function free()
56: {
57: if( $this->result ) {
58: $this->result->free();
59: $this->result = null;
60: }
61: }
62:
63: /**
64: * Getter function for the local mysqli_result object.
65: *
66: * @return \mysqli_result
67: */
68: public function getResult()
69: {
70: if( !$this->result ) {
71: throw new Exception('No result!');
72: }
73: return $this->result;
74: }
75:
76: /**
77: * Setter function for the local mysqli_result object.
78: *
79: * @param \mysqli_result $object
80: * @return \zsql\Result
81: */
82: protected function setResult(mysqli_result $object)
83: {
84: $this->result = $object;
85: return $this;
86: }
87:
88: /**
89: * Get result class
90: *
91: * @return string
92: */
93: public function getResultClass()
94: {
95: return $this->resultClass;
96: }
97:
98: /**
99: * Set result class
100: *
101: * @param string $class
102: * @return \zsql\Result
103: * @throws \zsql\Exception
104: */
105: public function setResultClass($class)
106: {
107: if( !is_string($class) || !class_exists($class) ) {
108: throw new \zsql\Exception('Class not found');
109: }
110: $this->resultClass = $class;
111: return $this;
112: }
113:
114: /**
115: * Get current result mode
116: *
117: * @return integer
118: */
119: public function getResultMode()
120: {
121: return $this->resultMode !== null ? $this->resultMode : self::FETCH_OBJECT;
122: }
123:
124: /**
125: * Set result mode
126: *
127: * @param integer $mode
128: * @return \zsql\Result
129: * @throws \zsql\Exception
130: */
131: public function setResultMode($mode)
132: {
133: if( !is_int($mode) || $mode < 0 || $mode > 3 ) {
134: throw new Exception("Invalid result mode");
135: }
136: $this->resultMode = $mode;
137: return $this;
138: }
139:
140: /**
141: * Fetches a single result row
142: *
143: * @param integer $mode This optional parameter is a constant indicating what
144: * type of result should be produced from the current row data. The possible
145: * values for this parameter are the constants FETCH_ASSOC, FETCH_OBJECT,
146: * FETCH_COLUMN, or FETCH_NUM.
147: * @return mixed
148: */
149: public function fetchRow($mode = null)
150: {
151: $spec = $this->getResult();
152:
153: if( $mode === null ) {
154: $mode = $this->getResultMode();
155: }
156:
157: $data = null;
158: switch( $mode ) {
159: case self::FETCH_ASSOC:
160: $data = $spec->fetch_assoc();
161: break;
162: case self::FETCH_COLUMN:
163: if( ($row = $spec->fetch_row()) && is_array($row) ) {
164: $data = current($row);
165: } else {
166: $data = null;
167: }
168: break;
169: case self::FETCH_NUM:
170: $data = $spec->fetch_row();
171: break;
172: case self::FETCH_OBJECT:
173: $data = ($this->resultClass ?
174: $spec->fetch_object($this->resultClass) :
175: $spec->fetch_object());
176: break;
177: }
178:
179: $this->free();
180: return $data;
181: }
182:
183:
184: /**
185: * Fetches all result rows
186: *
187: * @param integer $mode This optional parameter is a constant indicating what
188: * type of result should be produced from the current row data. The possible
189: * values for this parameter are the constants FETCH_ASSOC, FETCH_OBJECT,
190: * FETCH_COLUMN, or FETCH_NUM.
191: *
192: * @return mixed
193: */
194: public function fetchAll($mode = null)
195: {
196: $spec = $this->getResult();
197:
198: if( $mode === null ) {
199: $mode = $this->getResultMode();
200: }
201:
202: $data = array();
203: switch( $mode ) {
204: case self::FETCH_ASSOC:
205: while( ($row = $spec->fetch_assoc()) ) {
206: $data[] = $row;
207: }
208: break;
209: case self::FETCH_COLUMN:
210: while( ($row = $spec->fetch_row()) ) {
211: $data[] = current($row);
212: }
213: break;
214: case self::FETCH_NUM:
215: while( ($row = $spec->fetch_row()) ) {
216: $data[] = $row;
217: }
218: break;
219: case self::FETCH_OBJECT:
220: if( $this->resultClass ) {
221: while( ($row = $spec->fetch_object($this->resultClass)) ) {
222: $data[] = $row;
223: }
224: } else {
225: while( ($row = $spec->fetch_object()) ) {
226: $data[] = $row;
227: }
228: }
229: break;
230: }
231:
232: $this->free();
233: return $data;
234: }
235:
236: /**
237: * Returns a single value from the first result row.
238: *
239: * @return mixed
240: */
241: public function fetchColumn()
242: {
243: $spec = $this->getResult();
244:
245: $data = $spec->fetch_row();
246: if( is_array($data) ) {
247: $data = current($data);
248: }
249:
250: $this->free();
251: return $data;
252: }
253: }
254: