/**
* Handle Horde-style configuration arrays, PEAR DB/MDB2 arrays or DSNs, or
* PDO DSNS.
+ *
+ * @return Horde_Db_Adapter_Base
+ * @throws Horde_Exception
*/
public function create(Horde_Injector $injector)
{
- $config = $GLOBALS['conf']['sql'];
+ return $this->_createDb($GLOBALS['conf']['sql'], $injector);
+ }
+
+ protected function _createDb($config, $injector)
+ {
+ if (!empty($config['splitread'])) {
+ unset($config['splitread']);
+ $config['write_db'] = $this->_createDb($config, $injector);
+ $config = array_merge($config, $config['read']);
+ }
+
if (!isset($config['adapter'])) {
if ($config['phptype'] == 'oci8') {
$config['phptype'] = 'oci';
}
- $config['adapter'] = $config['phptype'] == 'mysqli' ? 'mysqli' : 'pdo_' . $config['phptype'];
+ $config['adapter'] = ($config['phptype'] == 'mysqli')
+ ? 'mysqli'
+ : 'pdo_' . $config['phptype'];
}
+
if (!empty($config['hostspec'])) {
$config['host'] = $config['hostspec'];
}
$adapter = str_replace(' ', '_' , ucwords(str_replace('_', ' ', basename($config['adapter']))));
$class = 'Horde_Db_Adapter_' . $adapter;
- if (!class_exists($class)) {
- throw new Horde_Exception('Adapter class "' . $class . '" not found');
+
+ if (class_exists($class)) {
+ return new $class($config);
}
- return new $class($config);
+ throw new Horde_Exception('Adapter class "' . $class . '" not found');
}
public function equals(Horde_Injector_Binder $binder)
{
/**
* Config options
+ *
* @var array
*/
protected $_config = array();
*/
protected $_schemaMethods = array();
+ /**
+ * Write DB
+ *
+ * @var Horde_Db_Adapter_Base
+ */
+ protected $_write;
+
/*##########################################################################
# Construct/Destruct
##########################################################################*/
/**
- * @param array $config Configuration options and optional objects (logger,
- * cache, etc.)
+ * Constructor.
+ *
+ * @param array $config Configuration options and optional objects:
+ * <pre>
+ * 'cache' - TODO
+ * 'charset' - (string) TODO
+ * 'logger' - TODO
+ * 'write_db' - (Horde_Db_Adapter_Base) Use this DB for write operations.
+ * </pre>
*/
public function __construct($config)
{
*/
public function insert($sql, $arg1 = null, $arg2 = null, $pk = null, $idValue = null, $sequenceName = null)
{
+ if ($this->_write) {
+ return $this->_write->insert($sql, $arg1, $arg2, $pk, $idValue, $sequenceName);
+ }
+
$this->execute($sql, $arg1, $arg2);
return isset($idValue) ? $idValue : $this->_connection->lastInsertId();
}
*/
public function update($sql, $arg1 = null, $arg2 = null)
{
+ if ($this->_write) {
+ return $this->_write->update($sql, $arg1, $arg2);
+ }
+
$this->execute($sql, $arg1, $arg2);
return $this->_rowCount;
}
*/
public function delete($sql, $arg1 = null, $arg2 = null)
{
+ if ($this->_write) {
+ return $this->_write->delete($sql, $arg1, $arg2);
+ }
+
$this->execute($sql, $arg1, $arg2);
return $this->_rowCount;
}
*/
public function transactionStarted()
{
- return $this->_transactionStarted;
+ return $this->_write
+ ? $this->_write->transactionStarted()
+ : $this->_transactionStarted;
}
/**
*/
public function beginDbTransaction()
{
- $this->_transactionStarted = true;
- $this->_connection->beginTransaction();
+ if ($this->_write) {
+ $this->_write->beginDbTransaction();
+ } else {
+ $this->_transactionStarted = true;
+ $this->_connection->beginTransaction();
+ }
}
/**
*/
public function commitDbTransaction()
{
- $this->_connection->commit();
- $this->_transactionStarted = false;
+ if ($this->_write) {
+ $this->_write->commitDbTransaction();
+ } else {
+ $this->_connection->commit();
+ $this->_transactionStarted = false;
+ }
}
/**
{
if (! $this->_transactionStarted) { return; }
- $this->_connection->rollBack();
- $this->_transactionStarted = false;
+ if ($this->_write) {
+ $this->_write->rollbackDbTransaction();
+ } else {
+ $this->_connection->rollBack();
+ $this->_transactionStarted = false;
+ }
}
/**
*/
public function insertFixture($fixture, $tableName)
{
- /*@TODO*/
- return $this->execute("INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert');
+ if ($this->_write) {
+ return $this->_write->insertFixture($fixture, $tableName);
+ } else {
+ /*@TODO*/
+ return $this->execute("INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert');
+ }
}
public function emptyInsertStatement($tableName)