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

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

class Query {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

256 257 258
		$type = 'where';

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

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

		return $this;
	}

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

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

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

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

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

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

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

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

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

408 409 410
		// 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
411 412 413 414
		$query = new Query($this->connection, $this->grammar, $this->from);

		call_user_func($callback, $query);

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

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

		return $this;
	}

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

439 440 441
		$flags = PREG_SPLIT_DELIM_CAPTURE;

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

443 444
		// The connector variable will determine which connector will be used
		// for the condition. We'll change it as we come across new boolean
445
		// connectors in the dynamic method string.
Taylor Otwell committed
446
		//
447 448 449
		// 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
450 451 452 453 454 455
		$connector = 'AND';

		$index = 0;

		foreach ($segments as $segment)
		{
456 457 458
			// 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
459 460 461 462 463 464
			if ($segment != '_and_' and $segment != '_or_')
			{
				$this->where($segment, '=', $parameters[$index], $connector);

				$index++;
			}
465 466 467
			// 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
468 469 470 471 472 473 474 475 476 477
			else
			{
				$connector = trim(strtoupper($segment), '_');
			}
		}

		return $this;
	}

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

	/**
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	 * 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;
	}

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

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

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

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

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

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

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

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

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

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

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

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

		return $results;
	}

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

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

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

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

		return $result;
	}

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

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

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

713 714
		$this->orderings = $orderings;

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

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

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

		$bindings = array();

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

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

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

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

762
		$result = $this->connection->query($sql, array_values($values));
763

764 765 766 767 768 769 770 771
		if ($this->grammar instanceof Postgres)
		{
			return (int) $result[0]->$column;
		}
		else
		{
			return (int) $this->connection->pdo->lastInsertId();
		}
Taylor Otwell committed
772 773 774
	}

	/**
775 776 777 778 779 780 781 782
	 * Increment the value of a column by a given amount.
	 *
	 * @param  string  $column
	 * @param  int     $amount
	 * @return int
	 */
	public function increment($column, $amount = 1)
	{
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
		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)
	{
808 809
		$wrapped = $this->grammar->wrap($column);

810 811 812
		// 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.
813
		$value = Database::raw($wrapped.$operator.$amount);
814 815

		return $this->update(array($column => $value));
816 817 818
	}

	/**
Taylor Otwell committed
819 820
	 * Update an array of values in the database table.
	 *
821
	 * @param  array  $values
Taylor Otwell committed
822
	 * @return int
823 824 825
	 */
	public function update($values)
	{
826 827 828
		// 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.
829 830
		$bindings =  array_merge(array_values($values), $this->bindings);

831 832
		$sql = $this->grammar->update($this, $values);

833
		return $this->connection->query($sql, $bindings);
834 835 836 837 838
	}

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

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

856
		return $this->connection->query($sql, $this->bindings);		
857 858 859
	}

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

871 872 873
		// 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.
874
		if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
875
		{
876 877 878
			if (count($parameters) == 0) $parameters[0] = '*';

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

881
		throw new \Exception("Method [$method] is not defined on the Query class.");
882 883
	}

Phill Sparks committed
884
}