Changeset 4765 for trunk/lib

Show
Ignore:
Timestamp:
08/09/08 10:45:28 (5 months ago)
Author:
romanb
Message:

refactoring.

Location:
trunk/lib/Doctrine
Files:
9 added
21 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Doctrine/ClassMetadata/Factory.php

    r4723 r4765  
    2828 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi> 
    2929 * @author      Roman Borschel <roman@code-factory.org> 
    30  * @package     Doctrine 
    31  * @subpackage  ClassMetadata 
    3230 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    3331 * @version     $Revision$ 
     
    189187        } 
    190188         
    191         // complete identifier mapping 
    192         $this->_initIdentifier($class); 
     189        $class->completeIdentifierMapping(); 
    193190         
    194191        return $class; 
    195     } 
    196      
    197     /** 
    198      * Initializes the class identifier(s)/primary key(s). 
    199      * 
    200      * @param Doctrine_Metadata  The metadata container of the class in question. 
    201      */ 
    202     protected function _initIdentifier(Doctrine_ClassMetadata $class) 
    203     { 
    204         /*switch (count($class->getIdentifier())) { 
    205             case 0: // No identifier in the class mapping yet 
    206                  
    207                 // If its a subclass, inherit the identifier from the parent. 
    208                 if ($class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED && 
    209                         count($class->getParentClasses()) > 0) {        
    210                     $parents = $class->getParentClasses(); 
    211                     $root = end($parents); 
    212                     $rootClass = $class->getConnection()->getMetadata($root); 
    213                     $class->setIdentifier($rootClass->getIdentifier()); 
    214                      
    215                     if ($class->getIdentifierType() !== Doctrine::IDENTIFIER_AUTOINC) { 
    216                         $class->setIdentifierType($rootClass->getIdentifierType()); 
    217                     } else { 
    218                         $class->setIdentifierType(Doctrine::IDENTIFIER_NATURAL); 
    219                     } 
    220  
    221                     // add all inherited primary keys 
    222                     foreach ($class->getIdentifier() as $id) { 
    223                         $definition = $rootClass->getDefinitionOf($id); 
    224  
    225                         // inherited primary keys shouldn't contain autoinc 
    226                         // and sequence definitions 
    227                         unset($definition['autoincrement']); 
    228                         unset($definition['sequence']); 
    229  
    230                         // add the inherited primary key column 
    231                         $fullName = $rootClass->getColumnName($id) . ' as ' . $id; 
    232                         $class->setColumn($fullName, $definition['type'], $definition['length'], 
    233                                 $definition, true); 
    234                     } 
    235                 } else { 
    236                     throw Doctrine_MappingException::identifierRequired($class->getClassName()); 
    237                 } 
    238                 break; 
    239             case 1: // A single identifier is in the mapping 
    240                 foreach ($class->getIdentifier() as $pk) { 
    241                     $columnName = $class->getColumnName($pk); 
    242                     $thisColumns = $class->getFieldMappings(); 
    243                     $e = $thisColumns[$columnName]; 
    244  
    245                     $found = false; 
    246  
    247                     foreach ($e as $option => $value) { 
    248                         if ($found) { 
    249                             break; 
    250                         } 
    251  
    252                         $e2 = explode(':', $option); 
    253  
    254                         switch (strtolower($e2[0])) { 
    255                             case 'autoincrement': 
    256                             case 'autoinc': 
    257                                 $class->setIdentifierType(Doctrine::IDENTIFIER_AUTOINC); 
    258                                 $found = true; 
    259                                 break; 
    260                             case 'seq': 
    261                             case 'sequence': 
    262                                 $class->setIdentifierType(Doctrine::IDENTIFIER_SEQUENCE); 
    263                                 $found = true; 
    264  
    265                                 if ($value) { 
    266                                     $class->setTableOption('sequenceName', $value); 
    267                                 } else { 
    268                                     if (($sequence = $class->getAttribute(Doctrine::ATTR_DEFAULT_SEQUENCE)) !== null) { 
    269                                         $class->setTableOption('sequenceName', $sequence); 
    270                                     } else { 
    271                                         $class->setTableOption('sequenceName', $class->getConnection() 
    272                                                 ->getSequenceName($class->getTableName())); 
    273                                     } 
    274                                 } 
    275                                 break; 
    276                         } 
    277                     } 
    278                     $identifierType = $class->getIdentifierType(); 
    279                     if ( ! isset($identifierType)) { 
    280                         $class->setIdentifierType(Doctrine::IDENTIFIER_NATURAL); 
    281                     } 
    282                 } 
    283  
    284                 $class->setIdentifier(array($pk)); 
    285  
    286                 break; 
    287             default: // Multiple identifiers are in the mapping so its a composite id 
    288                 $class->setIdentifierType(Doctrine::IDENTIFIER_COMPOSITE); 
    289         }*/ 
    290          
    291         // If the chosen generator type is "auto", then pick the one appropriate for 
    292         // the database. 
    293          
    294         // FIXME: This is very ugly here. Such switch()es on the database driver 
    295         // are unnecessary as we can easily replace them with polymorphic calls on 
    296         // the connection (or another) object. We just need to decide where to put 
    297         // the id generation types. 
    298         $class->completeIdentifierMapping(); 
    299192    } 
    300193     
  • trunk/lib/Doctrine/Connection.php

    r4725 r4765  
    167167     */ 
    168168    protected $_sequenceManager; 
     169     
     170    /** 
     171     * The schema manager. 
     172     * 
     173     * @var Doctrine::DBAL::Schema::SchemaManager 
     174     */ 
     175    protected $_schemaManager; 
    169176 
    170177    /** 
     
    11691176        return $this->_sequenceManager; 
    11701177    } 
     1178     
     1179    /** 
     1180     * Gets the SchemaManager that can be used to inspect or change the  
     1181     * database schema through the connection. 
     1182     * 
     1183     * @return Doctrine::DBAL::Schema::SchemaManager 
     1184     */ 
     1185    public function getSchemaManager() 
     1186    { 
     1187        if ( ! $this->_schemaManager) { 
     1188            $class = "Doctrine_Schema_" . $this->_driverName . "SchemaManager"; 
     1189            $this->_schemaManager = new $class($this); 
     1190        } 
     1191        return $this->_schemaManager; 
     1192    } 
    11711193} 
  • trunk/lib/Doctrine/DatabasePlatform.php

    r4725 r4765  
    66 * Base class for all DatabasePlatforms. The DatabasePlatforms are the central 
    77 * point of abstraction of platform-specific behaviors, features and SQL dialects. 
     8 * They are a passive source of information. 
    89 * 
    910 * @since 2.0 
  • trunk/lib/Doctrine/EntityManager.php

    r4723 r4765  
    299299    /** 
    300300     * Flushes all changes to objects that have been queued up to now to the database. 
    301      * 
    302      * @todo package:orm 
    303301     */ 
    304302    public function flush() 
  • trunk/lib/Doctrine/Export.php

    r4723 r4765  
    3737class Doctrine_Export extends Doctrine_Connection_Module 
    3838{ 
    39     protected $valid_default_values = array( 
    40         'text'      => '', 
    41         'boolean'   => true, 
    42         'integer'   => 0, 
    43         'decimal'   => 0.0, 
    44         'float'     => 0.0, 
    45         'timestamp' => '1970-01-01 00:00:00', 
    46         'time'      => '00:00:00', 
    47         'date'      => '1970-01-01', 
    48         'clob'      => '', 
    49         'blob'      => '', 
    50         'string'    => '', 
    51     ); 
    52  
    53     /** 
    54      * drop an existing database 
    55      * (this method is implemented by the drivers) 
    56      * 
    57      * @param string $name name of the database that should be dropped 
    58      * @return void 
    59      */ 
    60     public function dropDatabase($database) 
    61     { 
    62         $this->conn->execute($this->dropDatabaseSql($database)); 
    63     } 
    64  
    65     /** 
    66      * drop an existing database 
    67      * (this method is implemented by the drivers) 
    68      * 
    69      * @param string $name name of the database that should be dropped 
    70      * @return void 
    71      */ 
    72     public function dropDatabaseSql($database) 
    73     { 
    74         throw new Doctrine_Export_Exception('Drop database not supported by this driver.'); 
    75     } 
    76  
    77     /** 
    78      * dropTableSql 
    79      * drop an existing table 
    80      * 
    81      * @param string $table           name of table that should be dropped from the database 
    82      * @return string 
    83      */ 
    84     public function dropTableSql($table) 
    85     { 
    86         return 'DROP TABLE ' . $this->conn->quoteIdentifier($table); 
    87     } 
    88  
    89     /** 
    90      * dropTable 
    91      * drop an existing table 
    92      * 
    93      * @param string $table           name of table that should be dropped from the database 
    94      * @return void 
    95      */ 
    96     public function dropTable($table) 
    97     { 
    98         $this->conn->execute($this->dropTableSql($table)); 
    99     } 
    100  
    101     /** 
    102      * drop existing index 
    103      * 
    104      * @param string    $table        name of table that should be used in method 
    105      * @param string    $name         name of the index to be dropped 
    106      * @return void 
    107      */ 
    108     public function dropIndex($table, $name) 
    109     { 
    110         return $this->conn->exec($this->dropIndexSql($table, $name)); 
    111     } 
    112  
    113     /** 
    114      * dropIndexSql 
    115      * 
    116      * @param string    $table        name of table that should be used in method 
    117      * @param string    $name         name of the index to be dropped 
    118      * @return string                 SQL that is used for dropping an index 
    119      */ 
    120     public function dropIndexSql($table, $name) 
    121     { 
    122         $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)); 
    123  
    124         return 'DROP INDEX ' . $name; 
    125     } 
    126  
    127     /** 
    128      * drop existing constraint 
    129      * 
    130      * @param string    $table        name of table that should be used in method 
    131      * @param string    $name         name of the constraint to be dropped 
    132      * @param string    $primary      hint if the constraint is primary 
    133      * @return void 
    134      */ 
    135     public function dropConstraint($table, $name, $primary = false) 
    136     { 
    137         $table = $this->conn->quoteIdentifier($table); 
    138         $name  = $this->conn->quoteIdentifier($name); 
    139  
    140         return $this->conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name); 
    141     } 
    142  
    143     /** 
    144      * drop existing foreign key 
    145      * 
    146      * @param string    $table        name of table that should be used in method 
    147      * @param string    $name         name of the foreign key to be dropped 
    148      * @return void 
    149      */ 
    150     public function dropForeignKey($table, $name) 
    151     { 
    152         return $this->dropConstraint($table, $name); 
    153     } 
    154  
    155     /** 
    156      * dropSequenceSql 
    157      * drop existing sequence 
    158      * (this method is implemented by the drivers) 
    159      * 
    160      * @throws Doctrine_Connection_Exception     if something fails at database level 
    161      * @param string $sequenceName      name of the sequence to be dropped 
    162      * @return void 
    163      */ 
    164     public function dropSequence($sequenceName) 
    165     { 
    166         $this->conn->exec($this->dropSequenceSql($sequenceName)); 
    167     } 
    168  
    169     /** 
    170      * dropSequenceSql 
    171      * drop existing sequence 
    172      * 
    173      * @throws Doctrine_Connection_Exception     if something fails at database level 
    174      * @param string $sequenceName name of the sequence to be dropped 
    175      * @return void 
    176      */ 
    177     public function dropSequenceSql($sequenceName) 
    178     { 
    179         throw new Doctrine_Export_Exception('Drop sequence not supported by this driver.'); 
    180     } 
    181  
    182     /** 
    183      * create a new database 
    184      * (this method is implemented by the drivers) 
    185      * 
    186      * @param string $name name of the database that should be created 
    187      * @return void 
    188      */ 
    189     public function createDatabase($database) 
    190     { 
    191         $this->conn->execute($this->createDatabaseSql($database)); 
    192     } 
    193  
    194     /** 
    195      * create a new database 
    196      * (this method is implemented by the drivers) 
    197      * 
    198      * @param string $name name of the database that should be created 
    199      * @return string 
    200      */ 
    201     public function createDatabaseSql($database) 
    202     { 
    203         throw new Doctrine_Export_Exception('Create database not supported by this driver.'); 
    204     } 
    205  
    206     /** 
    207      * create a new table 
    208      * 
    209      * @param string $name   Name of the database that should be created 
    210      * @param array $fields  Associative array that contains the definition of each field of the new table 
    211      *                       The indexes of the array entries are the names of the fields of the table an 
    212      *                       the array entry values are associative arrays like those that are meant to be 
    213      *                       passed with the field definitions to get[Type]Declaration() functions. 
    214      *                          array( 
    215      *                              'id' => array( 
    216      *                                  'type' => 'integer', 
    217      *                                  'unsigned' => 1 
    218      *                                  'notnull' => 1 
    219      *                                  'default' => 0 
    220      *                              ), 
    221      *                              'name' => array( 
    222      *                                  'type' => 'text', 
    223      *                                  'length' => 12 
    224      *                              ), 
    225      *                              'password' => array( 
    226      *                                  'type' => 'text', 
    227      *                                  'length' => 12 
    228      *                              ) 
    229      *                          ); 
    230      * @param array $options  An associative array of table options: 
    231      * 
    232      * @return string 
    233      */ 
    234     public function createTableSql($name, array $fields, array $options = array()) 
    235     { 
    236         if ( ! $name) { 
    237             throw new Doctrine_Export_Exception('no valid table name specified'); 
    238         } 
    239  
    240         if (empty($fields)) { 
    241             throw new Doctrine_Export_Exception('no fields specified for table ' . $name); 
    242         } 
    243  
    244         $queryFields = $this->getFieldDeclarationList($fields); 
    245  
    246  
    247         if (isset($options['primary']) && ! empty($options['primary'])) { 
    248             $queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')'; 
    249         } 
    250  
    251         if (isset($options['indexes']) && ! empty($options['indexes'])) { 
    252             foreach($options['indexes'] as $index => $definition) { 
    253                 $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition); 
    254             } 
    255         } 
    256  
    257         $query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields; 
    258  
    259         $check = $this->getCheckDeclaration($fields); 
    260  
    261         if ( ! empty($check)) { 
    262             $query .= ', ' . $check; 
    263         } 
    264  
    265         $query .= ')'; 
    266  
    267  
    268  
    269         $sql[] = $query; 
    270  
    271         if (isset($options['foreignKeys'])) { 
    272  
    273             foreach ((array) $options['foreignKeys'] as $k => $definition) { 
    274                 if (is_array($definition)) { 
    275                     $sql[] = $this->createForeignKeySql($name, $definition); 
    276                 } 
    277             } 
    278         } 
    279         return $sql; 
    280     } 
    281  
    282     /** 
    283      * create a new table 
    284      * 
    285      * @param string $name   Name of the database that should be created 
    286      * @param array $fields  Associative array that contains the definition of each field of the new table 
    287      * @param array $options  An associative array of table options: 
    288      * @see Doctrine_Export::createTableSql() 
    289      * 
    290      * @return void 
    291      */ 
    292     public function createTable($name, array $fields, array $options = array()) 
    293     { 
    294         $sql = (array) $this->createTableSql($name, $fields, $options); 
    295  
    296         foreach ($sql as $query) { 
    297             $this->conn->execute($query); 
    298         } 
    299     } 
    300  
    301     /** 
    302      * create sequence 
    303      * 
    304      * @throws Doctrine_Connection_Exception     if something fails at database level 
    305      * @param string    $seqName        name of the sequence to be created 
    306      * @param string    $start          start value of the sequence; default is 1 
    307      * @param array     $options  An associative array of table options: 
    308      *                          array( 
    309      *                              'comment' => 'Foo', 
    310      *                              'charset' => 'utf8', 
    311      *                              'collate' => 'utf8_unicode_ci', 
    312      *                          ); 
    313      * @return void 
    314      */ 
    315     public function createSequence($seqName, $start = 1, array $options = array()) 
    316     { 
    317         return $this->conn->execute($this->createSequenceSql($seqName, $start = 1, $options)); 
    318     } 
    319  
    320     /** 
    321      * return RDBMS specific create sequence statement 
    322      * (this method is implemented by the drivers) 
    323      * 
    324      * @throws Doctrine_Connection_Exception     if something fails at database level 
    325      * @param string    $seqName        name of the sequence to be created 
    326      * @param string    $start          start value of the sequence; default is 1 
    327      * @param array     $options  An associative array of table options: 
    328      *                          array( 
    329      *                              'comment' => 'Foo', 
    330      *                              'charset' => 'utf8', 
    331      *                              'collate' => 'utf8_unicode_ci', 
    332      *                          ); 
    333      * @return string 
    334      */ 
    335     public function createSequenceSql($seqName, $start = 1, array $options = array()) 
    336     { 
    337         throw new Doctrine_Export_Exception('Create sequence not supported by this driver.'); 
    338     } 
    339  
    340     /** 
    341      * create a constraint on a table 
    342      * 
    343      * @param string    $table         name of the table on which the constraint is to be created 
    344      * @param string    $name          name of the constraint to be created 
    345      * @param array     $definition    associative array that defines properties of the constraint to be created. 
    346      *                                 Currently, only one property named FIELDS is supported. This property 
    347      *                                 is also an associative with the names of the constraint fields as array 
    348      *                                 constraints. Each entry of this array is set to another type of associative 
    349      *                                 array that specifies properties of the constraint that are specific to 
    350      *                                 each field. 
    351      * 
    352      *                                 Example 
    353      *                                    array( 
    354      *                                        'fields' => array( 
    355      *                                            'user_name' => array(), 
    356      *                                            'last_login' => array() 
    357      *                                        ) 
    358      *                                    ) 
    359      * @return void 
    360      */ 
    361     public function createConstraint($table, $name, $definition) 
    362     { 
    363         $sql = $this->createConstraintSql($table, $name, $definition); 
    364  
    365         return $this->conn->exec($sql); 
    366     } 
    367  
    368     /** 
    369      * create a constraint on a table 
    370      * 
    371      * @param string    $table         name of the table on which the constraint is to be created 
    372      * @param string    $name          name of the constraint to be created 
    373      * @param array     $definition    associative array that defines properties of the constraint to be created. 
    374      *                                 Currently, only one property named FIELDS is supported. This property 
    375      *                                 is also an associative with the names of the constraint fields as array 
    376      *                                 constraints. Each entry of this array is set to another type of associative 
    377      *                                 array that specifies properties of the constraint that are specific to 
    378      *                                 each field. 
    379      * 
    380      *                                 Example 
    381      *                                    array( 
    382      *                                        'fields' => array( 
    383      *                                            'user_name' => array(), 
    384      *                                            'last_login' => array() 
    385      *                                        ) 
    386      *                                    ) 
    387      * @return void 
    388      */ 
    389     public function createConstraintSql($table, $name, $definition) 
    390     { 
    391         $table = $this->conn->quoteIdentifier($table); 
    392         $name  = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)); 
    393         $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name; 
    394  
    395         if (isset($definition['primary']) && $definition['primary']) { 
    396             $query .= ' PRIMARY KEY'; 
    397         } elseif (isset($definition['unique']) && $definition['unique']) { 
    398             $query .= ' UNIQUE'; 
    399         } 
    400  
    401         $fields = array(); 
    402         foreach (array_keys($definition['fields']) as $field) { 
    403             $fields[] = $this->conn->quoteIdentifier($field, true); 
    404         } 
    405         $query .= ' ('. implode(', ', $fields) . ')'; 
    406  
    407         return $query; 
    408     } 
    409  
    410     /** 
    411      * Get the stucture of a field into an array 
    412      * 
    413      * @param string    $table         name of the table on which the index is to be created 
    414      * @param string    $name          name of the index to be created 
    415      * @param array     $definition    associative array that defines properties of the index to be created. 
    416      *                                 Currently, only one property named FIELDS is supported. This property 
    417      *                                 is also an associative with the names of the index fields as array 
    418      *                                 indexes. Each entry of this array is set to another type of associative 
    419      *                                 array that specifies properties of the index that are specific to 
    420      *                                 each field. 
    421      * 
    422      *                                 Currently, only the sorting property is supported. It should be used 
    423      *                                 to define the sorting direction of the index. It may be set to either 
    424      *                                 ascending or descending. 
    425      * 
    426      *                                 Not all DBMS support index sorting direction configuration. The DBMS 
    427      *                                 drivers of those that do not support it ignore this property. Use the 
    428      *                                 function supports() to determine whether the DBMS driver can manage indexes. 
    429      * 
    430      *                                 Example 
    431      *                                    array( 
    432      *                                        'fields' => array( 
    433      *                                            'user_name' => array( 
    434      *                                                'sorting' => 'ascending' 
    435      *                                            ), 
    436      *                                            'last_login' => array() 
    437      *                                        ) 
    438      *                                    ) 
    439      * @return void 
    440      */ 
    441     public function createIndex($table, $name, array $definition) 
    442     { 
    443         return $this->conn->execute($this->createIndexSql($table, $name, $definition)); 
    444     } 
    445  
    446     /** 
    447      * Get the stucture of a field into an array 
    448      * 
    449      * @param string    $table         name of the table on which the index is to be created 
    450      * @param string    $name          name of the index to be created 
    451      * @param array     $definition    associative array that defines properties of the index to be created. 
    452      * @see Doctrine_Export::createIndex() 
    453      * @return string 
    454      */ 
    455     public function createIndexSql($table, $name, array $definition) 
    456     { 
    457         $table  = $this->conn->quoteIdentifier($table); 
    458         $name   = $this->conn->quoteIdentifier($name); 
    459         $type   = ''; 
    460  
    461         if (isset($definition['type'])) { 
    462             switch (strtolower($definition['type'])) { 
    463                 case 'unique': 
    464                     $type = strtoupper($definition['type']) . ' '; 
    465                 break;