Complex classes like DataQuery often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class DataQuery { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $dataClass; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var SQLSelect |
||
| 23 | */ |
||
| 24 | protected $query; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $collidingFields = array(); |
||
| 30 | |||
| 31 | private $queriedColumns = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Boolean |
||
| 35 | */ |
||
| 36 | private $queryFinalised = false; |
||
| 37 | |||
| 38 | // TODO: replace subclass_access with this |
||
| 39 | protected $querySubclasses = true; |
||
| 40 | // TODO: replace restrictclasses with this |
||
| 41 | protected $filterByClassName = true; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Create a new DataQuery. |
||
| 45 | * |
||
| 46 | * @param String The name of the DataObject class that you wish to query |
||
| 47 | */ |
||
| 48 | public function __construct($dataClass) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Clone this object |
||
| 55 | */ |
||
| 56 | public function __clone() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Return the {@link DataObject} class that is being queried. |
||
| 62 | */ |
||
| 63 | public function dataClass() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Return the {@link SQLSelect} object that represents the current query; note that it will |
||
| 69 | * be a clone of the object. |
||
| 70 | */ |
||
| 71 | public function query() { |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Remove a filter from the query |
||
| 78 | * |
||
| 79 | * @param string|array $fieldExpression The predicate of the condition to remove |
||
| 80 | * (ignoring parameters). The expression will be considered a match if it's |
||
| 81 | * contained within any other predicate. |
||
| 82 | * @return DataQuery Self reference |
||
| 83 | */ |
||
| 84 | public function removeFilterOn($fieldExpression) { |
||
| 85 | $matched = false; |
||
| 86 | |||
| 87 | // If given a parameterised condition extract only the condition |
||
| 88 | if(is_array($fieldExpression)) { |
||
| 89 | reset($fieldExpression); |
||
| 90 | $fieldExpression = key($fieldExpression); |
||
| 91 | } |
||
| 92 | |||
| 93 | $where = $this->query->getWhere(); |
||
| 94 | // Iterate through each condition |
||
| 95 | foreach($where as $i => $condition) { |
||
| 96 | |||
| 97 | // Rewrite condition groups as plain conditions before comparison |
||
| 98 | if($condition instanceof SQLConditionGroup) { |
||
| 99 | $predicate = $condition->conditionSQL($parameters); |
||
| 100 | $condition = array($predicate => $parameters); |
||
| 101 | } |
||
| 102 | |||
| 103 | // As each condition is a single length array, do a single |
||
| 104 | // iteration to extract the predicate and parameters |
||
| 105 | foreach($condition as $predicate => $parameters) { |
||
| 106 | // @see SQLSelect::addWhere for why this is required here |
||
| 107 | if(strpos($predicate, $fieldExpression) !== false) { |
||
| 108 | unset($where[$i]); |
||
| 109 | $matched = true; |
||
| 110 | } |
||
| 111 | // Enforce single-item condition predicate => parameters structure |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // set the entire where clause back, but clear the original one first |
||
| 117 | if($matched) { |
||
| 118 | $this->query->setWhere($where); |
||
| 119 | } else { |
||
| 120 | throw new InvalidArgumentException("Couldn't find $fieldExpression in the query filter."); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $this; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Set up the simplest initial query |
||
| 128 | */ |
||
| 129 | public function initialiseQuery() { |
||
| 130 | // Get the tables to join to. |
||
| 131 | // Don't get any subclass tables - let lazy loading do that. |
||
| 132 | $tableClasses = ClassInfo::ancestry($this->dataClass, true); |
||
| 133 | |||
| 134 | // Error checking |
||
| 135 | if(!$tableClasses) { |
||
|
|
|||
| 136 | if(!SS_ClassLoader::instance()->hasManifest()) { |
||
| 137 | user_error("DataObjects have been requested before the manifest is loaded. Please ensure you are not" |
||
| 138 | . " querying the database in _config.php.", E_USER_ERROR); |
||
| 139 | } else { |
||
| 140 | user_error("DataList::create Can't find data classes (classes linked to tables) for" |
||
| 141 | . " $this->dataClass. Please ensure you run dev/build after creating a new DataObject.", |
||
| 142 | E_USER_ERROR); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | $baseClass = array_shift($tableClasses); |
||
| 147 | |||
| 148 | // Build our intial query |
||
| 149 | $this->query = new SQLSelect(array()); |
||
| 150 | $this->query->setDistinct(true); |
||
| 151 | |||
| 152 | if($sort = singleton($this->dataClass)->stat('default_sort')) { |
||
| 153 | $this->sort($sort); |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->query->setFrom("\"$baseClass\""); |
||
| 157 | |||
| 158 | $obj = Injector::inst()->get($baseClass); |
||
| 159 | $obj->extend('augmentDataQueryCreation', $this->query, $this); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function setQueriedColumns($queriedColumns) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Ensure that the query is ready to execute. |
||
| 168 | * |
||
| 169 | * @param array|null $queriedColumns Any columns to filter the query by |
||
| 170 | * @return SQLSelect The finalised sql query |
||
| 171 | */ |
||
| 172 | public function getFinalisedQuery($queriedColumns = null) { |
||
| 173 | if(!$queriedColumns) $queriedColumns = $this->queriedColumns; |
||
| 174 | if($queriedColumns) { |
||
| 175 | $queriedColumns = array_merge($queriedColumns, array('Created', 'LastEdited', 'ClassName')); |
||
| 176 | } |
||
| 177 | |||
| 178 | $query = clone $this->query; |
||
| 179 | $ancestorTables = ClassInfo::ancestry($this->dataClass, true); |
||
| 180 | |||
| 181 | // Generate the list of tables to iterate over and the list of columns required |
||
| 182 | // by any existing where clauses. This second step is skipped if we're fetching |
||
| 183 | // the whole dataobject as any required columns will get selected regardless. |
||
| 184 | if($queriedColumns) { |
||
| 185 | // Specifying certain columns allows joining of child tables |
||
| 186 | $tableClasses = ClassInfo::dataClassesFor($this->dataClass); |
||
| 187 | |||
| 188 | foreach ($query->getWhereParameterised($parameters) as $where) { |
||
| 189 | // Check for just the column, in the form '"Column" = ?' and the form '"Table"."Column"' = ? |
||
| 190 | if (preg_match('/^"([^"]+)"/', $where, $matches) || |
||
| 191 | preg_match('/^"([^"]+)"\."[^"]+"/', $where, $matches)) { |
||
| 192 | if (!in_array($matches[1], $queriedColumns)) $queriedColumns[] = $matches[1]; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } else { |
||
| 196 | $tableClasses = $ancestorTables; |
||
| 197 | } |
||
| 198 | |||
| 199 | $tableNames = array_values($tableClasses); |
||
| 200 | $baseClass = $tableNames[0]; |
||
| 201 | |||
| 202 | // Iterate over the tables and check what we need to select from them. If any selects are made (or the table is |
||
| 203 | // required for a select) |
||
| 204 | foreach($tableClasses as $tableClass) { |
||
| 205 | |||
| 206 | // Determine explicit columns to select |
||
| 207 | $selectColumns = null; |
||
| 208 | if ($queriedColumns) { |
||
| 209 | // Restrict queried columns to that on the selected table |
||
| 210 | $tableFields = DataObject::database_fields($tableClass); |
||
| 211 | unset($tableFields['ID']); |
||
| 212 | $selectColumns = array_intersect($queriedColumns, array_keys($tableFields)); |
||
| 213 | } |
||
| 214 | |||
| 215 | // If this is a subclass without any explicitly requested columns, omit this from the query |
||
| 216 | if(!in_array($tableClass, $ancestorTables) && empty($selectColumns)) continue; |
||
| 217 | |||
| 218 | // Select necessary columns (unless an explicitly empty array) |
||
| 219 | if($selectColumns !== array()) { |
||
| 220 | $this->selectColumnsFromTable($query, $tableClass, $selectColumns); |
||
| 221 | } |
||
| 222 | |||
| 223 | // Join if not the base table |
||
| 224 | if($tableClass !== $baseClass) { |
||
| 225 | $query->addLeftJoin($tableClass, "\"$tableClass\".\"ID\" = \"$baseClass\".\"ID\"", $tableClass, 10); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | |||
| 231 | // Resolve colliding fields |
||
| 232 | if($this->collidingFields) { |
||
| 233 | foreach($this->collidingFields as $k => $collisions) { |
||
| 234 | $caseClauses = array(); |
||
| 235 | foreach($collisions as $collision) { |
||
| 236 | if(preg_match('/^"([^"]+)"/', $collision, $matches)) { |
||
| 237 | $collisionBase = $matches[1]; |
||
| 238 | if(class_exists($collisionBase)) { |
||
| 239 | $collisionClasses = ClassInfo::subclassesFor($collisionBase); |
||
| 240 | $collisionClasses = Convert::raw2sql($collisionClasses, true); |
||
| 241 | $caseClauses[] = "WHEN \"$baseClass\".\"ClassName\" IN (" |
||
| 242 | . implode(", ", $collisionClasses) . ") THEN $collision"; |
||
| 243 | } |
||
| 244 | } else { |
||
| 245 | user_error("Bad collision item '$collision'", E_USER_WARNING); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | $query->selectField("CASE " . implode( " ", $caseClauses) . " ELSE NULL END", $k); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | |||
| 253 | if($this->filterByClassName) { |
||
| 254 | // If querying the base class, don't bother filtering on class name |
||
| 255 | if($this->dataClass != $baseClass) { |
||
| 256 | // Get the ClassName values to filter to |
||
| 257 | $classNames = ClassInfo::subclassesFor($this->dataClass); |
||
| 258 | if(!$classNames) user_error("DataList::create() Can't find data sub-classes for '$callerClass'"); |
||
| 259 | $classNamesPlaceholders = DB::placeholders($classNames); |
||
| 260 | $query->addWhere(array( |
||
| 261 | "\"$baseClass\".\"ClassName\" IN ($classNamesPlaceholders)" => $classNames |
||
| 262 | )); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | $query->selectField("\"$baseClass\".\"ID\"", "ID"); |
||
| 267 | $query->selectField(" |
||
| 268 | CASE WHEN \"$baseClass\".\"ClassName\" IS NOT NULL THEN \"$baseClass\".\"ClassName\" |
||
| 269 | ELSE ".Convert::raw2sql($baseClass, true)." END", |
||
| 270 | "RecordClassName" |
||
| 271 | ); |
||
| 272 | |||
| 273 | // TODO: Versioned, Translatable, SiteTreeSubsites, etc, could probably be better implemented as subclasses |
||
| 274 | // of DataQuery |
||
| 275 | |||
| 276 | $obj = Injector::inst()->get($this->dataClass); |
||
| 277 | $obj->extend('augmentSQL', $query, $this); |
||
| 278 | |||
| 279 | $this->ensureSelectContainsOrderbyColumns($query); |
||
| 280 | |||
| 281 | return $query; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Ensure that if a query has an order by clause, those columns are present in the select. |
||
| 286 | * |
||
| 287 | * @param SQLSelect $query |
||
| 288 | * @return null |
||
| 289 | */ |
||
| 290 | protected function ensureSelectContainsOrderbyColumns($query, $originalSelect = array()) { |
||
| 291 | $tableClasses = ClassInfo::dataClassesFor($this->dataClass); |
||
| 292 | $baseClass = array_shift($tableClasses); |
||
| 293 | |||
| 294 | if($orderby = $query->getOrderBy()) { |
||
| 295 | $newOrderby = array(); |
||
| 296 | $i = 0; |
||
| 297 | foreach($orderby as $k => $dir) { |
||
| 298 | $newOrderby[$k] = $dir; |
||
| 299 | |||
| 300 | // don't touch functions in the ORDER BY or public function calls |
||
| 301 | // selected as fields |
||
| 302 | if(strpos($k, '(') !== false) continue; |
||
| 303 | |||
| 304 | $col = str_replace('"', '', trim($k)); |
||
| 305 | $parts = explode('.', $col); |
||
| 306 | |||
| 307 | // Pull through SortColumn references from the originalSelect variables |
||
| 308 | if(preg_match('/_SortColumn/', $col)) { |
||
| 309 | if(isset($originalSelect[$col])) { |
||
| 310 | $query->selectField($originalSelect[$col], $col); |
||
| 311 | } |
||
| 312 | |||
| 313 | continue; |
||
| 314 | } |
||
| 315 | |||
| 316 | if(count($parts) == 1) { |
||
| 317 | |||
| 318 | if(DataObject::has_own_table_database_field($baseClass, $parts[0])) { |
||
| 319 | $qualCol = "\"$baseClass\".\"{$parts[0]}\""; |
||
| 320 | } else { |
||
| 321 | $qualCol = "\"$parts[0]\""; |
||
| 322 | } |
||
| 323 | |||
| 324 | // remove original sort |
||
| 325 | unset($newOrderby[$k]); |
||
| 326 | |||
| 327 | // add new columns sort |
||
| 328 | $newOrderby[$qualCol] = $dir; |
||
| 329 | |||
| 330 | // To-do: Remove this if block once SQLSelect::$select has been refactored to store getSelect() |
||
| 331 | // format internally; then this check can be part of selectField() |
||
| 332 | $selects = $query->getSelect(); |
||
| 333 | if(!isset($selects[$col]) && !in_array($qualCol, $selects)) { |
||
| 334 | $query->selectField($qualCol); |
||
| 335 | } |
||
| 336 | } else { |
||
| 337 | $qualCol = '"' . implode('"."', $parts) . '"'; |
||
| 338 | |||
| 339 | if(!in_array($qualCol, $query->getSelect())) { |
||
| 340 | unset($newOrderby[$k]); |
||
| 341 | |||
| 342 | $newOrderby["\"_SortColumn$i\""] = $dir; |
||
| 343 | $query->selectField($qualCol, "_SortColumn$i"); |
||
| 344 | |||
| 345 | $i++; |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | } |
||
| 350 | |||
| 351 | $query->setOrderBy($newOrderby); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Execute the query and return the result as {@link SS_Query} object. |
||
| 357 | * |
||
| 358 | * @return SS_Query |
||
| 359 | */ |
||
| 360 | public function execute() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Return this query's SQL |
||
| 366 | * |
||
| 367 | * @param array $parameters Out variable for parameters required for this query |
||
| 368 | * @return string The resulting SQL query (may be paramaterised) |
||
| 369 | */ |
||
| 370 | public function sql(&$parameters = array()) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Return the number of records in this query. |
||
| 376 | * Note that this will issue a separate SELECT COUNT() query. |
||
| 377 | */ |
||
| 378 | public function count() { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Return the maximum value of the given field in this DataList |
||
| 385 | * |
||
| 386 | * @param String $field Unquoted database column name. Will be ANSI quoted |
||
| 387 | * automatically so must not contain double quotes. |
||
| 388 | */ |
||
| 389 | public function max($field) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Return the minimum value of the given field in this DataList |
||
| 395 | * |
||
| 396 | * @param String $field Unquoted database column name. Will be ANSI quoted |
||
| 397 | * automatically so must not contain double quotes. |
||
| 398 | */ |
||
| 399 | public function min($field) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Return the average value of the given field in this DataList |
||
| 405 | * |
||
| 406 | * @param String $field Unquoted database column name. Will be ANSI quoted |
||
| 407 | * automatically so must not contain double quotes. |
||
| 408 | */ |
||
| 409 | public function avg($field) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Return the sum of the values of the given field in this DataList |
||
| 415 | * |
||
| 416 | * @param String $field Unquoted database column name. Will be ANSI quoted |
||
| 417 | * automatically so must not contain double quotes. |
||
| 418 | */ |
||
| 419 | public function sum($field) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Runs a raw aggregate expression. Please handle escaping yourself |
||
| 425 | */ |
||
| 426 | public function aggregate($expression) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return the first row that would be returned by this full DataQuery |
||
| 432 | * Note that this will issue a separate SELECT ... LIMIT 1 query. |
||
| 433 | */ |
||
| 434 | public function firstRow() { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Return the last row that would be returned by this full DataQuery |
||
| 440 | * Note that this will issue a separate SELECT ... LIMIT query. |
||
| 441 | */ |
||
| 442 | public function lastRow() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Update the SELECT clause of the query with the columns from the given table |
||
| 448 | */ |
||
| 449 | protected function selectColumnsFromTable(SQLSelect &$query, $tableClass, $columns = null) { |
||
| 450 | // Add SQL for multi-value fields |
||
| 451 | $databaseFields = DataObject::database_fields($tableClass); |
||
| 452 | $compositeFields = DataObject::composite_fields($tableClass, false); |
||
| 453 | unset($databaseFields['ID']); |
||
| 454 | foreach($databaseFields as $k => $v) { |
||
| 455 | if((is_null($columns) || in_array($k, $columns)) && !isset($compositeFields[$k])) { |
||
| 456 | // Update $collidingFields if necessary |
||
| 457 | if($expressionForField = $query->expressionForField($k)) { |
||
| 458 | if(!isset($this->collidingFields[$k])) $this->collidingFields[$k] = array($expressionForField); |
||
| 459 | $this->collidingFields[$k][] = "\"$tableClass\".\"$k\""; |
||
| 460 | |||
| 461 | } else { |
||
| 462 | $query->selectField("\"$tableClass\".\"$k\"", $k); |
||
| 463 | } |
||
| 464 | } |
||
| 465 | } |
||
| 466 | foreach($compositeFields as $k => $v) { |
||
| 467 | if((is_null($columns) || in_array($k, $columns)) && $v) { |
||
| 468 | $dbO = Object::create_from_string($v, $k); |
||
| 469 | $dbO->setTable($tableClass); |
||
| 470 | $dbO->addToQuery($query); |
||
| 471 | } |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Append a GROUP BY clause to this query. |
||
| 477 | * |
||
| 478 | * @param String $groupby Escaped SQL statement |
||
| 479 | */ |
||
| 480 | public function groupby($groupby) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Append a HAVING clause to this query. |
||
| 487 | * |
||
| 488 | * @param String $having Escaped SQL statement |
||
| 489 | */ |
||
| 490 | public function having($having) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Create a disjunctive subgroup. |
||
| 497 | * |
||
| 498 | * That is a subgroup joined by OR |
||
| 499 | * |
||
| 500 | * @return DataQuery_SubGroup |
||
| 501 | */ |
||
| 502 | public function disjunctiveGroup() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Create a conjunctive subgroup |
||
| 508 | * |
||
| 509 | * That is a subgroup joined by AND |
||
| 510 | * |
||
| 511 | * @return DataQuery_SubGroup |
||
| 512 | */ |
||
| 513 | public function conjunctiveGroup() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Adds a WHERE clause. |
||
| 519 | * |
||
| 520 | * @see SQLSelect::addWhere() for syntax examples, although DataQuery |
||
| 521 | * won't expand multiple arguments as SQLSelect does. |
||
| 522 | * |
||
| 523 | * @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or |
||
| 524 | * paramaterised queries |
||
| 525 | * @return DataQuery |
||
| 526 | */ |
||
| 527 | public function where($filter) { |
||
| 528 | if($filter) { |
||
| 529 | $this->query->addWhere($filter); |
||
| 530 | } |
||
| 531 | return $this; |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Append a WHERE with OR. |
||
| 536 | * |
||
| 537 | * @see SQLSelect::addWhere() for syntax examples, although DataQuery |
||
| 538 | * won't expand multiple method arguments as SQLSelect does. |
||
| 539 | * |
||
| 540 | * @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or |
||
| 541 | * paramaterised queries |
||
| 542 | * @return DataQuery |
||
| 543 | */ |
||
| 544 | public function whereAny($filter) { |
||
| 545 | if($filter) { |
||
| 546 | $this->query->addWhereAny($filter); |
||
| 547 | } |
||
| 548 | return $this; |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set the ORDER BY clause of this query |
||
| 553 | * |
||
| 554 | * @see SQLSelect::orderby() |
||
| 555 | * |
||
| 556 | * @param String $sort Column to sort on (escaped SQL statement) |
||
| 557 | * @param String $direction Direction ("ASC" or "DESC", escaped SQL statement) |
||
| 558 | * @param Boolean $clear Clear existing values |
||
| 559 | * @return DataQuery |
||
| 560 | */ |
||
| 561 | public function sort($sort = null, $direction = null, $clear = true) { |
||
| 562 | if($clear) { |
||
| 563 | $this->query->setOrderBy($sort, $direction); |
||
| 564 | } else { |
||
| 565 | $this->query->addOrderBy($sort, $direction); |
||
| 566 | } |
||
| 567 | |||
| 568 | return $this; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Reverse order by clause |
||
| 573 | * |
||
| 574 | * @return DataQuery |
||
| 575 | */ |
||
| 576 | public function reverseSort() { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Set the limit of this query. |
||
| 583 | * |
||
| 584 | * @param int $limit |
||
| 585 | * @param int $offset |
||
| 586 | */ |
||
| 587 | public function limit($limit, $offset = 0) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Set whether this query should be distinct or not. |
||
| 594 | * |
||
| 595 | * @param bool $value |
||
| 596 | * @return DataQuery |
||
| 597 | */ |
||
| 598 | public function distinct($value) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Add an INNER JOIN clause to this query. |
||
| 605 | * |
||
| 606 | * @param String $table The unquoted table name to join to. |
||
| 607 | * @param String $onClause The filter for the join (escaped SQL statement) |
||
| 608 | * @param String $alias An optional alias name (unquoted) |
||
| 609 | * @param int $order A numerical index to control the order that joins are added to the query; lower order values |
||
| 610 | * will cause the query to appear first. The default is 20, and joins created automatically by the |
||
| 611 | * ORM have a value of 10. |
||
| 612 | * @param array $parameters Any additional parameters if the join is a parameterised subquery |
||
| 613 | */ |
||
| 614 | public function innerJoin($table, $onClause, $alias = null, $order = 20, $parameters = array()) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Add a LEFT JOIN clause to this query. |
||
| 623 | * |
||
| 624 | * @param string $table The unquoted table to join to. |
||
| 625 | * @param string $onClause The filter for the join (escaped SQL statement). |
||
| 626 | * @param string $alias An optional alias name (unquoted) |
||
| 627 | * @param int $order A numerical index to control the order that joins are added to the query; lower order values |
||
| 628 | * will cause the query to appear first. The default is 20, and joins created automatically by the |
||
| 629 | * ORM have a value of 10. |
||
| 630 | * @param array $parameters Any additional parameters if the join is a parameterised subquery |
||
| 631 | */ |
||
| 632 | public function leftJoin($table, $onClause, $alias = null, $order = 20, $parameters = array()) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Traverse the relationship fields, and add the table |
||
| 641 | * mappings to the query object state. This has to be called |
||
| 642 | * in any overloaded {@link SearchFilter->apply()} methods manually. |
||
| 643 | * |
||
| 644 | * @param string|array $relation The array/dot-syntax relation to follow |
||
| 645 | * @param bool $linearOnly Set to true to restrict to linear relations only. Set this |
||
| 646 | * if this relation will be used for sorting, and should not include duplicate rows. |
||
| 647 | * @return string The model class of the related item |
||
| 648 | */ |
||
| 649 | public function applyRelation($relation, $linearOnly = false) { |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Join the given class to this query with the given key |
||
| 699 | * |
||
| 700 | * @param string $localClass Name of class that has the has_one to the joined class |
||
| 701 | * @param string $localField Name of the has_one relationship to joi |
||
| 702 | * @param string $foreignClass Class to join |
||
| 703 | */ |
||
| 704 | protected function joinHasOneRelation($localClass, $localField, $foreignClass) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Join the given has_many relation to this query. |
||
| 745 | * |
||
| 746 | * Doesn't work with polymorphic relationships |
||
| 747 | * |
||
| 748 | * @param string $localClass Name of class that has the has_many to the joined class |
||
| 749 | * @param string $localField Name of the has_many relationship to join |
||
| 750 | * @param string $foreignClass Class to join |
||
| 751 | */ |
||
| 752 | protected function joinHasManyRelation($localClass, $localField, $foreignClass) { |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Join table via many_many relationship |
||
| 794 | * |
||
| 795 | * @param string $parentClass |
||
| 796 | * @param string $componentClass |
||
| 797 | * @param string $parentField |
||
| 798 | * @param string $componentField |
||
| 799 | * @param string $relationTable Name of relation table |
||
| 800 | */ |
||
| 801 | protected function joinManyManyRelationship($parentClass, $componentClass, $parentField, $componentField, $relationTable) { |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Removes the result of query from this query. |
||
| 830 | * |
||
| 831 | * @param DataQuery $subtractQuery |
||
| 832 | * @param string $field |
||
| 833 | */ |
||
| 834 | public function subtract(DataQuery $subtractQuery, $field='ID') { |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Select the given fields from the given table. |
||
| 848 | * |
||
| 849 | * @param String $table Unquoted table name (will be escaped automatically) |
||
| 850 | * @param Array $fields Database column names (will be escaped automatically) |
||
| 851 | */ |
||
| 852 | public function selectFromTable($table, $fields) { |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Query the given field column from the database and return as an array. |
||
| 864 | * |
||
| 865 | * @param string $field See {@link expressionForField()}. |
||
| 866 | * @return array List of column values for the specified column |
||
| 867 | */ |
||
| 868 | public function column($field = 'ID') { |
||
| 878 | |||
| 879 | /** |
||
| 880 | * @param String $field Select statement identifier, either the unquoted column name, |
||
| 881 | * the full composite SQL statement, or the alias set through {@link SQLSelect->selectField()}. |
||
| 882 | * @return String The expression used to query this field via this DataQuery |
||
| 883 | */ |
||
| 884 | protected function expressionForField($field) { |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Select the given field expressions. |
||
| 902 | * |
||
| 903 | * @param $fieldExpression String The field to select (escaped SQL statement) |
||
| 904 | * @param $alias String The alias of that field (escaped SQL statement) |
||
| 905 | */ |
||
| 906 | public function selectField($fieldExpression, $alias = null) { |
||
| 909 | |||
| 910 | //// QUERY PARAMS |
||
| 911 | |||
| 912 | /** |
||
| 913 | * An arbitrary store of query parameters that can be used by decorators. |
||
| 914 | * @todo This will probably be made obsolete if we have subclasses of DataList and/or DataQuery. |
||
| 915 | */ |
||
| 916 | private $queryParams; |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query. |
||
| 920 | * It's expected that the $key will be namespaced, e.g, 'Versioned.stage' instead of just 'stage'. |
||
| 921 | */ |
||
| 922 | public function setQueryParam($key, $value) { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query. |
||
| 928 | */ |
||
| 929 | public function getQueryParam($key) { |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Returns all query parameters |
||
| 936 | * @return array query parameters array |
||
| 937 | */ |
||
| 938 | public function getQueryParams() { |
||
| 941 | } |
||
| 942 | |||
| 999 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.