allow a dsn override to the database via the server argument. This could be used to allow unix domain sockets and other unusual configurations.

This commit is contained in:
zotlabs 2016-10-20 17:04:43 -07:00
parent 29340152b6
commit 04ac04e0ad
2 changed files with 21 additions and 15 deletions

View File

@ -25,16 +25,14 @@ class DBA {
/** /**
* @brief Returns the database driver object. * @brief Returns the database driver object.
* *
* If available it will use PHP's mysqli otherwise mysql driver. * @param string $server DB server name (or PDO dsn - e.g. mysqli:foobar.com;)
*
* @param string $server DB server name
* @param string $port DB port * @param string $port DB port
* @param string $user DB username * @param string $user DB username
* @param string $pass DB password * @param string $pass DB password
* @param string $db database name * @param string $db database name
* @param string $dbtype 0 for mysql, 1 for postgres * @param string $dbtype 0 for mysql, 1 for postgres
* @param bool $install Defaults to false * @param bool $install Defaults to false
* @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found. * @return null|dba_driver A database driver object (dba_pdo) or null if no driver found.
*/ */
static public function dba_factory($server,$port,$user,$pass,$db,$dbtype,$install = false) { static public function dba_factory($server,$port,$user,$pass,$db,$dbtype,$install = false) {
@ -57,8 +55,6 @@ class DBA {
else { else {
if(!($port)) if(!($port))
$port = 3306; $port = 3306;
if($server === 'localhost')
$server = '127.0.0.1';
} }
require_once('include/dba/dba_pdo.php'); require_once('include/dba/dba_pdo.php');
@ -66,10 +62,16 @@ class DBA {
if(is_object(self::$dba) && self::$dba->connected) { if(is_object(self::$dba) && self::$dba->connected) {
$dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'pgsql' : 'mysql') if(strpbrk($server,':;')) {
. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) $dsn = $server;
. ';dbname=' . $db; }
self::$dba->pdo_set(array($dns,$user,$pass)); else {
$dsn = self::$scheme . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port);
}
$dsn .= ';dbname=' . $db;
self::$dba->pdo_set(array($dsn,$user,$pass));
} }
define('NULL_DATE', self::$null_date); define('NULL_DATE', self::$null_date);

View File

@ -10,14 +10,18 @@ class dba_pdo extends dba_driver {
function connect($server,$scheme,$port,$user,$pass,$db) { function connect($server,$scheme,$port,$user,$pass,$db) {
$this->driver_dbtype = $scheme; $this->driver_dbtype = $scheme;
$dns = $this->driver_dbtype
. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
. ';dbname=' . $db;
// db_logger('dns: ' . $dns); if(strpbrk($server,':;')) {
$dsn = $server;
}
else {
$dsn = $this->driver_dbtype . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port);
}
$dsn .= ';dbname=' . $db;
try { try {
$this->db = new PDO($dns,$user,$pass); $this->db = new PDO($dsn,$user,$pass);
$this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} }
catch(PDOException $e) { catch(PDOException $e) {