Skip to content

Commit

Permalink
Simplified db connection initialisation code
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed Jun 8, 2013
1 parent 66407a7 commit d186405
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 68 deletions.
48 changes: 9 additions & 39 deletions program/lib/Roundcube/rcube_db.php
Expand Up @@ -100,27 +100,15 @@ public function __construct($db_dsnw, $db_dsnr = '', $pconn = false)

$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;
Expand Down Expand Up @@ -158,9 +146,10 @@ protected function dsn_connect($dsn)
return null;
}

$this->dbh = $dbh;
$this->db_mode = $mode;
$this->db_connected = true;
$this->conn_configure($dsn, $dbh);

return $dbh;
}

/**
Expand All @@ -182,16 +171,6 @@ protected function conn_configure($dsn, $dbh)
{
}

/**
* 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
*
Expand Down Expand Up @@ -219,23 +198,14 @@ public function db_connect($mode)

$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;
}

/**
Expand Down
18 changes: 8 additions & 10 deletions program/lib/Roundcube/rcube_db_mssql.php
Expand Up @@ -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
*
Expand Down
21 changes: 19 additions & 2 deletions program/lib/Roundcube/rcube_db_mysql.php
Expand Up @@ -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',
Expand All @@ -41,11 +45,24 @@ protected function init()
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
*
Expand Down
11 changes: 11 additions & 0 deletions program/lib/Roundcube/rcube_db_pgsql.php
Expand Up @@ -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
*
Expand Down
7 changes: 0 additions & 7 deletions program/lib/Roundcube/rcube_db_sqlite.php
Expand Up @@ -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
*/
Expand Down
18 changes: 8 additions & 10 deletions program/lib/Roundcube/rcube_db_sqlsrv.php
Expand Up @@ -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
*
Expand Down

0 comments on commit d186405

Please sign in to comment.