DB2Statement.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Driver\IBMDB2;
  22. use \Doctrine\DBAL\Driver\Statement;
  23. class DB2Statement implements \IteratorAggregate, Statement
  24. {
  25. private $_stmt = null;
  26. private $_bindParam = array();
  27. private $_defaultFetchStyle = \PDO::FETCH_BOTH;
  28. /**
  29. * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG
  30. * @var array
  31. */
  32. static private $_typeMap = array(
  33. \PDO::PARAM_INT => DB2_LONG,
  34. \PDO::PARAM_STR => DB2_CHAR,
  35. );
  36. public function __construct($stmt)
  37. {
  38. $this->_stmt = $stmt;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function bindValue($param, $value, $type = null)
  44. {
  45. return $this->bindParam($param, $value, $type);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function bindParam($column, &$variable, $type = null)
  51. {
  52. $this->_bindParam[$column] =& $variable;
  53. if ($type && isset(self::$_typeMap[$type])) {
  54. $type = self::$_typeMap[$type];
  55. } else {
  56. $type = DB2_CHAR;
  57. }
  58. if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) {
  59. throw new DB2Exception(db2_stmt_errormsg());
  60. }
  61. return true;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function closeCursor()
  67. {
  68. if (!$this->_stmt) {
  69. return false;
  70. }
  71. $this->_bindParam = array();
  72. db2_free_result($this->_stmt);
  73. $ret = db2_free_stmt($this->_stmt);
  74. $this->_stmt = false;
  75. return $ret;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function columnCount()
  81. {
  82. if (!$this->_stmt) {
  83. return false;
  84. }
  85. return db2_num_fields($this->_stmt);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function errorCode()
  91. {
  92. return db2_stmt_error();
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function errorInfo()
  98. {
  99. return array(
  100. 0 => db2_stmt_errormsg(),
  101. 1 => db2_stmt_error(),
  102. );
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function execute($params = null)
  108. {
  109. if (!$this->_stmt) {
  110. return false;
  111. }
  112. /*$retval = true;
  113. if ($params !== null) {
  114. $retval = @db2_execute($this->_stmt, $params);
  115. } else {
  116. $retval = @db2_execute($this->_stmt);
  117. }*/
  118. if ($params === null) {
  119. ksort($this->_bindParam);
  120. $params = array_values($this->_bindParam);
  121. }
  122. $retval = @db2_execute($this->_stmt, $params);
  123. if ($retval === false) {
  124. throw new DB2Exception(db2_stmt_errormsg());
  125. }
  126. return $retval;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function setFetchMode($fetchStyle = \PDO::FETCH_BOTH)
  132. {
  133. $this->_defaultFetchStyle = $fetchStyle;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function getIterator()
  139. {
  140. $data = $this->fetchAll($this->_defaultFetchStyle);
  141. return new \ArrayIterator($data);
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function fetch($fetchStyle = null)
  147. {
  148. $fetchStyle = $fetchStyle ?: $this->_defaultFetchStyle;
  149. switch ($fetchStyle) {
  150. case \PDO::FETCH_BOTH:
  151. return db2_fetch_both($this->_stmt);
  152. case \PDO::FETCH_ASSOC:
  153. return db2_fetch_assoc($this->_stmt);
  154. case \PDO::FETCH_NUM:
  155. return db2_fetch_array($this->_stmt);
  156. default:
  157. throw new DB2Exception("Given Fetch-Style " . $fetchStyle . " is not supported.");
  158. }
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function fetchAll($fetchStyle = null)
  164. {
  165. $fetchStyle = $fetchStyle ?: $this->_defaultFetchStyle;
  166. $rows = array();
  167. while ($row = $this->fetch($fetchStyle)) {
  168. $rows[] = $row;
  169. }
  170. return $rows;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function fetchColumn($columnIndex = 0)
  176. {
  177. $row = $this->fetch(\PDO::FETCH_NUM);
  178. if ($row && isset($row[$columnIndex])) {
  179. return $row[$columnIndex];
  180. }
  181. return false;
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function rowCount()
  187. {
  188. return (@db2_num_rows($this->_stmt))?:0;
  189. }
  190. }