1
0

connection.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\DB;
  26. use Doctrine\DBAL\DBALException;
  27. use Doctrine\DBAL\Driver;
  28. use Doctrine\DBAL\Configuration;
  29. use Doctrine\DBAL\Cache\QueryCacheProfile;
  30. use Doctrine\Common\EventManager;
  31. use OCP\IDBConnection;
  32. class Connection extends \Doctrine\DBAL\Connection implements IDBConnection {
  33. /**
  34. * @var string $tablePrefix
  35. */
  36. protected $tablePrefix;
  37. /**
  38. * @var \OC\DB\Adapter $adapter
  39. */
  40. protected $adapter;
  41. public function connect() {
  42. try {
  43. return parent::connect();
  44. } catch (DBALException $e) {
  45. // throw a new exception to prevent leaking info from the stacktrace
  46. throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode());
  47. }
  48. }
  49. /**
  50. * @return string
  51. */
  52. public function getPrefix() {
  53. return $this->tablePrefix;
  54. }
  55. /**
  56. * Initializes a new instance of the Connection class.
  57. *
  58. * @param array $params The connection parameters.
  59. * @param \Doctrine\DBAL\Driver $driver
  60. * @param \Doctrine\DBAL\Configuration $config
  61. * @param \Doctrine\Common\EventManager $eventManager
  62. * @throws \Exception
  63. */
  64. public function __construct(array $params, Driver $driver, Configuration $config = null,
  65. EventManager $eventManager = null)
  66. {
  67. if (!isset($params['adapter'])) {
  68. throw new \Exception('adapter not set');
  69. }
  70. if (!isset($params['tablePrefix'])) {
  71. throw new \Exception('tablePrefix not set');
  72. }
  73. parent::__construct($params, $driver, $config, $eventManager);
  74. $this->adapter = new $params['adapter']($this);
  75. $this->tablePrefix = $params['tablePrefix'];
  76. }
  77. /**
  78. * Prepares an SQL statement.
  79. *
  80. * @param string $statement The SQL statement to prepare.
  81. * @param int $limit
  82. * @param int $offset
  83. * @return \Doctrine\DBAL\Driver\Statement The prepared statement.
  84. */
  85. public function prepare( $statement, $limit=null, $offset=null ) {
  86. if ($limit === -1) {
  87. $limit = null;
  88. }
  89. if (!is_null($limit)) {
  90. $platform = $this->getDatabasePlatform();
  91. $statement = $platform->modifyLimitQuery($statement, $limit, $offset);
  92. }
  93. $statement = $this->replaceTablePrefix($statement);
  94. $statement = $this->adapter->fixupStatement($statement);
  95. if(\OC_Config::getValue( 'log_query', false)) {
  96. \OC_Log::write('core', 'DB prepare : '.$statement, \OC_Log::DEBUG);
  97. }
  98. return parent::prepare($statement);
  99. }
  100. /**
  101. * Executes an, optionally parametrized, SQL query.
  102. *
  103. * If the query is parametrized, a prepared statement is used.
  104. * If an SQLLogger is configured, the execution is logged.
  105. *
  106. * @param string $query The SQL query to execute.
  107. * @param array $params The parameters to bind to the query, if any.
  108. * @param array $types The types the previous parameters are in.
  109. * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
  110. *
  111. * @return \Doctrine\DBAL\Driver\Statement The executed statement.
  112. *
  113. * @throws \Doctrine\DBAL\DBALException
  114. */
  115. public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
  116. {
  117. $query = $this->replaceTablePrefix($query);
  118. $query = $this->adapter->fixupStatement($query);
  119. return parent::executeQuery($query, $params, $types, $qcp);
  120. }
  121. /**
  122. * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
  123. * and returns the number of affected rows.
  124. *
  125. * This method supports PDO binding types as well as DBAL mapping types.
  126. *
  127. * @param string $query The SQL query.
  128. * @param array $params The query parameters.
  129. * @param array $types The parameter types.
  130. *
  131. * @return integer The number of affected rows.
  132. *
  133. * @throws \Doctrine\DBAL\DBALException
  134. */
  135. public function executeUpdate($query, array $params = array(), array $types = array())
  136. {
  137. $query = $this->replaceTablePrefix($query);
  138. $query = $this->adapter->fixupStatement($query);
  139. return parent::executeUpdate($query, $params, $types);
  140. }
  141. /**
  142. * Returns the ID of the last inserted row, or the last value from a sequence object,
  143. * depending on the underlying driver.
  144. *
  145. * Note: This method may not return a meaningful or consistent result across different drivers,
  146. * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY
  147. * columns or sequences.
  148. *
  149. * @param string $seqName Name of the sequence object from which the ID should be returned.
  150. * @return string A string representation of the last inserted ID.
  151. */
  152. public function lastInsertId($seqName = null)
  153. {
  154. if ($seqName) {
  155. $seqName = $this->replaceTablePrefix($seqName);
  156. }
  157. return $this->adapter->lastInsertId($seqName);
  158. }
  159. // internal use
  160. public function realLastInsertId($seqName = null) {
  161. return parent::lastInsertId($seqName);
  162. }
  163. /**
  164. * Insert a row if the matching row does not exists.
  165. *
  166. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  167. * @param array $input data that should be inserted into the table (column name => value)
  168. * @param array|null $compare List of values that should be checked for "if not exists"
  169. * If this is null or an empty array, all keys of $input will be compared
  170. * Please note: text fields (clob) must not be used in the compare array
  171. * @return int number of inserted rows
  172. * @throws \Doctrine\DBAL\DBALException
  173. */
  174. public function insertIfNotExist($table, $input, array $compare = null) {
  175. return $this->adapter->insertIfNotExist($table, $input, $compare);
  176. }
  177. /**
  178. * returns the error code and message as a string for logging
  179. * works with DoctrineException
  180. * @return string
  181. */
  182. public function getError() {
  183. $msg = $this->errorCode() . ': ';
  184. $errorInfo = $this->errorInfo();
  185. if (is_array($errorInfo)) {
  186. $msg .= 'SQLSTATE = '.$errorInfo[0] . ', ';
  187. $msg .= 'Driver Code = '.$errorInfo[1] . ', ';
  188. $msg .= 'Driver Message = '.$errorInfo[2];
  189. }
  190. return $msg;
  191. }
  192. /**
  193. * Drop a table from the database if it exists
  194. *
  195. * @param string $table table name without the prefix
  196. */
  197. public function dropTable($table) {
  198. $table = $this->tablePrefix . trim($table);
  199. $schema = $this->getSchemaManager();
  200. if($schema->tablesExist(array($table))) {
  201. $schema->dropTable($table);
  202. }
  203. }
  204. /**
  205. * Check if a table exists
  206. *
  207. * @param string $table table name without the prefix
  208. * @return bool
  209. */
  210. public function tableExists($table){
  211. $table = $this->tablePrefix . trim($table);
  212. $schema = $this->getSchemaManager();
  213. return $schema->tablesExist(array($table));
  214. }
  215. // internal use
  216. /**
  217. * @param string $statement
  218. * @return string
  219. */
  220. protected function replaceTablePrefix($statement) {
  221. return str_replace( '*PREFIX*', $this->tablePrefix, $statement );
  222. }
  223. }