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.
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>.
22 namespace Doctrine\DBAL\Driver\IBMDB2;
24 use \Doctrine\DBAL\Driver\Statement;
26 class DB2Statement implements \IteratorAggregate, Statement
28 private $_stmt = null;
30 private $_bindParam = array();
32 private $_defaultFetchMode = \PDO::FETCH_BOTH;
35 * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG
38 static private $_typeMap = array(
39 \PDO::PARAM_INT => DB2_LONG,
40 \PDO::PARAM_STR => DB2_CHAR,
43 public function __construct($stmt)
51 public function bindValue($param, $value, $type = null)
53 return $this->bindParam($param, $value, $type);
59 public function bindParam($column, &$variable, $type = null, $length = null)
61 $this->_bindParam[$column] =& $variable;
63 if ($type && isset(self::$_typeMap[$type])) {
64 $type = self::$_typeMap[$type];
69 if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) {
70 throw new DB2Exception(db2_stmt_errormsg());
78 public function closeCursor()
80 if ( ! $this->_stmt) {
84 $this->_bindParam = array();
85 db2_free_result($this->_stmt);
86 $ret = db2_free_stmt($this->_stmt);
94 public function columnCount()
96 if ( ! $this->_stmt) {
99 return db2_num_fields($this->_stmt);
105 public function errorCode()
107 return db2_stmt_error();
113 public function errorInfo()
116 0 => db2_stmt_errormsg(),
117 1 => db2_stmt_error(),
124 public function execute($params = null)
126 if ( ! $this->_stmt) {
131 if ($params !== null) {
132 $retval = @db2_execute($this->_stmt, $params);
134 $retval = @db2_execute($this->_stmt);
136 if ($params === null) {
137 ksort($this->_bindParam);
138 $params = array_values($this->_bindParam);
140 $retval = @db2_execute($this->_stmt, $params);
142 if ($retval === false) {
143 throw new DB2Exception(db2_stmt_errormsg());
151 public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
153 $this->_defaultFetchMode = $fetchMode;
159 public function getIterator()
161 $data = $this->fetchAll();
162 return new \ArrayIterator($data);
168 public function fetch($fetchMode = null)
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);
179 throw new DB2Exception("Given Fetch-Style " . $fetchMode . " is not supported.");
186 public function fetchAll($fetchMode = null)
189 while ($row = $this->fetch($fetchMode)) {
198 public function fetchColumn($columnIndex = 0)
200 $row = $this->fetch(\PDO::FETCH_NUM);
201 if ($row && isset($row[$columnIndex])) {
202 return $row[$columnIndex];
210 public function rowCount()
212 return (@db2_num_rows($this->_stmt))?:0;