[limb-svn] r6864 - in 3.x/trunk/limb/dbal: src/toolkit tests/cases tests/cases/toolkit

svn at limb-project.com svn at limb-project.com
Fri Mar 28 13:40:16 MSK 2008


Author: korchasa
Date: 2008-03-28 13:40:16 +0300 (Fri, 28 Mar 2008)
New Revision: 6864
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6864

Added:
   3.x/trunk/limb/dbal/tests/cases/toolkit/
   3.x/trunk/limb/dbal/tests/cases/toolkit/.setup.php
   3.x/trunk/limb/dbal/tests/cases/toolkit/lmbDbToolsTest.class.php
Modified:
   3.x/trunk/limb/dbal/src/toolkit/lmbDbTools.class.php
Log:
-- added getDbDSNByName(), setDbDSNByName(), getDdConnectionByName(), setDdConnectionByName()

Modified: 3.x/trunk/limb/dbal/src/toolkit/lmbDbTools.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/toolkit/lmbDbTools.class.php	2008-03-27 17:40:53 UTC (rev 6863)
+++ 3.x/trunk/limb/dbal/src/toolkit/lmbDbTools.class.php	2008-03-28 10:40:16 UTC (rev 6864)
@@ -20,8 +20,8 @@
  */
 class lmbDbTools extends lmbAbstractTools
 {
-  protected $default_connection;
-  protected $default_db_config;
+  protected $_db_configs = array('dsn' => null);
+  protected $_connections = array('dsn' => null);
   protected $cache_db_info = true;
   protected $db_info = array();
   protected $db_tables = array();
@@ -30,8 +30,6 @@
   function setDbEnvironment($env)
   {
     $this->db_env = $env;
-    $this->default_db_config = null;
-    $this->default_connection = null;
   }
 
   function getDbEnvironment()
@@ -39,38 +37,48 @@
     return $this->db_env;
   }
 
-  function setDefaultDbDSN($dsn)
+  function setDbDSNByName($name, $dsn)
   {
     if(is_object($dsn))
-      $this->default_db_config = $dsn;
+      $this->_db_configs[$name] = $dsn;
     else
-      $this->default_db_config = new lmbDbDSN($dsn);
+      $this->_db_configs[$name] = new lmbDbDSN($dsn);
   }
 
-  function getDefaultDbDSN()
+  function setDefaultDbDSN($dsn)
   {
-    if(is_object($this->default_db_config))
-      return $this->default_db_config;
+    $this->setDbDSNByName('dsn', $dsn);
+  }
 
+  function getDbDSNByName($name)
+  {
+    if(isset($this->_db_configs[$name]) && is_object($this->_db_configs[$name]))
+      return $this->_db_configs[$name];
+
     $conf = $this->toolkit->getConf('db');
 
     //for BC 'dsn' overrides other db environments
-    if($dsn = $conf->get('dsn'))
+    if($dsn = $conf->get($name))
     {
-      $this->default_db_config = new lmbDbDSN($dsn);
+      $this->setDbDSNByName($name, new lmbDbDSN($dsn));
     }
     else
     {
       $env = $conf->get($this->db_env);
-      if(!is_array($env) || !isset($env['dsn']))
+      if(!is_array($env) || !isset($env[$name]))
         throw new lmbException("Could not find database connection settings for environment '{$this->db_env}'");
 
-      $this->default_db_config = new lmbDbDSN($env['dsn']);
+      $this->setDbDSNByName($name, new lmbDbDSN($env[$name]));
     }
 
-    return $this->default_db_config;
+    return $this->_db_configs[$name];
   }
 
+  function getDefaultDbDSN()
+  {
+    return $this->getDbDSNByName('dsn');
+  }
+
   function getDbDSN($env)
   {
     $conf = $this->toolkit->getConf('db');
@@ -82,18 +90,33 @@
     return new lmbDbDSN($array['dsn']);
   }
 
-  function getDefaultDbConnection()
+  function setDbConnectionByName($name, $conn)
   {
-    if(is_object($this->default_connection))
-      return $this->default_connection;
+    $this->_connections[$name] = $conn;
+  }
 
-    if(!is_object($dsn = $this->toolkit->getDefaultDbDSN()))
-      throw new lmbException('Default database DSN is not valid');
+  function setDefaultDbConnection($conn)
+  {
+    $this->setDbConnectionByName('dsn', $conn);
+  }
 
-    $this->default_connection = $this->toolkit->createDbConnection($dsn);
-    return $this->default_connection;
+  function getDbConnectionByName($name)
+  {
+    if(isset($this->_connections[$name]) && is_object($this->_connections[$name]))
+      return $this->_connections[$name];
+
+    if(!is_object($dsn = $this->toolkit->getDbDSNByName($name)))
+      throw new lmbException($name . ' database DSN is not valid');
+
+    $this->setDbConnectionByName($name, $this->createDbConnection($dsn));
+    return $this->_connections[$name];
   }
 
+  function getDefaultDbConnection()
+  {
+    return $this->getDbConnectionByName('dsn');
+  }
+
   function createDbConnection($dsn)
   {
     if(!is_object($dsn))
@@ -134,11 +157,6 @@
     return $this->db_info[$id];
   }
 
-  function setDefaultDbConnection($conn)
-  {
-    $this->default_connection = $conn;
-  }
-
   function createTableGateway($table_name, $conn = null)
   {
     if(!$conn)
@@ -154,4 +172,3 @@
     return $db_table;
   }
 }
-

Added: 3.x/trunk/limb/dbal/tests/cases/toolkit/.setup.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/toolkit/.setup.php	                        (rev 0)
+++ 3.x/trunk/limb/dbal/tests/cases/toolkit/.setup.php	2008-03-28 10:40:16 UTC (rev 6864)
@@ -0,0 +1,3 @@
+<?php
+require_once('limb/dbal/common.inc.php');
+

Added: 3.x/trunk/limb/dbal/tests/cases/toolkit/lmbDbToolsTest.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/toolkit/lmbDbToolsTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/dbal/tests/cases/toolkit/lmbDbToolsTest.class.php	2008-03-28 10:40:16 UTC (rev 6864)
@@ -0,0 +1,62 @@
+<?php
+lmb_require('limb/core/src/lmbSet.class.php');
+lmb_require('limb/toolkit/src/lmbToolkit.class.php');
+lmb_require('limb/dbal/src/toolkit/lmbDbTools.class.php');
+
+class lmbDbToolsTest extends UnitTestCase
+{
+  public $toolkit;
+  public $config;
+
+  function setUp()
+  {
+    parent::setUp();
+    $this->config = array(
+      'dsn' => 'mysql://root:test@localhost/hello_from_foo?charset=cp1251',
+      'another_dsn' => 'sqlite://kraynopp:pasha@ksu/kadrs?charset=utf8'
+    );
+    lmbToolkit::instance()->setConf('db', new lmbSet($this->config));
+
+    $this->toolkit = new lmbDbTools();
+  }
+
+  function testGetDbDSNByName()
+  {
+    $this->assertEqual($this->toolkit->getDbDSNByName('another_dsn'), new lmbDbDSN($this->config['another_dsn']));
+  }
+
+  function testGetDefaultDbDSN()
+  {
+    $this->assertEqual(
+      $this->toolkit->getDefaultDbDSN(),
+      $this->toolkit->getDbDSNByName('dsn')
+    );
+  }
+
+  function testSetDbDSNByName()
+  {
+    $this->assertEqual($this->toolkit->getDbDSNByName('another_dsn'), new lmbDbDSN($this->config['another_dsn']));
+    $dsn = new lmbDbDSN($this->config['dsn']);
+    $this->toolkit->setDbDSNByName('another_dsn', $dsn);
+    $this->assertEqual($this->toolkit->getDbDSNByName('another_dsn'), new lmbDbDSN($this->config['dsn']));
+  }
+
+  function testGetDbConnectionByName()
+  {
+    $connection = $this->toolkit->createDbConnection(new lmbDbDSN($this->config['another_dsn']));
+    $this->assertIdentical($connection, $this->toolkit->getDbConnectionByName('another_dsn'));
+  }
+
+  function testSetDbConnectionByName()
+  {
+    $connection = $this->toolkit->createDbConnection(new lmbDbDSN($this->config['dsn']));
+    $another_connection = $this->toolkit->createDbConnection(new lmbDbDSN($this->config['another_dsn']));
+
+    $this->assertEqual($connection, $this->toolkit->getDefaultDbConnection());
+
+    $this->toolkit->setDbConnectionByName('dsn', $another_connection);
+
+    $this->assertIdentical($another_connection, $this->toolkit->getDefaultDbConnection());
+  }
+}
+?>



More information about the limb-svn mailing list