1
0

PreparedStatement.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\DB;
  25. use Doctrine\DBAL\Exception;
  26. use Doctrine\DBAL\ParameterType;
  27. use Doctrine\DBAL\Statement;
  28. use OCP\DB\IPreparedStatement;
  29. use OCP\DB\IResult;
  30. use PDO;
  31. /**
  32. * Adapts our public API to what doctrine/dbal exposed with 2.6
  33. *
  34. * The old dbal statement had stateful methods e.g. to fetch data from an executed
  35. * prepared statement. To provide backwards compatibility to apps we need to make
  36. * this class stateful. As soon as those now deprecated exposed methods are gone,
  37. * we can limit the API of this adapter to the methods that map to the direct dbal
  38. * methods without much magic.
  39. */
  40. class PreparedStatement implements IPreparedStatement {
  41. /** @var Statement */
  42. private $statement;
  43. /** @var IResult|null */
  44. private $result;
  45. public function __construct(Statement $statement) {
  46. $this->statement = $statement;
  47. }
  48. public function closeCursor(): bool {
  49. $this->getResult()->closeCursor();
  50. return true;
  51. }
  52. public function fetch(int $fetchMode = PDO::FETCH_ASSOC) {
  53. return $this->getResult()->fetch($fetchMode);
  54. }
  55. public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array {
  56. return $this->getResult()->fetchAll($fetchMode);
  57. }
  58. public function fetchColumn() {
  59. return $this->getResult()->fetchOne();
  60. }
  61. public function fetchOne() {
  62. return $this->getResult()->fetchOne();
  63. }
  64. public function bindValue($param, $value, $type = ParameterType::STRING): bool {
  65. return $this->statement->bindValue($param, $value, $type);
  66. }
  67. public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool {
  68. return $this->statement->bindParam($param, $variable, $type, $length);
  69. }
  70. public function execute($params = null): IResult {
  71. return ($this->result = new ResultAdapter($this->statement->execute($params)));
  72. }
  73. public function rowCount(): int {
  74. return $this->getResult()->rowCount();
  75. }
  76. private function getResult(): IResult {
  77. if ($this->result !== null) {
  78. return $this->result;
  79. }
  80. throw new Exception("You have to execute the prepared statement before accessing the results");
  81. }
  82. }