Statement.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 DriverStatement
  33. {
  34. /**
  35. * @var string The SQL statement.
  36. */
  37. private $_sql;
  38. /**
  39. * @var array The bound parameters.
  40. */
  41. private $_params = array();
  42. /**
  43. * @var Doctrine\DBAL\Driver\Statement The underlying driver statement.
  44. */
  45. private $_stmt;
  46. /**
  47. * @var Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform.
  48. */
  49. private $_platform;
  50. /**
  51. * @var Doctrine\DBAL\Connection The connection this statement is bound to and executed on.
  52. */
  53. private $_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. /**
  167. * Fetches the next row from a result set.
  168. *
  169. * @param integer $fetchStyle
  170. * @return mixed The return value of this function on success depends on the fetch type.
  171. * In all cases, FALSE is returned on failure.
  172. */
  173. public function fetch($fetchStyle = PDO::FETCH_BOTH)
  174. {
  175. return $this->_stmt->fetch($fetchStyle);
  176. }
  177. /**
  178. * Returns an array containing all of the result set rows.
  179. *
  180. * @param integer $fetchStyle
  181. * @param integer $columnIndex
  182. * @return array An array containing all of the remaining rows in the result set.
  183. */
  184. public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
  185. {
  186. if ($columnIndex != 0) {
  187. return $this->_stmt->fetchAll($fetchStyle, $columnIndex);
  188. }
  189. return $this->_stmt->fetchAll($fetchStyle);
  190. }
  191. /**
  192. * Returns a single column from the next row of a result set.
  193. *
  194. * @param integer $columnIndex
  195. * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
  196. */
  197. public function fetchColumn($columnIndex = 0)
  198. {
  199. return $this->_stmt->fetchColumn($columnIndex);
  200. }
  201. /**
  202. * Returns the number of rows affected by the last execution of this statement.
  203. *
  204. * @return integer The number of affected rows.
  205. */
  206. public function rowCount()
  207. {
  208. return $this->_stmt->rowCount();
  209. }
  210. /**
  211. * Gets the wrapped driver statement.
  212. *
  213. * @return Doctrine\DBAL\Driver\Statement
  214. */
  215. public function getWrappedStatement()
  216. {
  217. return $this->_stmt;
  218. }
  219. }