Changeset 173
- Timestamp:
- 01/29/07 18:10:50 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/pdo/stubDatabasePDOConnection.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnection.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionData.php (added)
- trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionPool.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/pdo/stubDatabasePDOConnection.php
r170 r173 20 20 { 21 21 /** 22 * The Data Source Name, or DSN, contains the information required to 23 * connect to the database. 24 * 25 * @var string 26 */ 27 protected $dsn = ''; 28 /** 29 * The user name for the DSN string. This parameter is optional for 30 * some PDO drivers. 31 * 32 * @var string 33 */ 34 protected $userName = ''; 35 /** 36 * The password for the DSN string. This parameter is optional for 37 * some PDO drivers. 38 * 39 * @var string 40 */ 41 protected $passwd = ''; 42 /** 43 * A key => value array of driver-specific connection options. 44 * 45 * @var array 46 */ 47 protected $driverOptions = array(); 22 * container that contains the data required to establish the connection 23 * 24 * @var stubDatabaseConnectionData 25 */ 26 protected $connectionData; 48 27 /** 49 28 * instance of pdo … … 56 35 * constructor 57 36 * 58 * @param string $dsn Data Source Name, or DSN, contains the information required to connect to the database 59 * @param string $userName optional user name for the DSN string 60 * @param string $passwd optional password for the DSN string 61 * @param array $driverOptions optional a key=>value array of driver-specific connection options 62 */ 63 public function __construct($dsn, $userName = '', $passwd = '', array $driverOptions = array()) 64 { 65 $this->dsn = $dsn; 66 $this->userName = $userName; 67 $this->passwd = $passwd; 68 $this->driverOptions = $driverOptions; 37 * @param stubDatabaseConnectionData $connectionData container that contains the data required to establish the connection 38 */ 39 public function __construct(stubDatabaseConnectionData $connectionData) 40 { 41 $this->connectionData = $connectionData; 69 42 } 70 43 … … 103 76 protected function createPDO() 104 77 { 105 $this->pdo = new PDO($this->dsn, $this->userName, $this->passwd, $this->driverOptions); 78 $this->pdo = new PDO($this->connectionData->getDSN(), 79 $this->connectionData->getUserName(), 80 $this->connectionData->getPassword(), 81 $this->connectionData->getDriverOptions() 82 ); 106 83 } 107 84 … … 112 89 { 113 90 $this->pdo = null; 91 } 92 93 /** 94 * returns the connection data used for the connection 95 * 96 * @return stubDatabaseConnectionData 97 */ 98 public function getConnectionData() 99 { 100 return $this->connectionData; 114 101 } 115 102 trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnection.php
r170 r173 7 7 * @subpackage rdbms 8 8 */ 9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseException'); 9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnectionData', 10 'net.stubbles.rdbms.stubDatabaseException' 11 ); 10 12 /** 11 13 * Interface for database connections. … … 19 21 * constructor 20 22 * 21 * @param string $dsn Data Source Name, or DSN, contains the information required to connect to the database 22 * @param string $userName optional user name for the DSN string 23 * @param string $passwd optional password for the DSN string 24 * @param array $driverOptions optional a key=>value array of driver-specific connection options 23 * @param stubDatabaseConnectionData $connectionData container that contains the data required to establish the connection 25 24 */ 26 #public function __construct( $dsn, $userName = '', $passwd = '', array $driverOptions = array());25 #public function __construct(stubDatabaseConnectionData $connectionData); 27 26 28 27 /** … … 35 34 */ 36 35 public function disconnect(); 36 37 /** 38 * returns the connection data used for the connection 39 * 40 * @return stubDatabaseConnectionData 41 */ 42 public function getConnectionData(); 37 43 38 44 /** trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionPool.php
r170 r173 8 8 */ 9 9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 10 'net.stubbles.rdbms.stubDatabaseConnectionData', 10 11 'net.stubbles.rdbms.stubDatabaseException' 11 12 ); … … 13 14 * Pool for database connections. 14 15 * 16 * @static 15 17 * @package stubbles 16 18 * @subpackage rdbms … … 18 20 class stubDatabaseConnectionPool 19 21 { 20 /**21 * id of the default connection22 */23 const ID_DEFAULT = '__default';24 22 /** 25 23 * a set of database connections … … 31 29 * list of known connection data 32 30 * 33 * The connection data has to consist of five elements: 34 * <code> 35 * id => id of the connection (string) 36 * class => full qualified classname of connection class to use (string) 37 * dsn => the data source name (string) 38 * userName => user name for the connection (string) 39 * passwd => password for the connection (string) 40 * driverOptions => driver options for the connection (array) 41 * </code> 42 * 43 * @var array<string,array> 31 * @var array<string,stubDatabaseConnectionData> 44 32 */ 45 33 protected static $connectionData = array(); … … 55 43 * @throws stubDatabaseException 56 44 */ 57 public static function getConnection($id = s elf::ID_DEFAULT)45 public static function getConnection($id = stubDatabaseConnectionData::ID_DEFAULT) 58 46 { 59 47 if (isset(self::$connections[$id]) == false) { … … 62 50 } 63 51 64 $nqClassName = stubClassLoader::getNonQualifiedClassName(self::$connectionData[$id] ['class']);52 $nqClassName = stubClassLoader::getNonQualifiedClassName(self::$connectionData[$id]->getConnectionClassName()); 65 53 if (class_exists($nqClassName, false) == false) { 66 stubClassLoader::load(self::$connectionData[$id] ['class']);54 stubClassLoader::load(self::$connectionData[$id]->getConnectionClassName()); 67 55 } 68 56 69 self::$connections[$id] = new $nqClassName(self::$connectionData[$id]['dsn'], 70 self::$connectionData[$id]['userName'], 71 self::$connectionData[$id]['passwd'], 72 self::$connectionData[$id]['driverOptions'] 73 ); 57 $connection = new $nqClassName(self::$connectionData[$id]); 58 if (($connection instanceof stubDatabaseConnection) == false) { 59 throw new stubDatabaseException(self::$connectionData[$id]['class'] . ' is not an instance of stubDatabaseConnection.'); 60 } 61 62 self::$connections[$id] = $connection; 74 63 } 75 64 … … 79 68 /** 80 69 * adds connection data to the list of known connection data 81 *82 * The connection data array has to consist of five elements:83 * <code>84 * id => id of the connection (string)85 * class => full qualified classname of connection class to use (string)86 * dsn => the data source name (string)87 * userName => user name for the connection (string)88 * passwd => password for the connection (string)89 * driverOptions => driver options for the connection (array)90 * </code>91 70 * 92 * @param array$connectionData71 * @param stubDatabaseConnectionData $connectionData 93 72 */ 94 public function addConnectionData(array$connectionData)73 public static function addConnectionData(stubDatabaseConnectionData $connectionData) 95 74 { 96 self::$connectionData[$connectionData ['id']] = $connectionData;75 self::$connectionData[$connectionData->getId()] = $connectionData; 97 76 } 98 77 99 78 /** 100 79 * checks whether connection data for a given id is available 101 *102 * The connection data array consists of five elements:103 * <code>104 * id => id of the connection (string)105 * class => full qualified classname of connection class to use (string)106 * dsn => the data source name (string)107 * userName => user name for the connection (string)108 * passwd => password for the connection (string)109 * driverOptions => driver options for the connection (array)110 * </code>111 80 * 112 81 * @param string $id optional id of the connection data 113 82 * @return bool 114 83 */ 115 public function hasConnectionData($id = self::ID_DEFAULT)84 public static function hasConnectionData($id = stubDatabaseConnectionData::ID_DEFAULT) 116 85 { 117 86 return isset(self::$connectionData[$id]); … … 120 89 /** 121 90 * returns the connection data for a given id 122 * 91 * 123 92 * @param string $id optional id of the connection data 124 * @return array93 * @return stubDatabaseConnectionData 125 94 */ 126 public function getConnectionData($id = self::ID_DEFAULT)95 public static function getConnectionData($id = stubDatabaseConnectionData::ID_DEFAULT) 127 96 { 128 97 if (isset(self::$connectionData[$id]) == true) { … … 130 99 } 131 100 132 return array();101 return null; 133 102 } 134 103 … … 140 109 * @param string $id optional id of the connection to close 141 110 */ 142 public function closeConnection($id = self::ID_DEFAULT)111 public static function closeConnection($id = stubDatabaseConnectionData::ID_DEFAULT) 143 112 { 144 113 if (isset(self::$connections[$id]) == true) { … … 152 121 * 153 122 * @param stubDatabaseConnection $connection the connection to use for the given id 154 * @param string $id optional id of the connection to set155 123 */ 156 public static function setConnection(stubDatabaseConnection $connection , $id = self::ID_DEFAULT)124 public static function setConnection(stubDatabaseConnection $connection) 157 125 { 158 self::$connections[$ id] = $connection;126 self::$connections[$connection->getConnectionData()->getId()] = $connection; 159 127 } 160 128 }
