statementwrapper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. /**
  29. * small wrapper around \Doctrine\DBAL\Driver\Statement to make it behave, more like an MDB2 Statement
  30. *
  31. * @method boolean bindValue(mixed $param, mixed $value, integer $type = null);
  32. * @method string errorCode();
  33. * @method array errorInfo();
  34. * @method integer rowCount();
  35. * @method array fetchAll(integer $fetchMode = null);
  36. */
  37. class OC_DB_StatementWrapper {
  38. /**
  39. * @var \Doctrine\DBAL\Driver\Statement
  40. */
  41. private $statement = null;
  42. private $isManipulation = false;
  43. private $lastArguments = array();
  44. /**
  45. * @param boolean $isManipulation
  46. */
  47. public function __construct($statement, $isManipulation) {
  48. $this->statement = $statement;
  49. $this->isManipulation = $isManipulation;
  50. }
  51. /**
  52. * pass all other function directly to the \Doctrine\DBAL\Driver\Statement
  53. */
  54. public function __call($name,$arguments) {
  55. return call_user_func_array(array($this->statement,$name), $arguments);
  56. }
  57. /**
  58. * make execute return the result instead of a bool
  59. *
  60. * @param array $input
  61. * @return \OC_DB_StatementWrapper|int
  62. */
  63. public function execute($input=array()) {
  64. if(\OC::$server->getSystemConfig()->getValue( "log_query", false)) {
  65. $backTrace = debug_backtrace();
  66. $class = $backTrace[1]['class'] . ':' . $backTrace[1]['function'];
  67. $file = substr($backTrace[0]['file'], strlen(\OC::$SERVERROOT)) . ':' . $backTrace[0]['line'];
  68. $params_str = str_replace("\n", " ", var_export($input, true));
  69. \OCP\Util::writeLog('core', "DB execute with arguments : $params_str in $class; $file", \OCP\Util::DEBUG);
  70. }
  71. $this->lastArguments = $input;
  72. if (count($input) > 0) {
  73. $result = $this->statement->execute($input);
  74. } else {
  75. $result = $this->statement->execute();
  76. }
  77. if ($result === false) {
  78. return false;
  79. }
  80. if ($this->isManipulation) {
  81. $count = $this->statement->rowCount();
  82. return $count;
  83. } else {
  84. return $this;
  85. }
  86. }
  87. /**
  88. * provide an alias for fetch
  89. *
  90. * @return mixed
  91. */
  92. public function fetchRow() {
  93. return $this->statement->fetch();
  94. }
  95. /**
  96. * Provide a simple fetchOne.
  97. *
  98. * fetch single column from the next row
  99. * @param int $column the column number to fetch
  100. * @return string
  101. */
  102. public function fetchOne($column = 0) {
  103. return $this->statement->fetchColumn($column);
  104. }
  105. /**
  106. * Binds a PHP variable to a corresponding named or question mark placeholder in the
  107. * SQL statement that was use to prepare the statement.
  108. *
  109. * @param mixed $column Either the placeholder name or the 1-indexed placeholder index
  110. * @param mixed $variable The variable to bind
  111. * @param integer|null $type one of the PDO::PARAM_* constants
  112. * @param integer|null $length max length when using an OUT bind
  113. * @return boolean
  114. */
  115. public function bindParam($column, &$variable, $type = null, $length = null){
  116. return $this->statement->bindParam($column, $variable, $type, $length);
  117. }
  118. }