connectionfactory.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  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\Event\Listeners\OracleSessionInit;
  27. use Doctrine\DBAL\Event\Listeners\SQLSessionInit;
  28. use Doctrine\DBAL\Event\Listeners\MysqlSessionInit;
  29. /**
  30. * Takes care of creating and configuring Doctrine connections.
  31. */
  32. class ConnectionFactory {
  33. /**
  34. * @var array
  35. *
  36. * Array mapping DBMS type to default connection parameters passed to
  37. * \Doctrine\DBAL\DriverManager::getConnection().
  38. */
  39. protected $defaultConnectionParams = array(
  40. 'mssql' => array(
  41. 'adapter' => '\OC\DB\AdapterSQLSrv',
  42. 'charset' => 'UTF8',
  43. 'driver' => 'pdo_sqlsrv',
  44. 'wrapperClass' => 'OC\DB\Connection',
  45. ),
  46. 'mysql' => array(
  47. 'adapter' => '\OC\DB\AdapterMySQL',
  48. 'charset' => 'UTF8',
  49. 'driver' => 'pdo_mysql',
  50. 'wrapperClass' => 'OC\DB\Connection',
  51. ),
  52. 'oci' => array(
  53. 'adapter' => '\OC\DB\AdapterOCI8',
  54. 'charset' => 'AL32UTF8',
  55. 'driver' => 'oci8',
  56. 'wrapperClass' => 'OC\DB\OracleConnection',
  57. ),
  58. 'pgsql' => array(
  59. 'adapter' => '\OC\DB\AdapterPgSql',
  60. 'driver' => 'pdo_pgsql',
  61. 'wrapperClass' => 'OC\DB\Connection',
  62. ),
  63. 'sqlite3' => array(
  64. 'adapter' => '\OC\DB\AdapterSqlite',
  65. 'driver' => 'pdo_sqlite',
  66. 'wrapperClass' => 'OC\DB\Connection',
  67. ),
  68. );
  69. /**
  70. * @brief Get default connection parameters for a given DBMS.
  71. * @param string $type DBMS type
  72. * @throws \InvalidArgumentException If $type is invalid
  73. * @return array Default connection parameters.
  74. */
  75. public function getDefaultConnectionParams($type) {
  76. $normalizedType = $this->normalizeType($type);
  77. if (!isset($this->defaultConnectionParams[$normalizedType])) {
  78. throw new \InvalidArgumentException("Unsupported type: $type");
  79. }
  80. $result = $this->defaultConnectionParams[$normalizedType];
  81. // \PDO::MYSQL_ATTR_FOUND_ROWS may not be defined, e.g. when the MySQL
  82. // driver is missing. In this case, we won't be able to connect anyway.
  83. if ($normalizedType === 'mysql' && defined('\PDO::MYSQL_ATTR_FOUND_ROWS')) {
  84. $result['driverOptions'] = array(
  85. \PDO::MYSQL_ATTR_FOUND_ROWS => true,
  86. );
  87. }
  88. return $result;
  89. }
  90. /**
  91. * @brief Get default connection parameters for a given DBMS.
  92. * @param string $type DBMS type
  93. * @param array $additionalConnectionParams Additional connection parameters
  94. * @return \OC\DB\Connection
  95. */
  96. public function getConnection($type, $additionalConnectionParams) {
  97. $normalizedType = $this->normalizeType($type);
  98. $eventManager = new \Doctrine\Common\EventManager();
  99. switch ($normalizedType) {
  100. case 'mysql':
  101. // Send "SET NAMES utf8". Only required on PHP 5.3 below 5.3.6.
  102. // See http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names#4361485
  103. $eventManager->addEventSubscriber(new MysqlSessionInit);
  104. $eventManager->addEventSubscriber(
  105. new SQLSessionInit("SET SESSION AUTOCOMMIT=1"));
  106. break;
  107. case 'oci':
  108. $eventManager->addEventSubscriber(new OracleSessionInit);
  109. break;
  110. case 'sqlite3':
  111. $journalMode = $additionalConnectionParams['sqlite.journal_mode'];
  112. $additionalConnectionParams['platform'] = new OCSqlitePlatform();
  113. $eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode));
  114. break;
  115. }
  116. $connection = \Doctrine\DBAL\DriverManager::getConnection(
  117. array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams),
  118. new \Doctrine\DBAL\Configuration(),
  119. $eventManager
  120. );
  121. return $connection;
  122. }
  123. /**
  124. * @brief Normalize DBMS type
  125. * @param string $type DBMS type
  126. * @return string Normalized DBMS type
  127. */
  128. public function normalizeType($type) {
  129. return $type === 'sqlite' ? 'sqlite3' : $type;
  130. }
  131. /**
  132. * @brief Checks whether the specified DBMS type is valid.
  133. * @return bool
  134. */
  135. public function isValidType($type) {
  136. $normalizedType = $this->normalizeType($type);
  137. return isset($this->defaultConnectionParams[$normalizedType]);
  138. }
  139. /**
  140. * Create the connection parameters for the config
  141. *
  142. * @param \OC\SystemConfig $config
  143. * @return array
  144. */
  145. public function createConnectionParams($config) {
  146. $type = $config->getValue('dbtype', 'sqlite');
  147. $connectionParams = array(
  148. 'user' => $config->getValue('dbuser', ''),
  149. 'password' => $config->getValue('dbpassword', ''),
  150. );
  151. $name = $config->getValue('dbname', 'owncloud');
  152. if ($this->normalizeType($type) === 'sqlite3') {
  153. $dataDir = $config->getValue("datadirectory", \OC::$SERVERROOT . '/data');
  154. $connectionParams['path'] = $dataDir . '/' . $name . '.db';
  155. } else {
  156. $host = $config->getValue('dbhost', '');
  157. if (strpos($host, ':')) {
  158. // Host variable may carry a port or socket.
  159. list($host, $portOrSocket) = explode(':', $host, 2);
  160. if (ctype_digit($portOrSocket)) {
  161. $connectionParams['port'] = $portOrSocket;
  162. } else {
  163. $connectionParams['unix_socket'] = $portOrSocket;
  164. }
  165. }
  166. $connectionParams['host'] = $host;
  167. $connectionParams['dbname'] = $name;
  168. }
  169. $connectionParams['tablePrefix'] = $config->getValue('dbtableprefix', 'oc_');
  170. $connectionParams['sqlite.journal_mode'] = $config->getValue('sqlite.journal_mode', 'WAL');
  171. //additional driver options, eg. for mysql ssl
  172. $driverOptions = $config->getValue('dbdriveroptions', null);
  173. if ($driverOptions) {
  174. $connectionParams['driverOptions'] = $driverOptions;
  175. }
  176. return $connectionParams;
  177. }
  178. }