+ add the support of master and slave databases.

This commit is contained in:
wangchunsheng
2011-05-13 06:46:07 +00:00
parent d2fdb3d574
commit afbad45d3f
3 changed files with 72 additions and 26 deletions

View File

@@ -47,12 +47,19 @@ $config->default->method = 'index'; // Default method.
$config->file->dangers = 'php,jsp,py,rb,asp,'; // Dangerous files.
$config->file->maxSize = 1024 * 1024; // Max size.
/* Database settings. */
$config->db->persistant = false; // Pconnect or not.
$config->db->driver = 'mysql'; // Must be MySQL. Don't support other database server yet.
$config->db->encoding = 'UTF8'; // Encoding of database.
$config->db->strictMode = false; // Turn off the strict mode of MySQL.
$config->db->checkCentOS= true; // Whether check is centos or not, thus to fix the error of pdo in centos.
/* Master database settings. */
$config->db->persistant = false; // Pconnect or not.
$config->db->driver = 'mysql'; // Must be MySQL. Don't support other database server yet.
$config->db->encoding = 'UTF8'; // Encoding of database.
$config->db->strictMode = false; // Turn off the strict mode of MySQL.
$config->db->checkCentOS= true; // Whether check is centos or not, thus to fix the error of pdo in centos.
/* Slave database settings. */
$config->slaveDB->persistant = false;
$config->slaveDB->driver = 'mysql';
$config->slaveDB->encoding = 'UTF8';
$config->slaveDB->strictMode = false;
$config->slaveDB->checkCentOS= true;
/* Include the custom config file. */
$myConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my.php';

View File

@@ -131,7 +131,7 @@ class router
/**
* The control object of current module.
*
* @var string
* @var object
* @access public
*/
public $control;
@@ -195,7 +195,7 @@ class router
/**
* The global $config object.
*
* @var string
* @var object
* @access public
*/
public $config;
@@ -203,7 +203,7 @@ class router
/**
* The global $lang object.
*
* @var string
* @var object
* @access public
*/
public $lang;
@@ -211,11 +211,19 @@ class router
/**
* The global $dbh object, the database connection handler.
*
* @var string
* @var object
* @access private
*/
public $dbh;
/**
* The slave database handler.
*
* @var object
* @access private
*/
public $slaveDBH;
/**
* The $post object, used to access the $_POST var.
*
@@ -1459,26 +1467,39 @@ class router
*/
public function connectDB()
{
global $config, $dbh;
if(!isset($config->db->driver)) self::error('no pdo driver defined, it should be mysql or sqlite', __FILE__, __LINE__, $exit = true);
if(!isset($config->db->user)) return false;
if($config->db->driver == 'mysql')
global $config, $dbh, $slaveDBH;
if(isset($config->db->host)) $this->dbh = $dbh = $this->connectByPDO($config->db);
if(isset($config->slaveDB->host)) $this->slaveDBH = $slaveDBH = $this->connectByPDO($config->slaveDB);
}
/**
* Connect database by PDO.
*
* @param object $params the database params.
* @access private
* @return object|bool
*/
private function connectByPDO($params)
{
if(!isset($params->driver)) self::error('no pdo driver defined, it should be mysql or sqlite', __FILE__, __LINE__, $exit = true);
if(!isset($params->user)) return false;
if($params->driver == 'mysql')
{
$dsn = "mysql:host={$config->db->host}; port={$config->db->port}; dbname={$config->db->name}";
$dsn = "mysql:host={$params->host}; port={$params->port}; dbname={$params->name}";
}
try
{
$dbh = new PDO($dsn, $config->db->user, $config->db->password, array(PDO::ATTR_PERSISTENT => $config->db->persistant));
$dbh = new PDO($dsn, $params->user, $params->password, array(PDO::ATTR_PERSISTENT => $params->persistant));
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec("SET NAMES {$config->db->encoding}");
if(isset($config->db->strictMode) and $config->db->strictMode == false) $dbh->exec("SET @@sql_mode= ''");
if(isset($config->db->checkCentOS) and $config->db->checkCentOS and helper::isCentOS())
$dbh->exec("SET NAMES {$params->encoding}");
if(isset($params->strictMode) and $params->strictMode == false) $dbh->exec("SET @@sql_mode= ''");
if(isset($params->checkCentOS) and $params->checkCentOS and helper::isCentOS())
{
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
}
$this->dbh = $dbh;
return $dbh;
}
catch (PDOException $exception)

View File

@@ -56,6 +56,14 @@ class dao
*/
protected $dbh;
/**
* The global slaveDBH(database handler) object.
*
* @var object
* @access protected
*/
protected $slaveDBH;
/**
* The sql object, used to creat the query sql.
*
@@ -130,11 +138,12 @@ class dao
*/
public function __construct()
{
global $app, $config, $lang, $dbh;
$this->app = $app;
$this->config = $config;
$this->lang = $lang;
$this->dbh = $dbh;
global $app, $config, $lang, $dbh, $slaveDBH;
$this->app = $app;
$this->config = $config;
$this->lang = $lang;
$this->dbh = $dbh;
$this->slaveDBH = $slaveDBH ? $slaveDBH : false;
$this->reset();
}
@@ -476,8 +485,17 @@ class dao
$sql = $this->processSQL($autoCompany);
try
{
$method = $this->method;
$this->reset();
return $this->dbh->query($sql);
if($this->slaveDBH and $method == 'select')
{
return $this->slaveDBH->query($sql);
}
else
{
return $this->dbh->query($sql);
}
}
catch (PDOException $e)
{