Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Public Member Functions | Public Attributes | List of all members
DbConnectionMan Class Reference

Public Member Functions

 init ()
 createCommand ($sql=null)
 getSlave ()
 isReadOperation ($sql)

Public Attributes

 $slaves = array()
 $enableSlave = true

Detailed Description

Gentics Portal.Node PHP Author & Copyright (c) by Gentics Software GmbH sales.nosp@m.@gen.nosp@m.tics..nosp@m.com http://www.gentics.com Licenses can be found in the LICENSE.txt file in the root-folder of this installation You must not use this software without a valid license agreement.

DbConnectionMan(Database Connection Manager) class is a manager of database connections. for the purpose of database read/write splitting. It override the createCommand method, detect the sql statement to decide which connection will be used. Default it use the master connection.

Definition at line 17 of file DbConnectionMan.php.

Member Function Documentation

DbConnectionMan::createCommand (   $sql = null)

Creates a CDbCommand object for excuting sql statement. It will detect the sql statement's behavior. While the sql is a simple read operation. It will use a slave database connection to contruct a CDbCommand object. Default it use current connection(master database).

Parameters
string$sqlquery
Returns
CDbCommand

Definition at line 70 of file DbConnectionMan.php.

References getSlave().

{
if ($this->enableSlave && !$this->getCurrentTransaction() && self::isReadOperation($sql)) {
return $this->getSlave()->createCommand($sql);
} else {
return parent::createCommand($sql);
}
}
DbConnectionMan::getSlave ( )

Construct a slave connection CDbConnection for read operation.

Returns
DbConnectionMan

Definition at line 85 of file DbConnectionMan.php.

Referenced by createCommand().

{
if (!isset($this->_slave)) {
$slaves = $this->slaves;
//Add randomness to slaves
shuffle($slaves);
//create first available slave
foreach ($slaves as $slaveConfig) {
try {
if ($slaveConfig instanceof DbConnectionMan) {
$slave = $slaveConfig;
} elseif (!isset($slaveConfig['class'])) {
//cloning for copying parent properties
$slave = clone $this;
foreach ($slaveConfig as $prop => $val) {
$slave->$prop = $val;
}
} else {
$slave = Yii::createComponent($slaveConfig);
}
Yii::app()->setComponent('dbslave', $slave);
$this->_slave = $slave;
Yii::log('Slave connection: ' . $this->_slave->connectionString, CLogger::LEVEL_TRACE, 'system.db.CDbCommand');
break;
} catch (Exception $e) {
Yii::log('Create slave database connection failed!', CLogger::LEVEL_WARNING);
continue;
}
}
//if slave not created use this connection as slave
if (!$this->_slave) {
$this->_slave = clone $this;
}
$this->_slave->enableSlave = false;
}
return $this->_slave;
}
DbConnectionMan::isReadOperation (   $sql)

Detect whether the sql statement is just a simple read operation. Read Operation means this sql will not change any thing ang aspect of the database. Such as SELECT,DECRIBE,SHOW etc. On the other hand:UPDATE,INSERT,DELETE is write operation.

Parameters
string$sqlquery
Returns
bool

Definition at line 133 of file DbConnectionMan.php.

{
return preg_match('/^\s*(SELECT|SHOW|DESCRIBE|PRAGMA)/i', $sql);
}

Member Data Documentation

bool DbConnectionMan::$enableSlave = true

Whether enable the slave database connection. Defaut is true.Set this property to false for the purpose of only use the master database.

Definition at line 43 of file DbConnectionMan.php.


The documentation for this class was generated from the following file: