Commit da6892e2 authored by till's avatar till

Enhancement: add support for $driverOptions

 * introduced $_supportedDriverOptions to validate what is passed into the object
 * added `setDrivrOptions()` which sets the array and throws exceptions
parent 0e4de6ba
......@@ -31,6 +31,20 @@ class MysqliConnection implements Connection
*/
private $_conn;
/**
* @var array
*/
private $_driverOptions;
private $_supportedDriverOpions = array(
\MYSQLI_OPT_CONNECT_TIMEOUT,
\MYSQLI_OPT_LOCAL_INFILE,
\MYSQLI_INIT_COMMAND,
\MYSQLI_READ_DEFAULT_FILE,
\MYSQLI_READ_DEFAULT_GROUP,
//\MYSQLI_SERVER_PUBLIC_KEY,
);
/**
* @param array $params
* @param string $username
......@@ -52,6 +66,8 @@ class MysqliConnection implements Connection
if (isset($params['charset'])) {
$this->_conn->set_charset($params['charset']);
}
$this->setDriverOptions($driverOptions);
}
/**
......@@ -151,4 +167,38 @@ class MysqliConnection implements Connection
{
return $this->_conn->error;
}
/**
* Apply the driver options to the connection.
*
* @param array $driverOptions
*
* @return MysqliConnection
*
* @throws MysqliException When one of of the options is not supported.
* @throws MysqliException When applying doesn't work - e.g. due to incorrect value.
*/
public function setDriverOptions(array $driverOptions = array())
{
$this->_driverOptions = $driverOptions;
static $exceptionMsg = "%s option '%s' with value '%s'";
foreach ($driverOptions as $option => $value) {
if (!in_array($option, $this->_supportedDriverOpions)) {
throw new MysqliException(
sprintf($exceptionMsg, 'Unsupported', $option, $value)
);
}
if (!mysqli_options($this->_conn, $option, $value)) {
throw new MysqliException(
sprintf($exceptionMsg, 'Failed to set', $option, $value)
);
}
}
return $this;
}
}
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