Changeset 4455 for trunk/lib/Doctrine
- Timestamp:
- 05/28/08 07:01:04 (8 months ago)
- Location:
- trunk/lib/Doctrine/Query
- Files:
-
- 6 modified
-
ParserResult.php (modified) (2 diffs)
-
Production.php (modified) (1 diff)
-
Production/FieldIdentificationVariable.php (modified) (2 diffs)
-
Production/GroupByClause.php (modified) (1 diff)
-
Production/PathExpression.php (modified) (4 diffs)
-
Production/SelectExpression.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Doctrine/Query/ParserResult.php
r4452 r4455 87 87 * @param string $queryField Alias declaration. 88 88 */ 89 public function setQueryField($fieldAlias, array$queryField)89 public function setQueryField($fieldAlias, $queryField) 90 90 { 91 91 $this->_queryFields[$fieldAlias] = $queryField; … … 100 100 public function getQueryFields() 101 101 { 102 return $this->_query Components;102 return $this->_queryFields; 103 103 } 104 104 -
trunk/lib/Doctrine/Query/Production.php
r4422 r4455 37 37 { 38 38 /** 39 * @nodoc 40 */ 41 const SQLALIAS_SEPARATOR = '__'; 42 43 44 /** 45 * @nodoc 46 */ 47 const DEFAULT_QUERYCOMPONENT = 'dctrn'; 48 49 50 /** 39 51 * Parser object 40 52 * -
trunk/lib/Doctrine/Query/Production/FieldIdentificationVariable.php
r4451 r4455 33 33 class Doctrine_Query_Production_FieldIdentificationVariable extends Doctrine_Query_Production 34 34 { 35 protected $_componentAlias; 35 protected $_fieldAlias; 36 37 protected $_columnAlias; 36 38 37 39 … … 50 52 if ($parserResult->hasQueryField($this->_fieldAlias)) { 51 53 // 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); 54 55 55 56 $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')."'."; 58 58 59 59 $this->_parser->semanticalError($message); 60 60 } 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; 61 76 } 62 77 } -
trunk/lib/Doctrine/Query/Production/GroupByClause.php
r4422 r4455 53 53 public function buildSql() 54 54 { 55 return 'GROUP BY ' . implode(', ', $this->_mapGroupByItems()) . ')';55 return 'GROUP BY ' . implode(', ', $this->_mapGroupByItems()); 56 56 } 57 57 -
trunk/lib/Doctrine/Query/Production/PathExpression.php
r4451 r4455 38 38 protected $_fieldName; 39 39 40 protected $_ queryComponent;40 protected $_componentAlias; 41 41 42 42 … … 74 74 // Retrieve ClassMetadata 75 75 $k = array_keys($queryComponents); 76 $ componentAlias = $k[1];76 $this->_componentAlias = $k[1]; 77 77 78 $this->_queryComponent = $queryComponents[$componentAlias]; 79 $classMetadata = $this->_queryComponent['metadata']; 78 $classMetadata = $queryComponents[$this->_componentAlias]['metadata']; 80 79 } else { 81 $ path = $this->_identifiers[0];82 $ this->_queryComponent = $parserResult->getQueryComponent($path);80 $this->_componentAlias = $path = $this->_identifiers[0]; 81 $queryComponent = $parserResult->getQueryComponent($path); 83 82 84 83 // We should have a semantical error if the queryComponent does not exists yet 85 if ($ this->_queryComponent === null) {84 if ($queryComponent === null) { 86 85 $this->_parser->semanticalError("Undefined component alias for '{$path}'", $this->_parser->token); 87 86 } 88 87 89 88 // Initializing ClassMetadata 90 $classMetadata = $ this->_queryComponent['metadata'];89 $classMetadata = $queryComponent['metadata']; 91 90 92 91 // Looping through relations … … 109 108 } 110 109 111 // Assigning new queryComponent and classMetadata112 $this->_ queryComponent = $parserResult->getQueryComponent($path);110 // Assigning new componentAlias, queryComponent and classMetadata 111 $this->_componentAlias = $path; 113 112 114 $classMetadata = $this->_queryComponent['metadata']; 113 $queryComponent = $parserResult->getQueryComponent($path); 114 $classMetadata = $queryComponent['metadata']; 115 115 } 116 116 } … … 134 134 $conn = $manager->getConnection(); 135 135 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); 146 138 147 139 // 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); 150 142 151 143 return $conn->quoteIdentifier($str); -
trunk/lib/Doctrine/Query/Production/SelectExpression.php
r4451 r4455 87 87 $parserResult = $this->_parser->getParserResult(); 88 88 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.* 92 90 if ($this->_leftExpression instanceof Doctrine_Query_Production_PathExpressionEndingWithAsterisk && $this->_fieldIdentificationVariable !== null) { 93 91 $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)." 95 93 ); 96 94 } 97 95 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 98 104 $this->_leftExpression->semantical($paramHolder); 99 105 … … 128 134 $parserResult = $this->_parser->getParserResult(); 129 135 136 // Retrieving connection 137 $manager = Doctrine_EntityManager::getManager(); 138 $conn = $manager->getConnection(); 139 130 140 switch (get_class($this->_leftExpression)) { 131 141 case 'Doctrine_Query_Production_PathExpressionEndingWithAsterisk': … … 135 145 case 'Doctrine_Query_Production_PathExpression': 136 146 // 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); 138 155 break; 139 156 140 157 default: 141 158 // 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 } 143 181 break; 144 }145 146 // Retrieving connection147 $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 name161 $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;169 182 } 170 183 171 184 return ' AS ' . $conn->quoteIdentifier($columnAlias); 172 185 } 186 187 188 protected function _nonIdentifiedVariable($value) 189 { 190 return ! is_string($value); 191 } 173 192 }