You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreparedStatement.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2021 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. namespace OC\DB;
  24. use Doctrine\DBAL\Exception;
  25. use Doctrine\DBAL\ParameterType;
  26. use Doctrine\DBAL\Statement;
  27. use OCP\DB\IPreparedStatement;
  28. use OCP\DB\IResult;
  29. use PDO;
  30. /**
  31. * Adapts our public API to what doctrine/dbal exposed with 2.6
  32. *
  33. * The old dbal statement had stateful methods e.g. to fetch data from an executed
  34. * prepared statement. To provide backwards compatibility to apps we need to make
  35. * this class stateful. As soon as those now deprecated exposed methods are gone,
  36. * we can limit the API of this adapter to the methods that map to the direct dbal
  37. * methods without much magic.
  38. */
  39. class PreparedStatement implements IPreparedStatement {
  40. /** @var Statement */
  41. private $statement;
  42. /** @var IResult|null */
  43. private $result;
  44. public function __construct(Statement $statement) {
  45. $this->statement = $statement;
  46. }
  47. public function closeCursor(): bool {
  48. $this->getResult()->closeCursor();
  49. return true;
  50. }
  51. public function fetch(int $fetchMode = PDO::FETCH_ASSOC) {
  52. return $this->getResult()->fetch($fetchMode);
  53. }
  54. public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array {
  55. return $this->getResult()->fetchAll($fetchMode);
  56. }
  57. public function fetchColumn() {
  58. return $this->getResult()->fetchOne();
  59. }
  60. public function fetchOne() {
  61. return $this->getResult()->fetchOne();
  62. }
  63. public function bindValue($param, $value, $type = ParameterType::STRING): bool {
  64. return $this->statement->bindValue($param, $value, $type);
  65. }
  66. public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool {
  67. return $this->statement->bindParam($param, $variable, $type, $length);
  68. }
  69. public function execute($params = null): IResult {
  70. return ($this->result = new ResultAdapter($this->statement->execute($params)));
  71. }
  72. public function rowCount(): int {
  73. return $this->getResult()->rowCount();
  74. }
  75. private function getResult(): IResult {
  76. if ($this->result !== null) {
  77. return $this->result;
  78. }
  79. throw new Exception("You have to execute the prepared statement before accessing the results");
  80. }
  81. }