1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
5 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
6 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
7 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
8 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
9 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
10 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
11 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
12 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
13 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
14 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
15 | * |
---|
16 | * This software consists of voluntary contributions made by many individuals |
---|
17 | * and is licensed under the LGPL. For more information, see |
---|
18 | * <http://www.doctrine-project.org>. |
---|
19 | */ |
---|
20 | |
---|
21 | namespace Doctrine\DBAL\Portability; |
---|
22 | |
---|
23 | use PDO; |
---|
24 | |
---|
25 | /** |
---|
26 | * Portability Wrapper for a Statement |
---|
27 | * |
---|
28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
29 | * @link www.doctrine-project.com |
---|
30 | * @since 2.0 |
---|
31 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
32 | */ |
---|
33 | class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement |
---|
34 | { |
---|
35 | |
---|
36 | /** |
---|
37 | * @var int |
---|
38 | */ |
---|
39 | private $portability; |
---|
40 | |
---|
41 | /** |
---|
42 | * @var Doctrine\DBAL\Driver\Statement |
---|
43 | */ |
---|
44 | private $stmt; |
---|
45 | |
---|
46 | /** |
---|
47 | * @var int |
---|
48 | */ |
---|
49 | private $case; |
---|
50 | |
---|
51 | /** |
---|
52 | * @var int |
---|
53 | */ |
---|
54 | private $defaultFetchStyle = PDO::FETCH_BOTH; |
---|
55 | |
---|
56 | /** |
---|
57 | * Wraps <tt>Statement</tt> and applies portability measures |
---|
58 | * |
---|
59 | * @param Doctrine\DBAL\Driver\Statement $stmt |
---|
60 | * @param Doctrine\DBAL\Connection $conn |
---|
61 | */ |
---|
62 | public function __construct($stmt, Connection $conn) |
---|
63 | { |
---|
64 | $this->stmt = $stmt; |
---|
65 | $this->portability = $conn->getPortability(); |
---|
66 | $this->case = $conn->getFetchCase(); |
---|
67 | } |
---|
68 | |
---|
69 | public function bindParam($column, &$variable, $type = null) |
---|
70 | { |
---|
71 | return $this->stmt->bindParam($column, $variable, $type); |
---|
72 | } |
---|
73 | |
---|
74 | public function bindValue($param, $value, $type = null) |
---|
75 | { |
---|
76 | return $this->stmt->bindValue($param, $value, $type); |
---|
77 | } |
---|
78 | |
---|
79 | public function closeCursor() |
---|
80 | { |
---|
81 | return $this->stmt->closeCursor(); |
---|
82 | } |
---|
83 | |
---|
84 | public function columnCount() |
---|
85 | { |
---|
86 | return $this->stmt->columnCount(); |
---|
87 | } |
---|
88 | |
---|
89 | public function errorCode() |
---|
90 | { |
---|
91 | return $this->stmt->errorCode(); |
---|
92 | } |
---|
93 | |
---|
94 | public function errorInfo() |
---|
95 | { |
---|
96 | return $this->stmt->errorInfo(); |
---|
97 | } |
---|
98 | |
---|
99 | public function execute($params = null) |
---|
100 | { |
---|
101 | return $this->stmt->execute($params); |
---|
102 | } |
---|
103 | |
---|
104 | public function setFetchMode($fetchStyle) |
---|
105 | { |
---|
106 | $this->defaultFetchStyle = $fetchStyle; |
---|
107 | } |
---|
108 | |
---|
109 | public function getIterator() |
---|
110 | { |
---|
111 | $data = $this->fetchAll($this->defaultFetchStyle); |
---|
112 | return new \ArrayIterator($data); |
---|
113 | } |
---|
114 | |
---|
115 | public function fetch($fetchStyle = PDO::FETCH_BOTH) |
---|
116 | { |
---|
117 | $row = $this->stmt->fetch($fetchStyle); |
---|
118 | |
---|
119 | $row = $this->fixRow($row, |
---|
120 | $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM), |
---|
121 | !is_null($this->case) && ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE) |
---|
122 | ); |
---|
123 | |
---|
124 | return $row; |
---|
125 | } |
---|
126 | |
---|
127 | public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0) |
---|
128 | { |
---|
129 | if ($columnIndex != 0) { |
---|
130 | $rows = $this->stmt->fetchAll($fetchStyle, $columnIndex); |
---|
131 | } else { |
---|
132 | $rows = $this->stmt->fetchAll($fetchStyle); |
---|
133 | } |
---|
134 | |
---|
135 | $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); |
---|
136 | $fixCase = !is_null($this->case) && ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE); |
---|
137 | if (!$iterateRow && !$fixCase) { |
---|
138 | return $rows; |
---|
139 | } |
---|
140 | |
---|
141 | foreach ($rows AS $num => $row) { |
---|
142 | $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase); |
---|
143 | } |
---|
144 | |
---|
145 | return $rows; |
---|
146 | } |
---|
147 | |
---|
148 | protected function fixRow($row, $iterateRow, $fixCase) |
---|
149 | { |
---|
150 | if (!$row) { |
---|
151 | return $row; |
---|
152 | } |
---|
153 | |
---|
154 | if ($fixCase) { |
---|
155 | $row = array_change_key_case($row, $this->case); |
---|
156 | } |
---|
157 | |
---|
158 | if ($iterateRow) { |
---|
159 | foreach ($row AS $k => $v) { |
---|
160 | if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') { |
---|
161 | $row[$k] = null; |
---|
162 | } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) { |
---|
163 | $row[$k] = rtrim($v); |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | return $row; |
---|
168 | } |
---|
169 | |
---|
170 | public function fetchColumn($columnIndex = 0) |
---|
171 | { |
---|
172 | $value = $this->stmt->fetchColumn($columnIndex); |
---|
173 | |
---|
174 | if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) { |
---|
175 | if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') { |
---|
176 | $value = null; |
---|
177 | } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) { |
---|
178 | $value = rtrim($value); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | return $value; |
---|
183 | } |
---|
184 | |
---|
185 | public function rowCount() |
---|
186 | { |
---|
187 | return $this->stmt->rowCount(); |
---|
188 | } |
---|
189 | |
---|
190 | } |
---|