Changeset 1373
- Timestamp:
- 02/26/08 01:22:21 (8 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionProvider.php
r1370 r1373 9 9 stubClassLoader::load('net::stubbles::ioc::stubInjectionProvider', 10 10 'net::stubbles::rdbms::stubDatabaseConnectionData', 11 'net::stubbles::rdbms::stubDatabaseConnectionPool' 11 'net::stubbles::rdbms::stubDatabaseConnectionPool', 12 'net::stubbles::rdbms::stubDatabaseException' 12 13 ); 13 14 /** … … 20 21 { 21 22 /** 22 * the connection id to be used23 * switch whether to fallback to default connection if no named connection exists 23 24 * 24 * @var string25 * @var bool 25 26 */ 26 protected $ connectionId;27 protected $fallback = true; 27 28 28 29 /** 29 30 * constructor 30 31 * 31 * @param string $connectionId provider should return connections for this id32 * @param bool $fallback whether to fallback to default connection if no named connection exists 32 33 */ 33 public function __construct($ connectionId = stubDatabaseConnectionData::DEFAULT_ID)34 public function __construct($fallback = true) 34 35 { 35 $this-> connectionId = $connectionId;36 $this->fallback = $fallback; 36 37 } 37 38 … … 39 40 * returns the connection to be injected 40 41 * 42 * If a name is provided and a condition with this name exists this 43 * connection will be returned. If fallback is enabled and the named 44 * connection does not exist the default connection will be returned, if 45 * fallback is disabled a stubDatabaseException will be thrown. 46 * 47 * If no name is provided the default connection will be returned. 48 * 41 49 * @param string $type 42 * @param string $name 50 * @param string $name optional 43 51 * @return stubDatabaseConnection 52 * @throws stubDatabaseException 44 53 */ 45 54 public function get($type, $name = null) 46 55 { 47 return stubDatabaseConnectionPool::getConnection($this->connectionId); 56 if (null !== $name) { 57 if (stubDatabaseConnectionPool::hasConnectionData($name) === true) { 58 return stubDatabaseConnectionPool::getConnection($name); 59 } 60 61 if (false === $this->fallback) { 62 throw new stubDatabaseException('No connection and no dsn known for connection associated with id ' . $name); 63 } 64 } 65 66 return stubDatabaseConnectionPool::getConnection(stubDatabaseConnectionData::DEFAULT_ID); 48 67 } 49 68 } trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php
r1303 r1373 26 26 $suite->addTestFile($dir . '/stubDatabaseConnectionDataTestCase.php'); 27 27 $suite->addTestFile($dir . '/stubDatabaseConnectionPoolTestCase.php'); 28 $suite->addTestFile($dir . '/stubDatabaseConnectionProviderTestCase.php'); 28 29 $suite->addTestFile($dir . '/stubDatabaseInitializerTestCase.php'); 29 30
