Commit a57e05a9 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #394 from easybiblabs/topics/mysqli-driver-params

Enhancement: add support for $driverOptions
parents d79ce18e 11a23e20
...@@ -52,6 +52,8 @@ class MysqliConnection implements Connection ...@@ -52,6 +52,8 @@ class MysqliConnection implements Connection
if (isset($params['charset'])) { if (isset($params['charset'])) {
$this->_conn->set_charset($params['charset']); $this->_conn->set_charset($params['charset']);
} }
$this->setDriverOptions($driverOptions);
} }
/** /**
...@@ -151,4 +153,50 @@ class MysqliConnection implements Connection ...@@ -151,4 +153,50 @@ class MysqliConnection implements Connection
{ {
return $this->_conn->error; return $this->_conn->error;
} }
/**
* Apply the driver options to the connection.
*
* @param array $driverOptions
*
* @throws MysqliException When one of of the options is not supported.
* @throws MysqliException When applying doesn't work - e.g. due to incorrect value.
*/
private function setDriverOptions(array $driverOptions = array())
{
$supportedDriverOptions = array(
\MYSQLI_OPT_CONNECT_TIMEOUT,
\MYSQLI_OPT_LOCAL_INFILE,
\MYSQLI_INIT_COMMAND,
\MYSQLI_READ_DEFAULT_FILE,
\MYSQLI_READ_DEFAULT_GROUP,
);
if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
$supportedDriverOptions[] = \MYSQLI_SERVER_PUBLIC_KEY;
}
$exceptionMsg = "%s option '%s' with value '%s'";
foreach ($driverOptions as $option => $value) {
if (!in_array($option, $supportedDriverOptions, true)) {
throw new MysqliException(
sprintf($exceptionMsg, 'Unsupported', $option, $value)
);
}
if (@mysqli_options($this->_conn, $option, $value)) {
continue;
}
$msg = sprintf($exceptionMsg, 'Failed to set', $option, $value);
$msg .= sprintf(', error: %s (%d)', mysqli_error($this->_conn), mysqli_errno($this->_conn));
throw new MysqliException(
$msg,
mysqli_errno($this->_conn)
);
}
}
} }
<?php
namespace Doctrine\Tests\DBAL\Functional\Mysqli;
class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
public function setUp()
{
if (!extension_loaded('mysqli')) {
$this->markTestSkipped('mysqli is not installed.');
}
$driver = getenv('DB');
if (false !== $driver && $driver !== 'mysqli') {
$this->markTestSkipped('this test case is for mysqli only');
}
$this->resetSharedConn();
parent::setUp();
}
public function tearDown()
{
parent::tearDown();
$this->resetSharedConn();
}
public function testDriverOptions()
{
$driverOptions = array(
\MYSQLI_OPT_CONNECT_TIMEOUT => 1,
);
$connection = $this->getConnection($driverOptions);
$this->assertInstanceOf("\Doctrine\DBAL\Driver\Mysqli\MysqliConnection", $connection);
}
/**
* @expectedException \Doctrine\DBAL\Driver\Mysqli\MysqliException
*/
public function testUnsupportedDriverOption()
{
$this->getConnection(array('hello' => 'world')); // use local infile
}
private function getConnection(array $driverOptions)
{
return new \Doctrine\DBAL\Driver\Mysqli\MysqliConnection(
array(
'host' => $GLOBALS['db_host'],
'dbname' => $GLOBALS['db_name'],
),
$GLOBALS['db_username'],
$GLOBALS['db_password'],
$driverOptions
);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment