Changeset 173

Show
Ignore:
Timestamp:
01/29/07 18:10:50 (1 year ago)
Author:
mikey
Message:

added net.stubbles.rdbms.stubDatabaseConnectionData

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/rdbms/pdo/stubDatabasePDOConnection.php

    r170 r173  
    2020{ 
    2121    /** 
    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; 
    4827    /** 
    4928     * instance of pdo 
     
    5635     * constructor 
    5736     * 
    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; 
    6942    } 
    7043     
     
    10376    protected function createPDO() 
    10477    { 
    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                     ); 
    10683    } 
    10784     
     
    11289    { 
    11390        $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; 
    114101    } 
    115102 
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnection.php

    r170 r173  
    77 * @subpackage  rdbms 
    88 */ 
    9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseException'); 
     9stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnectionData', 
     10                      'net.stubbles.rdbms.stubDatabaseException' 
     11); 
    1012/** 
    1113 * Interface for database connections. 
     
    1921     * constructor 
    2022     * 
    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 
    2524     */ 
    26     #public function __construct($dsn, $userName = '', $passwd = '', array $driverOptions = array()); 
     25    #public function __construct(stubDatabaseConnectionData $connectionData); 
    2726     
    2827    /** 
     
    3534     */ 
    3635    public function disconnect(); 
     36     
     37    /** 
     38     * returns the connection data used for the connection 
     39     * 
     40     * @return  stubDatabaseConnectionData 
     41     */ 
     42    public function getConnectionData(); 
    3743     
    3844    /** 
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionPool.php

    r170 r173  
    88 */ 
    99stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 
     10                      'net.stubbles.rdbms.stubDatabaseConnectionData', 
    1011                      'net.stubbles.rdbms.stubDatabaseException' 
    1112); 
     
    1314 * Pool for database connections. 
    1415 * 
     16 * @static 
    1517 * @package     stubbles 
    1618 * @subpackage  rdbms 
     
    1820class stubDatabaseConnectionPool 
    1921{ 
    20     /** 
    21      * id of the default connection 
    22      */ 
    23     const ID_DEFAULT                = '__default'; 
    2422    /** 
    2523     * a set of database connections 
     
    3129     * list of known connection data 
    3230     *  
    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> 
    4432     */ 
    4533    protected static $connectionData = array(); 
     
    5543     * @throws  stubDatabaseException 
    5644     */ 
    57     public static function getConnection($id = self::ID_DEFAULT) 
     45    public static function getConnection($id = stubDatabaseConnectionData::ID_DEFAULT) 
    5846    { 
    5947        if (isset(self::$connections[$id]) == false) { 
     
    6250            } 
    6351             
    64             $nqClassName = stubClassLoader::getNonQualifiedClassName(self::$connectionData[$id]['class']); 
     52            $nqClassName = stubClassLoader::getNonQualifiedClassName(self::$connectionData[$id]->getConnectionClassName()); 
    6553            if (class_exists($nqClassName, false) == false) { 
    66                 stubClassLoader::load(self::$connectionData[$id]['class']); 
     54                stubClassLoader::load(self::$connectionData[$id]->getConnectionClassName()); 
    6755            } 
    6856              
    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; 
    7463        } 
    7564         
     
    7968    /** 
    8069     * 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> 
    9170     * 
    92      * @param  array  $connectionData 
     71     * @param  stubDatabaseConnectionData  $connectionData 
    9372     */ 
    94     public function addConnectionData(array $connectionData) 
     73    public static function addConnectionData(stubDatabaseConnectionData $connectionData) 
    9574    { 
    96         self::$connectionData[$connectionData['id']] = $connectionData; 
     75        self::$connectionData[$connectionData->getId()] = $connectionData; 
    9776    } 
    9877     
    9978    /** 
    10079     * 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> 
    11180     * 
    11281     * @param   string  $id  optional  id of the connection data 
    11382     * @return  bool 
    11483     */ 
    115     public function hasConnectionData($id = self::ID_DEFAULT) 
     84    public static function hasConnectionData($id = stubDatabaseConnectionData::ID_DEFAULT) 
    11685    { 
    11786        return isset(self::$connectionData[$id]); 
     
    12089    /** 
    12190     * returns the connection data for a given id 
    122      * 
     91     *  
    12392     * @param   string  $id  optional  id of the connection data 
    124      * @return  array 
     93     * @return  stubDatabaseConnectionData 
    12594     */ 
    126     public function getConnectionData($id = self::ID_DEFAULT) 
     95    public static function getConnectionData($id = stubDatabaseConnectionData::ID_DEFAULT) 
    12796    { 
    12897        if (isset(self::$connectionData[$id]) == true) { 
     
    13099        } 
    131100         
    132         return array()
     101        return null
    133102    } 
    134103     
     
    140109     * @param  string  $id  optional  id of the connection to close 
    141110     */ 
    142     public function closeConnection($id = self::ID_DEFAULT) 
     111    public static function closeConnection($id = stubDatabaseConnectionData::ID_DEFAULT) 
    143112    { 
    144113        if (isset(self::$connections[$id]) == true) { 
     
    152121     * 
    153122     * @param  stubDatabaseConnection  $connection  the connection to use for the given id 
    154      * @param  string                  $id          optional  id of the connection to set 
    155123     */ 
    156     public static function setConnection(stubDatabaseConnection $connection, $id = self::ID_DEFAULT
     124    public static function setConnection(stubDatabaseConnection $connection
    157125    { 
    158         self::$connections[$id] = $connection; 
     126        self::$connections[$connection->getConnectionData()->getId()] = $connection; 
    159127    } 
    160128}