Changeset 1906

Show
Ignore:
Timestamp:
10/25/08 17:14:00 (2 months ago)
Author:
mikey
Message:

allow possibility to have an initial query

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • framework/trunk/projects/dist/config/xml/rdbms.xml

    r1670 r1906  
    88      <userName>root</userName> 
    99      <password>foo</password> 
     10      <initialQuery>set names utf8</initialQuery> 
    1011    </connection> 
    1112  </pool> 
  • framework/trunk/src/main/php/net/stubbles/rdbms/pdo/stubDatabasePDOConnection.php

    r1754 r1906  
    6464    { 
    6565        if (null !== $this->pdo) { 
    66             throw new stubDatabaseException('Already connected, can not connect twice.')
     66            return
    6767        } 
    6868         
     
    7070            $this->createPDO(); 
    7171            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     72            if ($this->connectionData->hasInitialQuery() === true) { 
     73                $this->pdo->query($this->connectionData->getInitialQuery()); 
     74            } 
    7275        } catch (PDOException $pdoe) { 
    7376            throw new stubDatabaseException($pdoe->getMessage(), $pdoe); 
     
    8083     * @throws  PDOException 
    8184     */ 
     85    // @codeCoverageIgnoreStart 
    8286    protected function createPDO() 
    8387    { 
     
    96100        } 
    97101    } 
     102    // @codeCoverageIgnoreEnd 
    98103 
    99104    /** 
  • framework/trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionData.php

    r1763 r1906  
    66 * @package     stubbles 
    77 * @subpackage  rdbms 
     8 * @version     $Id$ 
    89 */ 
    910/** 
     
    5556     */ 
    5657    protected $driverOptions = array(); 
     58    /** 
     59     * initial query to be executed after commit 
     60     * 
     61     * @var  string 
     62     */ 
     63    protected $initialQuery; 
    5764 
    5865    /** 
     
    198205        return $this->driverOptions; 
    199206    } 
     207 
     208    /** 
     209     * sets initial query to be send after establishing the connection 
     210     * 
     211     * @param  string  $initialQuery 
     212     */ 
     213    public function setInitialQuery($initialQuery) 
     214    { 
     215        $this->initialQuery = $initialQuery; 
     216    } 
     217 
     218    /** 
     219     * checks if an initial query should be send 
     220     * 
     221     * @return  string 
     222     */ 
     223    public function hasInitialQuery() 
     224    { 
     225        return (null != $this->initialQuery); 
     226    } 
     227 
     228    /** 
     229     * returns initial query to be send after establishing the connection 
     230     * 
     231     * @return  string 
     232     */ 
     233    public function getInitialQuery() 
     234    { 
     235        return $this->initialQuery; 
     236    } 
    200237} 
    201238?> 
  • framework/trunk/src/main/resources/xjconf/rdbms.xml

    r1670 r1906  
    22<defines> 
    33  <namespace uri="http://stubbles.net/rdbms"> 
    4     <tag name="pool" type="net::stubbles::rdbms::stubDatabaseConnectionPool" static="true" /> 
    5     <tag name="connection" type="net::stubbles::rdbms::stubDatabaseConnectionData" setter="addConnectionData" /> 
    6     <tag name="id" type="string" /> 
    7     <tag name="connectionClassName" type="string" /> 
    8     <tag name="dsn" type="string" /> 
    9     <tag name="userName" type="string" /> 
    10     <tag name="password" type="string" /> 
    11     <tag name="driverOptions" type="array" /> 
     4    <tag name="pool" type="net::stubbles::rdbms::stubDatabaseConnectionPool" static="true"/> 
     5    <tag name="connection" type="net::stubbles::rdbms::stubDatabaseConnectionData" setter="addConnectionData"/> 
     6    <tag name="id" type="string"/> 
     7    <tag name="connectionClassName" type="string"/> 
     8    <tag name="dsn" type="string"/> 
     9    <tag name="userName" type="string"/> 
     10    <tag name="password" type="string"/> 
     11    <tag name="driverOptions" type="array"/> 
    1212    <tag name="driverOption" keyAttribute="name" type="string"> 
    13       <attribute name="name" type="string" /> 
     13      <attribute name="name" type="string"/> 
    1414    </tag> 
     15    <tag name="initialQuery" type="string"/> 
    1516  </namespace> 
    1617</defines> 
  • framework/trunk/src/test/php/net/stubbles/integration/DatabaseTestCase.php

    r1763 r1906  
    66 * @package     stubbles 
    77 * @subpackage  test_integration 
     8 * @version     $Id$ 
    89 */ 
    910stubClassLoader::load('net::stubbles::rdbms::stubDatabaseXJConfInitializer'); 
     
    3839        $this->assertEquals('root', $connectionData->getUserName()); 
    3940        $this->assertEquals('foo', $connectionData->getPassword()); 
     41        $this->assertTrue($connectionData->hasInitialQuery()); 
     42        $this->assertEquals('set names utf8', $connectionData->getInitialQuery()); 
    4043         
    4144        // cached 
     
    4649        $this->assertEquals('root', $connectionData->getUserName()); 
    4750        $this->assertEquals('foo', $connectionData->getPassword()); 
     51        $this->assertTrue($connectionData->hasInitialQuery()); 
     52        $this->assertEquals('set names utf8', $connectionData->getInitialQuery()); 
    4853    } 
    4954} 
  • framework/trunk/src/test/php/net/stubbles/rdbms/pdo/stubDatabasePDOConnectionTestCase.php

    r1754 r1906  
    112112 
    113113    /** 
     114     * connect() can be called anytime,  but with not initial query nothing is done 
     115     * 
     116     * @test 
     117     */ 
     118    public function connectCanBeCalledAnytime() 
     119    { 
     120        $this->mockPDO->expects($this->never())->method('query'); 
     121        $this->pdoConnection->connect(); 
     122        $this->pdoConnection->connect(); 
     123    } 
     124 
     125    /** 
     126     * if an initial query is present it should be executed when connection is established 
     127     * 
     128     * @test 
     129     */ 
     130    public function connectShouldSetInitialQuery() 
     131    { 
     132        $this->connectionData->setInitialQuery('set names utf8'); 
     133        $this->mockPDO->expects($this->once())->method('query')->with($this->equalTo('set names utf8')); 
     134        $this->pdoConnection->connect(); 
     135    } 
     136 
     137    /** 
    114138     * assert that a call to the methods delivers the expected result 
    115139     * 
     
    262286        $this->assertType('stubDatabasePDOStatement', $statement); 
    263287    } 
     288 
     289    /** 
     290     * the database type should be returned 
     291     * 
     292     * @test 
     293     */ 
     294    public function databaseTypeIsReturned() 
     295    { 
     296        $this->connectionData->setDSN('mysql:host=localhost;dbname=example'); 
     297        $this->assertEquals('mysql', $this->pdoConnection->getDatabase()); 
     298    } 
    264299} 
    265300?> 
  • framework/trunk/src/test/php/net/stubbles/rdbms/stubDatabaseConnectionDataTestCase.php

    r1763 r1906  
    66 * @package     stubbles 
    77 * @subpackage  rdbms_test 
     8 * @version     $Id$ 
    89 */ 
    910stubClassLoader::load('net::stubbles::rdbms::stubDatabaseConnectionData'); 
     
    3132        $this->assertEquals('', $connectionData->getPassword()); 
    3233        $this->assertEquals(array(), $connectionData->getDriverOptions()); 
     34        $this->assertFalse($connectionData->hasInitialQuery()); 
     35        $this->assertNull($connectionData->getInitialQuery()); 
    3336    } 
    3437 
     
    4750        $connectionData->setPassword('example'); 
    4851        $connectionData->setDriverOptions(array('key' => 'value')); 
     52        $connectionData->setInitialQuery('set names utf8'); 
    4953        $this->assertEquals('foo', $connectionData->getId()); 
    5054        $this->assertEquals('bar', $connectionData->getConnectionClassName()); 
     
    5357        $this->assertEquals('example', $connectionData->getPassword()); 
    5458        $this->assertEquals(array('key' => 'value'), $connectionData->getDriverOptions()); 
     59        $this->assertTrue($connectionData->hasInitialQuery()); 
     60        $this->assertEquals('set names utf8', $connectionData->getInitialQuery()); 
    5561    } 
    5662