Changeset 1897

Show
Ignore:
Timestamp:
10/24/08 12:22:57 (2 months ago)
Author:
mikey
Message:

add support for unique keys

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • framework/trunk/src/main/php/net/stubbles/rdbms/persistence/annotation/stubDBColumnAnnotation.php

    r1763 r1897  
    66 * @package     stubbles 
    77 * @subpackage  rdbms_persistence_annotation 
     8 * @version     $Id$ 
    89 */ 
    910stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotation', 
     
    135136 
    136137    /** 
     138     * set whether the column is unique or not 
     139     * 
     140     * @param  bool  $isUnique 
     141     */ 
     142    public function setIsUnique($isUnique) 
     143    { 
     144        $this->dbTableColumn->setIsUnique($this->castToBool($isUnique)); 
     145    } 
     146 
     147    /** 
    137148     * set the name of the setter method 
    138149     * 
  • framework/trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseMySQLQueryBuilder.php

    r1895 r1897  
    66 * @package     stubbles 
    77 * @subpackage  rdbms_querybuilder 
     8 * @version     $Id$ 
    89 */ 
    910stubClassLoader::load('net::stubbles::rdbms::querybuilder::stubDatabaseQueryBuilder'); 
     
    215216        $primaryKeys = array(); 
    216217        $keys        = array(); 
     218        $uniques     = array(); 
    217219        $counter     = 0; 
    218220        foreach ($columns as $column) { 
     
    265267            } elseif ($column->isKey() === true) { 
    266268                $keys[] = $column->getName(); 
     269            } elseif ($column->isUnique() === true) { 
     270                $uniques[] = $column->getName(); 
    267271            } 
    268272        } 
     
    280284        } 
    281285         
     286        if (count($uniques) > 0) { 
     287            foreach ($uniques as $unique) { 
     288                $query .= ",\n  UNIQUE (`" . $unique . '`)'; 
     289            } 
     290        } 
     291         
    282292        $query .= "\n)"; 
    283293        if ($tableDescription->hasType() === true) { 
  • framework/trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableColumn.php

    r1763 r1897  
    66 * @package     stubbles 
    77 * @subpackage  rdbms_querybuilder 
     8 * @version     $Id$ 
    89 */ 
    910/** 
     
    8889    protected $isKey        = false; 
    8990    /** 
     91     * switch whether column is unique or not 
     92     * 
     93     * @var  bool 
     94     */ 
     95    protected $isUnique     = false; 
     96    /** 
    9097     * the name of the setter method to use for restoring the value from database 
    9198     * 
     
    352359    { 
    353360        return (false == $this->isPrimaryKey && true == $this->isKey); 
     361    } 
     362 
     363    /** 
     364     * set whether the column is unique or not 
     365     * 
     366     * @param  bool  $isUnique 
     367     */ 
     368    public function setIsUnique($isUnique) 
     369    { 
     370        $this->isUnique = $isUnique; 
     371    } 
     372 
     373    /** 
     374     * check whether the column is unique or not 
     375     * 
     376     * @return  bool 
     377     */ 
     378    public function isUnique() 
     379    { 
     380        return (false == $this->isPrimaryKey && true == $this->isUnique); 
    354381    } 
    355382 
  • framework/trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseMySQLQueryBuilderTestCase.php

    r1895 r1897  
    66 * @package     stubbles 
    77 * @subpackage  rdbms_querybuilder_test 
     8 * @version     $Id$ 
    89 */ 
    910stubClassLoader::load('net::stubbles::rdbms::querybuilder::stubDatabaseMySQLQueryBuilder'); 
     
    245246        $column4->setName('column4'); 
    246247        $column4->setType('DATETIME'); 
     248        $column4->setIsUnique(true); 
    247249        $tableDescription->addColumn($column4); 
    248250         
     
    277279                            "  column8 YEAR DEFAULT NULL,\n" . 
    278280                            "  PRIMARY KEY (`column2`),\n" . 
    279                             "  KEY (`column3`)\n" . 
     281                            "  KEY (`column3`),\n" . 
     282                            "  UNIQUE (`column4`)\n" . 
    280283                            ") ENGINE = InnoDB", 
    281284                            $this->mySqlQueryBuilder->createTable($tableDescription)); 
  • framework/trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableColumnTestCase.php

    r1763 r1897  
    66 * @package     stubbles 
    77 * @subpackage  rdbms_querybuilder_test 
     8 * @version     $Id$ 
    89 */ 
    910stubClassLoader::load('net::stubbles::rdbms::querybuilder::stubDatabaseTableColumn'); 
     
    214215 
    215216    /** 
     217     * check that setting and getting the isUnique property works as expected 
     218     * 
     219     * @test 
     220     */ 
     221    public function isUnique() 
     222    { 
     223        $this->assertFalse($this->tableColumn->isUnique()); 
     224        $this->tableColumn->setIsUnique(true); 
     225        $this->assertTrue($this->tableColumn->isUnique()); 
     226        $this->tableColumn->setIsPrimaryKey(true); 
     227        $this->assertFalse($this->tableColumn->isUnique()); 
     228        $this->tableColumn->setIsUnique(false); 
     229        $this->assertFalse($this->tableColumn->isUnique()); 
     230    } 
     231 
     232    /** 
    216233     * check that setting and getting the setter method works as expected 
    217234     *