PageRenderTime 80ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mailpoet/vendor-prefixed/doctrine/dbal/lib/Doctrine/DBAL/Statement.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 342 lines | 339 code | 0 blank | 3 comment | 37 complexity | 9fcee09ca931dd2298a3ff41b5cb5316 MD5 | raw file
  1. <?php
  2. namespace MailPoetVendor\Doctrine\DBAL;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Doctrine\DBAL\Abstraction\Result;
  5. use MailPoetVendor\Doctrine\DBAL\Driver\Exception;
  6. use MailPoetVendor\Doctrine\DBAL\Driver\Statement as DriverStatement;
  7. use MailPoetVendor\Doctrine\DBAL\Exception\NoKeyValue;
  8. use MailPoetVendor\Doctrine\DBAL\Platforms\AbstractPlatform;
  9. use MailPoetVendor\Doctrine\DBAL\Result as BaseResult;
  10. use MailPoetVendor\Doctrine\DBAL\Types\Type;
  11. use MailPoetVendor\Doctrine\Deprecations\Deprecation;
  12. use IteratorAggregate;
  13. use PDO;
  14. use PDOStatement;
  15. use ReturnTypeWillChange;
  16. use Throwable;
  17. use Traversable;
  18. use function array_shift;
  19. use function func_get_args;
  20. use function is_array;
  21. use function is_string;
  22. class Statement implements IteratorAggregate, DriverStatement, Result
  23. {
  24. protected $sql;
  25. protected $params = [];
  26. protected $types = [];
  27. protected $stmt;
  28. protected $platform;
  29. protected $conn;
  30. public function __construct($sql, Connection $conn)
  31. {
  32. $this->sql = $sql;
  33. $this->stmt = $conn->getWrappedConnection()->prepare($sql);
  34. $this->conn = $conn;
  35. $this->platform = $conn->getDatabasePlatform();
  36. }
  37. public function bindValue($param, $value, $type = ParameterType::STRING)
  38. {
  39. $this->params[$param] = $value;
  40. $this->types[$param] = $type;
  41. if ($type !== null) {
  42. if (is_string($type)) {
  43. $type = Type::getType($type);
  44. }
  45. if ($type instanceof Type) {
  46. $value = $type->convertToDatabaseValue($value, $this->platform);
  47. $bindingType = $type->getBindingType();
  48. } else {
  49. $bindingType = $type;
  50. }
  51. return $this->stmt->bindValue($param, $value, $bindingType);
  52. }
  53. return $this->stmt->bindValue($param, $value);
  54. }
  55. public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
  56. {
  57. $this->params[$param] = $variable;
  58. $this->types[$param] = $type;
  59. if ($this->stmt instanceof PDOStatement) {
  60. $length = $length ?? 0;
  61. }
  62. return $this->stmt->bindParam($param, $variable, $type, $length);
  63. }
  64. public function execute($params = null)
  65. {
  66. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4580', 'Statement::execute() is deprecated, use Statement::executeQuery() or Statement::executeStatement() instead');
  67. if (is_array($params)) {
  68. $this->params = $params;
  69. }
  70. $logger = $this->conn->getConfiguration()->getSQLLogger();
  71. if ($logger) {
  72. $logger->startQuery($this->sql, $this->params, $this->types);
  73. }
  74. try {
  75. $stmt = $this->stmt->execute($params);
  76. } catch (Throwable $ex) {
  77. if ($logger) {
  78. $logger->stopQuery();
  79. }
  80. $this->conn->handleExceptionDuringQuery($ex, $this->sql, $this->params, $this->types);
  81. }
  82. if ($logger) {
  83. $logger->stopQuery();
  84. }
  85. return $stmt;
  86. }
  87. public function executeQuery(array $params = []) : BaseResult
  88. {
  89. if ($params === []) {
  90. $params = null;
  91. // Workaround as long execute() exists and used internally.
  92. }
  93. $this->execute($params);
  94. return new ForwardCompatibility\Result($this);
  95. }
  96. public function executeStatement(array $params = []) : int
  97. {
  98. if ($params === []) {
  99. $params = null;
  100. // Workaround as long execute() exists and used internally.
  101. }
  102. $this->execute($params);
  103. return $this->rowCount();
  104. }
  105. public function closeCursor()
  106. {
  107. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4049', 'Statement::closeCursor() is deprecated, use Result::free() instead.');
  108. return $this->stmt->closeCursor();
  109. }
  110. public function columnCount()
  111. {
  112. return $this->stmt->columnCount();
  113. }
  114. public function errorCode()
  115. {
  116. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/3507', 'Connection::errorCode() is deprecated, use getCode() or getSQLState() on Exception instead.');
  117. return $this->stmt->errorCode();
  118. }
  119. public function errorInfo()
  120. {
  121. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/3507', 'Connection::errorInfo() is deprecated, use getCode() or getSQLState() on Exception instead.');
  122. return $this->stmt->errorInfo();
  123. }
  124. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  125. {
  126. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4019', 'Statement::setFetchMode() is deprecated, use explicit Result::fetch*() APIs instead.');
  127. if ($arg2 === null) {
  128. return $this->stmt->setFetchMode($fetchMode);
  129. }
  130. if ($arg3 === null) {
  131. return $this->stmt->setFetchMode($fetchMode, $arg2);
  132. }
  133. return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
  134. }
  135. #[\ReturnTypeWillChange]
  136. public function getIterator()
  137. {
  138. Deprecation::trigger('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4019', 'Statement::getIterator() is deprecated, use Result::iterateNumeric(), iterateAssociative() ' . 'or iterateColumn() instead.');
  139. return $this->stmt;
  140. }
  141. public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
  142. {
  143. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4019', 'Statement::fetch() is deprecated, use Result::fetchNumeric(), fetchAssociative() or fetchOne() instead.');
  144. return $this->stmt->fetch(...func_get_args());
  145. }
  146. public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
  147. {
  148. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4019', 'Statement::fetchAll() is deprecated, use Result::fetchAllNumeric(), fetchAllAssociative() or ' . 'fetchFirstColumn() instead.');
  149. if ($ctorArgs !== null) {
  150. return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
  151. }
  152. if ($fetchArgument !== null) {
  153. return $this->stmt->fetchAll($fetchMode, $fetchArgument);
  154. }
  155. return $this->stmt->fetchAll($fetchMode);
  156. }
  157. public function fetchColumn($columnIndex = 0)
  158. {
  159. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4019', 'Statement::fetchColumn() is deprecated, use Result::fetchOne() instead.');
  160. return $this->stmt->fetchColumn($columnIndex);
  161. }
  162. public function fetchNumeric()
  163. {
  164. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  165. try {
  166. if ($this->stmt instanceof Result) {
  167. return $this->stmt->fetchNumeric();
  168. }
  169. return $this->stmt->fetch(FetchMode::NUMERIC);
  170. } catch (Exception $e) {
  171. $this->conn->handleDriverException($e);
  172. }
  173. }
  174. public function fetchAssociative()
  175. {
  176. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  177. try {
  178. if ($this->stmt instanceof Result) {
  179. return $this->stmt->fetchAssociative();
  180. }
  181. return $this->stmt->fetch(FetchMode::ASSOCIATIVE);
  182. } catch (Exception $e) {
  183. $this->conn->handleDriverException($e);
  184. }
  185. }
  186. public function fetchOne()
  187. {
  188. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  189. try {
  190. if ($this->stmt instanceof Result) {
  191. return $this->stmt->fetchOne();
  192. }
  193. return $this->stmt->fetch(FetchMode::COLUMN);
  194. } catch (Exception $e) {
  195. $this->conn->handleDriverException($e);
  196. }
  197. }
  198. public function fetchAllNumeric() : array
  199. {
  200. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  201. try {
  202. if ($this->stmt instanceof Result) {
  203. return $this->stmt->fetchAllNumeric();
  204. }
  205. return $this->stmt->fetchAll(FetchMode::NUMERIC);
  206. } catch (Exception $e) {
  207. $this->conn->handleDriverException($e);
  208. }
  209. }
  210. public function fetchAllAssociative() : array
  211. {
  212. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  213. try {
  214. if ($this->stmt instanceof Result) {
  215. return $this->stmt->fetchAllAssociative();
  216. }
  217. return $this->stmt->fetchAll(FetchMode::ASSOCIATIVE);
  218. } catch (Exception $e) {
  219. $this->conn->handleDriverException($e);
  220. }
  221. }
  222. public function fetchAllKeyValue() : array
  223. {
  224. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  225. $this->ensureHasKeyValue();
  226. $data = [];
  227. foreach ($this->fetchAllNumeric() as [$key, $value]) {
  228. $data[$key] = $value;
  229. }
  230. return $data;
  231. }
  232. public function fetchAllAssociativeIndexed() : array
  233. {
  234. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  235. $data = [];
  236. foreach ($this->fetchAll(FetchMode::ASSOCIATIVE) as $row) {
  237. $data[array_shift($row)] = $row;
  238. }
  239. return $data;
  240. }
  241. public function fetchFirstColumn() : array
  242. {
  243. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  244. try {
  245. if ($this->stmt instanceof Result) {
  246. return $this->stmt->fetchFirstColumn();
  247. }
  248. return $this->stmt->fetchAll(FetchMode::COLUMN);
  249. } catch (Exception $e) {
  250. $this->conn->handleDriverException($e);
  251. }
  252. }
  253. public function iterateNumeric() : Traversable
  254. {
  255. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  256. try {
  257. if ($this->stmt instanceof Result) {
  258. while (($row = $this->stmt->fetchNumeric()) !== \false) {
  259. (yield $row);
  260. }
  261. } else {
  262. while (($row = $this->stmt->fetch(FetchMode::NUMERIC)) !== \false) {
  263. (yield $row);
  264. }
  265. }
  266. } catch (Exception $e) {
  267. $this->conn->handleDriverException($e);
  268. }
  269. }
  270. public function iterateAssociative() : Traversable
  271. {
  272. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  273. try {
  274. if ($this->stmt instanceof Result) {
  275. while (($row = $this->stmt->fetchAssociative()) !== \false) {
  276. (yield $row);
  277. }
  278. } else {
  279. while (($row = $this->stmt->fetch(FetchMode::ASSOCIATIVE)) !== \false) {
  280. (yield $row);
  281. }
  282. }
  283. } catch (Exception $e) {
  284. $this->conn->handleDriverException($e);
  285. }
  286. }
  287. public function iterateKeyValue() : Traversable
  288. {
  289. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  290. $this->ensureHasKeyValue();
  291. foreach ($this->iterateNumeric() as [$key, $value]) {
  292. (yield $key => $value);
  293. }
  294. }
  295. public function iterateAssociativeIndexed() : Traversable
  296. {
  297. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  298. while (($row = $this->stmt->fetch(FetchMode::ASSOCIATIVE)) !== \false) {
  299. (yield array_shift($row) => $row);
  300. }
  301. }
  302. public function iterateColumn() : Traversable
  303. {
  304. Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/4554', 'Statement::%s() is deprecated, use Result::%s() instead.', __FUNCTION__, __FUNCTION__);
  305. try {
  306. if ($this->stmt instanceof Result) {
  307. while (($value = $this->stmt->fetchOne()) !== \false) {
  308. (yield $value);
  309. }
  310. } else {
  311. while (($value = $this->stmt->fetch(FetchMode::COLUMN)) !== \false) {
  312. (yield $value);
  313. }
  314. }
  315. } catch (Exception $e) {
  316. $this->conn->handleDriverException($e);
  317. }
  318. }
  319. public function rowCount()
  320. {
  321. return $this->stmt->rowCount();
  322. }
  323. public function free() : void
  324. {
  325. if ($this->stmt instanceof Result) {
  326. $this->stmt->free();
  327. return;
  328. }
  329. $this->stmt->closeCursor();
  330. }
  331. public function getWrappedStatement()
  332. {
  333. return $this->stmt;
  334. }
  335. private function ensureHasKeyValue() : void
  336. {
  337. $columnCount = $this->columnCount();
  338. if ($columnCount < 2) {
  339. throw NoKeyValue::fromColumnCount($columnCount);
  340. }
  341. }
  342. }