query.php 19.3 KB
Newer Older
1 2 3 4 5 6
<?php namespace Laravel\Database;

use Closure;
use Laravel\Database;
use Laravel\Paginator;
use Laravel\Database\Query\Grammars\SQLServer;
7 8 9 10

class Query {

	/**
Taylor Otwell committed
11
	 * The database connection.
12
	 *
13
	 * @var Connection
14
	 */
Taylor Otwell committed
15
	public $connection;
16 17

	/**
18
	 * The query grammar instance.
19
	 *
20
	 * @var Query\Grammars\Grammar
21
	 */
22
	public $grammar;
23 24

	/**
25 26
	 * The SELECT clause.
	 *
Taylor Otwell committed
27
	 * @var array
28
	 */
Taylor Otwell committed
29
	public $selects;
30 31

	/**
32
	 * The aggregating column and function.
33
	 *
Taylor Otwell committed
34
	 * @var array
35
	 */
Taylor Otwell committed
36
	public $aggregate;
37 38

	/**
Taylor Otwell committed
39
	 * Indicates if the query should return distinct results.
40
	 *
Taylor Otwell committed
41
	 * @var bool
42
	 */
Taylor Otwell committed
43
	public $distinct = false;
44 45 46 47 48 49

	/**
	 * The table name.
	 *
	 * @var string
	 */
Taylor Otwell committed
50
	public $from;
51 52

	/**
Taylor Otwell committed
53
	 * The table joins.
54
	 *
Taylor Otwell committed
55
	 * @var array
56
	 */
Taylor Otwell committed
57
	public $joins;
58 59

	/**
Taylor Otwell committed
60
	 * The WHERE clauses.
61
	 *
Taylor Otwell committed
62
	 * @var array
63
	 */
Taylor Otwell committed
64
	public $wheres;
65 66

	/**
67 68 69 70 71 72 73
	 * The GROUP BY clauses.
	 *
	 * @var array
	 */
	public $groupings;

	/**
Taylor Otwell committed
74
	 * The ORDER BY clauses.
75 76 77
	 *
	 * @var array
	 */
Taylor Otwell committed
78
	public $orderings;
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

	/**
	 * The LIMIT value.
	 *
	 * @var int
	 */
	public $limit;

	/**
	 * The OFFSET value.
	 *
	 * @var int
	 */
	public $offset;

	/**
	 * The query value bindings.
	 *
	 * @var array
	 */
	public $bindings = array();

	/**
	 * Create a new query instance.
	 *
104 105 106
	 * @param  Connection  $connection
	 * @param  Grammar     $grammar
	 * @param  string      $table
107 108
	 * @return void
	 */
109
	public function __construct(Connection $connection, Query\Grammars\Grammar $grammar, $table)
110
	{
Taylor Otwell committed
111
		$this->from = $table;
112
		$this->grammar = $grammar;
113
		$this->connection = $connection;
114 115 116 117 118 119 120 121 122 123 124 125 126 127
	}

	/**
	 * Force the query to return distinct results.
	 *
	 * @return Query
	 */
	public function distinct()
	{
		$this->distinct = true;
		return $this;
	}

	/**
Taylor Otwell committed
128 129
	 * Add an array of columns to the SELECT clause.
	 *
130
	 * @param  array  $columns
131 132
	 * @return Query
	 */
133
	public function select($columns = array('*'))
134
	{
Taylor Otwell committed
135
		$this->selects = (array) $columns;
136 137 138 139
		return $this;
	}

	/**
Taylor Otwell committed
140
	 * Add a join clause to the query.
141
	 *
142 143 144 145 146 147 148
	 * @param  string  $table
	 * @param  string  $column1
	 * @param  string  $operator
	 * @param  string  $column2
	 * @param  string  $type
	 * @return Query
	 */
149
	public function join($table, $column1, $operator = null, $column2 = null, $type = 'INNER')
150
	{
151 152
		// If the "column" is really an instance of a Closure, the developer is
		// trying to create a join with a complex "ON" clause. So, we will add
Taylor Otwell committed
153
		// the join, and then call the Closure with the join/
154 155 156 157 158 159
		if ($column1 instanceof Closure)
		{
			$this->joins[] = new Query\Join($type, $table);

			call_user_func($column1, end($this->joins));
		}
160

161 162 163 164 165 166 167 168 169 170 171
		// If the column is just a string, we can assume that the join just
		// has a simple on clause, and we'll create the join instance and
		// add the clause automatically for the develoepr.
		else
		{
			$join = new Query\Join($type, $table);

			$join->on($column1, $operator, $column2);

			$this->joins[] = $join;
		}
Taylor Otwell committed
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		return $this;
	}

	/**
	 * Add a left join to the query.
	 *
	 * @param  string  $table
	 * @param  string  $column1
	 * @param  string  $operator
	 * @param  string  $column2
	 * @return Query
	 */
	public function left_join($table, $column1, $operator, $column2)
	{
		return $this->join($table, $column1, $operator, $column2, 'LEFT');
	}

	/**
191
	 * Reset the where clause to its initial state.
Taylor Otwell committed
192 193 194 195 196
	 *
	 * @return void
	 */
	public function reset_where()
	{
197
		list($this->wheres, $this->bindings) = array(array(), array());
Taylor Otwell committed
198 199 200
	}

	/**
201 202 203 204 205 206 207 208 209
	 * Add a raw where condition to the query.
	 *
	 * @param  string  $where
	 * @param  array   $bindings
	 * @param  string  $connector
	 * @return Query
	 */
	public function raw_where($where, $bindings = array(), $connector = 'AND')
	{
210
		$this->wheres[] = array('type' => 'where_raw', 'connector' => $connector, 'sql' => $where);
Taylor Otwell committed
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
		$this->bindings = array_merge($this->bindings, $bindings);

		return $this;
	}

	/**
	 * Add a raw or where condition to the query.
	 *
	 * @param  string  $where
	 * @param  array   $bindings
	 * @return Query
	 */
	public function raw_or_where($where, $bindings = array())
	{
		return $this->raw_where($where, $bindings, 'OR');
	}

	/**
	 * Add a where condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $operator
	 * @param  mixed   $value
	 * @param  string  $connector
	 * @return Query
	 */
238
	public function where($column, $operator = null, $value = null, $connector = 'AND')
239
	{
240 241 242 243 244 245 246 247
		// If a CLosure is passed into the method, it means a nested where
		// clause is being initiated, so we will take a different course
		// of action than when the statement is just a simple where.
		if ($column instanceof Closure)
		{
			return $this->where_nested($column, $connector);
		}

248 249 250
		$type = 'where';

		$this->wheres[] = compact('type', 'column', 'operator', 'value', 'connector');
Taylor Otwell committed
251

252 253 254 255 256 257 258 259 260 261 262 263 264
		$this->bindings[] = $value;

		return $this;
	}

	/**
	 * Add an or where condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $operator
	 * @param  mixed   $value
	 * @return Query
	 */
265
	public function or_where($column, $operator = null, $value = null)
266 267 268 269 270
	{
		return $this->where($column, $operator, $value, 'OR');
	}

	/**
271
	 * Add an or where condition for the primary key to the query.
Taylor Otwell committed
272
	 *
273 274 275 276 277 278 279 280 281
	 * @param  mixed  $value
	 * @return Query
	 */
	public function or_where_id($value)
	{
		return $this->or_where('id', '=', $value);		
	}

	/**
282 283 284 285 286
	 * Add a where in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @param  string  $connector
Taylor Otwell committed
287
	 * @param  bool    $not
288 289
	 * @return Query
	 */
Taylor Otwell committed
290
	public function where_in($column, $values, $connector = 'AND', $not = false)
291
	{
292 293 294
		$type = ($not) ? 'where_not_in' : 'where_in';

		$this->wheres[] = compact('type', 'column', 'values', 'connector');
Taylor Otwell committed
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
		$this->bindings = array_merge($this->bindings, $values);

		return $this;
	}

	/**
	 * Add an or where in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @return Query
	 */
	public function or_where_in($column, $values)
	{
		return $this->where_in($column, $values, 'OR');
	}

	/**
	 * Add a where not in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @param  string  $connector
	 * @return Query
	 */
	public function where_not_in($column, $values, $connector = 'AND')
	{
Taylor Otwell committed
323
		return $this->where_in($column, $values, $connector, true);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	}

	/**
	 * Add an or where not in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @return Query
	 */
	public function or_where_not_in($column, $values)
	{
		return $this->where_not_in($column, $values, 'OR');
	}

	/**
	 * Add a where null condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $connector
Taylor Otwell committed
343
	 * @param  bool    $not
344 345
	 * @return Query
	 */
Taylor Otwell committed
346
	public function where_null($column, $connector = 'AND', $not = false)
347
	{
348 349 350
		$type = ($not) ? 'where_not_null' : 'where_null';

		$this->wheres[] = compact('type', 'column', 'connector');
Taylor Otwell committed
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		return $this;
	}

	/**
	 * Add an or where null condition to the query.
	 *
	 * @param  string  $column
	 * @return Query
	 */
	public function or_where_null($column)
	{
		return $this->where_null($column, 'OR');
	}

	/**
	 * Add a where not null condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $connector
	 * @return Query
	 */
	public function where_not_null($column, $connector = 'AND')
	{
Taylor Otwell committed
375
		return $this->where_null($column, $connector, true);
376 377 378 379 380 381 382 383 384 385 386 387 388 389
	}

	/**
	 * Add an or where not null condition to the query.
	 *
	 * @param  string  $column
	 * @return Query
	 */
	public function or_where_not_null($column)
	{
		return $this->where_not_null($column, 'OR');
	}

	/**
390
	 * Add a nested where condition to the query.
Taylor Otwell committed
391
	 *
392 393 394 395 396 397 398 399 400 401
	 * @param  Closure  $callback
	 * @param  string   $connector
	 * @return Query
	 */
	protected function where_nested($callback, $connector)
	{
		$type = 'where_nested';

		// To handle a nested where statement, we will actually instantiate a
		// new Query instance and run the callback over that instance, which
Taylor Otwell committed
402
		// will allow the developer to have a fresh query.
403 404 405 406
		$query = new Query($this->connection, $this->grammar, $this->from);

		call_user_func($callback, $query);

407 408 409
		// Once the callback has been run on the query, we will store the
		// nested query instance on the where clause array so that it's
		// passed to the query's query grammar instance.
410 411 412 413 414 415 416 417 418
		$this->wheres[] = compact('type', 'query', 'connector');

		$this->bindings = array_merge($this->bindings, $query->bindings);

		return $this;
	}

	/**
	 * Add dynamic where conditions to the query.
Taylor Otwell committed
419
	 *
Taylor Otwell committed
420 421 422 423 424 425 426 427
	 * @param  string  $method
	 * @param  array   $parameters
	 * @return Query
	 */
	private function dynamic_where($method, $parameters)
	{
		$finder = substr($method, 6);

428 429 430
		$flags = PREG_SPLIT_DELIM_CAPTURE;

		$segments = preg_split('/(_and_|_or_)/i', $finder, -1, $flags);
Taylor Otwell committed
431

432 433 434
		// The connector variable will determine which connector will be
		// used for the condition. We'll change it as we come across new
		// connectors in the dynamic method string.
Taylor Otwell committed
435
		//
436 437
		// The index variable helps us get the correct parameter value
		// for the where condition. We increment it each time we add
Taylor Otwell committed
438
		// a condition to the query's where.
Taylor Otwell committed
439 440 441 442 443 444
		$connector = 'AND';

		$index = 0;

		foreach ($segments as $segment)
		{
445 446 447 448 449 450 451
			// If the segment is not a boolean connector, we can assume it
			// it is a column name, and we'll add it to the query as a new
			// where clause.
			//
			// Otherwise, we'll store the connector so that we know how to
			// connection the next where clause we find to the query, as
			// all connectors should precede a new where clause.
Taylor Otwell committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
			if ($segment != '_and_' and $segment != '_or_')
			{
				$this->where($segment, '=', $parameters[$index], $connector);

				$index++;
			}
			else
			{
				$connector = trim(strtoupper($segment), '_');
			}
		}

		return $this;
	}

	/**
468 469 470 471 472 473 474 475 476 477 478 479
	 * Add a grouping to the query.
	 *
	 * @param  string  $column
	 * @return Query
	 */
	public function group_by($column)
	{
		$this->groupings[] = $column;
		return $this;
	}

	/**
480 481 482 483 484 485
	 * Add an ordering to the query.
	 *
	 * @param  string  $column
	 * @param  string  $direction
	 * @return Query
	 */
486
	public function order_by($column, $direction = 'asc')
487
	{
Taylor Otwell committed
488
		$this->orderings[] = compact('column', 'direction');
489 490 491 492 493 494 495 496 497 498 499
		return $this;
	}

	/**
	 * Set the query offset.
	 *
	 * @param  int  $value
	 * @return Query
	 */
	public function skip($value)
	{
Taylor Otwell committed
500
		$this->offset = $value;
501 502 503 504 505 506 507 508 509 510 511
		return $this;
	}

	/**
	 * Set the query limit.
	 *
	 * @param  int  $value
	 * @return Query
	 */
	public function take($value)
	{
Taylor Otwell committed
512
		$this->limit = $value;
513 514 515 516
		return $this;
	}

	/**
517
	 * Set the query limit and offset for a given page.
518 519 520 521 522
	 *
	 * @param  int    $page
	 * @param  int    $per_page
	 * @return Query
	 */
Taylor Otwell committed
523
	public function for_page($page, $per_page)
524
	{
Taylor Otwell committed
525
		return $this->skip(($page - 1) * $per_page)->take($per_page);
526 527 528
	}

	/**
Taylor Otwell committed
529
	 * Find a record by the primary key.
530
	 *
Taylor Otwell committed
531
	 * @param  int     $id
532
	 * @param  array   $columns
533 534
	 * @return object
	 */
Taylor Otwell committed
535
	public function find($id, $columns = array('*'))
536
	{
Taylor Otwell committed
537
		return $this->where('id', '=', $id)->first($columns);
538 539 540
	}

	/**
541
	 * Execute the query as a SELECT statement and return a single column.
542 543 544 545
	 *
	 * @param  string  $column
	 * @return mixed
	 */
546
	public function only($column)
547
	{
548
		$sql = $this->grammar->select($this->select(array($column)));
Taylor Otwell committed
549

550
		return $this->connection->only($sql, $this->bindings);
551 552 553
	}

	/**
Taylor Otwell committed
554
	 * Execute the query as a SELECT statement and return the first result.
555
	 *
556 557
	 * @param  array  $columns
	 * @return mixed
558
	 */
Taylor Otwell committed
559
	public function first($columns = array('*'))
560
	{
561 562
		$columns = (array) $columns;

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
		// Since we only need the first result, we'll go ahead and set the
		// limit clause to 1, since this will be much faster than getting
		// all of the rows and then only returning the first.
		$results = $this->take(1)->get($columns);

		return (count($results) > 0) ? $results[0] : null;
	}

	/**
	 * Get an array with the values of a given column.
	 *
	 * @param  string  $column
	 * @param  string  $key
	 * @return array
	 */
	public function lists($column, $key = null)
	{
		$columns = (is_null($key)) ? array($column) : array($column, $key);

		$results = $this->get($columns);

		// First we will get the array of values for the requested column.
		// Of course, this array will simply have numeric keys. After we
		// have this array we will determine if we need to key the array
		// by another column from the result set.
		$values = array_map(function($row) use ($column)
		{
			return $row->$column;

		}, $results);

		// If a key was provided, we will extract an array of keys and
		// set the keys on the array of values using the array_combine
		// function provided by PHP, which should give us the proper
		// array form to return from the method.
		if ( ! is_null($key))
		{
			return array_combine(array_map(function($row) use ($key)
			{
				return $row->$key;

			}, $results), $values);
		}

		return $values;
Taylor Otwell committed
608 609 610 611 612 613 614 615 616 617
	}

	/**
	 * Execute the query as a SELECT statement.
	 *
	 * @param  array  $columns
	 * @return array
	 */
	public function get($columns = array('*'))
	{
Taylor Otwell committed
618
		if (is_null($this->selects)) $this->select($columns);
Taylor Otwell committed
619

620 621 622 623 624 625
		$sql = $this->grammar->select($this);

		$results = $this->connection->query($sql, $this->bindings);

		// If the query has an offset and we are using the SQL Server grammar,
		// we need to spin through the results and remove the "rownum" from
Taylor Otwell committed
626
		// each of the objects since there is no "offset".
627 628 629 630 631 632 633
		if ($this->offset > 0 and $this->grammar instanceof SQLServer)
		{
			array_walk($results, function($result)
			{
				unset($result->rownum);
			});
		}
634

635 636
		// Reset the SELECT clause so more queries can be performed using
		// the same instance. This is helpful for getting aggregates and
637
		// then getting actual results from the query.
Taylor Otwell committed
638
		$this->selects = null;
639 640 641 642 643

		return $results;
	}

	/**
644 645
	 * Get an aggregate value.
	 *
Phill Sparks committed
646
	 * @param  string  $aggregator
647
	 * @param  array   $columns
648 649
	 * @return mixed
	 */
650
	public function aggregate($aggregator, $columns)
651
	{
652 653 654
		// We'll set the aggregate value so the grammar does not try to compile
		// a SELECT clause on the query. If an aggregator is present, it's own
		// grammar function will be used to build the SQL syntax.
655
		$this->aggregate = compact('aggregator', 'columns');
656

657 658 659
		$sql = $this->grammar->select($this);

		$result = $this->connection->only($sql, $this->bindings);
660

661 662 663
		// Reset the aggregate so more queries can be performed using the same
		// instance. This is helpful for getting aggregates and then getting
		// actual results from the query such as during paging.
664 665 666 667 668 669
		$this->aggregate = null;

		return $result;
	}

	/**
670 671 672 673 674 675
	 * Get the paginated query results as a Paginator instance.
	 *
	 * @param  int        $per_page
	 * @param  array      $columns
	 * @return Paginator
	 */
676
	public function paginate($per_page = 20, $columns = array('*'))
677
	{
678
		// Because some database engines may throw errors if we leave orderings
Taylor Otwell committed
679
		// on the query when retrieving the total number of records, we'll drop
680
		// all of the ordreings and put them back on the query.
681 682
		list($orderings, $this->orderings) = array($this->orderings, null);

Taylor Otwell committed
683 684 685
		$total = $this->count(reset($columns));

		$page = Paginator::page($total, $per_page);
686

687 688
		$this->orderings = $orderings;

Taylor Otwell committed
689 690
		// Now we're ready to get the actual pagination results from the table
		// using the for_page and get methods. The "for_page" method provides
691
		// a convenient way to set the paging limit and offset.
692 693 694
		$results = $this->for_page($page, $per_page)->get($columns);

		return Paginator::make($results, $total, $per_page);
695 696 697
	}

	/**
Taylor Otwell committed
698 699
	 * Insert an array of values into the database table.
	 *
700 701 702 703 704
	 * @param  array  $values
	 * @return bool
	 */
	public function insert($values)
	{
705 706 707
		// Force every insert to be treated like a batch insert to make creating
		// the binding array simpler since we can just spin through the inserted
		// rows as if there/ was more than one every time.
708 709 710 711
		if ( ! is_array(reset($values))) $values = array($values);

		$bindings = array();

712 713 714
		// We need to merge the the insert values into the array of the query
		// bindings so that they will be bound to the PDO statement when it
		// is executed by the database connection.
715 716 717 718 719
		foreach ($values as $value)
		{
			$bindings = array_merge($bindings, array_values($value));
		}

720 721
		$sql = $this->grammar->insert($this, $values);

722
		return $this->connection->query($sql, $bindings);
723 724 725
	}

	/**
726
	 * Insert an array of values into the database table and return the ID.
Taylor Otwell committed
727
	 *
728 729
	 * @param  array   $values
	 * @param  string  $sequence
730 731
	 * @return int
	 */
732
	public function insert_get_id($values, $sequence = null)
733
	{
734 735
		$sql = $this->grammar->insert($this, $values);

736
		$this->connection->query($sql, array_values($values));
737

738 739 740
		// Some database systems (Postgres) require a sequence name to be
		// given when retrieving the auto-incrementing ID, so we'll pass
		// the given sequence into the method just in case.
741
		return (int) $this->connection->pdo->lastInsertId($sequence);
Taylor Otwell committed
742 743 744
	}

	/**
745 746 747 748 749 750 751 752
	 * Increment the value of a column by a given amount.
	 *
	 * @param  string  $column
	 * @param  int     $amount
	 * @return int
	 */
	public function increment($column, $amount = 1)
	{
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
		return $this->adjust($column, $amount, ' + ');
	}

	/**
	 * Decrement the value of a column by a given amount.
	 *
	 * @param  string  $column
	 * @param  int     $amount
	 * @return int
	 */
	public function decrement($column, $amount = 1)
	{
		return $this->adjust($column, $amount, ' - ');
	}

	/**
	 * Adjust the value of a column up or down by a given amount.
	 *
	 * @param  string  $column
	 * @param  int     $amount
	 * @param  string  $operator
	 * @return int
	 */
	protected function adjust($column, $amount, $operator)
	{
778 779 780 781 782 783
		$wrapped = $this->grammar->wrap($column);

		// To make the adjustment to the column, we'll wrap the expression in
		// an Expression instance, which forces the adjustment to be injected
		// into the query as a string instead of bound.
		$value = Database::raw($wrapped.$operator.$amount);
784 785

		return $this->update(array($column => $value));
786 787 788
	}

	/**
Taylor Otwell committed
789 790
	 * Update an array of values in the database table.
	 *
791
	 * @param  array  $values
Taylor Otwell committed
792
	 * @return int
793 794 795
	 */
	public function update($values)
	{
796 797 798 799
		// For update statements, we need to merge the bindings such that
		// the update values occur before the where bindings in the array
		// since the set statements will precede any of the where clauses
		// in the SQL syntax that is generated.
800 801
		$bindings =  array_merge(array_values($values), $this->bindings);

802 803
		$sql = $this->grammar->update($this, $values);

804
		return $this->connection->query($sql, $bindings);
805 806 807 808 809
	}

	/**
	 * Execute the query as a DELETE statement.
	 *
Taylor Otwell committed
810 811
	 * Optionally, an ID may be passed to the method do delete a specific row.
	 *
812
	 * @param  int   $id
Taylor Otwell committed
813
	 * @return int
814 815 816
	 */
	public function delete($id = null)
	{
817 818 819 820 821 822 823 824 825
		// If an ID is given to the method, we'll set the where clause
		// to match on the value of the ID. This allows the developer
		// to quickly delete a row by its primary key value.
		if ( ! is_null($id))
		{
			$this->where('id', '=', $id);
		}

		$sql = $this->grammar->delete($this);
826

827
		return $this->connection->query($sql, $this->bindings);		
828 829 830
	}

	/**
Taylor Otwell committed
831
	 * Magic Method for handling dynamic functions.
Taylor Otwell committed
832
	 *
833
	 * This method handles calls to aggregates as well as dynamic where clauses.
834 835 836
	 */
	public function __call($method, $parameters)
	{
837 838
		if (strpos($method, 'where_') === 0)
		{
Taylor Otwell committed
839
			return $this->dynamic_where($method, $parameters, $this);
840 841
		}

842
		if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
843
		{
844 845 846
			if (count($parameters) == 0) $parameters[0] = '*';

			return $this->aggregate(strtoupper($method), (array) $parameters[0]);
847
		}
848

849
		throw new \Exception("Method [$method] is not defined on the Query class.");
850 851
	}

Phill Sparks committed
852
}