|
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 \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 * Wraps <tt>Statement</tt> and applies portability measures |
|
53 * |
|
54 * @param Doctrine\DBAL\Driver\Statement $stmt |
|
55 * @param Doctrine\DBAL\Connection $conn |
|
56 */ |
|
57 public function __construct($stmt, Connection $conn) |
|
58 { |
|
59 $this->stmt = $stmt; |
|
60 $this->portability = $conn->getPortability(); |
|
61 $this->case = $conn->getFetchCase(); |
|
62 } |
|
63 |
|
64 public function bindParam($column, &$variable, $type = null) |
|
65 { |
|
66 return $this->stmt->bindParam($column, $variable, $type); |
|
67 } |
|
68 |
|
69 public function bindValue($param, $value, $type = null) |
|
70 { |
|
71 return $this->stmt->bindValue($param, $value, $type); |
|
72 } |
|
73 |
|
74 public function closeCursor() |
|
75 { |
|
76 return $this->stmt->closeCursor(); |
|
77 } |
|
78 |
|
79 public function columnCount() |
|
80 { |
|
81 return $this->stmt->columnCount(); |
|
82 } |
|
83 |
|
84 public function errorCode() |
|
85 { |
|
86 return $this->stmt->errorCode(); |
|
87 } |
|
88 |
|
89 public function errorInfo() |
|
90 { |
|
91 return $this->stmt->errorInfo(); |
|
92 } |
|
93 |
|
94 public function execute($params = null) |
|
95 { |
|
96 return $this->stmt->execute($params); |
|
97 } |
|
98 |
|
99 public function fetch($fetchStyle = PDO::FETCH_BOTH) |
|
100 { |
|
101 $row = $this->stmt->fetch($fetchStyle); |
|
102 |
|
103 $row = $this->fixRow($row, |
|
104 $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM), |
|
105 ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE) |
|
106 ); |
|
107 |
|
108 return $row; |
|
109 } |
|
110 |
|
111 public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0) |
|
112 { |
|
113 if ($columnIndex != 0) { |
|
114 $rows = $this->stmt->fetchAll($fetchStyle, $columnIndex); |
|
115 } else { |
|
116 $rows = $this->stmt->fetchAll($fetchStyle); |
|
117 } |
|
118 |
|
119 $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); |
|
120 $fixCase = ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE); |
|
121 if (!$iterateRow && !$fixCase) { |
|
122 return $rows; |
|
123 } |
|
124 |
|
125 foreach ($rows AS $num => $row) { |
|
126 $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase); |
|
127 } |
|
128 |
|
129 return $rows; |
|
130 } |
|
131 |
|
132 protected function fixRow($row, $iterateRow, $fixCase) |
|
133 { |
|
134 if (!$row) { |
|
135 return $row; |
|
136 } |
|
137 |
|
138 if ($fixCase) { |
|
139 $row = array_change_key_case($row, $this->case); |
|
140 } |
|
141 |
|
142 if ($iterateRow) { |
|
143 foreach ($row AS $k => $v) { |
|
144 if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') { |
|
145 $row[$k] = null; |
|
146 } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) { |
|
147 $row[$k] = rtrim($v); |
|
148 } |
|
149 } |
|
150 } |
|
151 return $row; |
|
152 } |
|
153 |
|
154 public function fetchColumn($columnIndex = 0) |
|
155 { |
|
156 $value = $this->stmt->fetchColumn($columnIndex); |
|
157 |
|
158 if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) { |
|
159 if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') { |
|
160 $value = null; |
|
161 } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) { |
|
162 $value = rtrim($value); |
|
163 } |
|
164 } |
|
165 |
|
166 return $value; |
|
167 } |
|
168 |
|
169 public function rowCount() |
|
170 { |
|
171 return $this->stmt->rowCount(); |
|
172 } |
|
173 |
|
174 } |