Changeset 316

Show
Ignore:
Timestamp:
03/01/07 18:22:27 (1 year ago)
Author:
mikey
Message:

cleaned net.stubbles.rdbms.querybuilder.stubQueryBuilder from reflection stuff
added net.stubbles.rdbms.persistence.creator
(all untested)

Files:

Legend:

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

    r256 r316  
    9898     * creates the query to create a table for the given class 
    9999     * 
    100      * @param   string  $fqClassName  the full qualified classname of the class to create the table for 
     100     * @param   stubDatabaseTableDescription       $tableDescription 
    101101     * @throws  stubDatabaseQueryBuilderException 
    102      * @todo    put all reflection related stuff to persistence package,  
    103      *          introduce a class that represents a table description 
    104102     */ 
    105     public function createTable($fqClassName
     103    public function createTable(stubDatabaseTableDescription $tableDescription
    106104    { 
    107         $nqClassName = stubClassLoader::getNonQualifiedClassName($fqClassName); 
    108         if (class_exists($nqClassName, false) == false) { 
    109             stubClassLoader::load($fqClassName); 
    110         } 
    111          
    112         $refClass = new stubReflectionClass($nqClassName); 
    113         if ($refClass->hasAnnotation('DBTable') == false) { 
    114             throw new stubDatabaseQueryBuilderException('Class ' . $refClass->getName() . ' is missing the DBTable annotation.'); 
    115         } 
    116          
    117         $dbTable     = $refClass->getAnnotation('DBTable'); 
    118         $tableName   = $dbTable->getName(); 
    119         $query       = 'CREATE TABLE `' . $tableName . "` (\n"; 
    120         $methods     = $refClass->getMethods(); 
     105        $query       = 'CREATE TABLE `' . $tableDescription->getName() . "` (\n"; 
     106        $columns     = $tableDescription->getColumns(); 
    121107        $primaryKeys = array(); 
    122108        $keys        = array(); 
    123         foreach ($methods as $method) { 
    124             if ($method->hasAnnotation('DBColumn') == false || $method->getDeclaringClass()->equals($refClass) == false) { 
    125                 continue; 
    126             } elseif ($method->hasAnnotation('DBTable') == true && $method->getAnnotation('DBTable')->getName() != $tableName) { 
    127                 continue; 
     109        foreach ($columns as $column) { 
     110            $query   .= '  ' . $column->getName() . ' ' . $column->getType(); 
     111            if (strtoupper($column->getType()) != 'TEXT') { 
     112                $query   .= '(' . $column->getSize() . ')'; 
    128113            } 
    129114             
    130             $dbColumn = $method->getAnnotation('DBColumn'); 
    131             $query   .= '  ' . $dbColumn->getName() . ' ' . $dbColumn->getType(); 
    132             if (strtoupper($dbColumn->getType()) != 'TEXT') { 
    133                 $query   .= '(' . str_replace('_', ',', $dbColumn->getSize()) . ')'; 
    134             } 
    135              
    136             if ($dbColumn->isUnsigned() == true) { 
     115            if ($column->isUnsigned() == true) { 
    137116                $query .= ' UNSIGNED'; 
    138117            } 
    139118             
    140             if ($dbColumn->isNullable() == true && $dbColumn->getDefaultValue() == null) { 
     119            if ($column->isNullable() == true && $column->getDefaultValue() == null) { 
    141120                $query .= ' DEFAULT NULL'; 
    142121            } else { 
    143                 if ($dbColumn->isNullable() == false) { 
     122                if ($column->isNullable() == false) { 
    144123                    $query .= ' NOT NULL'; 
    145124                } 
    146125                 
    147                 if ($dbColumn->getDefaultValue() !== null) { 
    148                     $query .= " DEFAULT '" . $dbColumn->getDefaultValue() . "'"; 
     126                if ($column->getDefaultValue() !== null) { 
     127                    $query .= " DEFAULT '" . $column->getDefaultValue() . "'"; 
    149128                } 
    150129            } 
    151130             
    152             if ($dbColumn->isPrimaryKey() == true) { 
     131            if ($column->isPrimaryKey() == true) { 
    153132                $query        .= ' AUTO_INCREMENT'; 
    154                 $primaryKeys[] = $dbColumn->getName(); 
    155             } elseif ($dbColumn->isKey() == true) { 
    156                 $keys[] = $dbColumn->getName(); 
     133                $primaryKeys[] = $column->getName(); 
     134            } elseif ($column->isKey() == true) { 
     135                $keys[] = $column->getName(); 
    157136            } 
    158137             
     
    172151        } 
    173152         
    174         $query .= "\n) TYPE=" . $dbTable->getType(); 
    175         if ($dbTable->hasCharacterSet() == true) { 
    176             $query .= ' CHARACTER SET=' . $dbTable->getCharacterSet(); 
     153        $query .= "\n) TYPE=" . $tableDescription->getType(); 
     154        if ($tableDescription->hasCharacterSet() == true) { 
     155            $query .= ' CHARACTER SET=' . $tableDescription->getCharacterSet(); 
    177156        } 
    178157         
    179         if ($dbTable->hasCollation() == true) { 
    180             $query .= ' COLLATE=' . $dbTable->getCollation(); 
     158        if ($tableDescription->hasCollation() == true) { 
     159            $query .= ' COLLATE=' . $tableDescription->getCollation(); 
    181160        } 
    182161         
    183         if ($dbTable->hasComment() == true) { 
    184             $query .= ' COMMENT=' . $dbTable->getComment(); 
     162        if ($tableDescription->hasComment() == true) { 
     163            $query .= ' COMMENT=' . $tableDescription->getComment(); 
    185164        } 
    186165         
  • trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseQueryBuilder.php

    r256 r316  
    99stubClassLoader::load('net.stubbles.rdbms.criteria.stubCriterion', 
    1010                      'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderException', 
     11                      'net.stubbles.rdbms.querybuilder.stubDatabaseTableDescription', 
    1112                      'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 
    12                       'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 
    13                       'net.stubbles.reflection.reflection' 
     13                      'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation' 
    1414); 
    1515/** 
     
    4949     * creates the query to create a table for the given class 
    5050     * 
    51      * @param   string  $fqClassName  the full qualified classname of the class to create the table for 
     51     * @param   stubDatabaseTableDescription       $tableDescription 
    5252     * @throws  stubDatabaseQueryBuilderException 
    5353     */ 
    54     public function createTable($fqClassName); 
     54    public function createTable(stubDatabaseTableDescription $tableDescription); 
    5555} 
    5656?>