Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / IBMDB2 / DB2Statement.php
1 <?php
2 /*
3  *  $Id$
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the MIT license. For more information, see
19  * <http://www.doctrine-project.org>.
20 */
21
22 namespace Doctrine\DBAL\Driver\IBMDB2;
23
24 use \Doctrine\DBAL\Driver\Statement;
25
26 class DB2Statement implements \IteratorAggregate, Statement
27 {
28     private $_stmt = null;
29
30     private $_bindParam = array();
31
32     private $_defaultFetchMode = \PDO::FETCH_BOTH;
33
34     /**
35      * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG
36      * @var array
37      */
38     static private $_typeMap = array(
39         \PDO::PARAM_INT => DB2_LONG,
40         \PDO::PARAM_STR => DB2_CHAR,
41     );
42
43     public function __construct($stmt)
44     {
45         $this->_stmt = $stmt;
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function bindValue($param, $value, $type = null)
52     {
53         return $this->bindParam($param, $value, $type);
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     public function bindParam($column, &$variable, $type = null, $length = null)
60     {
61         $this->_bindParam[$column] =& $variable;
62
63         if ($type && isset(self::$_typeMap[$type])) {
64             $type = self::$_typeMap[$type];
65         } else {
66             $type = DB2_CHAR;
67         }
68
69         if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) {
70             throw new DB2Exception(db2_stmt_errormsg());
71         }
72         return true;
73     }
74
75     /**
76      * {@inheritdoc}
77      */
78     public function closeCursor()
79     {
80         if ( ! $this->_stmt) {
81             return false;
82         }
83
84         $this->_bindParam = array();
85         db2_free_result($this->_stmt);
86         $ret = db2_free_stmt($this->_stmt);
87         $this->_stmt = false;
88         return $ret;
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     public function columnCount()
95     {
96         if ( ! $this->_stmt) {
97             return false;
98         }
99         return db2_num_fields($this->_stmt);
100     }
101
102     /**
103      * {@inheritdoc}
104      */
105     public function errorCode()
106     {
107         return db2_stmt_error();
108     }
109
110     /**
111      * {@inheritdoc}
112      */
113     public function errorInfo()
114     {
115         return array(
116             0 => db2_stmt_errormsg(),
117             1 => db2_stmt_error(),
118         );
119     }
120
121     /**
122      * {@inheritdoc}
123      */
124     public function execute($params = null)
125     {
126         if ( ! $this->_stmt) {
127             return false;
128         }
129
130         /*$retval = true;
131         if ($params !== null) {
132             $retval = @db2_execute($this->_stmt, $params);
133         } else {
134             $retval = @db2_execute($this->_stmt);
135         }*/
136         if ($params === null) {
137             ksort($this->_bindParam);
138             $params = array_values($this->_bindParam);
139         }
140         $retval = @db2_execute($this->_stmt, $params);
141
142         if ($retval === false) {
143             throw new DB2Exception(db2_stmt_errormsg());
144         }
145         return $retval;
146     }
147
148     /**
149      * {@inheritdoc}
150      */
151     public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
152     {
153         $this->_defaultFetchMode = $fetchMode;
154     }
155
156     /**
157      * {@inheritdoc}
158      */
159     public function getIterator()
160     {
161         $data = $this->fetchAll();
162         return new \ArrayIterator($data);
163     }
164
165     /**
166      * {@inheritdoc}
167      */
168     public function fetch($fetchMode = null)
169     {
170         $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
171         switch ($fetchMode) {
172             case \PDO::FETCH_BOTH:
173                 return db2_fetch_both($this->_stmt);
174             case \PDO::FETCH_ASSOC:
175                 return db2_fetch_assoc($this->_stmt);
176             case \PDO::FETCH_NUM:
177                 return db2_fetch_array($this->_stmt);
178             default:
179                 throw new DB2Exception("Given Fetch-Style " . $fetchMode . " is not supported.");
180         }
181     }
182
183     /**
184      * {@inheritdoc}
185      */
186     public function fetchAll($fetchMode = null)
187     {
188         $rows = array();
189         while ($row = $this->fetch($fetchMode)) {
190             $rows[] = $row;
191         }
192         return $rows;
193     }
194
195     /**
196      * {@inheritdoc}
197      */
198     public function fetchColumn($columnIndex = 0)
199     {
200         $row = $this->fetch(\PDO::FETCH_NUM);
201         if ($row && isset($row[$columnIndex])) {
202             return $row[$columnIndex];
203         }
204         return false;
205     }
206
207     /**
208      * {@inheritdoc}
209      */
210     public function rowCount()
211     {
212         return (@db2_num_rows($this->_stmt))?:0;
213     }
214 }