DB2Statement.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. class DB2Statement implements \Doctrine\DBAL\Driver\Statement
  23. {
  24. private $_stmt = null;
  25. private $_bindParam = array();
  26. /**
  27. * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG
  28. * @var <type>
  29. */
  30. static private $_typeMap = array(
  31. \PDO::PARAM_INT => DB2_LONG,
  32. \PDO::PARAM_STR => DB2_CHAR,
  33. );
  34. public function __construct($stmt)
  35. {
  36. $this->_stmt = $stmt;
  37. }
  38. /**
  39. * Binds a value to a corresponding named or positional
  40. * placeholder in the SQL statement that was used to prepare the statement.
  41. *
  42. * @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
  43. * this will be a parameter name of the form :name. For a prepared statement
  44. * using question mark placeholders, this will be the 1-indexed position of the parameter
  45. *
  46. * @param mixed $value The value to bind to the parameter.
  47. * @param integer $type Explicit data type for the parameter using the PDO::PARAM_* constants.
  48. *
  49. * @return boolean Returns TRUE on success or FALSE on failure.
  50. */
  51. function bindValue($param, $value, $type = null)
  52. {
  53. return $this->bindParam($param, $value, $type);
  54. }
  55. /**
  56. * Binds a PHP variable to a corresponding named or question mark placeholder in the
  57. * SQL statement that was use to prepare the statement. Unlike PDOStatement->bindValue(),
  58. * the variable is bound as a reference and will only be evaluated at the time
  59. * that PDOStatement->execute() is called.
  60. *
  61. * Most parameters are input parameters, that is, parameters that are
  62. * used in a read-only fashion to build up the query. Some drivers support the invocation
  63. * of stored procedures that return data as output parameters, and some also as input/output
  64. * parameters that both send in data and are updated to receive it.
  65. *
  66. * @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
  67. * this will be a parameter name of the form :name. For a prepared statement
  68. * using question mark placeholders, this will be the 1-indexed position of the parameter
  69. *
  70. * @param mixed $variable Name of the PHP variable to bind to the SQL statement parameter.
  71. *
  72. * @param integer $type Explicit data type for the parameter using the PDO::PARAM_* constants. To return
  73. * an INOUT parameter from a stored procedure, use the bitwise OR operator to set the
  74. * PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter.
  75. * @return boolean Returns TRUE on success or FALSE on failure.
  76. */
  77. function bindParam($column, &$variable, $type = null)
  78. {
  79. $this->_bindParam[$column] =& $variable;
  80. if ($type && isset(self::$_typeMap[$type])) {
  81. $type = self::$_typeMap[$type];
  82. } else {
  83. $type = DB2_CHAR;
  84. }
  85. if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) {
  86. throw new DB2Exception(db2_stmt_errormsg());
  87. }
  88. return true;
  89. }
  90. /**
  91. * Closes the cursor, enabling the statement to be executed again.
  92. *
  93. * @return boolean Returns TRUE on success or FALSE on failure.
  94. */
  95. function closeCursor()
  96. {
  97. if (!$this->_stmt) {
  98. return false;
  99. }
  100. $this->_bindParam = array();
  101. db2_free_result($this->_stmt);
  102. $ret = db2_free_stmt($this->_stmt);
  103. $this->_stmt = false;
  104. return $ret;
  105. }
  106. /**
  107. * columnCount
  108. * Returns the number of columns in the result set
  109. *
  110. * @return integer Returns the number of columns in the result set represented
  111. * by the PDOStatement object. If there is no result set,
  112. * this method should return 0.
  113. */
  114. function columnCount()
  115. {
  116. if (!$this->_stmt) {
  117. return false;
  118. }
  119. return db2_num_fields($this->_stmt);
  120. }
  121. /**
  122. * errorCode
  123. * Fetch the SQLSTATE associated with the last operation on the statement handle
  124. *
  125. * @see Doctrine_Adapter_Interface::errorCode()
  126. * @return string error code string
  127. */
  128. function errorCode()
  129. {
  130. return db2_stmt_error();
  131. }
  132. /**
  133. * errorInfo
  134. * Fetch extended error information associated with the last operation on the statement handle
  135. *
  136. * @see Doctrine_Adapter_Interface::errorInfo()
  137. * @return array error info array
  138. */
  139. function errorInfo()
  140. {
  141. return array(
  142. 0 => db2_stmt_errormsg(),
  143. 1 => db2_stmt_error(),
  144. );
  145. }
  146. /**
  147. * Executes a prepared statement
  148. *
  149. * If the prepared statement included parameter markers, you must either:
  150. * call PDOStatement->bindParam() to bind PHP variables to the parameter markers:
  151. * bound variables pass their value as input and receive the output value,
  152. * if any, of their associated parameter markers or pass an array of input-only
  153. * parameter values
  154. *
  155. *
  156. * @param array $params An array of values with as many elements as there are
  157. * bound parameters in the SQL statement being executed.
  158. * @return boolean Returns TRUE on success or FALSE on failure.
  159. */
  160. function execute($params = null)
  161. {
  162. if (!$this->_stmt) {
  163. return false;
  164. }
  165. /*$retval = true;
  166. if ($params !== null) {
  167. $retval = @db2_execute($this->_stmt, $params);
  168. } else {
  169. $retval = @db2_execute($this->_stmt);
  170. }*/
  171. if ($params === null) {
  172. ksort($this->_bindParam);
  173. $params = array_values($this->_bindParam);
  174. }
  175. $retval = @db2_execute($this->_stmt, $params);
  176. if ($retval === false) {
  177. throw new DB2Exception(db2_stmt_errormsg());
  178. }
  179. return $retval;
  180. }
  181. /**
  182. * fetch
  183. *
  184. * @see Query::HYDRATE_* constants
  185. * @param integer $fetchStyle Controls how the next row will be returned to the caller.
  186. * This value must be one of the Query::HYDRATE_* constants,
  187. * defaulting to Query::HYDRATE_BOTH
  188. *
  189. * @param integer $cursorOrientation For a PDOStatement object representing a scrollable cursor,
  190. * this value determines which row will be returned to the caller.
  191. * This value must be one of the Query::HYDRATE_ORI_* constants, defaulting to
  192. * Query::HYDRATE_ORI_NEXT. To request a scrollable cursor for your
  193. * PDOStatement object,
  194. * you must set the PDO::ATTR_CURSOR attribute to Doctrine::CURSOR_SCROLL when you
  195. * prepare the SQL statement with Doctrine_Adapter_Interface->prepare().
  196. *
  197. * @param integer $cursorOffset For a PDOStatement object representing a scrollable cursor for which the
  198. * $cursorOrientation parameter is set to Query::HYDRATE_ORI_ABS, this value specifies
  199. * the absolute number of the row in the result set that shall be fetched.
  200. *
  201. * For a PDOStatement object representing a scrollable cursor for
  202. * which the $cursorOrientation parameter is set to Query::HYDRATE_ORI_REL, this value
  203. * specifies the row to fetch relative to the cursor position before
  204. * PDOStatement->fetch() was called.
  205. *
  206. * @return mixed
  207. */
  208. function fetch($fetchStyle = \PDO::FETCH_BOTH)
  209. {
  210. switch ($fetchStyle) {
  211. case \PDO::FETCH_BOTH:
  212. return db2_fetch_both($this->_stmt);
  213. case \PDO::FETCH_ASSOC:
  214. return db2_fetch_assoc($this->_stmt);
  215. case \PDO::FETCH_NUM:
  216. return db2_fetch_array($this->_stmt);
  217. default:
  218. throw new DB2Exception("Given Fetch-Style " . $fetchStyle . " is not supported.");
  219. }
  220. }
  221. /**
  222. * Returns an array containing all of the result set rows
  223. *
  224. * @param integer $fetchStyle Controls how the next row will be returned to the caller.
  225. * This value must be one of the Query::HYDRATE_* constants,
  226. * defaulting to Query::HYDRATE_BOTH
  227. *
  228. * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is
  229. * Query::HYDRATE_COLUMN. Defaults to 0.
  230. *
  231. * @return array
  232. */
  233. function fetchAll($fetchStyle = \PDO::FETCH_BOTH)
  234. {
  235. $rows = array();
  236. while ($row = $this->fetch($fetchStyle)) {
  237. $rows[] = $row;
  238. }
  239. return $rows;
  240. }
  241. /**
  242. * fetchColumn
  243. * Returns a single column from the next row of a
  244. * result set or FALSE if there are no more rows.
  245. *
  246. * @param integer $columnIndex 0-indexed number of the column you wish to retrieve from the row. If no
  247. * value is supplied, PDOStatement->fetchColumn()
  248. * fetches the first column.
  249. *
  250. * @return string returns a single column in the next row of a result set.
  251. */
  252. function fetchColumn($columnIndex = 0)
  253. {
  254. $row = $this->fetch(\PDO::FETCH_NUM);
  255. if ($row && isset($row[$columnIndex])) {
  256. return $row[$columnIndex];
  257. }
  258. return false;
  259. }
  260. /**
  261. * rowCount
  262. * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
  263. * executed by the corresponding object.
  264. *
  265. * If the last SQL statement executed by the associated Statement object was a SELECT statement,
  266. * some databases may return the number of rows returned by that statement. However,
  267. * this behaviour is not guaranteed for all databases and should not be
  268. * relied on for portable applications.
  269. *
  270. * @return integer Returns the number of rows.
  271. */
  272. function rowCount()
  273. {
  274. return (@db2_num_rows($this->_stmt))?:0;
  275. }
  276. }