DataQuery.php 24.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
<?php

/**
 * An object representing a query of data from the DataObject's supporting database.
 * Acts as a wrapper over {@link SQLQuery} and performs all of the query generation.
 * Used extensively by {@link DataList}.
 *
 * Unlike DataList, modifiers on DataQuery modify the object rather than returning a clone.
 * DataList is immutable, DataQuery is mutable.
 *
 * @subpackage model
 * @package framework
 */
class DataQuery {
	
	/**
	 * @var string
	 */
	protected $dataClass;
	
	/**
	 * @var SQLQuery
	 */
	protected $query;
	
	/**
	 * @var array
	 */
	protected $collidingFields = array();

	private $queriedColumns = null;
	
	/**
	 * @var Boolean
	 */
	private $queryFinalised = false;
	
	// TODO: replace subclass_access with this
	protected $querySubclasses = true;
	// TODO: replace restrictclasses with this
	protected $filterByClassName = true;
	
	/**
	 * Create a new DataQuery.
	 *
	 * @param String The name of the DataObject class that you wish to query
	 */
	public function __construct($dataClass) {
		$this->dataClass = $dataClass;
		$this->initialiseQuery();
	}
	
	/**
	 * Clone this object
	 */
	public function __clone() {
		$this->query = clone $this->query;
	}
	
	/**
	 * Return the {@link DataObject} class that is being queried.
	 */
	public function dataClass() {
		return $this->dataClass;
	}

	/**
	 * Return the {@link SQLQuery} object that represents the current query; note that it will
	 * be a clone of the object.
	 */
	public function query() {
		return $this->getFinalisedQuery();
	}
	
	
	/**
	 * Remove a filter from the query
	 */
	public function removeFilterOn($fieldExpression) {
		$matched = false;

		$where = $this->query->getWhere();
		foreach($where as $i => $clause) {
			if(strpos($clause, $fieldExpression) !== false) {
				unset($where[$i]);
				$matched = true;
			}
		}

		// set the entire where clause back, but clear the original one first
		if($matched) {
			$this->query->setWhere($where);
		} else {
			throw new InvalidArgumentException("Couldn't find $fieldExpression in the query filter.");
		}

		return $this;
	}
	
	/**
	 * Set up the simplest initial query
	 */
	public function initialiseQuery() {
		// Get the tables to join to.
		// Don't get any subclass tables - let lazy loading do that.
		$tableClasses = ClassInfo::ancestry($this->dataClass, true);
		
		// Error checking
		if(!$tableClasses) {
			if(!SS_ClassLoader::instance()->hasManifest()) {
				user_error("DataObjects have been requested before the manifest is loaded. Please ensure you are not"
					. " querying the database in _config.php.", E_USER_ERROR);
			} else {
				user_error("DataList::create Can't find data classes (classes linked to tables) for"
					. " $this->dataClass. Please ensure you run dev/build after creating a new DataObject.",
					E_USER_ERROR);
			}
		}

		$baseClass = array_shift($tableClasses);

		// Build our intial query
		$this->query = new SQLQuery(array());
		$this->query->setDistinct(true);
		
		if($sort = singleton($this->dataClass)->stat('default_sort')) {
			$this->sort($sort);
		}

		$this->query->setFrom("\"$baseClass\"");

		$obj = Injector::inst()->get($baseClass);
		$obj->extend('augmentDataQueryCreation', $this->query, $this);
	}

	public function setQueriedColumns($queriedColumns) {
		$this->queriedColumns = $queriedColumns;
	}

	/**
	 * Ensure that the query is ready to execute.
	 *
	 * @return SQLQuery
	 */
	public function getFinalisedQuery($queriedColumns = null) {
		if(!$queriedColumns) $queriedColumns = $this->queriedColumns;
		if($queriedColumns) {
			$queriedColumns = array_merge($queriedColumns, array('Created', 'LastEdited', 'ClassName'));
		}

		$query = clone $this->query;
		$ancestorTables = ClassInfo::ancestry($this->dataClass, true);

		// Generate the list of tables to iterate over and the list of columns required
		// by any existing where clauses. This second step is skipped if we're fetching
		// the whole dataobject as any required columns will get selected regardless.
		if($queriedColumns) {
			// Specifying certain columns allows joining of child tables
			$tableClasses = ClassInfo::dataClassesFor($this->dataClass);

			foreach ($query->getWhere() as $where) {
				// Check for just the column, in the form '"Column" = ?' and the form '"Table"."Column"' = ?
				if (preg_match('/^"([^"]+)"/', $where, $matches) ||
					preg_match('/^"([^"]+)"\."[^"]+"/', $where, $matches)) {
					if (!in_array($matches[1], $queriedColumns)) $queriedColumns[] = $matches[1];
				}
			}
		} else {
			$tableClasses = $ancestorTables;
		}

		$tableNames = array_keys($tableClasses);
		$baseClass = $tableNames[0];

		// Iterate over the tables and check what we need to select from them. If any selects are made (or the table is
		// required for a select)
		foreach($tableClasses as $tableClass) {

			// Determine explicit columns to select
			$selectColumns = null;
			if ($queriedColumns) {
				// Restrict queried columns to that on the selected table
				$tableFields = DataObject::database_fields($tableClass);
				$selectColumns = array_intersect($queriedColumns, array_keys($tableFields));
			}
			
			// If this is a subclass without any explicitly requested columns, omit this from the query
			if(!in_array($tableClass, $ancestorTables) && empty($selectColumns)) continue;
			
			// Select necessary columns (unless an explicitly empty array)
			if($selectColumns !== array()) {
				$this->selectColumnsFromTable($query, $tableClass, $selectColumns);
			}

			// Join if not the base table
			if($tableClass !== $baseClass) {
				$query->addLeftJoin($tableClass, "\"$tableClass\".\"ID\" = \"$baseClass\".\"ID\"", $tableClass, 10);
			}
		}
		
		// Resolve colliding fields
		if($this->collidingFields) {
			foreach($this->collidingFields as $k => $collisions) {
				$caseClauses = array();
				foreach($collisions as $collision) {
					if(preg_match('/^"([^"]+)"/', $collision, $matches)) {
						$collisionBase = $matches[1];
						$collisionClasses = ClassInfo::subclassesFor($collisionBase);
						$collisionClasses = array_map(array(DB::getConn(), 'prepStringForDB'), $collisionClasses);
						$caseClauses[] = "WHEN \"$baseClass\".\"ClassName\" IN ("
							. implode(", ", $collisionClasses) . ") THEN $collision";
					} else {
						user_error("Bad collision item '$collision'", E_USER_WARNING);
					}
				}
				$query->selectField("CASE " . implode( " ", $caseClauses) . " ELSE NULL END", $k);
			}
		}


		if($this->filterByClassName) {
			// If querying the base class, don't bother filtering on class name
			if($this->dataClass != $baseClass) {
				// Get the ClassName values to filter to
				$classNames = ClassInfo::subclassesFor($this->dataClass);
				if(!$classNames) user_error("DataList::create() Can't find data sub-classes for '$callerClass'");
				$classNames = array_map(array(DB::getConn(), 'prepStringForDB'), $classNames);
				$query->addWhere("\"$baseClass\".\"ClassName\" IN (" . implode(",", $classNames) . ")");
			}
		}

		$query->selectField("\"$baseClass\".\"ID\"", "ID");
		$query->selectField("CASE WHEN \"$baseClass\".\"ClassName\" IS NOT NULL THEN \"$baseClass\".\"ClassName\""
			. " ELSE ".DB::getConn()->prepStringForDB($baseClass)." END", "RecordClassName");

		// TODO: Versioned, Translatable, SiteTreeSubsites, etc, could probably be better implemented as subclasses
		// of DataQuery

		$obj = Injector::inst()->get($this->dataClass);
		$obj->extend('augmentSQL', $query, $this);

		$this->ensureSelectContainsOrderbyColumns($query);

		return $query;
	}

	/**
	 * Ensure that if a query has an order by clause, those columns are present in the select.
	 * 
	 * @param SQLQuery $query
	 * @return null
	 */
	protected function ensureSelectContainsOrderbyColumns($query, $originalSelect = array()) {
		$tableClasses = ClassInfo::dataClassesFor($this->dataClass);
		$baseClass = array_shift($tableClasses);

		if($orderby = $query->getOrderBy()) {
			$newOrderby = array();
			$i = 0;
			foreach($orderby as $k => $dir) {
				$newOrderby[$k] = $dir;
				
				// don't touch functions in the ORDER BY or public function calls
				// selected as fields
				if(strpos($k, '(') !== false) continue;

				$col = str_replace('"', '', trim($k));
				$parts = explode('.', $col);

				// Pull through SortColumn references from the originalSelect variables
				if(preg_match('/_SortColumn/', $col)) {
					if(isset($originalSelect[$col])) {
						$query->selectField($originalSelect[$col], $col);
					}

					continue;
				}
				
				if(count($parts) == 1) {
					$databaseFields = DataObject::database_fields($baseClass);
	
					// database_fields() doesn't return ID, so we need to 
					// manually add it here
					$databaseFields['ID'] = true;
				
					if(isset($databaseFields[$parts[0]])) {
						$qualCol = "\"$baseClass\".\"{$parts[0]}\"";
					} else {
						$qualCol = "\"$parts[0]\"";
					}
						
					// remove original sort
					unset($newOrderby[$k]);

					// add new columns sort
					$newOrderby[$qualCol] = $dir;
							
					// To-do: Remove this if block once SQLQuery::$select has been refactored to store getSelect()
					// format internally; then this check can be part of selectField()
					$selects = $query->getSelect();
					if(!isset($selects[$col]) && !in_array($qualCol, $selects)) {
						$query->selectField($qualCol);
					}
				} else {
					$qualCol = '"' . implode('"."', $parts) . '"';

					if(!in_array($qualCol, $query->getSelect())) {
						unset($newOrderby[$k]);
						
						$newOrderby["\"_SortColumn$i\""] = $dir;
						$query->selectField($qualCol, "_SortColumn$i");

						$i++;
					}
				}

			}

			$query->setOrderBy($newOrderby);
		}
	}

	/**
	 * Execute the query and return the result as {@link Query} object.
	 */
	public function execute() {
		return $this->getFinalisedQuery()->execute();
	}

	/**
	 * Return this query's SQL
	 */
	public function sql() {
		return $this->getFinalisedQuery()->sql();
	}

	/**
	 * Return the number of records in this query.
	 * Note that this will issue a separate SELECT COUNT() query.
	 */
	public function count() {
		$baseClass = ClassInfo::baseDataClass($this->dataClass);
		return $this->getFinalisedQuery()->count("DISTINCT \"$baseClass\".\"ID\"");
	}

	/**
	 * Return the maximum value of the given field in this DataList
	 * 
	 * @param String $field Unquoted database column name (will be escaped automatically)
	 */
	public function max($field) {
		return $this->aggregate(sprintf('MAX("%s")', Convert::raw2sql($field)));
	}

	/**
	 * Return the minimum value of the given field in this DataList
	 * 
	 * @param String $field Unquoted database column name (will be escaped automatically)
	 */
	public function min($field) {
		return $this->aggregate(sprintf('MIN("%s")', Convert::raw2sql($field)));
	}
	
	/**
	 * Return the average value of the given field in this DataList
	 * 
	 * @param String $field Unquoted database column name (will be escaped automatically)
	 */
	public function avg($field) {
		return $this->aggregate(sprintf('AVG("%s")', Convert::raw2sql($field)));
	}

	/**
	 * Return the sum of the values of the given field in this DataList
	 * 
	 * @param String $field Unquoted database column name (will be escaped automatically)
	 */
	public function sum($field) {
		return $this->aggregate(sprintf('SUM("%s")', Convert::raw2sql($field)));
	}
	
	/**
	 * Runs a raw aggregate expression.  Please handle escaping yourself
	 */
	public function aggregate($expression) {
		return $this->getFinalisedQuery()->aggregate($expression)->execute()->value();
	}

	/**
	 * Return the first row that would be returned by this full DataQuery
	 * Note that this will issue a separate SELECT ... LIMIT 1 query.
	 */
	public function firstRow() {
		return $this->getFinalisedQuery()->firstRow();
	}

	/**
	 * Return the last row that would be returned by this full DataQuery
	 * Note that this will issue a separate SELECT ... LIMIT query.
	 */
	public function lastRow() {
		return $this->getFinalisedQuery()->lastRow();
	}

	/**
	 * Update the SELECT clause of the query with the columns from the given table
	 */
	protected function selectColumnsFromTable(SQLQuery &$query, $tableClass, $columns = null) {
		// Add SQL for multi-value fields
		$databaseFields = DataObject::database_fields($tableClass);
		$compositeFields = DataObject::composite_fields($tableClass, false);
		if($databaseFields) foreach($databaseFields as $k => $v) {
			if((is_null($columns) || in_array($k, $columns)) && !isset($compositeFields[$k])) {
				// Update $collidingFields if necessary
				if($expressionForField = $query->expressionForField($k)) {
					if(!isset($this->collidingFields[$k])) $this->collidingFields[$k] = array($expressionForField);
					$this->collidingFields[$k][] = "\"$tableClass\".\"$k\"";
				
				} else {
					$query->selectField("\"$tableClass\".\"$k\"", $k);
				}
			}
		}
		if($compositeFields) foreach($compositeFields as $k => $v) {
			if((is_null($columns) || in_array($k, $columns)) && $v) {
				$dbO = Object::create_from_string($v, $k);
				$dbO->addToQuery($query);
			}
		}
	}
	
	/**
	 * Append a GROUP BY clause to this query.
	 * 
	 * @param String $groupby Escaped SQL statement
	 */
	public function groupby($groupby) {
		$this->query->addGroupBy($groupby);
		return $this;
	}
	
	/**
	 * Append a HAVING clause to this query.
	 * 
	 * @param String $having Escaped SQL statement
	 */
	public function having($having) {
		$this->query->addHaving($having);
		return $this;
	}

	/**
	 * Create a disjunctive subgroup.
	 *
	 * That is a subgroup joined by OR
	 *
	 * @return DataQuery_SubGroup
	 */
	public function disjunctiveGroup() {
		return new DataQuery_SubGroup($this, 'OR');
	}

	/**
	 * Create a conjunctive subgroup
	 *
	 * That is a subgroup joined by AND
	 *
	 * @return DataQuery_SubGroup
	 */
	public function conjunctiveGroup() {
		return new DataQuery_SubGroup($this, 'AND');
	}

	/**
	 * Append a WHERE clause to this query.
	 * There are two different ways of doing this:
	 *
	 * <code>
	 *  // the entire predicate as a single string
	 *  $query->where("\"Column\" = 'Value'");
	 *
	 *  // multiple predicates as an array
	 *  $query->where(array("\"Column\" = 'Value'", "\"Column\" != 'Value'"));
	 * </code>
	 *
	 * @param string|array $where Predicate(s) to set, as escaped SQL statements.
	 */
	public function where($filter) {
		if($filter) {
			$this->query->addWhere($filter);
		}
		return $this;
	}

	/**
	 * Append a WHERE with OR.
	 * 
	 * @example $dataQuery->whereAny(array("\"Monkey\" = 'Chimp'", "\"Color\" = 'Brown'"));
	 * @see where()
	 *
	 * @param array $filter Escaped SQL statement.
	 * @return DataQuery
	 */
	public function whereAny($filter) {
		if($filter) {
			$this->query->addWhereAny($filter);
		}
		return $this;
	}
	
	/**
	 * Set the ORDER BY clause of this query
	 *
	 * @see SQLQuery::orderby()
	 *
	 * @param String $sort Column to sort on (escaped SQL statement)
	 * @param String $direction Direction ("ASC" or "DESC", escaped SQL statement)
	 * @param Boolean $clear Clear existing values
	 * @return DataQuery
	 */
	public function sort($sort = null, $direction = null, $clear = true) {
		if($clear) {
			$this->query->setOrderBy($sort, $direction);
		} else {
			$this->query->addOrderBy($sort, $direction);
		}
			
		return $this;
	}
	
	/**
	 * Reverse order by clause
	 *
	 * @return DataQuery
	 */
	public function reverseSort() {
		$this->query->reverseOrderBy();
		return $this;
	}
	
	/**
	 * Set the limit of this query.
	 * 
	 * @param int $limit
	 * @param int $offset
	 */
	public function limit($limit, $offset = 0) {
		$this->query->setLimit($limit, $offset);
		return $this;
	}

	/**
	 * Set whether this query should be distinct or not.
	 *
	 * @param bool $value
	 * @return DataQuery
	 */
	public function distinct($value) {
		$this->query->setDistinct($value);
		return $this;
	}

	/**
	 * Add an INNER JOIN clause to this query.
	 * 
	 * @param String $table The unquoted table name to join to.
	 * @param String $onClause The filter for the join (escaped SQL statement)
	 * @param String $alias An optional alias name (unquoted)
	 */
	public function innerJoin($table, $onClause, $alias = null) {
		if($table) {
			$this->query->addInnerJoin($table, $onClause, $alias);
		}
		return $this;
	}

	/**
	 * Add a LEFT JOIN clause to this query.
	 * 
	 * @param String $table The unquoted table to join to.
	 * @param String $onClause The filter for the join (escaped SQL statement).
	 * @param String $alias An optional alias name (unquoted)
	 */
	public function leftJoin($table, $onClause, $alias = null) {
		if($table) {
			$this->query->addLeftJoin($table, $onClause, $alias);
		}
		return $this;
	}

	/**
	 * Traverse the relationship fields, and add the table
	 * mappings to the query object state. This has to be called
	 * in any overloaded {@link SearchFilter->apply()} methods manually.
	 * 
	 * @param String|array $relation The array/dot-syntax relation to follow
	 * @return The model class of the related item
	 */
	public function applyRelation($relation) {
		// NO-OP
		if(!$relation) return $this->dataClass;
		
		if(is_string($relation)) $relation = explode(".", $relation);

		$modelClass = $this->dataClass;
		
		foreach($relation as $rel) {
			$model = singleton($modelClass);
			if ($component = $model->has_one($rel)) {
				if(!$this->query->isJoinedTo($component)) {
					$foreignKey = $rel;
					$realModelClass = ClassInfo::table_for_object_field($modelClass, "{$foreignKey}ID");
					$this->query->addLeftJoin($component,
						"\"$component\".\"ID\" = \"{$realModelClass}\".\"{$foreignKey}ID\"");
				
					/**
					 * add join clause to the component's ancestry classes so that the search filter could search on
					 * its ancestor fields.
					 */
					$ancestry = ClassInfo::ancestry($component, true);
					if(!empty($ancestry)){
						$ancestry = array_reverse($ancestry);
						foreach($ancestry as $ancestor){
							if($ancestor != $component){
								$this->query->addInnerJoin($ancestor, "\"$component\".\"ID\" = \"$ancestor\".\"ID\"");
							}
						}
					}
				}
				$modelClass = $component;

			} elseif ($component = $model->has_many($rel)) {
				if(!$this->query->isJoinedTo($component)) {
					$ancestry = $model->getClassAncestry();
					$foreignKey = $model->getRemoteJoinField($rel);
					$this->query->addLeftJoin($component,
						"\"$component\".\"{$foreignKey}\" = \"{$ancestry[0]}\".\"ID\"");
					/**
					 * add join clause to the component's ancestry classes so that the search filter could search on
					 * its ancestor fields.
					 */
					$ancestry = ClassInfo::ancestry($component, true);
					if(!empty($ancestry)){
						$ancestry = array_reverse($ancestry);
						foreach($ancestry as $ancestor){
							if($ancestor != $component){
								$this->query->addInnerJoin($ancestor, "\"$component\".\"ID\" = \"$ancestor\".\"ID\"");
							}
						}
					}
				}
				$modelClass = $component;

			} elseif ($component = $model->many_many($rel)) {
				list($parentClass, $componentClass, $parentField, $componentField, $relationTable) = $component;
				$parentBaseClass = ClassInfo::baseDataClass($parentClass);
				$componentBaseClass = ClassInfo::baseDataClass($componentClass);
				$this->query->addInnerJoin($relationTable,
					"\"$relationTable\".\"$parentField\" = \"$parentBaseClass\".\"ID\"");
				$this->query->addLeftJoin($componentBaseClass,
					"\"$relationTable\".\"$componentField\" = \"$componentBaseClass\".\"ID\"");
				if(ClassInfo::hasTable($componentClass)) {
					$this->query->addLeftJoin($componentClass,
						"\"$relationTable\".\"$componentField\" = \"$componentClass\".\"ID\"");
				}
				$modelClass = $componentClass;

			}
		}
		
		return $modelClass;
	}
	
	/**
	 * Removes the result of query from this query.
	 * 
	 * @param DataQuery $subtractQuery
	 * @param string $field 
	 */
	public function subtract(DataQuery $subtractQuery, $field='ID') {
		$fieldExpression = $subtractQuery->expressionForField($field);
		$subSelect = $subtractQuery->getFinalisedQuery();
		$subSelect->setSelect(array());
		$subSelect->selectField($fieldExpression, $field);
		$subSelect->setOrderBy(null);
		$this->where($this->expressionForField($field).' NOT IN ('.$subSelect->sql().')');

		return $this;
	}

	/**
	 * Select the given fields from the given table.
	 * 
	 * @param String $table Unquoted table name (will be escaped automatically)
	 * @param Array $fields Database column names (will be escaped automatically)
	 */
	public function selectFromTable($table, $fields) {
		$table = Convert::raw2sql($table);
		$fieldExpressions = array_map(create_function('$item', 
			"return '\"$table\".\"' . Convert::raw2sql(\$item) . '\"';"), $fields);
		
		$this->query->setSelect($fieldExpressions);

		return $this;
	}

	/**
	 * Query the given field column from the database and return as an array.
	 * 
	 * @param string $field See {@link expressionForField()}.
	 * @return array List of column values for the specified column
	 */
	public function column($field = 'ID') {
		$fieldExpression = $this->expressionForField($field);
		$query = $this->getFinalisedQuery(array($field));
		$originalSelect = $query->getSelect();
		$query->setSelect(array());
		$query->selectField($fieldExpression, $field);
		$this->ensureSelectContainsOrderbyColumns($query, $originalSelect);

		return $query->execute()->column($field);
	}
	
	/**
	 * @param  String $field Select statement identifier, either the unquoted column name,
	 * the full composite SQL statement, or the alias set through {@link SQLQuery->selectField()}.
	 * @return String The expression used to query this field via this DataQuery
	 */
	protected function expressionForField($field) {
		
		// Prepare query object for selecting this field
		$query = $this->getFinalisedQuery(array($field));
		
		// Allow query to define the expression for this field
		$expression = $query->expressionForField($field);
		if(!empty($expression)) return $expression;
		
		// Special case for ID, if not provided
		if($field === 'ID') {
			$baseClass = ClassInfo::baseDataClass($this->dataClass);
			return "\"$baseClass\".\"ID\"";	
		}
	}

	/**
	 * Select the given field expressions.
	 * 
	 * @param $fieldExpression String The field to select (escaped SQL statement)
	 * @param $alias String The alias of that field (escaped SQL statement)
	 */
	protected function selectField($fieldExpression, $alias = null) {
		$this->query->selectField($fieldExpression, $alias);
	}

	//// QUERY PARAMS

	/**
	 * An arbitrary store of query parameters that can be used by decorators.
	 * @todo This will probably be made obsolete if we have subclasses of DataList and/or DataQuery.
	 */
	private $queryParams;
	
	/**
	 * Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query.
	 * It's expected that the $key will be namespaced, e.g, 'Versioned.stage' instead of just 'stage'.
	 */
	public function setQueryParam($key, $value) {
		$this->queryParams[$key] = $value;
	}
	
	/**
	 * Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query.
	 */
	public function getQueryParam($key) {
		if(isset($this->queryParams[$key])) return $this->queryParams[$key];
		else return null;
	}

	/**
	 * Returns all query parameters
	 * @return array query parameters array
	 */
	public function getQueryParams() {
		return $this->queryParams;
	}
}

/**
 * Represents a subgroup inside a WHERE clause in a {@link DataQuery}
 *
 * Stores the clauses for the subgroup inside a specific {@link SQLQuery} 
 * object.
 *
 * All non-where methods call their DataQuery versions, which uses the base
 * query object.
 *
 * @package framework
 */
class DataQuery_SubGroup extends DataQuery {

	protected $whereQuery;

	public function __construct(DataQuery $base, $connective) {
		$this->dataClass = $base->dataClass;
		$this->query = $base->query;
		$this->whereQuery = new SQLQuery;
		$this->whereQuery->setConnective($connective);

		$base->where($this);
	}

	/**
	 * Set the WHERE clause of this query.
	 * There are two different ways of doing this:
	 *
	 * <code>
	 *  // the entire predicate as a single string
	 *  $query->where("\"Column\" = 'Value'");
	 *
	 *  // multiple predicates as an array
	 *  $query->where(array("\"Column\" = 'Value'", "\"Column\" != 'Value'"));
	 * </code>
	 *
	 * @param string|array $where Predicate(s) to set, as escaped SQL statements.
	 */
	public function where($filter) {
		if($filter) {
			$this->whereQuery->addWhere($filter);
		}

		return $this;
	}

	/**
	 * Set a WHERE with OR.
	 * 
	 * @example $dataQuery->whereAny(array("\"Monkey\" = 'Chimp'", "\"Color\" = 'Brown'"));
	 * @see where()
	 *
	 * @param array $filter Escaped SQL statement.
	 * @return DataQuery
	 */
	public function whereAny($filter) {
		if($filter) {
			$this->whereQuery->addWhereAny($filter);
		}

		return $this;
	}

	public function __toString() {
		if(!$this->whereQuery->getWhere()) {
			// We always need to have something so we don't end up with something like '... AND () AND ...'
			return '1=1';
		}

		$sql = DB::getConn()->sqlWhereToString(
			$this->whereQuery->getWhere(), 
			$this->whereQuery->getConnective()
		);
		
		$sql = preg_replace('[^\s*WHERE\s*]', '', $sql);

		return $sql;
	}
}