- Timestamp:
- 07/26/08 20:44:54 (6 months ago)
- Location:
- branches
- Files:
-
- 2 added
- 4 modified
-
0.11/lib/Doctrine/Query.php (modified) (1 diff)
-
0.11/lib/Doctrine/RawSql.php (modified) (4 diffs)
-
0.11/tests/run.php (modified) (1 diff)
-
0.11/tests/Ticket/1195TestCase.php (added)
-
1.0/lib/Doctrine/RawSql.php (modified) (2 diffs)
-
1.0/tests/Ticket/1195TestCase.php (added)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.11/lib/Doctrine/Query.php
r4708 r4714 1176 1176 case 'pgsql': 1177 1177 // pgsql needs special nested LIMIT subquery 1178 $subquery = 'SELECT doctrine_subquery_alias.' . $idColumnName . ' FROM (' . $subquery . ') AS doctrine_subquery_alias'; 1178 $subquery = 'SELECT ' 1179 . $this->_conn->quoteIdentifier('doctrine_subquery_alias.' . $idColumnName) 1180 . ' FROM (' . $subquery . ') AS ' 1181 . $this->_conn->quoteIdentifier('doctrine_subquery_alias'); 1179 1182 break; 1180 1183 } -
branches/0.11/lib/Doctrine/RawSql.php
r4572 r4714 73 73 return $this; 74 74 } 75 if ( ! isset($this-> parts[$queryPartName])) {75 if ( ! isset($this->_sqlParts[$queryPartName])) { 76 76 $this->_sqlParts[$queryPartName] = array(); 77 77 } … … 221 221 } 222 222 223 $q = 'SELECT '; 224 225 if ($this->_sqlParts['distinct'] == true) { 226 $q .= 'DISTINCT '; 227 } 228 223 229 // first add the fields of the root component 224 230 reset($this->_queryComponents); 225 231 $componentAlias = key($this->_queryComponents); 226 232 227 $q = 'SELECT ' .implode(', ', $select[$componentAlias]);233 $q .= implode(', ', $select[$componentAlias]); 228 234 unset($select[$componentAlias]); 229 235 … … 235 241 236 242 $string = $this->getInheritanceCondition($this->getRootAlias()); 243 237 244 if ( ! empty($string)) { 238 245 $this->_sqlParts['where'][] = $string; 239 246 } 240 $copy = $this->_sqlParts;241 unset($copy['select']);242 247 243 248 $q .= ( ! empty($this->_sqlParts['from']))? ' FROM ' . implode(' ', $this->_sqlParts['from']) : ''; … … 253 258 } 254 259 return $q; 260 } 261 262 /** 263 * getCountQuery 264 * builds the count query. 265 * 266 * @return string the built sql query 267 */ 268 public function getCountQuery($params = array()) 269 { 270 //Doing COUNT( DISTINCT rootComponent.id ) 271 //This is not correct, if the result is not hydrated by doctrine, but it mimics the behaviour of Doctrine_Query::getCountQuery 272 reset($this->_queryComponents); 273 $componentAlias = key($this->_queryComponents); 274 $tableAlias = $this->getSqlTableAlias($componentAlias); 275 $fields = array(); 276 277 foreach ((array) $this->_queryComponents[$componentAlias]['table']->getIdentifierColumnNames() as $key) { 278 $fields[] = $tableAlias . '.' . $key; 279 } 280 281 $q = 'SELECT COUNT( DISTINCT '.implode(',',$fields).') as num_results'; 282 283 $string = $this->getInheritanceCondition($this->getRootAlias()); 284 if ( ! empty($string)) { 285 $this->_sqlParts['where'][] = $string; 286 } 287 288 $q .= ( ! empty($this->_sqlParts['from']))? ' FROM ' . implode(' ', $this->_sqlParts['from']) : ''; 289 $q .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_sqlParts['where']) : ''; 290 $q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : ''; 291 $q .= ( ! empty($this->_sqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_sqlParts['having']) : ''; 292 293 if ( ! empty($string)) { 294 array_pop($this->_sqlParts['where']); 295 } 296 297 return $q; 298 } 299 300 /** 301 * count 302 * fetches the count of the query 303 * 304 * This method executes the main query without all the 305 * selected fields, ORDER BY part, LIMIT part and OFFSET part. 306 * 307 * This is an exact copy of the Dql Version 308 * 309 * @see Doctrine_Query::count() 310 * @param array $params an array of prepared statement parameters 311 * @return integer the count of this query 312 */ 313 public function count($params = array()) 314 { 315 $q = $this->getCountQuery(); 316 317 if ( ! is_array($params)) { 318 $params = array($params); 319 } 320 321 $params = array_merge($this->_params['join'], $this->_params['where'], $this->_params['having'], $params); 322 323 $params = $this->convertEnums($params); 324 325 $results = $this->getConnection()->fetchAll($q, $params); 326 327 if (count($results) > 1) { 328 $count = count($results); 329 } else { 330 if (isset($results[0])) { 331 $results[0] = array_change_key_case($results[0], CASE_LOWER); 332 $count = $results[0]['num_results']; 333 } else { 334 $count = 0; 335 } 336 } 337 338 return (int) $count; 255 339 } 256 340 -
branches/0.11/tests/run.php
r4697 r4714 87 87 $tickets->addTestCase(new Doctrine_Ticket_1175_TestCase()); 88 88 $tickets->addTestCase(new Doctrine_Ticket_1192_TestCase()); 89 $tickets->addTestCase(new Doctrine_Ticket_1195_TestCase()); 89 90 $tickets->addTestCase(new Doctrine_Ticket_1205_TestCase()); 90 91 $tickets->addTestCase(new Doctrine_Ticket_1206_TestCase()); -
branches/1.0/lib/Doctrine/RawSql.php
r4684 r4714 222 222 223 223 $q = 'SELECT '; 224 if($this->_sqlParts['distinct'] == true) $q .= 'DISTINCT '; 224 225 if ($this->_sqlParts['distinct'] == true) { 226 $q .= 'DISTINCT '; 227 } 225 228 226 229 // first add the fields of the root component 227 230 reset($this->_queryComponents); 228 231 $componentAlias = key($this->_queryComponents); 232 229 233 $q .= implode(', ', $select[$componentAlias]); 230 234 unset($select[$componentAlias]); … … 237 241 238 242 $string = $this->getInheritanceCondition($this->getRootAlias()); 243 239 244 if ( ! empty($string)) { 240 245 $this->_sqlParts['where'][] = $string;