# Protected
##########################################################################*/
- /**
- * Parse configuration array into options for PDO constructor.
- *
- * @throws Horde_Db_Exception
- * @return array [dsn, username, password]
- */
- protected function _parseConfig()
+ protected function _checkRequiredConfig()
{
// check required config keys are present
$required = array('adapter', 'username');
if (!isset($this->_config['password'])) {
$this->_config['password'] = '';
}
+ }
- // collect options to build PDO Data Source Name (DSN) string
- $dsnOpts = $this->_config;
- unset($dsnOpts['adapter'], $dsnOpts['username'], $dsnOpts['password']);
-
+ protected function _railsToPdo($params)
+ {
// rewrite rails config key names to pdo equivalents
$rails2pdo = array('database' => 'dbname', 'socket' => 'unix_socket', 'hostspec' => 'host');
foreach ($rails2pdo as $from => $to) {
- if (isset($dsnOpts[$from])) {
- $dsnOpts[$to] = $dsnOpts[$from];
- unset($dsnOpts[$from]);
+ if (isset($params[$from])) {
+ $params[$to] = $params[$from];
+ unset($params[$from]);
}
}
- // build DSN string
+ return $params;
+ }
+
+ protected function _buildDsnString($params)
+ {
+ // build DSN string
$dsn = $this->_config['adapter'] . ':';
- foreach ($dsnOpts as $k => $v) {
+ foreach ($params as $k => $v) {
$dsn .= "$k=$v;";
}
$dsn = rtrim($dsn, ';');
+ return $dsn;
+ }
+
+ /**
+ * Parse configuration array into options for PDO constructor.
+ *
+ * @throws Horde_Db_Exception
+ * @return array [dsn, username, password]
+ */
+ protected function _parseConfig()
+ {
+ $this->_checkRequiredConfig();
+
+ // collect options to build PDO Data Source Name (DSN) string
+ $dsnOpts = $this->_config;
+ unset($dsnOpts['adapter'], $dsnOpts['username'], $dsnOpts['password']);
+ $dsnOpts = $this->_railsToPdo($dsnOpts);
+
+ // build DSN string
+ $dsn = $this->_buildDsnString($dsnOpts);
+
// return DSN and user/pass for connection
return array(
$dsn,
protected function _parseConfig()
{
$this->_config['adapter'] = 'mysql';
+ $this->_checkRequiredConfig();
- if (isset($this->_config['port'])) {
- if (empty($this->_config['host'])) {
+ // collect options to build PDO Data Source Name (DSN) string
+ $dsnOpts = $this->_config;
+ unset($dsnOpts['adapter'], $dsnOpts['username'], $dsnOpts['password']);
+ $dsnOpts = $this->_railsToPdo($dsnOpts);
+
+ if (isset($dsnOpts['port'])) {
+ if (empty($dsnOpts['host'])) {
$msg = 'host is required if port is specified';
throw new Horde_Db_Exception($msg);
}
- if (preg_match('/[^\d\.]/', $this->_config['host'])) {
+ if (preg_match('/[^\d\.]/', $dsnOpts['host'])) {
$msg = 'pdo_mysql ignores port unless IP address is used for host';
throw new Horde_Db_Exception($msg);
}
}
- return parent::_parseConfig();
+ $dsn = $this->_buildDsnString($dsnOpts);
+
+ // return DSN and user/pass for connection
+ return array(
+ $dsn,
+ $this->_config['username'],
+ $this->_config['password']);
}
}
// collect options to build PDO Data Source Name (DSN) string
$dsnOpts = $this->_config;
unset($dsnOpts['adapter'], $dsnOpts['username'], $dsnOpts['password']);
-
- // rewrite rails config key names to pdo equivalents
- $rails2pdo = array('database' => 'dbname');
- foreach ($rails2pdo as $from => $to) {
- if (isset($dsnOpts[$from])) {
- $dsnOpts[$to] = $dsnOpts[$from];
- unset($dsnOpts[$from]);
- }
- }
+ $dsnOpts = $this->_railsToPdo($dsnOpts);
// build DSN string
$dsn = 'sqlite:' . $dsnOpts['dbname'];