Statement.php 7.4 KB

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