Merge pull request #3927 from nextcloud/minor-fixes

Namespace and array syntax fixes
This commit is contained in:
Morris Jobke 2017-03-19 15:49:38 -06:00 committed by GitHub
commit c02527e414
8 changed files with 57 additions and 39 deletions

View File

@ -143,6 +143,8 @@ function cleanup_config {
if [ -f config/autotest-storage-swift.config.php ]; then if [ -f config/autotest-storage-swift.config.php ]; then
rm config/autotest-storage-swift.config.php rm config/autotest-storage-swift.config.php
fi fi
# Remove mysqlmb4.config.php
rm -f config/mysqlmb4.config.php
} }
# restore config on exit # restore config on exit

View File

@ -25,9 +25,11 @@
*/ */
namespace OC\DB; namespace OC\DB;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Event\Listeners\OracleSessionInit; use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
use Doctrine\DBAL\Event\Listeners\SQLSessionInit; use Doctrine\DBAL\Event\Listeners\SQLSessionInit;
use Doctrine\DBAL\Event\Listeners\MysqlSessionInit;
use OCP\IConfig; use OCP\IConfig;
/** /**
@ -40,30 +42,30 @@ class ConnectionFactory {
* Array mapping DBMS type to default connection parameters passed to * Array mapping DBMS type to default connection parameters passed to
* \Doctrine\DBAL\DriverManager::getConnection(). * \Doctrine\DBAL\DriverManager::getConnection().
*/ */
protected $defaultConnectionParams = array( protected $defaultConnectionParams = [
'mysql' => array( 'mysql' => [
'adapter' => '\OC\DB\AdapterMySQL', 'adapter' => '\OC\DB\AdapterMySQL',
'charset' => 'UTF8', 'charset' => 'UTF8',
'driver' => 'pdo_mysql', 'driver' => 'pdo_mysql',
'wrapperClass' => 'OC\DB\Connection', 'wrapperClass' => 'OC\DB\Connection',
), ],
'oci' => array( 'oci' => [
'adapter' => '\OC\DB\AdapterOCI8', 'adapter' => '\OC\DB\AdapterOCI8',
'charset' => 'AL32UTF8', 'charset' => 'AL32UTF8',
'driver' => 'oci8', 'driver' => 'oci8',
'wrapperClass' => 'OC\DB\OracleConnection', 'wrapperClass' => 'OC\DB\OracleConnection',
), ],
'pgsql' => array( 'pgsql' => [
'adapter' => '\OC\DB\AdapterPgSql', 'adapter' => '\OC\DB\AdapterPgSql',
'driver' => 'pdo_pgsql', 'driver' => 'pdo_pgsql',
'wrapperClass' => 'OC\DB\Connection', 'wrapperClass' => 'OC\DB\Connection',
), ],
'sqlite3' => array( 'sqlite3' => [
'adapter' => '\OC\DB\AdapterSqlite', 'adapter' => '\OC\DB\AdapterSqlite',
'driver' => 'pdo_sqlite', 'driver' => 'pdo_sqlite',
'wrapperClass' => 'OC\DB\Connection', 'wrapperClass' => 'OC\DB\Connection',
), ],
); ];
public function __construct(IConfig $config) { public function __construct(IConfig $config) {
if($config->getSystemValue('mysql.utf8mb4', false)) { if($config->getSystemValue('mysql.utf8mb4', false)) {
@ -101,14 +103,9 @@ class ConnectionFactory {
*/ */
public function getConnection($type, $additionalConnectionParams) { public function getConnection($type, $additionalConnectionParams) {
$normalizedType = $this->normalizeType($type); $normalizedType = $this->normalizeType($type);
$eventManager = new \Doctrine\Common\EventManager(); $eventManager = new EventManager();
switch ($normalizedType) { switch ($normalizedType) {
case 'mysql': 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
$eventManager->addEventSubscriber(new MysqlSessionInit(
$this->defaultConnectionParams['mysql']['charset']
));
$eventManager->addEventSubscriber( $eventManager->addEventSubscriber(
new SQLSessionInit("SET SESSION AUTOCOMMIT=1")); new SQLSessionInit("SET SESSION AUTOCOMMIT=1"));
break; break;
@ -125,9 +122,10 @@ class ConnectionFactory {
$eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode)); $eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode));
break; break;
} }
$connection = \Doctrine\DBAL\DriverManager::getConnection( /** @var Connection $connection */
$connection = DriverManager::getConnection(
array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams), array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams),
new \Doctrine\DBAL\Configuration(), new Configuration(),
$eventManager $eventManager
); );
return $connection; return $connection;
@ -143,9 +141,11 @@ class ConnectionFactory {
} }
/** /**
* @brief Checks whether the specified DBMS type is valid. * Checks whether the specified DBMS type is valid.
* @return bool *
*/ * @param string $type
* @return bool
*/
public function isValidType($type) { public function isValidType($type) {
$normalizedType = $this->normalizeType($type); $normalizedType = $this->normalizeType($type);
return isset($this->defaultConnectionParams[$normalizedType]); return isset($this->defaultConnectionParams[$normalizedType]);
@ -160,10 +160,10 @@ class ConnectionFactory {
public function createConnectionParams($config) { public function createConnectionParams($config) {
$type = $config->getValue('dbtype', 'sqlite'); $type = $config->getValue('dbtype', 'sqlite');
$connectionParams = array( $connectionParams = [
'user' => $config->getValue('dbuser', ''), 'user' => $config->getValue('dbuser', ''),
'password' => $config->getValue('dbpassword', ''), 'password' => $config->getValue('dbpassword', ''),
); ];
$name = $config->getValue('dbname', 'owncloud'); $name = $config->getValue('dbname', 'owncloud');
if ($this->normalizeType($type) === 'sqlite3') { if ($this->normalizeType($type) === 'sqlite3') {

View File

@ -34,6 +34,7 @@ namespace OC\DB;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\SchemaConfig; use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use OCP\IConfig; use OCP\IConfig;
class MDB2SchemaReader { class MDB2SchemaReader {
@ -77,7 +78,7 @@ class MDB2SchemaReader {
/** /**
* @param string $file * @param string $file
* @return \Doctrine\DBAL\Schema\Schema * @return Schema
* @throws \DomainException * @throws \DomainException
*/ */
public function loadSchemaFromFile($file) { public function loadSchemaFromFile($file) {
@ -284,6 +285,7 @@ class MDB2SchemaReader {
) { ) {
$options['primary'] = true; $options['primary'] = true;
} }
$table->addColumn($name, $type, $options); $table->addColumn($name, $type, $options);
if (!empty($options['primary']) && $options['primary']) { if (!empty($options['primary']) && $options['primary']) {
$table->setPrimaryKey(array($name)); $table->setPrimaryKey(array($name));

View File

@ -67,7 +67,7 @@ class Collation implements IRepairStep {
*/ */
public function run(IOutput $output) { public function run(IOutput $output) {
if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
$output->info('Not a mysql database -> nothing to no'); $output->info('Not a mysql database -> nothing to do');
return; return;
} }

View File

@ -80,13 +80,13 @@ class Setup {
$this->random = $random; $this->random = $random;
} }
static $dbSetupClasses = array( static $dbSetupClasses = [
'mysql' => '\OC\Setup\MySQL', 'mysql' => \OC\Setup\MySQL::class,
'pgsql' => '\OC\Setup\PostgreSQL', 'pgsql' => \OC\Setup\PostgreSQL::class,
'oci' => '\OC\Setup\OCI', 'oci' => \OC\Setup\OCI::class,
'sqlite' => '\OC\Setup\Sqlite', 'sqlite' => \OC\Setup\Sqlite::class,
'sqlite3' => '\OC\Setup\Sqlite', 'sqlite3' => \OC\Setup\Sqlite::class,
); ];
/** /**
* Wrapper around the "class_exists" PHP function to be able to mock it * Wrapper around the "class_exists" PHP function to be able to mock it

View File

@ -27,7 +27,6 @@
*/ */
namespace OC\Setup; namespace OC\Setup;
use OC\DB\ConnectionFactory;
use OCP\IDBConnection; use OCP\IDBConnection;
class MySQL extends AbstractDatabase { class MySQL extends AbstractDatabase {

View File

@ -3,3 +3,6 @@
innodb_large_prefix=true innodb_large_prefix=true
innodb_file_format=barracuda innodb_file_format=barracuda
innodb_file_per_table=true innodb_file_per_table=true
innodb_buffer_pool_size = 512M
innodb_flush_log_at_trx_commit = 2

View File

@ -10,18 +10,30 @@
namespace Test\DB; namespace Test\DB;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use OC\DB\MDB2SchemaReader;
use OCP\IConfig;
use Test\TestCase;
class MDB2SchemaReaderTest extends \Test\TestCase { /**
* Class MDB2SchemaReaderTest
*
* @group DB
*
* @package Test\DB
*/
class MDB2SchemaReaderTest extends TestCase {
/** /**
* @var \OC\DB\MDB2SchemaReader $reader * @var MDB2SchemaReader $reader
*/ */
protected $reader; protected $reader;
/** /**
* @return \OC\Config * @return IConfig
*/ */
protected function getConfig() { protected function getConfig() {
$config = $this->getMockBuilder('\OCP\IConfig') /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */
$config = $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$config->expects($this->any()) $config->expects($this->any())
@ -34,7 +46,7 @@ class MDB2SchemaReaderTest extends \Test\TestCase {
} }
public function testRead() { public function testRead() {
$reader = new \OC\DB\MDB2SchemaReader($this->getConfig(), new MySqlPlatform()); $reader = new MDB2SchemaReader($this->getConfig(), new MySqlPlatform());
$schema = $reader->loadSchemaFromFile(__DIR__ . '/testschema.xml'); $schema = $reader->loadSchemaFromFile(__DIR__ . '/testschema.xml');
$this->assertCount(1, $schema->getTables()); $this->assertCount(1, $schema->getTables());