2014-03-31 18:00:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-03-31 18:00:44 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-03-31 18:00:44 +00:00
|
|
|
namespace OC\DB;
|
2017-04-10 15:58:52 +00:00
|
|
|
|
2017-03-19 20:52:54 +00:00
|
|
|
use Doctrine\Common\EventManager;
|
|
|
|
use Doctrine\DBAL\Configuration;
|
|
|
|
use Doctrine\DBAL\DriverManager;
|
2015-01-19 16:20:49 +00:00
|
|
|
use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
|
|
|
|
use Doctrine\DBAL\Event\Listeners\SQLSessionInit;
|
2017-03-17 22:37:48 +00:00
|
|
|
use OC\SystemConfig;
|
2014-03-31 18:00:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-10 15:58:52 +00:00
|
|
|
* Takes care of creating and configuring Doctrine connections.
|
|
|
|
*/
|
2014-03-31 18:00:44 +00:00
|
|
|
class ConnectionFactory {
|
|
|
|
/**
|
2017-04-10 15:58:52 +00:00
|
|
|
* @var array
|
|
|
|
*
|
|
|
|
* Array mapping DBMS type to default connection parameters passed to
|
|
|
|
* \Doctrine\DBAL\DriverManager::getConnection().
|
|
|
|
*/
|
2017-03-19 20:52:54 +00:00
|
|
|
protected $defaultConnectionParams = [
|
|
|
|
'mysql' => [
|
2018-01-25 22:16:13 +00:00
|
|
|
'adapter' => AdapterMySQL::class,
|
2014-03-31 18:00:44 +00:00
|
|
|
'charset' => 'UTF8',
|
|
|
|
'driver' => 'pdo_mysql',
|
2018-01-25 22:16:13 +00:00
|
|
|
'wrapperClass' => Connection::class,
|
2017-03-19 20:52:54 +00:00
|
|
|
],
|
|
|
|
'oci' => [
|
2018-01-25 22:16:13 +00:00
|
|
|
'adapter' => AdapterOCI8::class,
|
2014-03-31 18:00:44 +00:00
|
|
|
'charset' => 'AL32UTF8',
|
|
|
|
'driver' => 'oci8',
|
2018-01-25 22:16:13 +00:00
|
|
|
'wrapperClass' => OracleConnection::class,
|
2017-03-19 20:52:54 +00:00
|
|
|
],
|
|
|
|
'pgsql' => [
|
2018-01-25 22:16:13 +00:00
|
|
|
'adapter' => AdapterPgSql::class,
|
2014-03-31 18:00:44 +00:00
|
|
|
'driver' => 'pdo_pgsql',
|
2018-01-25 22:16:13 +00:00
|
|
|
'wrapperClass' => Connection::class,
|
2017-03-19 20:52:54 +00:00
|
|
|
],
|
|
|
|
'sqlite3' => [
|
2018-01-25 22:16:13 +00:00
|
|
|
'adapter' => AdapterSqlite::class,
|
2014-03-31 18:00:44 +00:00
|
|
|
'driver' => 'pdo_sqlite',
|
2018-01-25 22:16:13 +00:00
|
|
|
'wrapperClass' => Connection::class,
|
2017-03-19 20:52:54 +00:00
|
|
|
],
|
|
|
|
];
|
2014-03-31 18:00:44 +00:00
|
|
|
|
2017-03-17 22:37:48 +00:00
|
|
|
/** @var SystemConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConnectionFactory constructor.
|
|
|
|
*
|
|
|
|
* @param SystemConfig $systemConfig
|
|
|
|
*/
|
|
|
|
public function __construct(SystemConfig $systemConfig) {
|
|
|
|
$this->config = $systemConfig;
|
2017-04-10 15:58:52 +00:00
|
|
|
if ($this->config->getValue('mysql.utf8mb4', false)) {
|
2016-10-18 08:22:02 +00:00
|
|
|
$this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4';
|
2015-07-30 11:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:00:44 +00:00
|
|
|
/**
|
2017-04-10 15:58:52 +00:00
|
|
|
* @brief Get default connection parameters for a given DBMS.
|
|
|
|
* @param string $type DBMS type
|
|
|
|
* @throws \InvalidArgumentException If $type is invalid
|
|
|
|
* @return array Default connection parameters.
|
|
|
|
*/
|
2014-03-31 18:00:44 +00:00
|
|
|
public function getDefaultConnectionParams($type) {
|
|
|
|
$normalizedType = $this->normalizeType($type);
|
|
|
|
if (!isset($this->defaultConnectionParams[$normalizedType])) {
|
|
|
|
throw new \InvalidArgumentException("Unsupported type: $type");
|
|
|
|
}
|
2014-07-16 12:35:47 +00:00
|
|
|
$result = $this->defaultConnectionParams[$normalizedType];
|
2014-07-16 13:06:59 +00:00
|
|
|
// \PDO::MYSQL_ATTR_FOUND_ROWS may not be defined, e.g. when the MySQL
|
|
|
|
// driver is missing. In this case, we won't be able to connect anyway.
|
2014-07-16 12:35:47 +00:00
|
|
|
if ($normalizedType === 'mysql' && defined('\PDO::MYSQL_ATTR_FOUND_ROWS')) {
|
|
|
|
$result['driverOptions'] = array(
|
|
|
|
\PDO::MYSQL_ATTR_FOUND_ROWS => true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $result;
|
2014-03-31 18:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-10 15:58:52 +00:00
|
|
|
* @brief Get default connection parameters for a given DBMS.
|
|
|
|
* @param string $type DBMS type
|
|
|
|
* @param array $additionalConnectionParams Additional connection parameters
|
|
|
|
* @return \OC\DB\Connection
|
|
|
|
*/
|
2014-03-31 18:00:44 +00:00
|
|
|
public function getConnection($type, $additionalConnectionParams) {
|
|
|
|
$normalizedType = $this->normalizeType($type);
|
2017-03-19 20:52:54 +00:00
|
|
|
$eventManager = new EventManager();
|
2014-03-31 18:00:44 +00:00
|
|
|
switch ($normalizedType) {
|
|
|
|
case 'mysql':
|
2015-01-19 16:20:49 +00:00
|
|
|
$eventManager->addEventSubscriber(
|
|
|
|
new SQLSessionInit("SET SESSION AUTOCOMMIT=1"));
|
2014-03-31 18:00:44 +00:00
|
|
|
break;
|
|
|
|
case 'oci':
|
2015-01-19 16:20:49 +00:00
|
|
|
$eventManager->addEventSubscriber(new OracleSessionInit);
|
2016-06-10 12:07:31 +00:00
|
|
|
// the driverOptions are unused in dbal and need to be mapped to the parameters
|
|
|
|
if (isset($additionalConnectionParams['driverOptions'])) {
|
|
|
|
$additionalConnectionParams = array_merge($additionalConnectionParams, $additionalConnectionParams['driverOptions']);
|
|
|
|
}
|
2017-04-10 15:58:52 +00:00
|
|
|
$host = $additionalConnectionParams['host'];
|
|
|
|
$port = isset($additionalConnectionParams['port']) ? $additionalConnectionParams['port'] : null;
|
|
|
|
$dbName = $additionalConnectionParams['dbname'];
|
|
|
|
|
|
|
|
// we set the connect string as dbname and unset the host to coerce doctrine into using it as connect string
|
|
|
|
if ($host === '') {
|
|
|
|
$additionalConnectionParams['dbname'] = $dbName; // use dbname as easy connect name
|
|
|
|
} else {
|
|
|
|
$additionalConnectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName;
|
|
|
|
}
|
|
|
|
unset($additionalConnectionParams['host']);
|
2014-03-31 18:00:44 +00:00
|
|
|
break;
|
2017-09-29 09:07:50 +00:00
|
|
|
|
2014-09-17 11:47:33 +00:00
|
|
|
case 'sqlite3':
|
2014-10-29 16:23:27 +00:00
|
|
|
$journalMode = $additionalConnectionParams['sqlite.journal_mode'];
|
2015-03-04 14:05:30 +00:00
|
|
|
$additionalConnectionParams['platform'] = new OCSqlitePlatform();
|
2014-10-29 16:23:27 +00:00
|
|
|
$eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode));
|
2014-09-17 11:47:33 +00:00
|
|
|
break;
|
2014-03-31 18:00:44 +00:00
|
|
|
}
|
2017-03-19 20:52:54 +00:00
|
|
|
/** @var Connection $connection */
|
|
|
|
$connection = DriverManager::getConnection(
|
2014-03-31 18:00:44 +00:00
|
|
|
array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams),
|
2017-03-19 20:52:54 +00:00
|
|
|
new Configuration(),
|
2014-03-31 18:00:44 +00:00
|
|
|
$eventManager
|
|
|
|
);
|
|
|
|
return $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-10 15:58:52 +00:00
|
|
|
* @brief Normalize DBMS type
|
|
|
|
* @param string $type DBMS type
|
|
|
|
* @return string Normalized DBMS type
|
|
|
|
*/
|
2014-03-31 18:00:44 +00:00
|
|
|
public function normalizeType($type) {
|
|
|
|
return $type === 'sqlite' ? 'sqlite3' : $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-19 20:52:54 +00:00
|
|
|
* Checks whether the specified DBMS type is valid.
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-03-31 18:00:44 +00:00
|
|
|
public function isValidType($type) {
|
|
|
|
$normalizedType = $this->normalizeType($type);
|
|
|
|
return isset($this->defaultConnectionParams[$normalizedType]);
|
|
|
|
}
|
2014-09-10 11:24:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the connection parameters for the config
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-03-17 22:37:48 +00:00
|
|
|
public function createConnectionParams() {
|
|
|
|
$type = $this->config->getValue('dbtype', 'sqlite');
|
2014-09-10 11:24:49 +00:00
|
|
|
|
2017-03-19 20:52:54 +00:00
|
|
|
$connectionParams = [
|
2017-03-17 22:37:48 +00:00
|
|
|
'user' => $this->config->getValue('dbuser', ''),
|
|
|
|
'password' => $this->config->getValue('dbpassword', ''),
|
2017-03-19 20:52:54 +00:00
|
|
|
];
|
2017-03-17 22:37:48 +00:00
|
|
|
$name = $this->config->getValue('dbname', 'owncloud');
|
2014-09-10 11:24:49 +00:00
|
|
|
|
|
|
|
if ($this->normalizeType($type) === 'sqlite3') {
|
2017-03-17 22:37:48 +00:00
|
|
|
$dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data');
|
2015-01-19 16:20:49 +00:00
|
|
|
$connectionParams['path'] = $dataDir . '/' . $name . '.db';
|
2014-09-10 11:24:49 +00:00
|
|
|
} else {
|
2017-03-17 22:37:48 +00:00
|
|
|
$host = $this->config->getValue('dbhost', '');
|
2018-04-19 10:49:36 +00:00
|
|
|
$connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host));
|
2014-09-10 11:24:49 +00:00
|
|
|
$connectionParams['dbname'] = $name;
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:37:48 +00:00
|
|
|
$connectionParams['tablePrefix'] = $this->config->getValue('dbtableprefix', 'oc_');
|
|
|
|
$connectionParams['sqlite.journal_mode'] = $this->config->getValue('sqlite.journal_mode', 'WAL');
|
2014-10-27 11:30:29 +00:00
|
|
|
|
|
|
|
//additional driver options, eg. for mysql ssl
|
2017-03-17 22:37:48 +00:00
|
|
|
$driverOptions = $this->config->getValue('dbdriveroptions', null);
|
2014-10-27 11:30:29 +00:00
|
|
|
if ($driverOptions) {
|
|
|
|
$connectionParams['driverOptions'] = $driverOptions;
|
|
|
|
}
|
|
|
|
|
2015-07-30 08:57:16 +00:00
|
|
|
// set default table creation options
|
|
|
|
$connectionParams['defaultTableOptions'] = [
|
|
|
|
'collate' => 'utf8_bin',
|
|
|
|
'tablePrefix' => $connectionParams['tablePrefix']
|
|
|
|
];
|
|
|
|
|
2017-04-10 15:58:52 +00:00
|
|
|
if ($this->config->getValue('mysql.utf8mb4', false)) {
|
2015-07-30 08:57:16 +00:00
|
|
|
$connectionParams['defaultTableOptions'] = [
|
|
|
|
'collate' => 'utf8mb4_bin',
|
|
|
|
'charset' => 'utf8mb4',
|
|
|
|
'row_format' => 'compressed',
|
|
|
|
'tablePrefix' => $connectionParams['tablePrefix']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2014-09-10 11:24:49 +00:00
|
|
|
return $connectionParams;
|
|
|
|
}
|
2018-04-19 10:49:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $host
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function splitHostFromPortAndSocket($host): array {
|
|
|
|
$params = [
|
|
|
|
'host' => $host,
|
|
|
|
];
|
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
if (preg_match('/^(.*):([^\]:]+)$/', $host, $matches)) {
|
|
|
|
// Host variable carries a port or socket.
|
|
|
|
$params['host'] = $matches[1];
|
|
|
|
if (is_numeric($matches[2])) {
|
|
|
|
$params['port'] = (int) $matches[2];
|
|
|
|
} else {
|
|
|
|
$params['unix_socket'] = $matches[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
2014-03-31 18:00:44 +00:00
|
|
|
}
|