ConnectionFactory.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\DB;
  27. use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
  28. use Doctrine\DBAL\Event\Listeners\SQLSessionInit;
  29. use Doctrine\DBAL\Event\Listeners\MysqlSessionInit;
  30. use OCP\IConfig;
  31. /**
  32. * Takes care of creating and configuring Doctrine connections.
  33. */
  34. class ConnectionFactory {
  35. /**
  36. * @var array
  37. *
  38. * Array mapping DBMS type to default connection parameters passed to
  39. * \Doctrine\DBAL\DriverManager::getConnection().
  40. */
  41. protected $defaultConnectionParams = array(
  42. 'mysql' => array(
  43. 'adapter' => '\OC\DB\AdapterMySQL',
  44. 'charset' => 'UTF8',
  45. 'driver' => 'pdo_mysql',
  46. 'wrapperClass' => 'OC\DB\Connection',
  47. ),
  48. 'oci' => array(
  49. 'adapter' => '\OC\DB\AdapterOCI8',
  50. 'charset' => 'AL32UTF8',
  51. 'driver' => 'oci8',
  52. 'wrapperClass' => 'OC\DB\OracleConnection',
  53. ),
  54. 'pgsql' => array(
  55. 'adapter' => '\OC\DB\AdapterPgSql',
  56. 'driver' => 'pdo_pgsql',
  57. 'wrapperClass' => 'OC\DB\Connection',
  58. ),
  59. 'sqlite3' => array(
  60. 'adapter' => '\OC\DB\AdapterSqlite',
  61. 'driver' => 'pdo_sqlite',
  62. 'wrapperClass' => 'OC\DB\Connection',
  63. ),
  64. );
  65. public function __construct(IConfig $config) {
  66. if($config->getSystemValue('mysql.utf8mb4', false)) {
  67. $this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4';
  68. }
  69. }
  70. /**
  71. * @brief Get default connection parameters for a given DBMS.
  72. * @param string $type DBMS type
  73. * @throws \InvalidArgumentException If $type is invalid
  74. * @return array Default connection parameters.
  75. */
  76. public function getDefaultConnectionParams($type) {
  77. $normalizedType = $this->normalizeType($type);
  78. if (!isset($this->defaultConnectionParams[$normalizedType])) {
  79. throw new \InvalidArgumentException("Unsupported type: $type");
  80. }
  81. $result = $this->defaultConnectionParams[$normalizedType];
  82. // \PDO::MYSQL_ATTR_FOUND_ROWS may not be defined, e.g. when the MySQL
  83. // driver is missing. In this case, we won't be able to connect anyway.
  84. if ($normalizedType === 'mysql' && defined('\PDO::MYSQL_ATTR_FOUND_ROWS')) {
  85. $result['driverOptions'] = array(
  86. \PDO::MYSQL_ATTR_FOUND_ROWS => true,
  87. );
  88. }
  89. return $result;
  90. }
  91. /**
  92. * @brief Get default connection parameters for a given DBMS.
  93. * @param string $type DBMS type
  94. * @param array $additionalConnectionParams Additional connection parameters
  95. * @return \OC\DB\Connection
  96. */
  97. public function getConnection($type, $additionalConnectionParams) {
  98. $normalizedType = $this->normalizeType($type);
  99. $eventManager = new \Doctrine\Common\EventManager();
  100. switch ($normalizedType) {
  101. case 'mysql':
  102. // Send "SET NAMES utf8". Only required on PHP 5.3 below 5.3.6.
  103. // See http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names#4361485
  104. $eventManager->addEventSubscriber(new MysqlSessionInit(
  105. $this->defaultConnectionParams['mysql']['charset']
  106. ));
  107. $eventManager->addEventSubscriber(
  108. new SQLSessionInit("SET SESSION AUTOCOMMIT=1"));
  109. break;
  110. case 'oci':
  111. $eventManager->addEventSubscriber(new OracleSessionInit);
  112. // the driverOptions are unused in dbal and need to be mapped to the parameters
  113. if (isset($additionalConnectionParams['driverOptions'])) {
  114. $additionalConnectionParams = array_merge($additionalConnectionParams, $additionalConnectionParams['driverOptions']);
  115. }
  116. break;
  117. case 'sqlite3':
  118. $journalMode = $additionalConnectionParams['sqlite.journal_mode'];
  119. $additionalConnectionParams['platform'] = new OCSqlitePlatform();
  120. $eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode));
  121. break;
  122. }
  123. $connection = \Doctrine\DBAL\DriverManager::getConnection(
  124. array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams),
  125. new \Doctrine\DBAL\Configuration(),
  126. $eventManager
  127. );
  128. return $connection;
  129. }
  130. /**
  131. * @brief Normalize DBMS type
  132. * @param string $type DBMS type
  133. * @return string Normalized DBMS type
  134. */
  135. public function normalizeType($type) {
  136. return $type === 'sqlite' ? 'sqlite3' : $type;
  137. }
  138. /**
  139. * @brief Checks whether the specified DBMS type is valid.
  140. * @return bool
  141. */
  142. public function isValidType($type) {
  143. $normalizedType = $this->normalizeType($type);
  144. return isset($this->defaultConnectionParams[$normalizedType]);
  145. }
  146. /**
  147. * Create the connection parameters for the config
  148. *
  149. * @param \OC\SystemConfig $config
  150. * @return array
  151. */
  152. public function createConnectionParams($config) {
  153. $type = $config->getValue('dbtype', 'sqlite');
  154. $connectionParams = array(
  155. 'user' => $config->getValue('dbuser', ''),
  156. 'password' => $config->getValue('dbpassword', ''),
  157. );
  158. $name = $config->getValue('dbname', 'owncloud');
  159. if ($this->normalizeType($type) === 'sqlite3') {
  160. $dataDir = $config->getValue("datadirectory", \OC::$SERVERROOT . '/data');
  161. $connectionParams['path'] = $dataDir . '/' . $name . '.db';
  162. } else {
  163. $host = $config->getValue('dbhost', '');
  164. if (strpos($host, ':')) {
  165. // Host variable may carry a port or socket.
  166. list($host, $portOrSocket) = explode(':', $host, 2);
  167. if (ctype_digit($portOrSocket)) {
  168. $connectionParams['port'] = $portOrSocket;
  169. } else {
  170. $connectionParams['unix_socket'] = $portOrSocket;
  171. }
  172. }
  173. $connectionParams['host'] = $host;
  174. $connectionParams['dbname'] = $name;
  175. }
  176. $connectionParams['tablePrefix'] = $config->getValue('dbtableprefix', 'oc_');
  177. $connectionParams['sqlite.journal_mode'] = $config->getValue('sqlite.journal_mode', 'WAL');
  178. //additional driver options, eg. for mysql ssl
  179. $driverOptions = $config->getValue('dbdriveroptions', null);
  180. if ($driverOptions) {
  181. $connectionParams['driverOptions'] = $driverOptions;
  182. }
  183. return $connectionParams;
  184. }
  185. }