Simplified db connection initialisation code

pull/80/head
Aleksander Machniak 12 years ago
parent 66407a75d1
commit d186405c00

@ -100,27 +100,15 @@ class rcube_db
$this->db_dsnw_array = self::parse_dsn($db_dsnw);
$this->db_dsnr_array = self::parse_dsn($db_dsnr);
// Initialize driver class
$this->init();
}
/**
* Initialization of the object with driver specific code
*/
protected function init()
{
// To be used by driver classes
}
/**
* Connect to specific database
*
* @param array $dsn DSN for DB connections
*
* @return PDO database handle
* @param array $dsn DSN for DB connections
* @param string $mode Connection mode (r|w)
*/
protected function dsn_connect($dsn)
protected function dsn_connect($dsn, $mode)
{
$this->db_error = false;
$this->db_error_msg = null;
@ -158,9 +146,10 @@ class rcube_db
return null;
}
$this->dbh = $dbh;
$this->db_mode = $mode;
$this->db_connected = true;
$this->conn_configure($dsn, $dbh);
return $dbh;
}
/**
@ -182,16 +171,6 @@ class rcube_db
{
}
/**
* Driver-specific database character set setting
*
* @param string $charset Character set name
*/
protected function set_charset($charset)
{
$this->query("SET NAMES 'utf8'");
}
/**
* Connect to appropriate database depending on the operation
*
@ -219,23 +198,14 @@ class rcube_db
$dsn = ($mode == 'r') ? $this->db_dsnr_array : $this->db_dsnw_array;
$this->dbh = $this->dsn_connect($dsn);
$this->db_connected = is_object($this->dbh);
$this->dsn_connect($dsn, $mode);
// use write-master when read-only fails
if (!$this->db_connected && $mode == 'r' && $this->is_replicated()) {
$mode = 'w';
$this->dbh = $this->dsn_connect($this->db_dsnw_array);
$this->db_connected = is_object($this->dbh);
$this->dsn_connect($this->db_dsnw_array, 'w');
}
if ($this->db_connected) {
$this->db_mode = $mode;
$this->set_charset('utf8');
}
else {
$this->conn_failure = true;
}
$this->conn_failure = !$this->db_connected;
}
/**

@ -29,22 +29,20 @@ class rcube_db_mssql extends rcube_db
public $db_provider = 'mssql';
/**
* Driver initialization
* Object constructor
*
* @param string $db_dsnw DSN for read/write operations
* @param string $db_dsnr Optional DSN for read only operations
* @param bool $pconn Enables persistent connections
*/
protected function init()
public function __construct($db_dsnw, $db_dsnr = '', $pconn = false)
{
parent::__construct($db_dsnw, $db_dsnr, $pconn);
$this->options['identifier_start'] = '[';
$this->options['identifier_end'] = ']';
}
/**
* Character setting
*/
protected function set_charset($charset)
{
// UTF-8 is default
}
/**
* Driver-specific configuration of database connection
*

@ -30,9 +30,13 @@ class rcube_db_mysql extends rcube_db
public $db_provider = 'mysql';
/**
* Driver initialization/configuration
* Object constructor
*
* @param string $db_dsnw DSN for read/write operations
* @param string $db_dsnr Optional DSN for read only operations
* @param bool $pconn Enables persistent connections
*/
protected function init()
public function __construct($db_dsnw, $db_dsnr = '', $pconn = false)
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
rcube::raise_error(array('code' => 600, 'type' => 'db',
@ -41,11 +45,24 @@ class rcube_db_mysql extends rcube_db
true, true);
}
parent::__construct($db_dsnw, $db_dsnr, $pconn);
// SQL identifiers quoting
$this->options['identifier_start'] = '`';
$this->options['identifier_end'] = '`';
}
/**
* Driver-specific configuration of database connection
*
* @param array $dsn DSN for DB connections
* @param PDO $dbh Connection handler
*/
protected function conn_configure($dsn, $dbh)
{
$this->query("SET NAMES 'utf8'");
}
/**
* Abstract SQL statement for value concatenation
*

@ -28,6 +28,17 @@ class rcube_db_pgsql extends rcube_db
{
public $db_provider = 'postgres';
/**
* Driver-specific configuration of database connection
*
* @param array $dsn DSN for DB connections
* @param PDO $dbh Connection handler
*/
protected function conn_configure($dsn, $dbh)
{
$this->query("SET NAMES 'utf8'");
}
/**
* Get last inserted record ID
*

@ -28,13 +28,6 @@ class rcube_db_sqlite extends rcube_db
{
public $db_provider = 'sqlite';
/**
* Database character set
*/
protected function set_charset($charset)
{
}
/**
* Prepare connection
*/

@ -29,22 +29,20 @@ class rcube_db_sqlsrv extends rcube_db
public $db_provider = 'mssql';
/**
* Driver initialization
* Object constructor
*
* @param string $db_dsnw DSN for read/write operations
* @param string $db_dsnr Optional DSN for read only operations
* @param bool $pconn Enables persistent connections
*/
protected function init()
public function __construct($db_dsnw, $db_dsnr = '', $pconn = false)
{
parent::__construct($db_dsnw, $db_dsnr, $pconn);
$this->options['identifier_start'] = '[';
$this->options['identifier_end'] = ']';
}
/**
* Database character set setting
*/
protected function set_charset($charset)
{
// UTF-8 is default
}
/**
* Driver-specific configuration of database connection
*

Loading…
Cancel
Save