Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Portability / Statement.php
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 MIT license. 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 $defaultFetchMode = 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,$length = 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($fetchMode, $arg1 = null, $arg2 = null)
105     {
106         $this->defaultFetchMode = $fetchMode;
107         $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
108     }
109
110     public function getIterator()
111     {
112         $data = $this->fetchAll();
113         return new \ArrayIterator($data);
114     }
115
116     public function fetch($fetchMode = null)
117     {
118         $fetchMode = $fetchMode ?: $this->defaultFetchMode;
119
120         $row = $this->stmt->fetch($fetchMode);
121
122         $row = $this->fixRow($row,
123             $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM),
124             !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE)
125         );
126
127         return $row;
128     }
129
130     public function fetchAll($fetchMode = null, $columnIndex = 0)
131     {
132         $fetchMode = $fetchMode ?: $this->defaultFetchMode;
133
134         if ($columnIndex != 0) {
135             $rows = $this->stmt->fetchAll($fetchMode, $columnIndex);
136         } else {
137             $rows = $this->stmt->fetchAll($fetchMode);
138         }
139
140         $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
141         $fixCase = !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE);
142         if ( ! $iterateRow && !$fixCase) {
143             return $rows;
144         }
145
146         foreach ($rows as $num => $row) {
147             $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
148         }
149
150         return $rows;
151     }
152
153     protected function fixRow($row, $iterateRow, $fixCase)
154     {
155         if ( ! $row) {
156             return $row;
157         }
158
159         if ($fixCase) {
160             $row = array_change_key_case($row, $this->case);
161         }
162
163         if ($iterateRow) {
164             foreach ($row as $k => $v) {
165                 if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
166                     $row[$k] = null;
167                 } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
168                     $row[$k] = rtrim($v);
169                 }
170             }
171         }
172         return $row;
173     }
174
175     public function fetchColumn($columnIndex = 0)
176     {
177         $value = $this->stmt->fetchColumn($columnIndex);
178
179         if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
180             if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
181                 $value = null;
182             } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
183                 $value = rtrim($value);
184             }
185         }
186
187         return $value;
188     }
189
190     public function rowCount()
191     {
192         return $this->stmt->rowCount();
193     }
194
195 }