DB2Statement.php 5.3 KB

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