Changeset 174

Show
Ignore:
Timestamp:
01/30/07 00:46:58 (2 years ago)
Author:
mikey
Message:

added tests for net.stubbles.rdbms.stubDatabaseConnectionData, net.stubbles.rdbms.stubDatabaseConnectionPool and net.stubbles.rdbms.pdo.stubDatabasePDOConnection

Files:

Legend:

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

    r173 r174  
    3030     * @var  PDO 
    3131     */ 
    32     protected  $pdo          = null; 
     32    protected  $pdo          = null; 
    3333     
    3434    /** 
     
    175175         
    176176        try { 
    177             $pdoStatement = $this->pdo->prepare($statement, $driverOptions); 
    178         } catch (PDOException $pdoe) { 
    179             throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
    180         } 
    181            
    182         return new stubDatabasePDOStatement($pdoStatement); 
     177            $statement = new stubDatabasePDOStatement($this->pdo->prepare($statement, $driverOptions)); 
     178            return $statement; 
     179        } catch (PDOException $pdoe) { 
     180            throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
     181        } 
    183182    } 
    184183     
     
    186185     * executes a SQL statement 
    187186     *  
     187     * The driver options can be: 
     188     * <code> 
     189     * fetchMode => one of the PDO::FETCH_* constants 
     190     * colNo     => if fetchMode == PDO::FETCH_COLUMN this denotes the column number to fetch 
     191     * object    => if fetchMode == PDO::FETCH_INTO this denotes the object to fetch the data into 
     192     * classname => if fetchMode == PDO::FETCH_CLASS this denotes the class to create and fetch the data into 
     193     * ctorargs  => (optional) if fetchMode == PDO::FETCH_CLASS this denotes the list of arguments for the constructor of the class to create and fetch the data into 
     194     * </code> 
     195     *  
    188196     * @param   string  $sql            the sql query to use 
    189      * @param   array   $driverOptions  optional  one or more driver specific options for the call to query() 
     197     * @param   array   $driverOptions  optional  how to fetch the data 
    190198     * @return  stubDatabasePDOStatement 
    191199     * @throws  stubDatabaseException 
    192200     * @see     http://php.net/pdo-query 
     201     * @see     http://php.net/pdostatement-setfetchmode for the details on the fetch mode options 
    193202     */ 
    194203    public function query($sql, array $driverOptions = array()) 
     
    199208         
    200209        try { 
    201             $pdoStatement = $this->pdo->query($sql, $driverOptions['fetchMode'], $driverOptions['type'],  $driverOptions['constructArgs']); 
    202         } catch (PDOException $pdoe) { 
    203             throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
    204         } 
    205          
    206         return new stubDatabasePDOStatement($pdoStatement); 
     210            if (isset($driverOptions['fetchMode']) == true) { 
     211                switch ($driverOptions['fetchMode']) { 
     212                    case PDO::FETCH_COLUMN: 
     213                        if (isset($driverOptions['colNo']) == false) { 
     214                            throw new stubDatabaseException('Fetch mode COLUMN requires driver option ŽcolNoŽ.'); 
     215                        } 
     216                         
     217                        $pdoStatement = $this->pdo->query($sql, $driverOptions['fetchMode'], $driverOptions['colNo']); 
     218                        break; 
     219                    case PDO::FETCH_INTO: 
     220                        if (isset($driverOptions['object']) == false) { 
     221                            throw new stubDatabaseException('Fetch mode INTO requires driver option ŽobjectŽ.'); 
     222                        } 
     223                         
     224                        $pdoStatement = $this->pdo->query($sql, $driverOptions['fetchMode'], $driverOptions['object']); 
     225                        break; 
     226                    case PDO::FETCH_CLASS: 
     227                        if (isset($driverOptions['classname']) == false) { 
     228                            throw new stubDatabaseException('Fetch mode CLASS requires driver option ŽclassnameŽ.'); 
     229                        } 
     230                         
     231                        if (isset($driverOptions['ctorargs']) == false) { 
     232                            $driverOptions['ctorargs'] = array(); 
     233                        } 
     234                         
     235                        $pdoStatement = $this->pdo->query($sql, $driverOptions['fetchMode'], $driverOptions['classname'], $driverOptions['ctorargs']); 
     236                        break; 
     237                    default: 
     238                        $pdoStatement = $this->pdo->query($sql, $driverOptions['fetchMode']); 
     239                } 
     240            } else { 
     241                $pdoStatement = $this->pdo->query($sql); 
     242            } 
     243        } catch (PDOException $pdoe) { 
     244            throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
     245        } 
     246         
     247        $statement = new stubDatabasePDOStatement($pdoStatement); 
     248        return $statement; 
    207249    } 
    208250     
     
    222264         
    223265        try { 
    224             $effectedRows = $this->pdo->exec($statement); 
    225         } catch (PDOException $pdoe) { 
    226             throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
    227         } 
    228          
    229         return $effectedRows; 
     266            return $this->pdo->exec($statement); 
     267        } catch (PDOException $pdoe) { 
     268            throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
     269        } 
    230270    } 
    231271     
     
    244284         
    245285        try { 
    246             $lastInsertId = $this->pdo->lastInsertId($name); 
    247         } catch (PDOException $pdoe) { 
    248             throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
    249         } 
    250          
    251         return $lastInsertId; 
     286            return $this->pdo->lastInsertId($name); 
     287        } catch (PDOException $pdoe) { 
     288            throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
     289        } 
    252290    } 
    253291} 
  • trunk/src/main/php/net/stubbles/rdbms/rdbms.php

    r170 r174  
    99stubClassLoader::load('net.stubbles.rdbms.stubDatabaseException', 
    1010                      'net.stubbles.rdbms.stubDatabaseStatement', 
     11                      'net.stubbles.rdbms.stubDatabaseConnectionData', 
    1112                      'net.stubbles.rdbms.stubDatabaseConnection', 
    1213                      'net.stubbles.rdbms.stubDatabaseConnectionPool' 
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionData.php

    r173 r174  
    1818     * id of the default connection 
    1919     */ 
    20     const ID_DEFAULT                = '__default'; 
     20    const ID_DEFAULT         = '__default'; 
    2121    /** 
    2222     * id to use for the connection 
     
    5959    /** 
    6060     * set the id to use for the connection 
     61     *  
     62     * Warning: two instances will be the same if they have the same id,  
     63     * regardless whether the concrete connection data is differant or not. 
     64     * You should never use the same id for differant connection datasets. 
    6165     * 
    6266     * @param  string  $id 
     
    7983    /** 
    8084     * checks whether a value is equal to the class 
     85     *  
     86     * Warning: two instances will be the same if they have the same id,  
     87     * regardless whether the concrete connection data is differant or not. 
     88     * You should never use the same id for differant connection datasets. 
    8189     * 
    8290     * @param   mixed  $compare 
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionPool.php

    r173 r174  
    4646    { 
    4747        if (isset(self::$connections[$id]) == false) { 
    48             if (isset(self::$dsns[$id]) == false) { 
     48            if (isset(self::$connectionData[$id]) == false) { 
    4949                throw new stubDatabaseException('No connection and no dsn known for connection associated with id ' . $id); 
    5050            } 
     
    5757            $connection = new $nqClassName(self::$connectionData[$id]); 
    5858            if (($connection instanceof stubDatabaseConnection) == false) { 
    59                 throw new stubDatabaseException(self::$connectionData[$id]['class'] . ' is not an instance of stubDatabaseConnection.'); 
     59                throw new stubDatabaseException(self::$connectionData[$id]->getConnectionClassName() . ' is not an instance of net.stubbles.rdbms.stubDatabaseConnection.'); 
    6060            } 
    6161             
     
    103103     
    104104    /** 
     105     * removes the connection data with the given id 
     106     * 
     107     * @param   string  $id  optional  id of the connection data to remove 
     108     */ 
     109    public static function removeConnectionData($id = stubDatabaseConnectionData::ID_DEFAULT) 
     110    { 
     111        if (isset(self::$connectionData[$id]) == true) { 
     112            self::$connectionData[$id] = null; 
     113        } 
     114    } 
     115     
     116    /** 
    105117     * closes the connection with the given id 
    106118     *  
  • trunk/src/test/run.php

    r154 r174  
    3232        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/ioc/IOCTestSuite.php'); 
    3333        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/ipo/IPOTestSuite.php'); 
     34        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/rdbms/RDBMSTestSuite.php'); 
    3435        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/reflection/ReflectionTestSuite.php'); 
    3536        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/util/UtilTestSuite.php');