PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/rokcommon/Doctrine/Expression/Driver.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 647 lines | 239 code | 51 blank | 357 comment | 10 complexity | 36918fddee2364aa6ec9b8b8ad5bcacb MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Driver.php 48519 2012-02-03 23:18:52Z btowles $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. /**
  22. * Doctrine_Expression_Driver
  23. *
  24. * @package Doctrine
  25. * @subpackage Expression
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.org
  28. * @since 1.0
  29. * @version $Revision$
  30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  31. */
  32. class Doctrine_Expression_Driver extends Doctrine_Connection_Module
  33. {
  34. public function getIdentifier($column)
  35. {
  36. return $column;
  37. }
  38. public function getIdentifiers($columns)
  39. {
  40. return $columns;
  41. }
  42. /**
  43. * regexp
  44. * returns the regular expression operator
  45. *
  46. * @return string
  47. */
  48. public function regexp()
  49. {
  50. throw new Doctrine_Expression_Exception('Regular expression operator is not supported by this database driver.');
  51. }
  52. /**
  53. * Returns the average value of a column
  54. *
  55. * @param string $column the column to use
  56. * @return string generated sql including an AVG aggregate function
  57. */
  58. public function avg($column)
  59. {
  60. $column = $this->getIdentifier($column);
  61. return 'AVG(' . $column . ')';
  62. }
  63. /**
  64. * Returns the number of rows (without a NULL value) of a column
  65. *
  66. * If a '*' is used instead of a column the number of selected rows
  67. * is returned.
  68. *
  69. * @param string|integer $column the column to use
  70. * @return string generated sql including a COUNT aggregate function
  71. */
  72. public function count($column)
  73. {
  74. $column = $this->getIdentifier($column);
  75. return 'COUNT(' . $column . ')';
  76. }
  77. /**
  78. * Returns the highest value of a column
  79. *
  80. * @param string $column the column to use
  81. * @return string generated sql including a MAX aggregate function
  82. */
  83. public function max($column)
  84. {
  85. $column = $this->getIdentifier($column);
  86. return 'MAX(' . $column . ')';
  87. }
  88. /**
  89. * Returns the lowest value of a column
  90. *
  91. * @param string $column the column to use
  92. * @return string
  93. */
  94. public function min($column)
  95. {
  96. $column = $this->getIdentifier($column);
  97. return 'MIN(' . $column . ')';
  98. }
  99. /**
  100. * Returns the total sum of a column
  101. *
  102. * @param string $column the column to use
  103. * @return string
  104. */
  105. public function sum($column)
  106. {
  107. $column = $this->getIdentifier($column);
  108. return 'SUM(' . $column . ')';
  109. }
  110. // scalar functions
  111. /**
  112. * Returns the md5 sum of a field.
  113. *
  114. * Note: Not SQL92, but common functionality
  115. *
  116. * @return string
  117. */
  118. public function md5($column)
  119. {
  120. $column = $this->getIdentifier($column);
  121. return 'MD5(' . $column . ')';
  122. }
  123. /**
  124. * Returns the length of a text field.
  125. *
  126. * @param string $expression1
  127. * @param string $expression2
  128. * @return string
  129. */
  130. public function length($column)
  131. {
  132. $column = $this->getIdentifier($column);
  133. return 'LENGTH(' . $column . ')';
  134. }
  135. /**
  136. * Rounds a numeric field to the number of decimals specified.
  137. *
  138. * @param string $expression1
  139. * @param string $expression2
  140. * @return string
  141. */
  142. public function round($column, $decimals = 0)
  143. {
  144. $column = $this->getIdentifier($column);
  145. return 'ROUND(' . $column . ', ' . $decimals . ')';
  146. }
  147. /**
  148. * Returns the remainder of the division operation
  149. * $expression1 / $expression2.
  150. *
  151. * @param string $expression1
  152. * @param string $expression2
  153. * @return string
  154. */
  155. public function mod($expression1, $expression2)
  156. {
  157. $expression1 = $this->getIdentifier($expression1);
  158. $expression2 = $this->getIdentifier($expression2);
  159. return 'MOD(' . $expression1 . ', ' . $expression2 . ')';
  160. }
  161. /**
  162. * trim
  163. * returns the string $str with leading and proceeding space characters removed
  164. *
  165. * @param string $str literal string or column name
  166. * @return string
  167. */
  168. public function trim($str)
  169. {
  170. return 'TRIM(' . $str . ')';
  171. }
  172. /**
  173. * rtrim
  174. * returns the string $str with proceeding space characters removed
  175. *
  176. * @param string $str literal string or column name
  177. * @return string
  178. */
  179. public function rtrim($str)
  180. {
  181. return 'RTRIM(' . $str . ')';
  182. }
  183. /**
  184. * ltrim
  185. * returns the string $str with leading space characters removed
  186. *
  187. * @param string $str literal string or column name
  188. * @return string
  189. */
  190. public function ltrim($str)
  191. {
  192. return 'LTRIM(' . $str . ')';
  193. }
  194. /**
  195. * upper
  196. * Returns the string $str with all characters changed to
  197. * uppercase according to the current character set mapping.
  198. *
  199. * @param string $str literal string or column name
  200. * @return string
  201. */
  202. public function upper($str)
  203. {
  204. return 'UPPER(' . $str . ')';
  205. }
  206. /**
  207. * lower
  208. * Returns the string $str with all characters changed to
  209. * lowercase according to the current character set mapping.
  210. *
  211. * @param string $str literal string or column name
  212. * @return string
  213. */
  214. public function lower($str)
  215. {
  216. return 'LOWER(' . $str . ')';
  217. }
  218. /**
  219. * locate
  220. * returns the position of the first occurrence of substring $substr in string $str
  221. *
  222. * @param string $substr literal string to find
  223. * @param string $str literal string
  224. * @return integer
  225. */
  226. public function locate($str, $substr)
  227. {
  228. return 'LOCATE(' . $str . ', ' . $substr . ')';
  229. }
  230. /**
  231. * Returns the current system date.
  232. *
  233. * @return string
  234. */
  235. public function now()
  236. {
  237. return 'NOW()';
  238. }
  239. /**
  240. * soundex
  241. * Returns a string to call a function to compute the
  242. * soundex encoding of a string
  243. *
  244. * The string "?000" is returned if the argument is NULL.
  245. *
  246. * @param string $value
  247. * @return string SQL soundex function with given parameter
  248. */
  249. public function soundex($value)
  250. {
  251. throw new Doctrine_Expression_Exception('SQL soundex function not supported by this driver.');
  252. }
  253. /**
  254. * return string to call a function to get a substring inside an SQL statement
  255. *
  256. * Note: Not SQL92, but common functionality.
  257. *
  258. * SQLite only supports the 2 parameter variant of this function
  259. *
  260. * @param string $value an sql string literal or column name/alias
  261. * @param integer $position where to start the substring portion
  262. * @param integer $length the substring portion length
  263. * @return string SQL substring function with given parameters
  264. */
  265. public function substring($value, $from, $len = null)
  266. {
  267. $value = $this->getIdentifier($value);
  268. if ($len === null)
  269. return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
  270. else {
  271. $len = $this->getIdentifier($len);
  272. return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $len . ')';
  273. }
  274. }
  275. /**
  276. * Returns a series of strings concatinated
  277. *
  278. * concat() accepts an arbitrary number of parameters. Each parameter
  279. * must contain an expression or an array with expressions.
  280. *
  281. * @param string|array(string) strings that will be concatinated.
  282. */
  283. public function concat()
  284. {
  285. $args = func_get_args();
  286. return 'CONCAT(' . join(', ', (array) $args) . ')';
  287. }
  288. /**
  289. * Returns the SQL for a logical not.
  290. *
  291. * @return string a logical expression
  292. */
  293. public function not($expression)
  294. {
  295. $expression = $this->getIdentifier($expression);
  296. return 'NOT(' . $expression . ')';
  297. }
  298. /**
  299. * Returns the SQL to perform the same mathematical operation over an array
  300. * of values or expressions.
  301. *
  302. * basicMath() accepts an arbitrary number of parameters. Each parameter
  303. * must contain a value or an expression or an array with values or
  304. * expressions.
  305. *
  306. * @param string $type the type of operation, can be '+', '-', '*' or '/'.
  307. * @param string|array(string)
  308. * @return string an expression
  309. */
  310. private function basicMath($type, array $args)
  311. {
  312. $elements = $this->getIdentifiers($args);
  313. if (count($elements) < 1) {
  314. return '';
  315. }
  316. if (count($elements) == 1) {
  317. return $elements[0];
  318. } else {
  319. return '(' . implode(' ' . $type . ' ', $elements) . ')';
  320. }
  321. }
  322. /**
  323. * Returns the SQL to add values or expressions together.
  324. *
  325. * add() accepts an arbitrary number of parameters. Each parameter
  326. * must contain a value or an expression or an array with values or
  327. * expressions.
  328. *
  329. * @param string|array(string)
  330. * @return string an expression
  331. */
  332. public function add(array $args)
  333. {
  334. return $this->basicMath('+', $args);
  335. }
  336. /**
  337. * Returns the SQL to subtract values or expressions from eachother.
  338. *
  339. * subtract() accepts an arbitrary number of parameters. Each parameter
  340. * must contain a value or an expression or an array with values or
  341. * expressions.
  342. *
  343. * @param string|array(string)
  344. * @return string an expression
  345. */
  346. public function sub(array $args)
  347. {
  348. return $this->basicMath('-', $args );
  349. }
  350. /**
  351. * Returns the SQL to multiply values or expressions by eachother.
  352. *
  353. * multiply() accepts an arbitrary number of parameters. Each parameter
  354. * must contain a value or an expression or an array with values or
  355. * expressions.
  356. *
  357. * @param string|array(string)
  358. * @return string an expression
  359. */
  360. public function mul(array $args)
  361. {
  362. return $this->basicMath('*', $args);
  363. }
  364. /**
  365. * Returns the SQL to divide values or expressions by eachother.
  366. *
  367. * divide() accepts an arbitrary number of parameters. Each parameter
  368. * must contain a value or an expression or an array with values or
  369. * expressions.
  370. *
  371. * @param string|array(string)
  372. * @return string an expression
  373. */
  374. public function div(array $args)
  375. {
  376. return $this->basicMath('/', $args);
  377. }
  378. /**
  379. * Returns the SQL to check if two values are equal.
  380. *
  381. * @param string $value1 logical expression to compare
  382. * @param string $value2 logical expression to compare with
  383. * @return string logical expression
  384. */
  385. public function eq($value1, $value2)
  386. {
  387. $value1 = $this->getIdentifier($value1);
  388. $value2 = $this->getIdentifier($value2);
  389. return $value1 . ' = ' . $value2;
  390. }
  391. /**
  392. * Returns the SQL to check if two values are unequal.
  393. *
  394. * @param string $value1 logical expression to compare
  395. * @param string $value2 logical expression to compare with
  396. * @return string logical expression
  397. */
  398. public function neq($value1, $value2)
  399. {
  400. $value1 = $this->getIdentifier($value1);
  401. $value2 = $this->getIdentifier($value2);
  402. return $value1 . ' <> ' . $value2;
  403. }
  404. /**
  405. * Returns the SQL to check if one value is greater than another value.
  406. *
  407. * @param string $value1 logical expression to compare
  408. * @param string $value2 logical expression to compare with
  409. * @return string logical expression
  410. */
  411. public function gt($value1, $value2)
  412. {
  413. $value1 = $this->getIdentifier($value1);
  414. $value2 = $this->getIdentifier($value2);
  415. return $value1 . ' > ' . $value2;
  416. }
  417. /**
  418. * Returns the SQL to check if one value is greater than or equal to
  419. * another value.
  420. *
  421. * @param string $value1 logical expression to compare
  422. * @param string $value2 logical expression to compare with
  423. * @return string logical expression
  424. */
  425. public function gte($value1, $value2)
  426. {
  427. $value1 = $this->getIdentifier($value1);
  428. $value2 = $this->getIdentifier($value2);
  429. return $value1 . ' >= ' . $value2;
  430. }
  431. /**
  432. * Returns the SQL to check if one value is less than another value.
  433. *
  434. * @param string $value1 logical expression to compare
  435. * @param string $value2 logical expression to compare with
  436. * @return string logical expression
  437. */
  438. public function lt($value1, $value2)
  439. {
  440. $value1 = $this->getIdentifier($value1);
  441. $value2 = $this->getIdentifier($value2);
  442. return $value1 . ' < ' . $value2;
  443. }
  444. /**
  445. * Returns the SQL to check if one value is less than or equal to
  446. * another value.
  447. *
  448. * @param string $value1 logical expression to compare
  449. * @param string $value2 logical expression to compare with
  450. * @return string logical expression
  451. */
  452. public function lte($value1, $value2)
  453. {
  454. $value1 = $this->getIdentifier($value1);
  455. $value2 = $this->getIdentifier($value2);
  456. return $value1 . ' <= ' . $value2;
  457. }
  458. /**
  459. * Returns the SQL to check if a value is one in a set of
  460. * given values..
  461. *
  462. * in() accepts an arbitrary number of parameters. The first parameter
  463. * must always specify the value that should be matched against. Successive
  464. * must contain a logical expression or an array with logical expressions.
  465. * These expressions will be matched against the first parameter.
  466. *
  467. * @param string $column the value that should be matched against
  468. * @param string|array(string) values that will be matched against $column
  469. * @return string logical expression
  470. */
  471. public function in($column, $values)
  472. {
  473. if ( ! is_array($values)) {
  474. $values = array($values);
  475. }
  476. $values = $this->getIdentifiers($values);
  477. $column = $this->getIdentifier($column);
  478. if (count($values) == 0) {
  479. throw new Doctrine_Expression_Exception('Values array for IN operator should not be empty.');
  480. }
  481. return $column . ' IN (' . implode(', ', $values) . ')';
  482. }
  483. /**
  484. * Returns SQL that checks if a expression is null.
  485. *
  486. * @param string $expression the expression that should be compared to null
  487. * @return string logical expression
  488. */
  489. public function isNull($expression)
  490. {
  491. $expression = $this->getIdentifier($expression);
  492. return $expression . ' IS NULL';
  493. }
  494. /**
  495. * Returns SQL that checks if a expression is not null.
  496. *
  497. * @param string $expression the expression that should be compared to null
  498. * @return string logical expression
  499. */
  500. public function isNotNull($expression)
  501. {
  502. $expression = $this->getIdentifier($expression);
  503. return $expression . ' IS NOT NULL';
  504. }
  505. /**
  506. * Returns SQL that checks if an expression evaluates to a value between
  507. * two values.
  508. *
  509. * The parameter $expression is checked if it is between $value1 and $value2.
  510. *
  511. * Note: There is a slight difference in the way BETWEEN works on some databases.
  512. * http://www.w3schools.com/sql/sql_between.asp. If you want complete database
  513. * independence you should avoid using between().
  514. *
  515. * @param string $expression the value to compare to
  516. * @param string $value1 the lower value to compare with
  517. * @param string $value2 the higher value to compare with
  518. * @return string logical expression
  519. */
  520. public function between($expression, $value1, $value2)
  521. {
  522. $expression = $this->getIdentifier($expression);
  523. $value1 = $this->getIdentifier($value1);
  524. $value2 = $this->getIdentifier($value2);
  525. return $expression . ' BETWEEN ' .$value1 . ' AND ' . $value2;
  526. }
  527. /**
  528. * Returns global unique identifier
  529. *
  530. * @return string to get global unique identifier
  531. */
  532. public function guid()
  533. {
  534. throw new Doctrine_Expression_Exception('method not implemented');
  535. }
  536. /**
  537. * returns arcus cosine SQL string
  538. *
  539. * @return string
  540. */
  541. public function acos($value)
  542. {
  543. return 'ACOS(' . $value . ')';
  544. }
  545. /**
  546. * sin
  547. *
  548. * @param string $value
  549. * @return void
  550. */
  551. public function sin($value)
  552. {
  553. return 'SIN(' . $value . ')';
  554. }
  555. /**
  556. * pi
  557. *
  558. * @return void
  559. */
  560. public function pi()
  561. {
  562. return 'PI()';
  563. }
  564. /**
  565. * cos
  566. *
  567. * @param string $value
  568. * @return void
  569. */
  570. public function cos($value)
  571. {
  572. return 'COS(' . $value . ')';
  573. }
  574. /**
  575. * coalesce
  576. *
  577. * @return string
  578. */
  579. public function coalesce()
  580. {
  581. $args = func_get_args();
  582. return 'COALESCE(' . join(', ', (array) $args) . ')';
  583. }
  584. /**
  585. * __call
  586. *
  587. * for all native RDBMS functions the function name itself is returned
  588. */
  589. public function __call($m, $a)
  590. {
  591. if ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_EXPR) {
  592. throw new Doctrine_Expression_Exception('Unknown expression: ' . $m);
  593. }
  594. return $m . '(' . implode(', ', $a) . ')';
  595. }
  596. }