Statement.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace Doctrine\DBAL\Portability;
  3. use Doctrine\DBAL\Driver\Result;
  4. use Doctrine\DBAL\Driver\ResultStatement;
  5. use Doctrine\DBAL\Driver\Statement as DriverStatement;
  6. use Doctrine\DBAL\Driver\StatementIterator;
  7. use Doctrine\DBAL\FetchMode;
  8. use Doctrine\DBAL\ParameterType;
  9. use IteratorAggregate;
  10. use PDO;
  11. use function array_change_key_case;
  12. use function assert;
  13. use function is_string;
  14. use function rtrim;
  15. /**
  16. * Portability wrapper for a Statement.
  17. */
  18. class Statement implements IteratorAggregate, DriverStatement, Result
  19. {
  20. /** @var int */
  21. private $portability;
  22. /** @var DriverStatement|ResultStatement */
  23. private $stmt;
  24. /** @var int|null */
  25. private $case;
  26. /** @var int */
  27. private $defaultFetchMode = FetchMode::MIXED;
  28. /**
  29. * Wraps <tt>Statement</tt> and applies portability measures.
  30. *
  31. * @param DriverStatement|ResultStatement $stmt
  32. */
  33. public function __construct($stmt, Connection $conn)
  34. {
  35. $this->stmt = $stmt;
  36. $this->portability = $conn->getPortability();
  37. $this->case = $conn->getFetchCase();
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
  43. {
  44. assert($this->stmt instanceof DriverStatement);
  45. return $this->stmt->bindParam($param, $variable, $type, $length);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function bindValue($param, $value, $type = ParameterType::STRING)
  51. {
  52. assert($this->stmt instanceof DriverStatement);
  53. return $this->stmt->bindValue($param, $value, $type);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. *
  58. * @deprecated Use free() instead.
  59. */
  60. public function closeCursor()
  61. {
  62. return $this->stmt->closeCursor();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function columnCount()
  68. {
  69. return $this->stmt->columnCount();
  70. }
  71. /**
  72. * {@inheritdoc}
  73. *
  74. * @deprecated The error information is available via exceptions.
  75. */
  76. public function errorCode()
  77. {
  78. assert($this->stmt instanceof DriverStatement);
  79. return $this->stmt->errorCode();
  80. }
  81. /**
  82. * {@inheritdoc}
  83. *
  84. * @deprecated The error information is available via exceptions.
  85. */
  86. public function errorInfo()
  87. {
  88. assert($this->stmt instanceof DriverStatement);
  89. return $this->stmt->errorInfo();
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function execute($params = null)
  95. {
  96. assert($this->stmt instanceof DriverStatement);
  97. return $this->stmt->execute($params);
  98. }
  99. /**
  100. * {@inheritdoc}
  101. *
  102. * @deprecated Use one of the fetch- or iterate-related methods.
  103. */
  104. public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
  105. {
  106. $this->defaultFetchMode = $fetchMode;
  107. return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
  108. }
  109. /**
  110. * {@inheritdoc}
  111. *
  112. * @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
  113. */
  114. public function getIterator()
  115. {
  116. return new StatementIterator($this);
  117. }
  118. /**
  119. * {@inheritdoc}
  120. *
  121. * @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
  122. */
  123. public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
  124. {
  125. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  126. $row = $this->stmt->fetch($fetchMode);
  127. $iterateRow = (
  128. $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL | Connection::PORTABILITY_RTRIM)
  129. ) !== 0;
  130. $fixCase = $this->case !== null
  131. && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
  132. && ($this->portability & Connection::PORTABILITY_FIX_CASE);
  133. $row = $this->fixRow($row, $iterateRow, $fixCase);
  134. return $row;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. *
  139. * @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
  140. */
  141. public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
  142. {
  143. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  144. if ($fetchArgument) {
  145. $rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
  146. } else {
  147. $rows = $this->stmt->fetchAll($fetchMode);
  148. }
  149. $fixCase = $this->case !== null
  150. && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
  151. && ($this->portability & Connection::PORTABILITY_FIX_CASE);
  152. return $this->fixResultSet($rows, $fixCase, $fetchMode !== FetchMode::COLUMN);
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function fetchNumeric()
  158. {
  159. if ($this->stmt instanceof Result) {
  160. $row = $this->stmt->fetchNumeric();
  161. } else {
  162. $row = $this->stmt->fetch(FetchMode::NUMERIC);
  163. }
  164. return $this->fixResult($row, false);
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function fetchAssociative()
  170. {
  171. if ($this->stmt instanceof Result) {
  172. $row = $this->stmt->fetchAssociative();
  173. } else {
  174. $row = $this->stmt->fetch(FetchMode::ASSOCIATIVE);
  175. }
  176. return $this->fixResult($row, true);
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function fetchOne()
  182. {
  183. if ($this->stmt instanceof Result) {
  184. $value = $this->stmt->fetchOne();
  185. } else {
  186. $value = $this->stmt->fetch(FetchMode::COLUMN);
  187. }
  188. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) !== 0 && $value === '') {
  189. $value = null;
  190. } elseif (($this->portability & Connection::PORTABILITY_RTRIM) !== 0 && is_string($value)) {
  191. $value = rtrim($value);
  192. }
  193. return $value;
  194. }
  195. /**
  196. * {@inheritdoc}
  197. */
  198. public function fetchAllNumeric(): array
  199. {
  200. if ($this->stmt instanceof Result) {
  201. $data = $this->stmt->fetchAllNumeric();
  202. } else {
  203. $data = $this->stmt->fetchAll(FetchMode::NUMERIC);
  204. }
  205. return $this->fixResultSet($data, false, true);
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function fetchAllAssociative(): array
  211. {
  212. if ($this->stmt instanceof Result) {
  213. $data = $this->stmt->fetchAllAssociative();
  214. } else {
  215. $data = $this->stmt->fetchAll(FetchMode::ASSOCIATIVE);
  216. }
  217. return $this->fixResultSet($data, true, true);
  218. }
  219. /**
  220. * {@inheritdoc}
  221. */
  222. public function fetchFirstColumn(): array
  223. {
  224. if ($this->stmt instanceof Result) {
  225. $data = $this->stmt->fetchFirstColumn();
  226. } else {
  227. $data = $this->stmt->fetchAll(FetchMode::COLUMN);
  228. }
  229. return $this->fixResultSet($data, true, false);
  230. }
  231. public function free(): void
  232. {
  233. if ($this->stmt instanceof Result) {
  234. $this->stmt->free();
  235. return;
  236. }
  237. $this->stmt->closeCursor();
  238. }
  239. /**
  240. * @param mixed $result
  241. *
  242. * @return mixed
  243. */
  244. private function fixResult($result, bool $fixCase)
  245. {
  246. $iterateRow = (
  247. $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL | Connection::PORTABILITY_RTRIM)
  248. ) !== 0;
  249. $fixCase = $fixCase && $this->case !== null && ($this->portability & Connection::PORTABILITY_FIX_CASE) !== 0;
  250. return $this->fixRow($result, $iterateRow, $fixCase);
  251. }
  252. /**
  253. * @param array<int,mixed> $resultSet
  254. *
  255. * @return array<int,mixed>
  256. */
  257. private function fixResultSet(array $resultSet, bool $fixCase, bool $isArray): array
  258. {
  259. $iterateRow = (
  260. $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL | Connection::PORTABILITY_RTRIM)
  261. ) !== 0;
  262. $fixCase = $fixCase && $this->case !== null && ($this->portability & Connection::PORTABILITY_FIX_CASE) !== 0;
  263. if (! $iterateRow && ! $fixCase) {
  264. return $resultSet;
  265. }
  266. if (! $isArray) {
  267. foreach ($resultSet as $num => $value) {
  268. $resultSet[$num] = [$value];
  269. }
  270. }
  271. foreach ($resultSet as $num => $row) {
  272. $resultSet[$num] = $this->fixRow($row, $iterateRow, $fixCase);
  273. }
  274. if (! $isArray) {
  275. foreach ($resultSet as $num => $row) {
  276. $resultSet[$num] = $row[0];
  277. }
  278. }
  279. return $resultSet;
  280. }
  281. /**
  282. * @param mixed $row
  283. * @param bool $iterateRow
  284. * @param bool $fixCase
  285. *
  286. * @return mixed
  287. */
  288. protected function fixRow($row, $iterateRow, $fixCase)
  289. {
  290. if (! $row) {
  291. return $row;
  292. }
  293. if ($fixCase) {
  294. $row = array_change_key_case($row, $this->case);
  295. }
  296. if ($iterateRow) {
  297. foreach ($row as $k => $v) {
  298. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
  299. $row[$k] = null;
  300. } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
  301. $row[$k] = rtrim($v);
  302. }
  303. }
  304. }
  305. return $row;
  306. }
  307. /**
  308. * {@inheritdoc}
  309. *
  310. * @deprecated Use fetchOne() instead.
  311. */
  312. public function fetchColumn($columnIndex = 0)
  313. {
  314. $value = $this->stmt->fetchColumn($columnIndex);
  315. if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL | Connection::PORTABILITY_RTRIM)) {
  316. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
  317. $value = null;
  318. } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
  319. $value = rtrim($value);
  320. }
  321. }
  322. return $value;
  323. }
  324. /**
  325. * {@inheritdoc}
  326. */
  327. public function rowCount()
  328. {
  329. assert($this->stmt instanceof DriverStatement);
  330. return $this->stmt->rowCount();
  331. }
  332. }