query.php 20 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;

	/**
74 75 76 77 78 79 80
	 * The HAVING clauses.
	 *
	 * @var array
	 */
	public $havings;

	/**
Taylor Otwell committed
81
	 * The ORDER BY clauses.
82 83 84
	 *
	 * @var array
	 */
Taylor Otwell committed
85
	public $orderings;
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

	/**
	 * 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.
	 *
111 112 113
	 * @param  Connection  $connection
	 * @param  Grammar     $grammar
	 * @param  string      $table
114 115
	 * @return void
	 */
116
	public function __construct(Connection $connection, Query\Grammars\Grammar $grammar, $table)
117
	{
Taylor Otwell committed
118
		$this->from = $table;
119
		$this->grammar = $grammar;
120
		$this->connection = $connection;
121 122 123 124 125 126 127 128 129 130 131 132 133 134
	}

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

	/**
Taylor Otwell committed
135 136
	 * Add an array of columns to the SELECT clause.
	 *
137
	 * @param  array  $columns
138 139
	 * @return Query
	 */
140
	public function select($columns = array('*'))
141
	{
Taylor Otwell committed
142
		$this->selects = (array) $columns;
143 144 145 146
		return $this;
	}

	/**
Taylor Otwell committed
147
	 * Add a join clause to the query.
148
	 *
149 150 151 152 153 154 155
	 * @param  string  $table
	 * @param  string  $column1
	 * @param  string  $operator
	 * @param  string  $column2
	 * @param  string  $type
	 * @return Query
	 */
156
	public function join($table, $column1, $operator = null, $column2 = null, $type = 'INNER')
157
	{
158 159
		// 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
160
		// the join, and then call the Closure with the join/
161 162 163 164 165 166
		if ($column1 instanceof Closure)
		{
			$this->joins[] = new Query\Join($type, $table);

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

168 169 170 171 172 173 174 175 176 177 178
		// 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
179

180 181 182 183 184 185 186 187 188 189 190 191
		return $this;
	}

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

	/**
198
	 * Reset the where clause to its initial state.
Taylor Otwell committed
199 200 201 202 203
	 *
	 * @return void
	 */
	public function reset_where()
	{
204
		list($this->wheres, $this->bindings) = array(array(), array());
Taylor Otwell committed
205 206 207
	}

	/**
208 209 210 211 212 213 214 215 216
	 * 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')
	{
217
		$this->wheres[] = array('type' => 'where_raw', 'connector' => $connector, 'sql' => $where);
Taylor Otwell committed
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
		$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
	 */
245
	public function where($column, $operator = null, $value = null, $connector = 'AND')
246
	{
247
		// If a Closure is passed into the method, it means a nested where
248 249 250 251 252 253 254
		// 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);
		}

255 256 257
		$type = 'where';

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

259 260 261 262 263 264 265 266 267 268 269 270 271
		$this->bindings[] = $value;

		return $this;
	}

	/**
	 * Add an or where condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $operator
	 * @param  mixed   $value
	 * @return Query
	 */
272
	public function or_where($column, $operator = null, $value = null)
273 274 275 276 277
	{
		return $this->where($column, $operator, $value, 'OR');
	}

	/**
278
	 * Add an or where condition for the primary key to the query.
Taylor Otwell committed
279
	 *
280 281 282 283 284 285 286 287 288
	 * @param  mixed  $value
	 * @return Query
	 */
	public function or_where_id($value)
	{
		return $this->or_where('id', '=', $value);		
	}

	/**
289 290 291 292 293
	 * Add a where in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @param  string  $connector
Taylor Otwell committed
294
	 * @param  bool    $not
295 296
	 * @return Query
	 */
Taylor Otwell committed
297
	public function where_in($column, $values, $connector = 'AND', $not = false)
298
	{
299 300 301
		$type = ($not) ? 'where_not_in' : 'where_in';

		$this->wheres[] = compact('type', 'column', 'values', 'connector');
Taylor Otwell committed
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
		$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
330
		return $this->where_in($column, $values, $connector, true);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	}

	/**
	 * 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
350
	 * @param  bool    $not
351 352
	 * @return Query
	 */
Taylor Otwell committed
353
	public function where_null($column, $connector = 'AND', $not = false)
354
	{
355 356 357
		$type = ($not) ? 'where_not_null' : 'where_null';

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

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
		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
382
		return $this->where_null($column, $connector, true);
383 384 385 386 387 388 389 390 391 392 393 394 395 396
	}

	/**
	 * 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');
	}

	/**
397
	 * Add a nested where condition to the query.
Taylor Otwell committed
398
	 *
399 400 401 402
	 * @param  Closure  $callback
	 * @param  string   $connector
	 * @return Query
	 */
403
	public function where_nested($callback, $connector = 'AND')
404 405 406
	{
		$type = 'where_nested';

407 408 409
		// To handle a nested where statement, we will actually instantiate a new
		// Query instance and run the callback over that instance, which will
		// allow the developer to have a fresh query instance
410 411 412 413
		$query = new Query($this->connection, $this->grammar, $this->from);

		call_user_func($callback, $query);

414 415 416
		// 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 when building.
417 418 419 420
		if ($query->wheres !== null)
		{
			$this->wheres[] = compact('type', 'query', 'connector');
		}
421 422 423 424 425 426 427 428

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

		return $this;
	}

	/**
	 * Add dynamic where conditions to the query.
Taylor Otwell committed
429
	 *
Taylor Otwell committed
430 431 432 433 434 435 436 437
	 * @param  string  $method
	 * @param  array   $parameters
	 * @return Query
	 */
	private function dynamic_where($method, $parameters)
	{
		$finder = substr($method, 6);

438 439 440
		$flags = PREG_SPLIT_DELIM_CAPTURE;

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

442 443
		// The connector variable will determine which connector will be used
		// for the condition. We'll change it as we come across new boolean
444
		// connectors in the dynamic method string.
Taylor Otwell committed
445
		//
446 447 448
		// The index variable helps us get the correct parameter value for
		// the where condition. We increment it each time we add another
		// condition to the query's where clause.
Taylor Otwell committed
449 450 451 452 453 454
		$connector = 'AND';

		$index = 0;

		foreach ($segments as $segment)
		{
455 456 457
			// 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 constraint
			// of the query's where clause and keep iterating the segments.
Taylor Otwell committed
458 459 460 461 462 463
			if ($segment != '_and_' and $segment != '_or_')
			{
				$this->where($segment, '=', $parameters[$index], $connector);

				$index++;
			}
464 465 466
			// Otherwise, we will store the connector so we know how the next
			// where clause we find in the query should be connected to the
			// previous one and will add it when we find the next one.
Taylor Otwell committed
467 468 469 470 471 472 473 474 475 476
			else
			{
				$connector = trim(strtoupper($segment), '_');
			}
		}

		return $this;
	}

	/**
477 478 479 480 481 482 483 484 485 486 487 488
	 * Add a grouping to the query.
	 *
	 * @param  string  $column
	 * @return Query
	 */
	public function group_by($column)
	{
		$this->groupings[] = $column;
		return $this;
	}

	/**
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	 * Add a having to the query.
	 *
	 * @param  string  $column
	 * @param  string  $operator
	 * @param  mixed   $value
	 */
	public function having($column, $operator, $value)
	{
		$this->havings[] = compact('column', 'operator', 'value');

		$this->bindings[] = $value;

		return $this;
	}

	/**
505 506 507 508 509 510
	 * Add an ordering to the query.
	 *
	 * @param  string  $column
	 * @param  string  $direction
	 * @return Query
	 */
511
	public function order_by($column, $direction = 'asc')
512
	{
Taylor Otwell committed
513
		$this->orderings[] = compact('column', 'direction');
514 515 516 517 518 519 520 521 522 523 524
		return $this;
	}

	/**
	 * Set the query offset.
	 *
	 * @param  int  $value
	 * @return Query
	 */
	public function skip($value)
	{
Taylor Otwell committed
525
		$this->offset = $value;
526 527 528 529 530 531 532 533 534 535 536
		return $this;
	}

	/**
	 * Set the query limit.
	 *
	 * @param  int  $value
	 * @return Query
	 */
	public function take($value)
	{
Taylor Otwell committed
537
		$this->limit = $value;
538 539 540 541
		return $this;
	}

	/**
542
	 * Set the query limit and offset for a given page.
543 544 545 546 547
	 *
	 * @param  int    $page
	 * @param  int    $per_page
	 * @return Query
	 */
Taylor Otwell committed
548
	public function for_page($page, $per_page)
549
	{
Taylor Otwell committed
550
		return $this->skip(($page - 1) * $per_page)->take($per_page);
551 552 553
	}

	/**
Taylor Otwell committed
554
	 * Find a record by the primary key.
555
	 *
Taylor Otwell committed
556
	 * @param  int     $id
557
	 * @param  array   $columns
558 559
	 * @return object
	 */
Taylor Otwell committed
560
	public function find($id, $columns = array('*'))
561
	{
Taylor Otwell committed
562
		return $this->where('id', '=', $id)->first($columns);
563 564 565
	}

	/**
566
	 * Execute the query as a SELECT statement and return a single column.
567 568 569 570
	 *
	 * @param  string  $column
	 * @return mixed
	 */
571
	public function only($column)
572
	{
573
		$sql = $this->grammar->select($this->select(array($column)));
Taylor Otwell committed
574

575
		return $this->connection->only($sql, $this->bindings);
576 577 578
	}

	/**
Taylor Otwell committed
579
	 * Execute the query as a SELECT statement and return the first result.
580
	 *
581 582
	 * @param  array  $columns
	 * @return mixed
583
	 */
Taylor Otwell committed
584
	public function first($columns = array('*'))
585
	{
586 587
		$columns = (array) $columns;

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
		// 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
633 634 635 636 637 638 639 640 641 642
	}

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

645 646 647 648 649 650
		$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
651
		// each of the objects since there is no "offset".
652 653 654 655 656 657 658
		if ($this->offset > 0 and $this->grammar instanceof SQLServer)
		{
			array_walk($results, function($result)
			{
				unset($result->rownum);
			});
		}
659

660 661
		// Reset the SELECT clause so more queries can be performed using
		// the same instance. This is helpful for getting aggregates and
662
		// then getting actual results from the query.
Taylor Otwell committed
663
		$this->selects = null;
664 665 666 667 668

		return $results;
	}

	/**
669 670
	 * Get an aggregate value.
	 *
Phill Sparks committed
671
	 * @param  string  $aggregator
672
	 * @param  array   $columns
673 674
	 * @return mixed
	 */
675
	public function aggregate($aggregator, $columns)
676
	{
677 678 679
		// 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.
680
		$this->aggregate = compact('aggregator', 'columns');
681

682 683 684
		$sql = $this->grammar->select($this);

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

686 687 688
		// 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.
689 690 691 692 693 694
		$this->aggregate = null;

		return $result;
	}

	/**
695 696 697 698 699 700
	 * Get the paginated query results as a Paginator instance.
	 *
	 * @param  int        $per_page
	 * @param  array      $columns
	 * @return Paginator
	 */
701
	public function paginate($per_page = 20, $columns = array('*'))
702
	{
703
		// Because some database engines may throw errors if we leave orderings
Taylor Otwell committed
704
		// on the query when retrieving the total number of records, we'll drop
705
		// all of the ordreings and put them back on the query.
706 707
		list($orderings, $this->orderings) = array($this->orderings, null);

Taylor Otwell committed
708 709 710
		$total = $this->count(reset($columns));

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

712 713
		$this->orderings = $orderings;

Taylor Otwell committed
714 715
		// 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
716
		// a convenient way to set the paging limit and offset.
717 718 719
		$results = $this->for_page($page, $per_page)->get($columns);

		return Paginator::make($results, $total, $per_page);
720 721 722
	}

	/**
Taylor Otwell committed
723 724
	 * Insert an array of values into the database table.
	 *
725 726 727 728 729
	 * @param  array  $values
	 * @return bool
	 */
	public function insert($values)
	{
730 731 732
		// 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.
733 734 735 736
		if ( ! is_array(reset($values))) $values = array($values);

		$bindings = array();

737 738 739
		// 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.
740 741 742 743 744
		foreach ($values as $value)
		{
			$bindings = array_merge($bindings, array_values($value));
		}

745 746
		$sql = $this->grammar->insert($this, $values);

747
		return $this->connection->query($sql, $bindings);
748 749 750
	}

	/**
751
	 * Insert an array of values into the database table and return the ID.
Taylor Otwell committed
752
	 *
753 754
	 * @param  array   $values
	 * @param  string  $sequence
755 756
	 * @return int
	 */
757
	public function insert_get_id($values, $sequence = null)
758
	{
759 760
		$sql = $this->grammar->insert($this, $values);

761
		$this->connection->query($sql, array_values($values));
762

763 764 765
		// 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.
766
		return (int) $this->connection->pdo->lastInsertId($sequence);
Taylor Otwell committed
767 768 769
	}

	/**
770 771 772 773 774 775 776 777
	 * Increment the value of a column by a given amount.
	 *
	 * @param  string  $column
	 * @param  int     $amount
	 * @return int
	 */
	public function increment($column, $amount = 1)
	{
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
		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)
	{
803 804
		$wrapped = $this->grammar->wrap($column);

805 806 807
		// 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.
808
		$value = Database::raw($wrapped.$operator.$amount);
809 810

		return $this->update(array($column => $value));
811 812 813
	}

	/**
Taylor Otwell committed
814 815
	 * Update an array of values in the database table.
	 *
816
	 * @param  array  $values
Taylor Otwell committed
817
	 * @return int
818 819 820
	 */
	public function update($values)
	{
821 822 823
		// For update statements, we need to merge the bindings such that the update
		// values occur before the where bindings in the array since the sets will
		// precede any of the where clauses in the SQL syntax that is generated.
824 825
		$bindings =  array_merge(array_values($values), $this->bindings);

826 827
		$sql = $this->grammar->update($this, $values);

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

	/**
	 * Execute the query as a DELETE statement.
	 *
Taylor Otwell committed
834 835
	 * Optionally, an ID may be passed to the method do delete a specific row.
	 *
836
	 * @param  int   $id
Taylor Otwell committed
837
	 * @return int
838 839 840
	 */
	public function delete($id = null)
	{
841 842 843
		// 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.
844 845 846 847 848 849
		if ( ! is_null($id))
		{
			$this->where('id', '=', $id);
		}

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

851
		return $this->connection->query($sql, $this->bindings);		
852 853 854
	}

	/**
Taylor Otwell committed
855
	 * Magic Method for handling dynamic functions.
Taylor Otwell committed
856
	 *
857
	 * This method handles calls to aggregates as well as dynamic where clauses.
858 859 860
	 */
	public function __call($method, $parameters)
	{
861 862
		if (strpos($method, 'where_') === 0)
		{
Taylor Otwell committed
863
			return $this->dynamic_where($method, $parameters, $this);
864 865
		}

866 867 868
		// All of the aggregate methods are handled by a single method, so we'll
		// catch them all here and then pass them off to the agregate method
		// instead of creating methods for each one of them.
869
		if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
870
		{
871 872 873
			if (count($parameters) == 0) $parameters[0] = '*';

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

876
		throw new \Exception("Method [$method] is not defined on the Query class.");
877 878
	}

Phill Sparks committed
879
}