Expr.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Query;
  20. /**
  21. * This class is used to generate DQL expressions via a set of PHP static functions
  22. *
  23. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  24. * @link www.doctrine-project.org
  25. * @since 2.0
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. * @author Jonathan Wage <jonwage@gmail.com>
  28. * @author Roman Borschel <roman@code-factory.org>
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. * @todo Rename: ExpressionBuilder
  31. */
  32. class Expr
  33. {
  34. /**
  35. * Creates a conjunction of the given boolean expressions.
  36. *
  37. * Example:
  38. *
  39. * [php]
  40. * // (u.type = ?1) AND (u.role = ?2)
  41. * $expr->andX($expr->eq('u.type', ':1'), $expr->eq('u.role', ':2'));
  42. *
  43. * @param \Doctrine\ORM\Query\Expr\Comparison |
  44. * \Doctrine\ORM\Query\Expr\Func |
  45. * \Doctrine\ORM\Query\Expr\Orx
  46. * $x Optional clause. Defaults = null, but requires at least one defined when converting to string.
  47. * @return Expr\Andx
  48. */
  49. public function andX($x = null)
  50. {
  51. return new Expr\Andx(func_get_args());
  52. }
  53. /**
  54. * Creates a disjunction of the given boolean expressions.
  55. *
  56. * Example:
  57. *
  58. * [php]
  59. * // (u.type = ?1) OR (u.role = ?2)
  60. * $q->where($q->expr()->orX('u.type = ?1', 'u.role = ?2'));
  61. *
  62. * @param mixed $x Optional clause. Defaults = null, but requires
  63. * at least one defined when converting to string.
  64. * @return Expr\Orx
  65. */
  66. public function orX($x = null)
  67. {
  68. return new Expr\Orx(func_get_args());
  69. }
  70. /**
  71. * Creates an ASCending order expression.
  72. *
  73. * @param $sort
  74. * @return Expr\OrderBy
  75. */
  76. public function asc($expr)
  77. {
  78. return new Expr\OrderBy($expr, 'ASC');
  79. }
  80. /**
  81. * Creates a DESCending order expression.
  82. *
  83. * @param $sort
  84. * @return Expr\OrderBy
  85. */
  86. public function desc($expr)
  87. {
  88. return new Expr\OrderBy($expr, 'DESC');
  89. }
  90. /**
  91. * Creates an equality comparison expression with the given arguments.
  92. *
  93. * First argument is considered the left expression and the second is the right expression.
  94. * When converted to string, it will generated a <left expr> = <right expr>. Example:
  95. *
  96. * [php]
  97. * // u.id = ?1
  98. * $expr->eq('u.id', '?1');
  99. *
  100. * @param mixed $x Left expression
  101. * @param mixed $y Right expression
  102. * @return Expr\Comparison
  103. */
  104. public function eq($x, $y)
  105. {
  106. return new Expr\Comparison($x, Expr\Comparison::EQ, $y);
  107. }
  108. /**
  109. * Creates an instance of Expr\Comparison, with the given arguments.
  110. * First argument is considered the left expression and the second is the right expression.
  111. * When converted to string, it will generated a <left expr> <> <right expr>. Example:
  112. *
  113. * [php]
  114. * // u.id <> ?1
  115. * $q->where($q->expr()->neq('u.id', '?1'));
  116. *
  117. * @param mixed $x Left expression
  118. * @param mixed $y Right expression
  119. * @return Expr\Comparison
  120. */
  121. public function neq($x, $y)
  122. {
  123. return new Expr\Comparison($x, Expr\Comparison::NEQ, $y);
  124. }
  125. /**
  126. * Creates an instance of Expr\Comparison, with the given arguments.
  127. * First argument is considered the left expression and the second is the right expression.
  128. * When converted to string, it will generated a <left expr> < <right expr>. Example:
  129. *
  130. * [php]
  131. * // u.id < ?1
  132. * $q->where($q->expr()->lt('u.id', '?1'));
  133. *
  134. * @param mixed $x Left expression
  135. * @param mixed $y Right expression
  136. * @return Expr\Comparison
  137. */
  138. public function lt($x, $y)
  139. {
  140. return new Expr\Comparison($x, Expr\Comparison::LT, $y);
  141. }
  142. /**
  143. * Creates an instance of Expr\Comparison, with the given arguments.
  144. * First argument is considered the left expression and the second is the right expression.
  145. * When converted to string, it will generated a <left expr> <= <right expr>. Example:
  146. *
  147. * [php]
  148. * // u.id <= ?1
  149. * $q->where($q->expr()->lte('u.id', '?1'));
  150. *
  151. * @param mixed $x Left expression
  152. * @param mixed $y Right expression
  153. * @return Expr\Comparison
  154. */
  155. public function lte($x, $y)
  156. {
  157. return new Expr\Comparison($x, Expr\Comparison::LTE, $y);
  158. }
  159. /**
  160. * Creates an instance of Expr\Comparison, with the given arguments.
  161. * First argument is considered the left expression and the second is the right expression.
  162. * When converted to string, it will generated a <left expr> > <right expr>. Example:
  163. *
  164. * [php]
  165. * // u.id > ?1
  166. * $q->where($q->expr()->gt('u.id', '?1'));
  167. *
  168. * @param mixed $x Left expression
  169. * @param mixed $y Right expression
  170. * @return Expr\Comparison
  171. */
  172. public function gt($x, $y)
  173. {
  174. return new Expr\Comparison($x, Expr\Comparison::GT, $y);
  175. }
  176. /**
  177. * Creates an instance of Expr\Comparison, with the given arguments.
  178. * First argument is considered the left expression and the second is the right expression.
  179. * When converted to string, it will generated a <left expr> >= <right expr>. Example:
  180. *
  181. * [php]
  182. * // u.id >= ?1
  183. * $q->where($q->expr()->gte('u.id', '?1'));
  184. *
  185. * @param mixed $x Left expression
  186. * @param mixed $y Right expression
  187. * @return Expr\Comparison
  188. */
  189. public function gte($x, $y)
  190. {
  191. return new Expr\Comparison($x, Expr\Comparison::GTE, $y);
  192. }
  193. /**
  194. * Creates an instance of AVG() function, with the given argument.
  195. *
  196. * @param mixed $x Argument to be used in AVG() function.
  197. * @return Expr\Func
  198. */
  199. public function avg($x)
  200. {
  201. return new Expr\Func('AVG', array($x));
  202. }
  203. /**
  204. * Creates an instance of MAX() function, with the given argument.
  205. *
  206. * @param mixed $x Argument to be used in MAX() function.
  207. * @return Expr\Func
  208. */
  209. public function max($x)
  210. {
  211. return new Expr\Func('MAX', array($x));
  212. }
  213. /**
  214. * Creates an instance of MIN() function, with the given argument.
  215. *
  216. * @param mixed $x Argument to be used in MIN() function.
  217. * @return Expr\Func
  218. */
  219. public function min($x)
  220. {
  221. return new Expr\Func('MIN', array($x));
  222. }
  223. /**
  224. * Creates an instance of COUNT() function, with the given argument.
  225. *
  226. * @param mixed $x Argument to be used in COUNT() function.
  227. * @return Expr\Func
  228. */
  229. public function count($x)
  230. {
  231. return new Expr\Func('COUNT', array($x));
  232. }
  233. /**
  234. * Creates an instance of COUNT(DISTINCT) function, with the given argument.
  235. *
  236. * @param mixed $x Argument to be used in COUNT(DISTINCT) function.
  237. * @return string
  238. */
  239. public function countDistinct($x)
  240. {
  241. return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
  242. }
  243. /**
  244. * Creates an instance of EXISTS() function, with the given DQL Subquery.
  245. *
  246. * @param mixed $subquery DQL Subquery to be used in EXISTS() function.
  247. * @return Expr\Func
  248. */
  249. public function exists($subquery)
  250. {
  251. return new Expr\Func('EXISTS', array($subquery));
  252. }
  253. /**
  254. * Creates an instance of ALL() function, with the given DQL Subquery.
  255. *
  256. * @param mixed $subquery DQL Subquery to be used in ALL() function.
  257. * @return Expr\Func
  258. */
  259. public function all($subquery)
  260. {
  261. return new Expr\Func('ALL', array($subquery));
  262. }
  263. /**
  264. * Creates a SOME() function expression with the given DQL subquery.
  265. *
  266. * @param mixed $subquery DQL Subquery to be used in SOME() function.
  267. * @return Expr\Func
  268. */
  269. public function some($subquery)
  270. {
  271. return new Expr\Func('SOME', array($subquery));
  272. }
  273. /**
  274. * Creates an ANY() function expression with the given DQL subquery.
  275. *
  276. * @param mixed $subquery DQL Subquery to be used in ANY() function.
  277. * @return Expr\Func
  278. */
  279. public function any($subquery)
  280. {
  281. return new Expr\Func('ANY', array($subquery));
  282. }
  283. /**
  284. * Creates a negation expression of the given restriction.
  285. *
  286. * @param mixed $restriction Restriction to be used in NOT() function.
  287. * @return Expr\Func
  288. */
  289. public function not($restriction)
  290. {
  291. return new Expr\Func('NOT', array($restriction));
  292. }
  293. /**
  294. * Creates an ABS() function expression with the given argument.
  295. *
  296. * @param mixed $x Argument to be used in ABS() function.
  297. * @return Expr\Func
  298. */
  299. public function abs($x)
  300. {
  301. return new Expr\Func('ABS', array($x));
  302. }
  303. /**
  304. * Creates a product mathematical expression with the given arguments.
  305. *
  306. * First argument is considered the left expression and the second is the right expression.
  307. * When converted to string, it will generated a <left expr> * <right expr>. Example:
  308. *
  309. * [php]
  310. * // u.salary * u.percentAnualSalaryIncrease
  311. * $q->expr()->prod('u.salary', 'u.percentAnualSalaryIncrease')
  312. *
  313. * @param mixed $x Left expression
  314. * @param mixed $y Right expression
  315. * @return Expr\Math
  316. */
  317. public function prod($x, $y)
  318. {
  319. return new Expr\Math($x, '*', $y);
  320. }
  321. /**
  322. * Creates a difference mathematical expression with the given arguments.
  323. * First argument is considered the left expression and the second is the right expression.
  324. * When converted to string, it will generated a <left expr> - <right expr>. Example:
  325. *
  326. * [php]
  327. * // u.monthlySubscriptionCount - 1
  328. * $q->expr()->diff('u.monthlySubscriptionCount', '1')
  329. *
  330. * @param mixed $x Left expression
  331. * @param mixed $y Right expression
  332. * @return Expr\Math
  333. */
  334. public function diff($x, $y)
  335. {
  336. return new Expr\Math($x, '-', $y);
  337. }
  338. /**
  339. * Creates a sum mathematical expression with the given arguments.
  340. * First argument is considered the left expression and the second is the right expression.
  341. * When converted to string, it will generated a <left expr> + <right expr>. Example:
  342. *
  343. * [php]
  344. * // u.numChildren + 1
  345. * $q->expr()->diff('u.numChildren', '1')
  346. *
  347. * @param mixed $x Left expression
  348. * @param mixed $y Right expression
  349. * @return Expr\Math
  350. */
  351. public function sum($x, $y)
  352. {
  353. return new Expr\Math($x, '+', $y);
  354. }
  355. /**
  356. * Creates a quotient mathematical expression with the given arguments.
  357. * First argument is considered the left expression and the second is the right expression.
  358. * When converted to string, it will generated a <left expr> / <right expr>. Example:
  359. *
  360. * [php]
  361. * // u.total / u.period
  362. * $expr->quot('u.total', 'u.period')
  363. *
  364. * @param mixed $x Left expression
  365. * @param mixed $y Right expression
  366. * @return Expr\Math
  367. */
  368. public function quot($x, $y)
  369. {
  370. return new Expr\Math($x, '/', $y);
  371. }
  372. /**
  373. * Creates a SQRT() function expression with the given argument.
  374. *
  375. * @param mixed $x Argument to be used in SQRT() function.
  376. * @return Expr\Func
  377. */
  378. public function sqrt($x)
  379. {
  380. return new Expr\Func('SQRT', array($x));
  381. }
  382. /**
  383. * Creates an IN() expression with the given arguments.
  384. *
  385. * @param string $x Field in string format to be restricted by IN() function
  386. * @param mixed $y Argument to be used in IN() function.
  387. * @return Expr\Func
  388. */
  389. public function in($x, $y)
  390. {
  391. if (is_array($y)) {
  392. foreach ($y as &$literal) {
  393. if ( ! ($literal instanceof Expr\Literal)) {
  394. $literal = $this->_quoteLiteral($literal);
  395. }
  396. }
  397. }
  398. return new Expr\Func($x . ' IN', (array) $y);
  399. }
  400. /**
  401. * Creates a NOT IN() expression with the given arguments.
  402. *
  403. * @param string $x Field in string format to be restricted by NOT IN() function
  404. * @param mixed $y Argument to be used in NOT IN() function.
  405. * @return Expr\Func
  406. */
  407. public function notIn($x, $y)
  408. {
  409. if (is_array($y)) {
  410. foreach ($y as &$literal) {
  411. if ( ! ($literal instanceof Expr\Literal)) {
  412. $literal = $this->_quoteLiteral($literal);
  413. }
  414. }
  415. }
  416. return new Expr\Func($x . ' NOT IN', (array) $y);
  417. }
  418. /**
  419. * Creates an IS NULL expression with the given arguments.
  420. *
  421. * @param string $x Field in string format to be restricted by IS NULL
  422. * @return string
  423. */
  424. public function isNull($x)
  425. {
  426. return $x . ' IS NULL';
  427. }
  428. /**
  429. * Creates an IS NOT NULL expression with the given arguments.
  430. *
  431. * @param string $x Field in string format to be restricted by IS NOT NULL
  432. * @return string
  433. */
  434. public function isNotNull($x)
  435. {
  436. return $x . ' IS NOT NULL';
  437. }
  438. /**
  439. * Creates a LIKE() comparison expression with the given arguments.
  440. *
  441. * @param string $x Field in string format to be inspected by LIKE() comparison.
  442. * @param mixed $y Argument to be used in LIKE() comparison.
  443. * @return Expr\Comparison
  444. */
  445. public function like($x, $y)
  446. {
  447. return new Expr\Comparison($x, 'LIKE', $y);
  448. }
  449. /**
  450. * Creates a CONCAT() function expression with the given arguments.
  451. *
  452. * @param mixed $x First argument to be used in CONCAT() function.
  453. * @param mixed $x Second argument to be used in CONCAT() function.
  454. * @return Expr\Func
  455. */
  456. public function concat($x, $y)
  457. {
  458. return new Expr\Func('CONCAT', array($x, $y));
  459. }
  460. /**
  461. * Creates a SUBSTRING() function expression with the given arguments.
  462. *
  463. * @param mixed $x Argument to be used as string to be cropped by SUBSTRING() function.
  464. * @param integer $from Initial offset to start cropping string. May accept negative values.
  465. * @param integer $len Length of crop. May accept negative values.
  466. * @return Expr\Func
  467. */
  468. public function substring($x, $from, $len = null)
  469. {
  470. $args = array($x, $from);
  471. if (null !== $len) {
  472. $args[] = $len;
  473. }
  474. return new Expr\Func('SUBSTRING', $args);
  475. }
  476. /**
  477. * Creates a LOWER() function expression with the given argument.
  478. *
  479. * @param mixed $x Argument to be used in LOWER() function.
  480. * @return Expr\Func A LOWER function expression.
  481. */
  482. public function lower($x)
  483. {
  484. return new Expr\Func('LOWER', array($x));
  485. }
  486. /**
  487. * Creates an UPPER() function expression with the given argument.
  488. *
  489. * @param mixed $x Argument to be used in UPPER() function.
  490. * @return Expr\Func An UPPER function expression.
  491. */
  492. public function upper($x)
  493. {
  494. return new Expr\Func('UPPER', array($x));
  495. }
  496. /**
  497. * Creates a LENGTH() function expression with the given argument.
  498. *
  499. * @param mixed $x Argument to be used as argument of LENGTH() function.
  500. * @return Expr\Func A LENGTH function expression.
  501. */
  502. public function length($x)
  503. {
  504. return new Expr\Func('LENGTH', array($x));
  505. }
  506. /**
  507. * Creates a literal expression of the given argument.
  508. *
  509. * @param mixed $literal Argument to be converted to literal.
  510. * @return Expr\Literal
  511. */
  512. public function literal($literal)
  513. {
  514. return new Expr\Literal($this->_quoteLiteral($literal));
  515. }
  516. /**
  517. * Quotes a literal value, if necessary, according to the DQL syntax.
  518. *
  519. * @param mixed $literal The literal value.
  520. * @return string
  521. */
  522. private function _quoteLiteral($literal)
  523. {
  524. if (is_numeric($literal) && !is_string($literal)) {
  525. return (string) $literal;
  526. } else if (is_bool($literal)) {
  527. return $literal ? "true" : "false";
  528. } else {
  529. return "'" . str_replace("'", "''", $literal) . "'";
  530. }
  531. }
  532. /**
  533. * Creates an instance of BETWEEN() function, with the given argument.
  534. *
  535. * @param mixed $val Valued to be inspected by range values.
  536. * @param integer $x Starting range value to be used in BETWEEN() function.
  537. * @param integer $y End point value to be used in BETWEEN() function.
  538. * @return Expr\Func A BETWEEN expression.
  539. */
  540. public function between($val, $x, $y)
  541. {
  542. return $val . ' BETWEEN ' . $x . ' AND ' . $y;
  543. }
  544. /**
  545. * Creates an instance of TRIM() function, with the given argument.
  546. *
  547. * @param mixed $x Argument to be used as argument of TRIM() function.
  548. * @return Expr\Func a TRIM expression.
  549. */
  550. public function trim($x)
  551. {
  552. return new Expr\Func('TRIM', $x);
  553. }
  554. }