Statement.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. namespace Doctrine\DBAL;
  22. use PDO,
  23. Doctrine\DBAL\Types\Type,
  24. Doctrine\DBAL\Driver\Statement as DriverStatement;
  25. /**
  26. * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
  27. * for logging, DBAL mapping types, etc.
  28. *
  29. * @author Roman Borschel <roman@code-factory.org>
  30. * @since 2.0
  31. */
  32. class Statement implements \IteratorAggregate, DriverStatement
  33. {
  34. /**
  35. * @var string The SQL statement.
  36. */
  37. protected $sql;
  38. /**
  39. * @var array The bound parameters.
  40. */
  41. protected $params = array();
  42. /**
  43. * @var array The parameter types
  44. */
  45. protected $types = array();
  46. /**
  47. * @var \Doctrine\DBAL\Driver\Statement The underlying driver statement.
  48. */
  49. protected $stmt;
  50. /**
  51. * @var \Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform.
  52. */
  53. protected $platform;
  54. /**
  55. * @var \Doctrine\DBAL\Connection The connection this statement is bound to and executed on.
  56. */
  57. protected $conn;
  58. /**
  59. * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
  60. *
  61. * @param string $sql The SQL of the statement.
  62. * @param \Doctrine\DBAL\Connection The connection on which the statement should be executed.
  63. */
  64. public function __construct($sql, Connection $conn)
  65. {
  66. $this->sql = $sql;
  67. $this->stmt = $conn->getWrappedConnection()->prepare($sql);
  68. $this->conn = $conn;
  69. $this->platform = $conn->getDatabasePlatform();
  70. }
  71. /**
  72. * Binds a parameter value to the statement.
  73. *
  74. * The value can optionally be bound with a PDO binding type or a DBAL mapping type.
  75. * If bound with a DBAL mapping type, the binding type is derived from the mapping
  76. * type and the value undergoes the conversion routines of the mapping type before
  77. * being bound.
  78. *
  79. * @param string $name The name or position of the parameter.
  80. * @param mixed $value The value of the parameter.
  81. * @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance.
  82. * @return boolean TRUE on success, FALSE on failure.
  83. */
  84. public function bindValue($name, $value, $type = null)
  85. {
  86. $this->params[$name] = $value;
  87. $this->types[$name] = $type;
  88. if ($type !== null) {
  89. if (is_string($type)) {
  90. $type = Type::getType($type);
  91. }
  92. if ($type instanceof Type) {
  93. $value = $type->convertToDatabaseValue($value, $this->platform);
  94. $bindingType = $type->getBindingType();
  95. } else {
  96. $bindingType = $type; // PDO::PARAM_* constants
  97. }
  98. return $this->stmt->bindValue($name, $value, $bindingType);
  99. } else {
  100. return $this->stmt->bindValue($name, $value);
  101. }
  102. }
  103. /**
  104. * Binds a parameter to a value by reference.
  105. *
  106. * Binding a parameter by reference does not support DBAL mapping types.
  107. *
  108. * @param string $name The name or position of the parameter.
  109. * @param mixed $var The reference to the variable to bind
  110. * @param integer $type The PDO binding type.
  111. * @return boolean TRUE on success, FALSE on failure.
  112. */
  113. public function bindParam($name, &$var, $type = PDO::PARAM_STR, $length = null)
  114. {
  115. return $this->stmt->bindParam($name, $var, $type, $length );
  116. }
  117. /**
  118. * Executes the statement with the currently bound parameters.
  119. *
  120. * @param array $params
  121. * @return boolean TRUE on success, FALSE on failure.
  122. */
  123. public function execute($params = null)
  124. {
  125. $logger = $this->conn->getConfiguration()->getSQLLogger();
  126. if ($logger) {
  127. $logger->startQuery($this->sql, $this->params, $this->types);
  128. }
  129. try {
  130. $stmt = $this->stmt->execute($params);
  131. } catch (\Exception $ex) {
  132. throw DBALException::driverExceptionDuringQuery($ex, $this->sql, $this->conn->resolveParams($this->params, $this->types));
  133. }
  134. if ($logger) {
  135. $logger->stopQuery();
  136. }
  137. $this->params = array();
  138. $this->types = array();
  139. return $stmt;
  140. }
  141. /**
  142. * Closes the cursor, freeing the database resources used by this statement.
  143. *
  144. * @return boolean TRUE on success, FALSE on failure.
  145. */
  146. public function closeCursor()
  147. {
  148. return $this->stmt->closeCursor();
  149. }
  150. /**
  151. * Returns the number of columns in the result set.
  152. *
  153. * @return integer
  154. */
  155. public function columnCount()
  156. {
  157. return $this->stmt->columnCount();
  158. }
  159. /**
  160. * Fetches the SQLSTATE associated with the last operation on the statement.
  161. *
  162. * @return string
  163. */
  164. public function errorCode()
  165. {
  166. return $this->stmt->errorCode();
  167. }
  168. /**
  169. * Fetches extended error information associated with the last operation on the statement.
  170. *
  171. * @return array
  172. */
  173. public function errorInfo()
  174. {
  175. return $this->stmt->errorInfo();
  176. }
  177. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  178. {
  179. if ($arg2 === null) {
  180. return $this->stmt->setFetchMode($fetchMode);
  181. } else if ($arg3 === null) {
  182. return $this->stmt->setFetchMode($fetchMode, $arg2);
  183. }
  184. return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
  185. }
  186. public function getIterator()
  187. {
  188. return $this->stmt;
  189. }
  190. /**
  191. * Fetches the next row from a result set.
  192. *
  193. * @param integer $fetchMode
  194. * @return mixed The return value of this function on success depends on the fetch type.
  195. * In all cases, FALSE is returned on failure.
  196. */
  197. public function fetch($fetchMode = null)
  198. {
  199. return $this->stmt->fetch($fetchMode);
  200. }
  201. /**
  202. * Returns an array containing all of the result set rows.
  203. *
  204. * @param integer $fetchMode
  205. * @param mixed $fetchArgument
  206. * @return array An array containing all of the remaining rows in the result set.
  207. */
  208. public function fetchAll($fetchMode = null, $fetchArgument = 0)
  209. {
  210. if ($fetchArgument !== 0) {
  211. return $this->stmt->fetchAll($fetchMode, $fetchArgument);
  212. }
  213. return $this->stmt->fetchAll($fetchMode);
  214. }
  215. /**
  216. * Returns a single column from the next row of a result set.
  217. *
  218. * @param integer $columnIndex
  219. * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
  220. */
  221. public function fetchColumn($columnIndex = 0)
  222. {
  223. return $this->stmt->fetchColumn($columnIndex);
  224. }
  225. /**
  226. * Returns the number of rows affected by the last execution of this statement.
  227. *
  228. * @return integer The number of affected rows.
  229. */
  230. public function rowCount()
  231. {
  232. return $this->stmt->rowCount();
  233. }
  234. /**
  235. * Gets the wrapped driver statement.
  236. *
  237. * @return \Doctrine\DBAL\Driver\Statement
  238. */
  239. public function getWrappedStatement()
  240. {
  241. return $this->stmt;
  242. }
  243. }