2014-03-31 18:00:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-26 10:37:37 +00:00
|
|
|
* Copyright (c) 2014 Andreas Fischer <bantu@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
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;
|
2015-01-19 16:20:49 +00:00
|
|
|
use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
|
|
|
|
use Doctrine\DBAL\Event\Listeners\SQLSessionInit;
|
|
|
|
use Doctrine\DBAL\Event\Listeners\MysqlSessionInit;
|
2014-03-31 18:00:44 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-12 19:58:09 +00:00
|
|
|
* Takes care of creating and configuring Doctrine connections.
|
2014-03-31 18:00:44 +00:00
|
|
|
*/
|
|
|
|
class ConnectionFactory {
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*
|
|
|
|
* Array mapping DBMS type to default connection parameters passed to
|
|
|
|
* \Doctrine\DBAL\DriverManager::getConnection().
|
|
|
|
*/
|
|
|
|
protected $defaultConnectionParams = array(
|
|
|
|
'mssql' => array(
|
|
|
|
'adapter' => '\OC\DB\AdapterSQLSrv',
|
|
|
|
'charset' => 'UTF8',
|
|
|
|
'driver' => 'pdo_sqlsrv',
|
|
|
|
'wrapperClass' => 'OC\DB\Connection',
|
|
|
|
),
|
|
|
|
'mysql' => array(
|
2014-09-09 11:57:02 +00:00
|
|
|
'adapter' => '\OC\DB\AdapterMySQL',
|
2014-03-31 18:00:44 +00:00
|
|
|
'charset' => 'UTF8',
|
|
|
|
'driver' => 'pdo_mysql',
|
|
|
|
'wrapperClass' => 'OC\DB\Connection',
|
|
|
|
),
|
|
|
|
'oci' => array(
|
|
|
|
'adapter' => '\OC\DB\AdapterOCI8',
|
|
|
|
'charset' => 'AL32UTF8',
|
|
|
|
'driver' => 'oci8',
|
|
|
|
'wrapperClass' => 'OC\DB\OracleConnection',
|
|
|
|
),
|
|
|
|
'pgsql' => array(
|
|
|
|
'adapter' => '\OC\DB\AdapterPgSql',
|
|
|
|
'driver' => 'pdo_pgsql',
|
|
|
|
'wrapperClass' => 'OC\DB\Connection',
|
|
|
|
),
|
|
|
|
'sqlite3' => array(
|
|
|
|
'adapter' => '\OC\DB\AdapterSqlite',
|
|
|
|
'driver' => 'pdo_sqlite',
|
|
|
|
'wrapperClass' => 'OC\DB\Connection',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get default connection parameters for a given DBMS.
|
|
|
|
* @param string $type DBMS type
|
|
|
|
* @param array $additionalConnectionParams Additional connection parameters
|
|
|
|
* @return \OC\DB\Connection
|
|
|
|
*/
|
|
|
|
public function getConnection($type, $additionalConnectionParams) {
|
|
|
|
$normalizedType = $this->normalizeType($type);
|
|
|
|
$eventManager = new \Doctrine\Common\EventManager();
|
|
|
|
switch ($normalizedType) {
|
|
|
|
case 'mysql':
|
|
|
|
// Send "SET NAMES utf8". Only required on PHP 5.3 below 5.3.6.
|
|
|
|
// See http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names#4361485
|
2015-01-19 16:20:49 +00:00
|
|
|
$eventManager->addEventSubscriber(new MysqlSessionInit);
|
|
|
|
$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);
|
2014-03-31 18:00:44 +00:00
|
|
|
break;
|
2014-09-17 11:47:33 +00:00
|
|
|
case 'sqlite3':
|
2014-10-29 16:23:27 +00:00
|
|
|
$journalMode = $additionalConnectionParams['sqlite.journal_mode'];
|
|
|
|
$eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode));
|
2014-09-17 11:47:33 +00:00
|
|
|
break;
|
2014-03-31 18:00:44 +00:00
|
|
|
}
|
|
|
|
$connection = \Doctrine\DBAL\DriverManager::getConnection(
|
|
|
|
array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams),
|
|
|
|
new \Doctrine\DBAL\Configuration(),
|
|
|
|
$eventManager
|
|
|
|
);
|
|
|
|
return $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Normalize DBMS type
|
|
|
|
* @param string $type DBMS type
|
|
|
|
* @return string Normalized DBMS type
|
|
|
|
*/
|
|
|
|
public function normalizeType($type) {
|
|
|
|
return $type === 'sqlite' ? 'sqlite3' : $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-12 19:58:09 +00:00
|
|
|
* @brief Checks whether the specified DBMS type is valid.
|
2014-03-31 18:00:44 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2014-11-27 15:40:12 +00:00
|
|
|
* @param \OC\SystemConfig $config
|
2014-09-10 11:24:49 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function createConnectionParams($config) {
|
2014-11-27 15:40:12 +00:00
|
|
|
$type = $config->getValue('dbtype', 'sqlite');
|
2014-09-10 11:24:49 +00:00
|
|
|
|
|
|
|
$connectionParams = array(
|
2014-11-27 15:40:12 +00:00
|
|
|
'user' => $config->getValue('dbuser', ''),
|
|
|
|
'password' => $config->getValue('dbpassword', ''),
|
2014-09-10 11:24:49 +00:00
|
|
|
);
|
2014-11-27 15:40:12 +00:00
|
|
|
$name = $config->getValue('dbname', 'owncloud');
|
2014-09-10 11:24:49 +00:00
|
|
|
|
|
|
|
if ($this->normalizeType($type) === 'sqlite3') {
|
2015-01-19 16:20:49 +00:00
|
|
|
$dataDir = $config->getValue("datadirectory", \OC::$SERVERROOT . '/data');
|
|
|
|
$connectionParams['path'] = $dataDir . '/' . $name . '.db';
|
2014-09-10 11:24:49 +00:00
|
|
|
} else {
|
2014-11-27 15:40:12 +00:00
|
|
|
$host = $config->getValue('dbhost', '');
|
2014-09-10 11:24:49 +00:00
|
|
|
if (strpos($host, ':')) {
|
|
|
|
// Host variable may carry a port or socket.
|
|
|
|
list($host, $portOrSocket) = explode(':', $host, 2);
|
|
|
|
if (ctype_digit($portOrSocket)) {
|
|
|
|
$connectionParams['port'] = $portOrSocket;
|
|
|
|
} else {
|
|
|
|
$connectionParams['unix_socket'] = $portOrSocket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$connectionParams['host'] = $host;
|
|
|
|
$connectionParams['dbname'] = $name;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:40:12 +00:00
|
|
|
$connectionParams['tablePrefix'] = $config->getValue('dbtableprefix', 'oc_');
|
|
|
|
$connectionParams['sqlite.journal_mode'] = $config->getValue('sqlite.journal_mode', 'WAL');
|
2014-10-27 11:30:29 +00:00
|
|
|
|
|
|
|
//additional driver options, eg. for mysql ssl
|
2014-11-27 15:40:12 +00:00
|
|
|
$driverOptions = $config->getValue('dbdriveroptions', null);
|
2014-10-27 11:30:29 +00:00
|
|
|
if ($driverOptions) {
|
|
|
|
$connectionParams['driverOptions'] = $driverOptions;
|
|
|
|
}
|
|
|
|
|
2014-09-10 11:24:49 +00:00
|
|
|
return $connectionParams;
|
|
|
|
}
|
2014-03-31 18:00:44 +00:00
|
|
|
}
|