Statement.php 8.6 KB

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