1
0

statementwrapper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Piotr Mrówczyński <mrow4a@yahoo.com>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. /**
  30. * small wrapper around \Doctrine\DBAL\Driver\Statement to make it behave, more like an MDB2 Statement
  31. *
  32. * @method boolean bindValue(mixed $param, mixed $value, integer $type = null);
  33. * @method string errorCode();
  34. * @method array errorInfo();
  35. * @method integer rowCount();
  36. * @method array fetchAll(integer $fetchMode = null);
  37. */
  38. class OC_DB_StatementWrapper {
  39. /**
  40. * @var \Doctrine\DBAL\Driver\Statement
  41. */
  42. private $statement = null;
  43. private $isManipulation = false;
  44. private $lastArguments = array();
  45. /**
  46. * @param boolean $isManipulation
  47. */
  48. public function __construct($statement, $isManipulation) {
  49. $this->statement = $statement;
  50. $this->isManipulation = $isManipulation;
  51. }
  52. /**
  53. * pass all other function directly to the \Doctrine\DBAL\Driver\Statement
  54. */
  55. public function __call($name,$arguments) {
  56. return call_user_func_array(array($this->statement,$name), $arguments);
  57. }
  58. /**
  59. * make execute return the result instead of a bool
  60. *
  61. * @param array $input
  62. * @return \OC_DB_StatementWrapper|int|bool
  63. */
  64. public function execute($input= []) {
  65. $this->lastArguments = $input;
  66. if (count($input) > 0) {
  67. $result = $this->statement->execute($input);
  68. } else {
  69. $result = $this->statement->execute();
  70. }
  71. if ($result === false) {
  72. return false;
  73. }
  74. if ($this->isManipulation) {
  75. return $this->statement->rowCount();
  76. } else {
  77. return $this;
  78. }
  79. }
  80. /**
  81. * provide an alias for fetch
  82. *
  83. * @return mixed
  84. */
  85. public function fetchRow() {
  86. return $this->statement->fetch();
  87. }
  88. /**
  89. * Provide a simple fetchOne.
  90. *
  91. * fetch single column from the next row
  92. * @param int $column the column number to fetch
  93. * @return string
  94. */
  95. public function fetchOne($column = 0) {
  96. return $this->statement->fetchColumn($column);
  97. }
  98. /**
  99. * Binds a PHP variable to a corresponding named or question mark placeholder in the
  100. * SQL statement that was use to prepare the statement.
  101. *
  102. * @param mixed $column Either the placeholder name or the 1-indexed placeholder index
  103. * @param mixed $variable The variable to bind
  104. * @param integer|null $type one of the PDO::PARAM_* constants
  105. * @param integer|null $length max length when using an OUT bind
  106. * @return boolean
  107. */
  108. public function bindParam($column, &$variable, $type = null, $length = null){
  109. return $this->statement->bindParam($column, $variable, $type, $length);
  110. }
  111. }