PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Doctrine/DBAL/Driver/PDOStatement.php

http://github.com/doctrine/dbal
PHP | 234 lines | 149 code | 30 blank | 55 comment | 12 complexity | 0dfefb07a3c83f105b76450889f9fe95 MD5 | raw file
Possible License(s): Unlicense
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\FetchMode;
  4. use Doctrine\DBAL\ParameterType;
  5. use PDO;
  6. use function array_slice;
  7. use function assert;
  8. use function func_get_args;
  9. use function is_array;
  10. use function sprintf;
  11. use function trigger_error;
  12. use const E_USER_DEPRECATED;
  13. /**
  14. * The PDO implementation of the Statement interface.
  15. * Used by all PDO-based drivers.
  16. */
  17. class PDOStatement extends \PDOStatement implements Statement
  18. {
  19. private const PARAM_TYPE_MAP = [
  20. ParameterType::NULL => PDO::PARAM_NULL,
  21. ParameterType::INTEGER => PDO::PARAM_INT,
  22. ParameterType::STRING => PDO::PARAM_STR,
  23. ParameterType::BINARY => PDO::PARAM_LOB,
  24. ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
  25. ParameterType::BOOLEAN => PDO::PARAM_BOOL,
  26. ];
  27. private const FETCH_MODE_MAP = [
  28. FetchMode::ASSOCIATIVE => PDO::FETCH_ASSOC,
  29. FetchMode::NUMERIC => PDO::FETCH_NUM,
  30. FetchMode::MIXED => PDO::FETCH_BOTH,
  31. FetchMode::STANDARD_OBJECT => PDO::FETCH_OBJ,
  32. FetchMode::COLUMN => PDO::FETCH_COLUMN,
  33. FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS,
  34. ];
  35. /**
  36. * Protected constructor.
  37. */
  38. protected function __construct()
  39. {
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  45. {
  46. $fetchMode = $this->convertFetchMode($fetchMode);
  47. // This thin wrapper is necessary to shield against the weird signature
  48. // of PDOStatement::setFetchMode(): even if the second and third
  49. // parameters are optional, PHP will not let us remove it from this
  50. // declaration.
  51. try {
  52. if ($arg2 === null && $arg3 === null) {
  53. return parent::setFetchMode($fetchMode);
  54. }
  55. if ($arg3 === null) {
  56. return parent::setFetchMode($fetchMode, $arg2);
  57. }
  58. return parent::setFetchMode($fetchMode, $arg2, $arg3);
  59. } catch (\PDOException $exception) {
  60. throw new PDOException($exception);
  61. }
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function bindValue($param, $value, $type = ParameterType::STRING)
  67. {
  68. $type = $this->convertParamType($type);
  69. try {
  70. return parent::bindValue($param, $value, $type);
  71. } catch (\PDOException $exception) {
  72. throw new PDOException($exception);
  73. }
  74. }
  75. /**
  76. * @param mixed $column
  77. * @param mixed $variable
  78. * @param int $type
  79. * @param int|null $length
  80. * @param mixed $driverOptions
  81. *
  82. * @return bool
  83. */
  84. public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
  85. {
  86. $type = $this->convertParamType($type);
  87. try {
  88. return parent::bindParam($column, $variable, $type, ...array_slice(func_get_args(), 3));
  89. } catch (\PDOException $exception) {
  90. throw new PDOException($exception);
  91. }
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function closeCursor()
  97. {
  98. try {
  99. return parent::closeCursor();
  100. } catch (\PDOException $exception) {
  101. // Exceptions not allowed by the interface.
  102. // In case driver implementations do not adhere to the interface, silence exceptions here.
  103. return true;
  104. }
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function execute($params = null)
  110. {
  111. try {
  112. return parent::execute($params);
  113. } catch (\PDOException $exception) {
  114. throw new PDOException($exception);
  115. }
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
  121. {
  122. $args = func_get_args();
  123. if (isset($args[0])) {
  124. $args[0] = $this->convertFetchMode($args[0]);
  125. }
  126. try {
  127. return parent::fetch(...$args);
  128. } catch (\PDOException $exception) {
  129. throw new PDOException($exception);
  130. }
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
  136. {
  137. $args = func_get_args();
  138. if (isset($args[0])) {
  139. $args[0] = $this->convertFetchMode($args[0]);
  140. }
  141. if ($fetchMode === null && $fetchArgument === null && $ctorArgs === null) {
  142. $args = [];
  143. } elseif ($fetchArgument === null && $ctorArgs === null) {
  144. $args = [$fetchMode];
  145. } elseif ($ctorArgs === null) {
  146. $args = [$fetchMode, $fetchArgument];
  147. } else {
  148. $args = [$fetchMode, $fetchArgument, $ctorArgs];
  149. }
  150. try {
  151. $data = parent::fetchAll(...$args);
  152. assert(is_array($data));
  153. return $data;
  154. } catch (\PDOException $exception) {
  155. throw new PDOException($exception);
  156. }
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function fetchColumn($columnIndex = 0)
  162. {
  163. try {
  164. return parent::fetchColumn($columnIndex);
  165. } catch (\PDOException $exception) {
  166. throw new PDOException($exception);
  167. }
  168. }
  169. /**
  170. * Converts DBAL parameter type to PDO parameter type
  171. *
  172. * @param int $type Parameter type
  173. */
  174. private function convertParamType(int $type) : int
  175. {
  176. if (! isset(self::PARAM_TYPE_MAP[$type])) {
  177. // TODO: next major: throw an exception
  178. @trigger_error(sprintf(
  179. 'Using a PDO parameter type (%d given) is deprecated and will cause an error in Doctrine DBAL 3.0',
  180. $type
  181. ), E_USER_DEPRECATED);
  182. return $type;
  183. }
  184. return self::PARAM_TYPE_MAP[$type];
  185. }
  186. /**
  187. * Converts DBAL fetch mode to PDO fetch mode
  188. *
  189. * @param int $fetchMode Fetch mode
  190. */
  191. private function convertFetchMode(int $fetchMode) : int
  192. {
  193. if (! isset(self::FETCH_MODE_MAP[$fetchMode])) {
  194. // TODO: next major: throw an exception
  195. @trigger_error(sprintf(
  196. 'Using a PDO fetch mode or their combination (%d given)' .
  197. ' is deprecated and will cause an error in Doctrine DBAL 3.0',
  198. $fetchMode
  199. ), E_USER_DEPRECATED);
  200. return $fetchMode;
  201. }
  202. return self::FETCH_MODE_MAP[$fetchMode];
  203. }
  204. }