1: <?php
2: /*
3: * Simpletools Framework.
4: * Copyright (c) 2009, Marcin Rosinski. (https://www.getsimpletools.com)
5: * All rights reserved.
6: *
7: * LICENCE
8: *
9: * Redistribution and use in source and binary forms, with or without modification,
10: * are permitted provided that the following conditions are met:
11: *
12: * - Redistributions of source code must retain the above copyright notice,
13: * this list of conditions and the following disclaimer.
14: *
15: * - Redistributions in binary form must reproduce the above copyright notice,
16: * this list of conditions and the following disclaimer in the documentation and/or other
17: * materials provided with the distribution.
18: *
19: * - Neither the name of the Simpletools nor the names of its contributors may be used to
20: * endorse or promote products derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29: * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: *
31: * @framework Simpletools
32: * @copyright Copyright (c) 2009 Marcin Rosinski. (http://www.getsimpletools.com)
33: * @license http://www.opensource.org/licenses/bsd-license.php - BSD
34: * @version Ver: 2.0.15 2014-12-31 10:45
35: *
36: */
37:
38: namespace Simpletools\Db\Mysql;
39:
40: class Result implements \Iterator
41: {
42: protected $_mysqlResult = '';
43: protected $_mysqli = '';
44:
45: protected $_firstRowCache = null;
46: protected $_firstRowCached = false;
47:
48: protected $_position = 0;
49: protected $_currentRow = false;
50:
51: public function __construct(&$mysqliResult,&$mysqli)
52: {
53: $this->_mysqlResult = $mysqliResult;
54: $this->_mysqli = $mysqli;
55:
56: if(is_object($mysqliResult))
57: {
58: $this->_position = 0;
59: $this->_loadFirstRowCache();
60: }
61: }
62:
63: public function isEmpty()
64: {
65: return !mysqli_num_rows($this->_mysqlResult);
66: }
67:
68: public function length()
69: {
70: return mysqli_num_rows($this->_mysqlResult);
71: }
72:
73: public function fetch($returnObject=true)
74: {
75: if($returnObject)
76: return mysqli_fetch_object($this->_mysqlResult);
77: else
78: return mysqli_fetch_assoc($this->_mysqlResult);
79: }
80:
81: public function fetchAll($returnObject=true)
82: {
83: if($this->isEmpty()) return array();
84:
85: $datas = array();
86: while($data = $this->fetch($returnObject))
87: {
88: $datas[] = $data;
89: }
90:
91: $this->free();
92: return $datas;
93: }
94:
95: public function &getRawResult()
96: {
97: return $this->_mysqlResult;
98: }
99:
100: public function free()
101: {
102: mysqli_free_result($this->_mysqlResult);
103: }
104:
105: public function __desctruct()
106: {
107: mysqli_free_result($this->_mysqlResult);
108: }
109:
110: public function getAffectedRows()
111: {
112: return $this->_mysqli->affected_rows;
113: }
114:
115: public function getInsertedId()
116: {
117: return $this->_mysqli->insert_id;
118: }
119:
120: protected function _loadFirstRowCache()
121: {
122: if(!$this->_firstRowCached)
123: {
124: $this->_firstRowCache = $this->fetch();
125: $this->_firstRowCached = true;
126: mysqli_data_seek($this->_mysqlResult,0);
127: }
128: }
129:
130: public function __get($name)
131: {
132: //$this->_loadFirstRowCache();
133: return isset($this->_firstRowCache->{$name}) ? $this->_firstRowCache->{$name} : null;
134: }
135:
136: public function __isset($name)
137: {
138: //$this->_loadFirstRowCache();
139: return isset($this->_firstRowCache->{$name});
140: }
141:
142: public function rewind()
143: {
144: mysqli_data_seek($this->_mysqlResult,0);
145: $this->_position = 0;
146:
147: if($this->_currentRow===false)
148: {
149: $this->_currentRow = $this->fetch();
150: }
151: }
152:
153: public function current()
154: {
155: return $this->_currentRow;
156: }
157:
158: public function key()
159: {
160: return $this->_position;
161: }
162:
163: public function next()
164: {
165: $this->_currentRow = $this->fetch();
166: ++$this->position;
167: return $this->_currentRow;
168: }
169:
170: public function valid()
171: {
172: return ($this->_currentRow!==null) ? true : false;
173: }
174: }
175:
176: ?>