Changeset 4455 for trunk/lib/Doctrine

Show
Ignore:
Timestamp:
05/28/08 07:01:04 (8 months ago)
Author:
guilhermeblanco
Message:

SelectExpression? rewrite to support dctrn queryComponent. Fixes in DQL. Added more test cases

Location:
trunk/lib/Doctrine/Query
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Doctrine/Query/ParserResult.php

    r4452 r4455  
    8787     * @param string $queryField Alias declaration. 
    8888     */ 
    89     public function setQueryField($fieldAlias, array $queryField) 
     89    public function setQueryField($fieldAlias, $queryField) 
    9090    { 
    9191        $this->_queryFields[$fieldAlias] = $queryField; 
     
    100100    public function getQueryFields() 
    101101    { 
    102         return $this->_queryComponents; 
     102        return $this->_queryFields; 
    103103    } 
    104104 
  • trunk/lib/Doctrine/Query/Production.php

    r4422 r4455  
    3737{ 
    3838    /** 
     39     * @nodoc 
     40     */ 
     41    const SQLALIAS_SEPARATOR = '__'; 
     42 
     43 
     44    /** 
     45     * @nodoc 
     46     */ 
     47    const DEFAULT_QUERYCOMPONENT = 'dctrn'; 
     48 
     49 
     50    /** 
    3951     * Parser object 
    4052     * 
  • trunk/lib/Doctrine/Query/Production/FieldIdentificationVariable.php

    r4451 r4455  
    3333class Doctrine_Query_Production_FieldIdentificationVariable extends Doctrine_Query_Production 
    3434{ 
    35     protected $_componentAlias; 
     35    protected $_fieldAlias; 
     36 
     37    protected $_columnAlias; 
    3638 
    3739 
     
    5052        if ($parserResult->hasQueryField($this->_fieldAlias)) { 
    5153            // We should throw semantical error if there's already a component for this alias 
    52             $queryComponent = $parserResult->getQueryField($this->_fieldAlias); 
    53             $fieldName = $queryComponent['fieldName']; 
     54            $fieldName = $parserResult->getQueryField($this->_fieldAlias); 
    5455 
    5556            $message  = "Cannot re-declare field alias '{$this->_fieldAlias}'" 
    56                       . "for '".$paramHolder->get('fieldName')."'. It was already declared for " 
    57                       . "field '{$fieldName}'."; 
     57                      . "for '".$paramHolder->get('fieldName')."'."; 
    5858 
    5959            $this->_parser->semanticalError($message); 
    6060        } 
     61 
     62        // Now we map it in queryComponent 
     63        $componentAlias = Doctrine_Query_Production::DEFAULT_QUERYCOMPONENT; 
     64        $queryComponent = $parserResult->getQueryComponent($componentAlias); 
     65 
     66        $idx = count($queryComponent['scalar']); 
     67        $queryComponent['scalar'][$idx] = $this->_fieldAlias; 
     68        $parserResult->setQueryComponent($componentAlias, $queryComponent); 
     69 
     70        // And also in field aliases 
     71        $parserResult->setQueryField($queryComponent['scalar'][$idx], $idx); 
     72 
     73        // Build the column alias 
     74        $this->_columnAlias = $parserResult->getTableAliasFromComponentAlias($componentAlias) 
     75                            . Doctrine_Query_Production::SQLALIAS_SEPARATOR . $idx; 
    6176    } 
    6277} 
  • trunk/lib/Doctrine/Query/Production/GroupByClause.php

    r4422 r4455  
    5353    public function buildSql() 
    5454    { 
    55         return 'GROUP BY ' . implode(', ', $this->_mapGroupByItems()) . ')'; 
     55        return 'GROUP BY ' . implode(', ', $this->_mapGroupByItems()); 
    5656    } 
    5757 
  • trunk/lib/Doctrine/Query/Production/PathExpression.php

    r4451 r4455  
    3838    protected $_fieldName; 
    3939 
    40     protected $_queryComponent; 
     40    protected $_componentAlias; 
    4141 
    4242 
     
    7474            // Retrieve ClassMetadata 
    7575            $k = array_keys($queryComponents); 
    76             $componentAlias = $k[1]; 
     76            $this->_componentAlias = $k[1]; 
    7777 
    78             $this->_queryComponent = $queryComponents[$componentAlias]; 
    79             $classMetadata = $this->_queryComponent['metadata']; 
     78            $classMetadata = $queryComponents[$this->_componentAlias]['metadata']; 
    8079        } else { 
    81             $path = $this->_identifiers[0]; 
    82             $this->_queryComponent = $parserResult->getQueryComponent($path); 
     80            $this->_componentAlias = $path = $this->_identifiers[0]; 
     81            $queryComponent = $parserResult->getQueryComponent($path); 
    8382 
    8483            // We should have a semantical error if the queryComponent does not exists yet 
    85             if ($this->_queryComponent === null) { 
     84            if ($queryComponent === null) { 
    8685                $this->_parser->semanticalError("Undefined component alias for '{$path}'", $this->_parser->token); 
    8786            } 
    8887 
    8988            // Initializing ClassMetadata 
    90             $classMetadata = $this->_queryComponent['metadata']; 
     89            $classMetadata = $queryComponent['metadata']; 
    9190 
    9291            // Looping through relations 
     
    109108                } 
    110109 
    111                 // Assigning new queryComponent and classMetadata 
    112                 $this->_queryComponent = $parserResult->getQueryComponent($path); 
     110                // Assigning new componentAlias, queryComponent and classMetadata 
     111                $this->_componentAlias = $path; 
    113112 
    114                 $classMetadata = $this->_queryComponent['metadata']; 
     113                $queryComponent = $parserResult->getQueryComponent($path); 
     114                $classMetadata = $queryComponent['metadata']; 
    115115            } 
    116116        } 
     
    134134        $conn = $manager->getConnection(); 
    135135 
    136         // Looking for componentAlias to fetch 
    137         $componentAlias = implode('.', $this->_identifiers); 
    138  
    139         if (count($this->_identifiers) == 0) { 
    140             $queryComponents = $parserResult->getQueryComponents(); 
    141  
    142             // Retrieve ClassMetadata 
    143             $k = array_keys($queryComponents); 
    144             $componentAlias = $k[1]; 
    145         } 
     136        // Looking for queryComponent to fetch 
     137        $queryComponent = $parserResult->getQueryComponent($this->_componentAlias); 
    146138 
    147139        // Generating the SQL piece 
    148         $str = $parserResult->getTableAliasFromComponentAlias($componentAlias) . '.' 
    149              . $this->_queryComponent['metadata']->getColumnName($this->_fieldName); 
     140        $str = $parserResult->getTableAliasFromComponentAlias($this->_componentAlias) . '.' 
     141             . $queryComponent['metadata']->getColumnName($this->_fieldName); 
    150142 
    151143        return $conn->quoteIdentifier($str); 
  • trunk/lib/Doctrine/Query/Production/SelectExpression.php

    r4451 r4455  
    8787        $parserResult = $this->_parser->getParserResult(); 
    8888 
    89         // Here we inspect for duplicate IdentificationVariable, and if the 
    90         // left expression needs the identification variable. If yes, check 
    91         // its existance. 
     89        // We cannot have aliases for foo.* 
    9290        if ($this->_leftExpression instanceof Doctrine_Query_Production_PathExpressionEndingWithAsterisk && $this->_fieldIdentificationVariable !== null) { 
    9391            $this->_parser->semanticalError( 
    94                 "Cannot assign an identification variable to a path expression with asterisk (ie. foo.bar.* AS foobaz)." 
     92                "Cannot assign an identification variable to a path expression ending with asterisk (ie. foo.bar.* AS foobaz)." 
    9593            ); 
    9694        } 
    9795 
     96        // Also, we cannot have aliases for path expressions: foo.bar 
     97        if ($this->_leftExpression instanceof Doctrine_Query_Production_PathExpressionEndingWithAsterisk && $this->_fieldIdentificationVariable !== null) { 
     98            $this->_parser->semanticalError( 
     99                "Cannot assign an identification variable to a path expression (ie. foo.bar AS foobaz)." 
     100            ); 
     101        } 
     102 
     103        // Make semantical checks 
    98104        $this->_leftExpression->semantical($paramHolder); 
    99105 
     
    128134        $parserResult = $this->_parser->getParserResult(); 
    129135 
     136        // Retrieving connection 
     137        $manager = Doctrine_EntityManager::getManager();  
     138        $conn = $manager->getConnection(); 
     139 
    130140        switch (get_class($this->_leftExpression)) { 
    131141            case 'Doctrine_Query_Production_PathExpressionEndingWithAsterisk': 
     
    135145            case 'Doctrine_Query_Production_PathExpression': 
    136146                // We bring the queryComponent from the class instance 
    137                 $queryComponent = $this->_leftExpression->getQueryComponent(); 
     147                $componentAlias = $this->_leftExpression->getComponentAlias(); 
     148                $queryComponent = $parserResult->getQueryComponent($componentAlias); 
     149                $fieldName = $this->_leftExpression->getFieldName(); 
     150 
     151                // Build the column alias now 
     152                $columnAlias = $parserResult->getTableAliasFromComponentAlias($componentAlias) 
     153                             . Doctrine_Query_Production::SQLALIAS_SEPARATOR 
     154                             . $queryComponent['metadata']->getColumnName($fieldName); 
    138155            break; 
    139156 
    140157            default: 
    141158                // We bring the default queryComponent 
    142                 $queryComponent = $parserResult->getQueryComponent('dctrn'); 
     159                $componentAlias = Doctrine_Query_Production::DEFAULT_QUERYCOMPONENT; 
     160                $queryComponent = $parserResult->getQueryComponent($componentAlias); 
     161 
     162                // If we have FieldIdentificationVariable, we have to use the scalar map of it 
     163                if ($this->_fieldIdentificationVariable !== null) { 
     164                    $columnAlias = $this->_fieldIdentificationVariable->getColumnAlias(); 
     165                } else { 
     166                    // We have to include the map now, since we don't have the scalar mapped 
     167                    $queryFields = $parserResult->getQueryFields(); 
     168                    $itemIndex = 'item' . count(array_filter($queryFields, array($this, "_nonIdentifiedVariable"))); 
     169                    $idx = count($queryFields); 
     170 
     171                    $queryComponent['scalar'][$idx] = $itemIndex; 
     172                    $parserResult->setQueryComponent($componentAlias, $queryComponent); 
     173 
     174                    // And also in field aliases 
     175                    $parserResult->setQueryField($itemIndex, $idx); 
     176 
     177                    // Build the column alias 
     178                    $columnAlias = $parserResult->getTableAliasFromComponentAlias($componentAlias) 
     179                                 . Doctrine_Query_Production::SQLALIAS_SEPARATOR . $idx; 
     180                } 
    143181            break; 
    144         } 
    145  
    146         // Retrieving connection 
    147         $manager = Doctrine_EntityManager::getManager();  
    148         $conn = $manager->getConnection(); 
    149  
    150         $componentAlias = $parserResult->getComponentAlias($queryComponent); 
    151  
    152         if ($this->_fieldIdentificationVariable !== null) { 
    153             // We need to add scalar map in queryComponent if iidentificationvariable is set. 
    154             $idx = count($queryComponent['scalar']); 
    155             $queryComponent['scalar'][$idx] = $this->_fieldIdentificationVariable; 
    156             $parserResult->setQueryComponent($componentAlias, $queryComponent); 
    157  
    158             $columnAlias = $parserResult->getTableAliasFromComponentAlias($componentAlias) . '__' . $idx; 
    159         } elseif ($this->_leftExpression instanceof Doctrine_Query_Production_PathExpression) { 
    160             // We need to build the column alias based on column name 
    161             $columnAlias = $parserResult->getTableAliasFromComponentAlias($componentAlias)  
    162                          . '__' . $queryComponent['metadata']->getColumnName($this->_leftExpression->getFieldName()); 
    163         } else { 
    164             // The right thing should be return the index alone... but we need a better solution. 
    165             // Possible we can search the index for mapped values and add our item there. 
    166  
    167             // [TODO] Find another return value. We cant return 0 here. 
    168             $columnAlias = 'idx__' . 0; 
    169182        } 
    170183 
    171184        return ' AS ' . $conn->quoteIdentifier($columnAlias); 
    172185    } 
     186 
     187 
     188    protected function _nonIdentifiedVariable($value) 
     189    { 
     190        return ! is_string($value); 
     191    } 
    173192}