Skip to content

Commit

Permalink
Executing multiple init commands rather than using PDO::MYSQL_ATTR_IN…
Browse files Browse the repository at this point in the history
…IT_COMMAND.

PDO::MYSQL_ATTR_INIT_COMMAND does not support multiple commands as reported in https://bugs.php.net/bug.php?id=48859
  • Loading branch information
renan committed Aug 21, 2014
1 parent e02e4f3 commit 89bb1fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/Database/Driver/Mysql.php
Expand Up @@ -66,10 +66,6 @@ public function connect() {
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

if ($config['init']) {
$config['flags'] += [PDO::MYSQL_ATTR_INIT_COMMAND => implode(';', (array)$config['init'])];
}

if (!empty($config['ssl_key']) && !empty($config['ssl_cert'])) {
$config['flags'][PDO::MYSQL_ATTR_SSL_KEY] = $config['ssl_key'];
$config['flags'][PDO::MYSQL_ATTR_SSL_CERT] = $config['ssl_cert'];
Expand All @@ -86,7 +82,14 @@ public function connect() {
}
}

return $this->_connect($config);
$this->_connect($config);

if (!empty($config['init'])) {
foreach ((array)$config['init'] as $command) {
$this->connection()->exec($command);
}
}
return true;
}

/**
Expand Down
14 changes: 11 additions & 3 deletions tests/TestCase/Database/Driver/MysqlTest.php
Expand Up @@ -88,7 +88,7 @@ public function testConnectionConfigCustom() {
];
$driver = $this->getMock(
'Cake\Database\Driver\Mysql',
['_connect'],
['_connect', 'connection'],
[$config]
);
$expected = $config;
Expand All @@ -98,11 +98,19 @@ public function testConnectionConfigCustom() {
PDO::ATTR_PERSISTENT => false,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "Execute this;this too;SET time_zone = 'Antartica'"
];

$connection = $this->getMock('StdClass', ['exec']);
$connection->expects($this->at(0))->method('exec')->with('Execute this');
$connection->expects($this->at(1))->method('exec')->with('this too');
$connection->expects($this->at(2))->method('exec')->with("SET time_zone = 'Antartica'");
$connection->expects($this->exactly(3))->method('exec');

$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->connect();
$driver->expects($this->any())->method('connection')
->will($this->returnValue($connection));
$driver->connect($config);
}

}

0 comments on commit 89bb1fa

Please sign in to comment.