statementwrapper.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * small wrapper around \Doctrine\DBAL\Driver\Statement to make it behave, more like an MDB2 Statement
  10. */
  11. class OC_DB_StatementWrapper {
  12. /**
  13. * @var \Doctrine\DBAL\Driver\Statement
  14. */
  15. private $statement = null;
  16. private $isManipulation = false;
  17. private $lastArguments = array();
  18. public function __construct($statement, $isManipulation) {
  19. $this->statement = $statement;
  20. $this->isManipulation = $isManipulation;
  21. }
  22. /**
  23. * pass all other function directly to the \Doctrine\DBAL\Driver\Statement
  24. */
  25. public function __call($name,$arguments) {
  26. return call_user_func_array(array($this->statement,$name), $arguments);
  27. }
  28. /**
  29. * provide numRows
  30. */
  31. public function numRows() {
  32. $type = OC_Config::getValue( "dbtype", "sqlite" );
  33. if ($type == 'oci') {
  34. // OCI doesn't have a queryString, just do a rowCount for now
  35. return $this->statement->rowCount();
  36. }
  37. $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
  38. $queryString = $this->statement->getWrappedStatement()->queryString;
  39. if (preg_match($regex, $queryString, $output) > 0) {
  40. $query = OC_DB::prepare("SELECT COUNT(*) FROM {$output[1]}");
  41. return $query->execute($this->lastArguments)->fetchColumn();
  42. }else{
  43. return $this->statement->rowCount();
  44. }
  45. }
  46. /**
  47. * make execute return the result instead of a bool
  48. */
  49. public function execute($input=array()) {
  50. if(OC_Config::getValue( "log_query", false)) {
  51. $params_str = str_replace("\n", " ", var_export($input, true));
  52. OC_Log::write('core', 'DB execute with arguments : '.$params_str, OC_Log::DEBUG);
  53. }
  54. $this->lastArguments = $input;
  55. if (count($input) > 0) {
  56. if (!isset($type)) {
  57. $type = OC_Config::getValue( "dbtype", "sqlite" );
  58. }
  59. if ($type == 'mssql') {
  60. $input = $this->tryFixSubstringLastArgumentDataForMSSQL($input);
  61. }
  62. $result = $this->statement->execute($input);
  63. } else {
  64. $result = $this->statement->execute();
  65. }
  66. if ($result === false) {
  67. return false;
  68. }
  69. if ($this->isManipulation) {
  70. return $this->statement->rowCount();
  71. } else {
  72. return $this;
  73. }
  74. }
  75. private function tryFixSubstringLastArgumentDataForMSSQL($input) {
  76. $query = $this->statement->getWrappedStatement()->queryString;
  77. $pos = stripos ($query, 'SUBSTRING');
  78. if ( $pos === false) {
  79. return $input;
  80. }
  81. try {
  82. $newQuery = '';
  83. $cArg = 0;
  84. $inSubstring = false;
  85. // Create new query
  86. for ($i = 0; $i < strlen ($query); $i++) {
  87. if ($inSubstring == false) {
  88. // Defines when we should start inserting values
  89. if (substr ($query, $i, 9) == 'SUBSTRING') {
  90. $inSubstring = true;
  91. }
  92. } else {
  93. // Defines when we should stop inserting values
  94. if (substr ($query, $i, 1) == ')') {
  95. $inSubstring = false;
  96. }
  97. }
  98. if (substr ($query, $i, 1) == '?') {
  99. // We found a question mark
  100. if ($inSubstring) {
  101. $newQuery .= $input[$cArg];
  102. //
  103. // Remove from input array
  104. //
  105. array_splice ($input, $cArg, 1);
  106. } else {
  107. $newQuery .= substr ($query, $i, 1);
  108. $cArg++;
  109. }
  110. } else {
  111. $newQuery .= substr ($query, $i, 1);
  112. }
  113. }
  114. // The global data we need
  115. $name = OC_Config::getValue( "dbname", "owncloud" );
  116. $host = OC_Config::getValue( "dbhost", "" );
  117. $user = OC_Config::getValue( "dbuser", "" );
  118. $pass = OC_Config::getValue( "dbpassword", "" );
  119. if (strpos($host, ':')) {
  120. list($host, $port) = explode(':', $host, 2);
  121. } else {
  122. $port = false;
  123. }
  124. $opts = array();
  125. if ($port) {
  126. $dsn = 'sqlsrv:Server='.$host.','.$port.';Database='.$name;
  127. } else {
  128. $dsn = 'sqlsrv:Server='.$host.';Database='.$name;
  129. }
  130. $PDO = new PDO($dsn, $user, $pass, $opts);
  131. $PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  132. $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  133. $this->statement = $PDO->prepare($newQuery);
  134. $this->lastArguments = $input;
  135. return $input;
  136. } catch (PDOException $e){
  137. $entry = 'PDO DB Error: "'.$e->getMessage().'"<br />';
  138. $entry .= 'Offending command was: '.$this->statement->queryString .'<br />';
  139. $entry .= 'Input parameters: ' .print_r($input, true).'<br />';
  140. $entry .= 'Stack trace: ' .$e->getTraceAsString().'<br />';
  141. OC_Log::write('core', $entry, OC_Log::FATAL);
  142. OC_User::setUserId(null);
  143. // send http status 503
  144. header('HTTP/1.1 503 Service Temporarily Unavailable');
  145. header('Status: 503 Service Temporarily Unavailable');
  146. OC_Template::printErrorPage('Failed to connect to database');
  147. die ($entry);
  148. }
  149. }
  150. /**
  151. * provide an alias for fetch
  152. */
  153. public function fetchRow() {
  154. return $this->statement->fetch();
  155. }
  156. /**
  157. * Provide a simple fetchOne.
  158. * fetch single column from the next row
  159. * @param int $colnum the column number to fetch
  160. * @return string
  161. */
  162. public function fetchOne($colnum = 0) {
  163. return $this->statement->fetchColumn($colnum);
  164. }
  165. }