query.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. <?php namespace Laravel\Database; use Laravel\Paginator;
  2. class Query {
  3. /**
  4. * The database connection.
  5. *
  6. * @var Connection
  7. */
  8. public $connection;
  9. /**
  10. * The query grammar instance.
  11. *
  12. * @var Grammars\Grammar
  13. */
  14. public $grammar;
  15. /**
  16. * The SELECT clause.
  17. *
  18. * @var array
  19. */
  20. public $selects;
  21. /**
  22. * If the query is performing an aggregate function, this will contain
  23. * the column and and function to use when aggregating.
  24. *
  25. * @var array
  26. */
  27. public $aggregate;
  28. /**
  29. * Indicates if the query should return distinct results.
  30. *
  31. * @var bool
  32. */
  33. public $distinct = false;
  34. /**
  35. * The table name.
  36. *
  37. * @var string
  38. */
  39. public $from;
  40. /**
  41. * The table joins.
  42. *
  43. * @var array
  44. */
  45. public $joins;
  46. /**
  47. * The WHERE clauses.
  48. *
  49. * @var array
  50. */
  51. public $wheres;
  52. /**
  53. * The ORDER BY clauses.
  54. *
  55. * @var array
  56. */
  57. public $orderings;
  58. /**
  59. * The LIMIT value.
  60. *
  61. * @var int
  62. */
  63. public $limit;
  64. /**
  65. * The OFFSET value.
  66. *
  67. * @var int
  68. */
  69. public $offset;
  70. /**
  71. * The query value bindings.
  72. *
  73. * @var array
  74. */
  75. public $bindings = array();
  76. /**
  77. * Create a new query instance.
  78. *
  79. * @param Connection $connection
  80. * @param Grammars\Grammar $grammar
  81. * @param string $table
  82. * @return void
  83. */
  84. public function __construct(Connection $connection, Grammars\Grammar $grammar, $table)
  85. {
  86. $this->from = $table;
  87. $this->grammar = $grammar;
  88. $this->connection = $connection;
  89. }
  90. /**
  91. * Force the query to return distinct results.
  92. *
  93. * @return Query
  94. */
  95. public function distinct()
  96. {
  97. $this->distinct = true;
  98. return $this;
  99. }
  100. /**
  101. * Add an array of columns to the SELECT clause.
  102. *
  103. * @param array $columns
  104. * @return Query
  105. */
  106. public function select($columns = array('*'))
  107. {
  108. $this->selects = (array) $columns;
  109. return $this;
  110. }
  111. /**
  112. * Add a join clause to the query.
  113. *
  114. * @param string $table
  115. * @param string $column1
  116. * @param string $operator
  117. * @param string $column2
  118. * @param string $type
  119. * @return Query
  120. */
  121. public function join($table, $column1, $operator, $column2, $type = 'INNER')
  122. {
  123. $this->joins[] = compact('type', 'table', 'column1', 'operator', 'column2');
  124. return $this;
  125. }
  126. /**
  127. * Add a left join to the query.
  128. *
  129. * @param string $table
  130. * @param string $column1
  131. * @param string $operator
  132. * @param string $column2
  133. * @return Query
  134. */
  135. public function left_join($table, $column1, $operator, $column2)
  136. {
  137. return $this->join($table, $column1, $operator, $column2, 'LEFT');
  138. }
  139. /**
  140. * Reset the where clause to its initial state. All bindings will be cleared.
  141. *
  142. * @return void
  143. */
  144. public function reset_where()
  145. {
  146. list($this->wheres, $this->bindings) = array(array(), array());
  147. }
  148. /**
  149. * Add a raw where condition to the query.
  150. *
  151. * @param string $where
  152. * @param array $bindings
  153. * @param string $connector
  154. * @return Query
  155. */
  156. public function raw_where($where, $bindings = array(), $connector = 'AND')
  157. {
  158. $this->wheres[] = array('type' => 'raw', 'connector' => $connector, 'sql' => $where);
  159. $this->bindings = array_merge($this->bindings, $bindings);
  160. return $this;
  161. }
  162. /**
  163. * Add a raw or where condition to the query.
  164. *
  165. * @param string $where
  166. * @param array $bindings
  167. * @return Query
  168. */
  169. public function raw_or_where($where, $bindings = array())
  170. {
  171. return $this->raw_where($where, $bindings, 'OR');
  172. }
  173. /**
  174. * Add a where condition to the query.
  175. *
  176. * @param string $column
  177. * @param string $operator
  178. * @param mixed $value
  179. * @param string $connector
  180. * @return Query
  181. */
  182. public function where($column, $operator, $value, $connector = 'AND')
  183. {
  184. $type = 'where';
  185. $this->wheres[] = compact('type', 'column', 'operator', 'value', 'connector');
  186. $this->bindings[] = $value;
  187. return $this;
  188. }
  189. /**
  190. * Add an or where condition to the query.
  191. *
  192. * @param string $column
  193. * @param string $operator
  194. * @param mixed $value
  195. * @return Query
  196. */
  197. public function or_where($column, $operator, $value)
  198. {
  199. return $this->where($column, $operator, $value, 'OR');
  200. }
  201. /**
  202. * Add an or where condition for the primary key to the query.
  203. *
  204. * @param mixed $value
  205. * @return Query
  206. */
  207. public function or_where_id($value)
  208. {
  209. return $this->or_where('id', '=', $value);
  210. }
  211. /**
  212. * Add a where in condition to the query.
  213. *
  214. * @param string $column
  215. * @param array $values
  216. * @param string $connector
  217. * @param bool $not
  218. * @return Query
  219. */
  220. public function where_in($column, $values, $connector = 'AND', $not = false)
  221. {
  222. $type = ($not) ? 'where_not_in' : 'where_in';
  223. $this->wheres[] = compact('type', 'column', 'values', 'connector');
  224. $this->bindings = array_merge($this->bindings, $values);
  225. return $this;
  226. }
  227. /**
  228. * Add an or where in condition to the query.
  229. *
  230. * @param string $column
  231. * @param array $values
  232. * @return Query
  233. */
  234. public function or_where_in($column, $values)
  235. {
  236. return $this->where_in($column, $values, 'OR');
  237. }
  238. /**
  239. * Add a where not in condition to the query.
  240. *
  241. * @param string $column
  242. * @param array $values
  243. * @param string $connector
  244. * @return Query
  245. */
  246. public function where_not_in($column, $values, $connector = 'AND')
  247. {
  248. return $this->where_in($column, $values, $connector, true);
  249. }
  250. /**
  251. * Add an or where not in condition to the query.
  252. *
  253. * @param string $column
  254. * @param array $values
  255. * @return Query
  256. */
  257. public function or_where_not_in($column, $values)
  258. {
  259. return $this->where_not_in($column, $values, 'OR');
  260. }
  261. /**
  262. * Add a where null condition to the query.
  263. *
  264. * @param string $column
  265. * @param string $connector
  266. * @param bool $not
  267. * @return Query
  268. */
  269. public function where_null($column, $connector = 'AND', $not = false)
  270. {
  271. $type = ($not) ? 'where_not_null' : 'where_null';
  272. $this->wheres[] = compact('type', 'column', 'connector');
  273. return $this;
  274. }
  275. /**
  276. * Add an or where null condition to the query.
  277. *
  278. * @param string $column
  279. * @return Query
  280. */
  281. public function or_where_null($column)
  282. {
  283. return $this->where_null($column, 'OR');
  284. }
  285. /**
  286. * Add a where not null condition to the query.
  287. *
  288. * @param string $column
  289. * @param string $connector
  290. * @return Query
  291. */
  292. public function where_not_null($column, $connector = 'AND')
  293. {
  294. return $this->where_null($column, $connector, true);
  295. }
  296. /**
  297. * Add an or where not null condition to the query.
  298. *
  299. * @param string $column
  300. * @return Query
  301. */
  302. public function or_where_not_null($column)
  303. {
  304. return $this->where_not_null($column, 'OR');
  305. }
  306. /**
  307. * Add dynamic where conditions to the query.
  308. *
  309. * Dynamic queries are caught by the __call magic method and are parsed here.
  310. * They provide a convenient, expressive API for building simple conditions.
  311. *
  312. * @param string $method
  313. * @param array $parameters
  314. * @return Query
  315. */
  316. private function dynamic_where($method, $parameters)
  317. {
  318. // Strip the "where_" off of the method.
  319. $finder = substr($method, 6);
  320. // Split the column names from the connectors.
  321. $segments = preg_split('/(_and_|_or_)/i', $finder, -1, PREG_SPLIT_DELIM_CAPTURE);
  322. // The connector variable will determine which connector will be
  323. // used for the condition. We'll change it as we come across new
  324. // connectors in the dynamic method string.
  325. //
  326. // The index variable helps us get the correct parameter value
  327. // for the where condition. We increment it each time we add
  328. // a condition to the query.
  329. $connector = 'AND';
  330. $index = 0;
  331. foreach ($segments as $segment)
  332. {
  333. if ($segment != '_and_' and $segment != '_or_')
  334. {
  335. $this->where($segment, '=', $parameters[$index], $connector);
  336. $index++;
  337. }
  338. else
  339. {
  340. $connector = trim(strtoupper($segment), '_');
  341. }
  342. }
  343. return $this;
  344. }
  345. /**
  346. * Add an ordering to the query.
  347. *
  348. * @param string $column
  349. * @param string $direction
  350. * @return Query
  351. */
  352. public function order_by($column, $direction = 'asc')
  353. {
  354. $this->orderings[] = compact('column', 'direction');
  355. return $this;
  356. }
  357. /**
  358. * Set the query offset.
  359. *
  360. * @param int $value
  361. * @return Query
  362. */
  363. public function skip($value)
  364. {
  365. $this->offset = $value;
  366. return $this;
  367. }
  368. /**
  369. * Set the query limit.
  370. *
  371. * @param int $value
  372. * @return Query
  373. */
  374. public function take($value)
  375. {
  376. $this->limit = $value;
  377. return $this;
  378. }
  379. /**
  380. * Set the query limit and offset for a given page and item per page count.
  381. *
  382. * @param int $page
  383. * @param int $per_page
  384. * @return Query
  385. */
  386. public function for_page($page, $per_page)
  387. {
  388. return $this->skip(($page - 1) * $per_page)->take($per_page);
  389. }
  390. /**
  391. * Find a record by the primary key.
  392. *
  393. * @param int $id
  394. * @param array $columns
  395. * @return object
  396. */
  397. public function find($id, $columns = array('*'))
  398. {
  399. return $this->where('id', '=', $id)->first($columns);
  400. }
  401. /**
  402. * Execute the query as a SELECT statement and return a single column.
  403. *
  404. * @param string $column
  405. * @return mixed
  406. */
  407. public function only($column)
  408. {
  409. $this->select(array($column));
  410. return $this->connection->only($this->grammar->select($this), $this->bindings);
  411. }
  412. /**
  413. * Execute the query as a SELECT statement and return the first result.
  414. *
  415. * If a single column is selected from the database, only the value of that column will be returned.
  416. *
  417. * @param array $columns
  418. * @return mixed
  419. */
  420. public function first($columns = array('*'))
  421. {
  422. $columns = (array) $columns;
  423. return (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null;
  424. }
  425. /**
  426. * Execute the query as a SELECT statement.
  427. *
  428. * @param array $columns
  429. * @return array
  430. */
  431. public function get($columns = array('*'))
  432. {
  433. if (is_null($this->selects)) $this->select($columns);
  434. $results = $this->connection->query($this->grammar->select($this), $this->bindings);
  435. // Reset the SELECT clause so more queries can be performed using
  436. // the same instance. This is helpful for getting aggregates and
  437. // then getting actual results.
  438. $this->selects = null;
  439. return $results;
  440. }
  441. /**
  442. * Get an aggregate value.
  443. *
  444. * @param string $aggregate
  445. * @param string $column
  446. * @return mixed
  447. */
  448. private function aggregate($aggregator, $column)
  449. {
  450. $this->aggregate = compact('aggregator', 'column');
  451. $result = $this->connection->only($this->grammar->select($this), $this->bindings);
  452. // Reset the aggregate so more queries can be performed using
  453. // the same instance. This is helpful for getting aggregates
  454. // and then getting actual results.
  455. $this->aggregate = null;
  456. return $result;
  457. }
  458. /**
  459. * Get the paginated query results as a Paginator instance.
  460. *
  461. * @param int $per_page
  462. * @param array $columns
  463. * @return Paginator
  464. */
  465. public function paginate($per_page = 20, $columns = array('*'))
  466. {
  467. // Calculate the current page for the request. The page number
  468. // will be validated and adjusted by the Paginator class,
  469. // so we can assume it is valid.
  470. $page = Paginator::page($total = $this->count(), $per_page);
  471. return Paginator::make($this->for_page($page, $per_page)->get($columns), $total, $per_page);
  472. }
  473. /**
  474. * Insert an array of values into the database table.
  475. *
  476. * @param array $values
  477. * @return bool
  478. */
  479. public function insert($values)
  480. {
  481. // Force every insert to be treated like a batch insert to make creating
  482. // the binding array simpler since we can just spin through the inserted
  483. // rows as if there/ was more than one every time.
  484. if ( ! is_array(reset($values))) $values = array($values);
  485. $bindings = array();
  486. foreach ($values as $value)
  487. {
  488. $bindings = array_merge($bindings, array_values($value));
  489. }
  490. return $this->connection->query($this->grammar->insert($this, $values), $bindings);
  491. }
  492. /**
  493. * Insert an array of values into the database table and
  494. * return the value of the ID column.
  495. *
  496. * @param array $values
  497. * @param string $sequence
  498. * @return int
  499. */
  500. public function insert_get_id($values, $sequence = null)
  501. {
  502. $this->connection->query($this->grammar->insert($this, $values), array_values($values));
  503. return (int) $this->connection->pdo->lastInsertId($sequence);
  504. }
  505. /**
  506. * Increment the value of a column by a given amount.
  507. *
  508. * @param string $column
  509. * @param int $amount
  510. * @return int
  511. */
  512. public function increment($column, $amount = 1)
  513. {
  514. return $this->adjust($column, $amount, ' + ');
  515. }
  516. /**
  517. * Decrement the value of a column by a given amount.
  518. *
  519. * @param string $column
  520. * @param int $amount
  521. * @return int
  522. */
  523. public function decrement($column, $amount = 1)
  524. {
  525. return $this->adjust($column, $amount, ' - ');
  526. }
  527. /**
  528. * Adjust the value of a column up or down by a given amount.
  529. *
  530. * @param string $column
  531. * @param int $amount
  532. * @param string $operator
  533. * @return int
  534. */
  535. protected function adjust($column, $amount, $operator)
  536. {
  537. return $this->update(array($column => Manager::raw($this->grammar->wrap($column).$operator.$amount)));
  538. }
  539. /**
  540. * Update an array of values in the database table.
  541. *
  542. * @param array $values
  543. * @return int
  544. */
  545. public function update($values)
  546. {
  547. $bindings = array_merge(array_values($values), $this->bindings);
  548. return $this->connection->query($this->grammar->update($this, $values), $bindings);
  549. }
  550. /**
  551. * Execute the query as a DELETE statement.
  552. *
  553. * Optionally, an ID may be passed to the method do delete a specific row.
  554. *
  555. * @param int $id
  556. * @return int
  557. */
  558. public function delete($id = null)
  559. {
  560. if ( ! is_null($id)) $this->where('id', '=', $id);
  561. return $this->connection->query($this->grammar->delete($this), $this->bindings);
  562. }
  563. /**
  564. * Magic Method for handling dynamic functions.
  565. *
  566. * This method handles all calls to aggregate functions as well
  567. * as the construction of dynamic where clauses.
  568. */
  569. public function __call($method, $parameters)
  570. {
  571. if (strpos($method, 'where_') === 0)
  572. {
  573. return $this->dynamic_where($method, $parameters, $this);
  574. }
  575. if (in_array($method, array('abs', 'count', 'min', 'max', 'avg', 'sum')))
  576. {
  577. if ($method == 'count')
  578. {
  579. return $this->aggregate(strtoupper($method), '*');
  580. }
  581. else
  582. {
  583. return $this->aggregate(strtoupper($method), $parameters[0]);
  584. }
  585. }
  586. throw new \Exception("Method [$method] is not defined on the Query class.");
  587. }
  588. }