Statement.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. $this->params[$name] = $var;
  130. $this->types[$name] = $type;
  131. return $this->stmt->bindParam($name, $var, $type, $length);
  132. }
  133. /**
  134. * Executes the statement with the currently bound parameters.
  135. *
  136. * @param array|null $params
  137. *
  138. * @return boolean TRUE on success, FALSE on failure.
  139. *
  140. * @throws \Doctrine\DBAL\DBALException
  141. */
  142. public function execute($params = null)
  143. {
  144. if (is_array($params)) {
  145. $this->params = $params;
  146. }
  147. $logger = $this->conn->getConfiguration()->getSQLLogger();
  148. if ($logger) {
  149. $logger->startQuery($this->sql, $this->params, $this->types);
  150. }
  151. try {
  152. $stmt = $this->stmt->execute($params);
  153. } catch (\Exception $ex) {
  154. if ($logger) {
  155. $logger->stopQuery();
  156. }
  157. throw DBALException::driverExceptionDuringQuery(
  158. $this->conn->getDriver(),
  159. $ex,
  160. $this->sql,
  161. $this->conn->resolveParams($this->params, $this->types)
  162. );
  163. }
  164. if ($logger) {
  165. $logger->stopQuery();
  166. }
  167. $this->params = array();
  168. $this->types = array();
  169. return $stmt;
  170. }
  171. /**
  172. * Closes the cursor, freeing the database resources used by this statement.
  173. *
  174. * @return boolean TRUE on success, FALSE on failure.
  175. */
  176. public function closeCursor()
  177. {
  178. return $this->stmt->closeCursor();
  179. }
  180. /**
  181. * Returns the number of columns in the result set.
  182. *
  183. * @return integer
  184. */
  185. public function columnCount()
  186. {
  187. return $this->stmt->columnCount();
  188. }
  189. /**
  190. * Fetches the SQLSTATE associated with the last operation on the statement.
  191. *
  192. * @return string
  193. */
  194. public function errorCode()
  195. {
  196. return $this->stmt->errorCode();
  197. }
  198. /**
  199. * Fetches extended error information associated with the last operation on the statement.
  200. *
  201. * @return array
  202. */
  203. public function errorInfo()
  204. {
  205. return $this->stmt->errorInfo();
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  211. {
  212. if ($arg2 === null) {
  213. return $this->stmt->setFetchMode($fetchMode);
  214. } elseif ($arg3 === null) {
  215. return $this->stmt->setFetchMode($fetchMode, $arg2);
  216. }
  217. return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
  218. }
  219. /**
  220. * Required by interface IteratorAggregate.
  221. *
  222. * {@inheritdoc}
  223. */
  224. public function getIterator()
  225. {
  226. return $this->stmt;
  227. }
  228. /**
  229. * Fetches the next row from a result set.
  230. *
  231. * @param integer|null $fetchMode
  232. *
  233. * @return mixed The return value of this function on success depends on the fetch type.
  234. * In all cases, FALSE is returned on failure.
  235. */
  236. public function fetch($fetchMode = null)
  237. {
  238. return $this->stmt->fetch($fetchMode);
  239. }
  240. /**
  241. * Returns an array containing all of the result set rows.
  242. *
  243. * @param integer|null $fetchMode
  244. * @param mixed $fetchArgument
  245. *
  246. * @return array An array containing all of the remaining rows in the result set.
  247. */
  248. public function fetchAll($fetchMode = null, $fetchArgument = 0)
  249. {
  250. if ($fetchArgument !== 0) {
  251. return $this->stmt->fetchAll($fetchMode, $fetchArgument);
  252. }
  253. return $this->stmt->fetchAll($fetchMode);
  254. }
  255. /**
  256. * Returns a single column from the next row of a result set.
  257. *
  258. * @param integer $columnIndex
  259. *
  260. * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
  261. */
  262. public function fetchColumn($columnIndex = 0)
  263. {
  264. return $this->stmt->fetchColumn($columnIndex);
  265. }
  266. /**
  267. * Returns the number of rows affected by the last execution of this statement.
  268. *
  269. * @return integer The number of affected rows.
  270. */
  271. public function rowCount()
  272. {
  273. return $this->stmt->rowCount();
  274. }
  275. /**
  276. * Gets the wrapped driver statement.
  277. *
  278. * @return \Doctrine\DBAL\Driver\Statement
  279. */
  280. public function getWrappedStatement()
  281. {
  282. return $this->stmt;
  283. }
  284. }