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 Iterator implements \Iterator
41: {
42: private $_query = '';
43: private $_currentId = 0;
44: private $_startId = 0;
45: private $_sqlRes = '';
46: private $_mysql = '';
47: private $_params = array();
48: private $_cursor = '';
49: private $_iteratorField = '';
50: private $_currentRow = false;
51:
52: public function __construct(&$mysql,$settings)
53: {
54: $this->_query = $settings['query'];
55: $this->_params = isset($settings['params']) ? $settings['params'] : array();
56: $this->_currentId = $settings['start'];
57: $this->_startId = $settings['start'];
58: $this->_mysql = $mysql;
59: $this->_iteratorField = $settings['iteratorField'];
60: }
61:
62: private function _runQuery($rewind=false)
63: {
64: if($rewind)
65: {
66: $this->_currentId = $this->_startId;
67: }
68:
69: $query = str_replace('::','"'.$this->_mysql->escape($this->_currentId).'"',$this->_query);
70:
71: if(!count($this->_params))
72: $this->_cursor = $this->_mysql->query($query);
73: else
74: $this->_cursor = $this->_mysql->prepare($query)->execute($this->_params);
75: }
76:
77: private function _setRow()
78: {
79: if(!$this->_cursor OR !($row = $this->_mysql->fetch($this->_cursor)))
80: {
81: $this->_runQuery();
82: if(!($row = $this->_mysql->fetch($this->_cursor)))
83: {
84: $this->_currentRow = false;
85: }
86: }
87:
88: if($row)
89: {
90: $this->_currentId = $row->{$this->_iteratorField};
91: $this->_currentRow = $row;
92: }
93: }
94:
95: function rewind()
96: {
97: $this->_runQuery(true);
98: }
99:
100: function current()
101: {
102: return $this->_currentRow;
103: }
104:
105: function key()
106: {
107: return $this->_currentId;
108: }
109:
110: function next()
111: {
112: $this->_setRow();
113: return $this->_currentRow;
114: }
115:
116: function valid()
117: {
118: if($this->_currentId === $this->_startId)
119: {
120: $this->next();
121: }
122:
123: return ($this->_currentRow===false) ? false : true;
124: }
125: }
126:
127: ?>