PageRenderTime 32ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/joomla/libraries/joomla/database/query/postgresql.php

https://gitlab.com/ricardosanchez/prueba
PHP | 641 lines | 295 code | 75 blank | 271 comment | 28 complexity | 616c2d2b5c2ce02107b605a4a636b693 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Database
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Query Building Class.
  12. *
  13. * @since 11.3
  14. */
  15. class JDatabaseQueryPostgresql extends JDatabaseQuery implements JDatabaseQueryLimitable
  16. {
  17. /**
  18. * @var object The FOR UPDATE element used in "FOR UPDATE" lock
  19. * @since 11.3
  20. */
  21. protected $forUpdate = null;
  22. /**
  23. * @var object The FOR SHARE element used in "FOR SHARE" lock
  24. * @since 11.3
  25. */
  26. protected $forShare = null;
  27. /**
  28. * @var object The NOWAIT element used in "FOR SHARE" and "FOR UPDATE" lock
  29. * @since 11.3
  30. */
  31. protected $noWait = null;
  32. /**
  33. * @var object The LIMIT element
  34. * @since 11.3
  35. */
  36. protected $limit = null;
  37. /**
  38. * @var object The OFFSET element
  39. * @since 11.3
  40. */
  41. protected $offset = null;
  42. /**
  43. * @var object The RETURNING element of INSERT INTO
  44. * @since 11.3
  45. */
  46. protected $returning = null;
  47. /**
  48. * Magic function to convert the query to a string, only for postgresql specific query
  49. *
  50. * @return string The completed query.
  51. *
  52. * @since 11.3
  53. */
  54. public function __toString()
  55. {
  56. $query = '';
  57. switch ($this->type)
  58. {
  59. case 'select':
  60. $query .= (string) $this->select;
  61. $query .= (string) $this->from;
  62. if ($this->join)
  63. {
  64. // Special case for joins
  65. foreach ($this->join as $join)
  66. {
  67. $query .= (string) $join;
  68. }
  69. }
  70. if ($this->where)
  71. {
  72. $query .= (string) $this->where;
  73. }
  74. if ($this->group)
  75. {
  76. $query .= (string) $this->group;
  77. }
  78. if ($this->having)
  79. {
  80. $query .= (string) $this->having;
  81. }
  82. if ($this->order)
  83. {
  84. $query .= (string) $this->order;
  85. }
  86. if ($this->forUpdate)
  87. {
  88. $query .= (string) $this->forUpdate;
  89. }
  90. else
  91. {
  92. if ($this->forShare)
  93. {
  94. $query .= (string) $this->forShare;
  95. }
  96. }
  97. if ($this->noWait)
  98. {
  99. $query .= (string) $this->noWait;
  100. }
  101. break;
  102. case 'update':
  103. $query .= (string) $this->update;
  104. $query .= (string) $this->set;
  105. if ($this->join)
  106. {
  107. $onWord = ' ON ';
  108. // Workaround for special case of JOIN with UPDATE
  109. foreach ($this->join as $join)
  110. {
  111. $joinElem = $join->getElements();
  112. $joinArray = explode($onWord, $joinElem[0]);
  113. $this->from($joinArray[0]);
  114. $this->where($joinArray[1]);
  115. }
  116. $query .= (string) $this->from;
  117. }
  118. if ($this->where)
  119. {
  120. $query .= (string) $this->where;
  121. }
  122. break;
  123. case 'insert':
  124. $query .= (string) $this->insert;
  125. if ($this->values)
  126. {
  127. if ($this->columns)
  128. {
  129. $query .= (string) $this->columns;
  130. }
  131. $elements = $this->values->getElements();
  132. if (!($elements[0] instanceof $this))
  133. {
  134. $query .= ' VALUES ';
  135. }
  136. $query .= (string) $this->values;
  137. if ($this->returning)
  138. {
  139. $query .= (string) $this->returning;
  140. }
  141. }
  142. break;
  143. default:
  144. $query = parent::__toString();
  145. break;
  146. }
  147. if ($this instanceof JDatabaseQueryLimitable)
  148. {
  149. $query = $this->processLimit($query, $this->limit, $this->offset);
  150. }
  151. return $query;
  152. }
  153. /**
  154. * Clear data from the query or a specific clause of the query.
  155. *
  156. * @param string $clause Optionally, the name of the clause to clear, or nothing to clear the whole query.
  157. *
  158. * @return JDatabaseQueryPostgresql Returns this object to allow chaining.
  159. *
  160. * @since 11.3
  161. */
  162. public function clear($clause = null)
  163. {
  164. switch ($clause)
  165. {
  166. case 'limit':
  167. $this->limit = null;
  168. break;
  169. case 'offset':
  170. $this->offset = null;
  171. break;
  172. case 'forUpdate':
  173. $this->forUpdate = null;
  174. break;
  175. case 'forShare':
  176. $this->forShare = null;
  177. break;
  178. case 'noWait':
  179. $this->noWait = null;
  180. break;
  181. case 'returning':
  182. $this->returning = null;
  183. break;
  184. case 'select':
  185. case 'update':
  186. case 'delete':
  187. case 'insert':
  188. case 'from':
  189. case 'join':
  190. case 'set':
  191. case 'where':
  192. case 'group':
  193. case 'having':
  194. case 'order':
  195. case 'columns':
  196. case 'values':
  197. parent::clear($clause);
  198. break;
  199. default:
  200. $this->type = null;
  201. $this->limit = null;
  202. $this->offset = null;
  203. $this->forUpdate = null;
  204. $this->forShare = null;
  205. $this->noWait = null;
  206. $this->returning = null;
  207. parent::clear($clause);
  208. break;
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Casts a value to a char.
  214. *
  215. * Ensure that the value is properly quoted before passing to the method.
  216. *
  217. * Usage:
  218. * $query->select($query->castAsChar('a'));
  219. *
  220. * @param string $value The value to cast as a char.
  221. *
  222. * @return string Returns the cast value.
  223. *
  224. * @since 11.3
  225. */
  226. public function castAsChar($value)
  227. {
  228. return $value . '::text';
  229. }
  230. /**
  231. * Concatenates an array of column names or values.
  232. *
  233. * Usage:
  234. * $query->select($query->concatenate(array('a', 'b')));
  235. *
  236. * @param array $values An array of values to concatenate.
  237. * @param string $separator As separator to place between each value.
  238. *
  239. * @return string The concatenated values.
  240. *
  241. * @since 11.3
  242. */
  243. public function concatenate($values, $separator = null)
  244. {
  245. if ($separator)
  246. {
  247. return implode(' || ' . $this->quote($separator) . ' || ', $values);
  248. }
  249. else
  250. {
  251. return implode(' || ', $values);
  252. }
  253. }
  254. /**
  255. * Gets the current date and time.
  256. *
  257. * @return string Return string used in query to obtain
  258. *
  259. * @since 11.3
  260. */
  261. public function currentTimestamp()
  262. {
  263. return 'NOW()';
  264. }
  265. /**
  266. * Sets the FOR UPDATE lock on select's output row
  267. *
  268. * @param string $table_name The table to lock
  269. * @param string $glue The glue by which to join the conditions. Defaults to ',' .
  270. *
  271. * @return JDatabaseQueryPostgresql FOR UPDATE query element
  272. *
  273. * @since 11.3
  274. */
  275. public function forUpdate($table_name, $glue = ',')
  276. {
  277. $this->type = 'forUpdate';
  278. if (is_null($this->forUpdate))
  279. {
  280. $glue = strtoupper($glue);
  281. $this->forUpdate = new JDatabaseQueryElement('FOR UPDATE', 'OF ' . $table_name, "$glue ");
  282. }
  283. else
  284. {
  285. $this->forUpdate->append($table_name);
  286. }
  287. return $this;
  288. }
  289. /**
  290. * Sets the FOR SHARE lock on select's output row
  291. *
  292. * @param string $table_name The table to lock
  293. * @param string $glue The glue by which to join the conditions. Defaults to ',' .
  294. *
  295. * @return JDatabaseQueryPostgresql FOR SHARE query element
  296. *
  297. * @since 11.3
  298. */
  299. public function forShare($table_name, $glue = ',')
  300. {
  301. $this->type = 'forShare';
  302. if (is_null($this->forShare))
  303. {
  304. $glue = strtoupper($glue);
  305. $this->forShare = new JDatabaseQueryElement('FOR SHARE', 'OF ' . $table_name, "$glue ");
  306. }
  307. else
  308. {
  309. $this->forShare->append($table_name);
  310. }
  311. return $this;
  312. }
  313. /**
  314. * Used to get a string to extract year from date column.
  315. *
  316. * Usage:
  317. * $query->select($query->year($query->quoteName('dateColumn')));
  318. *
  319. * @param string $date Date column containing year to be extracted.
  320. *
  321. * @return string Returns string to extract year from a date.
  322. *
  323. * @since 12.1
  324. */
  325. public function year($date)
  326. {
  327. return 'EXTRACT (YEAR FROM ' . $date . ')';
  328. }
  329. /**
  330. * Used to get a string to extract month from date column.
  331. *
  332. * Usage:
  333. * $query->select($query->month($query->quoteName('dateColumn')));
  334. *
  335. * @param string $date Date column containing month to be extracted.
  336. *
  337. * @return string Returns string to extract month from a date.
  338. *
  339. * @since 12.1
  340. */
  341. public function month($date)
  342. {
  343. return 'EXTRACT (MONTH FROM ' . $date . ')';
  344. }
  345. /**
  346. * Used to get a string to extract day from date column.
  347. *
  348. * Usage:
  349. * $query->select($query->day($query->quoteName('dateColumn')));
  350. *
  351. * @param string $date Date column containing day to be extracted.
  352. *
  353. * @return string Returns string to extract day from a date.
  354. *
  355. * @since 12.1
  356. */
  357. public function day($date)
  358. {
  359. return 'EXTRACT (DAY FROM ' . $date . ')';
  360. }
  361. /**
  362. * Used to get a string to extract hour from date column.
  363. *
  364. * Usage:
  365. * $query->select($query->hour($query->quoteName('dateColumn')));
  366. *
  367. * @param string $date Date column containing hour to be extracted.
  368. *
  369. * @return string Returns string to extract hour from a date.
  370. *
  371. * @since 12.1
  372. */
  373. public function hour($date)
  374. {
  375. return 'EXTRACT (HOUR FROM ' . $date . ')';
  376. }
  377. /**
  378. * Used to get a string to extract minute from date column.
  379. *
  380. * Usage:
  381. * $query->select($query->minute($query->quoteName('dateColumn')));
  382. *
  383. * @param string $date Date column containing minute to be extracted.
  384. *
  385. * @return string Returns string to extract minute from a date.
  386. *
  387. * @since 12.1
  388. */
  389. public function minute($date)
  390. {
  391. return 'EXTRACT (MINUTE FROM ' . $date . ')';
  392. }
  393. /**
  394. * Used to get a string to extract seconds from date column.
  395. *
  396. * Usage:
  397. * $query->select($query->second($query->quoteName('dateColumn')));
  398. *
  399. * @param string $date Date column containing second to be extracted.
  400. *
  401. * @return string Returns string to extract second from a date.
  402. *
  403. * @since 12.1
  404. */
  405. public function second($date)
  406. {
  407. return 'EXTRACT (SECOND FROM ' . $date . ')';
  408. }
  409. /**
  410. * Sets the NOWAIT lock on select's output row
  411. *
  412. * @return JDatabaseQueryPostgresql NO WAIT query element
  413. *
  414. * @since 11.3
  415. */
  416. public function noWait ()
  417. {
  418. $this->type = 'noWait';
  419. if (is_null($this->noWait))
  420. {
  421. $this->noWait = new JDatabaseQueryElement('NOWAIT', null);
  422. }
  423. return $this;
  424. }
  425. /**
  426. * Set the LIMIT clause to the query
  427. *
  428. * @param integer $limit An int of how many row will be returned
  429. *
  430. * @return JDatabaseQueryPostgresql Returns this object to allow chaining.
  431. *
  432. * @since 11.3
  433. */
  434. public function limit($limit = 0)
  435. {
  436. if (is_null($this->limit))
  437. {
  438. $this->limit = new JDatabaseQueryElement('LIMIT', (int) $limit);
  439. }
  440. return $this;
  441. }
  442. /**
  443. * Set the OFFSET clause to the query
  444. *
  445. * @param integer $offset An int for skipping row
  446. *
  447. * @return JDatabaseQueryPostgresql Returns this object to allow chaining.
  448. *
  449. * @since 11.3
  450. */
  451. public function offset($offset = 0)
  452. {
  453. if (is_null($this->offset))
  454. {
  455. $this->offset = new JDatabaseQueryElement('OFFSET', (int) $offset);
  456. }
  457. return $this;
  458. }
  459. /**
  460. * Add the RETURNING element to INSERT INTO statement.
  461. *
  462. * @param mixed $pkCol The name of the primary key column.
  463. *
  464. * @return JDatabaseQueryPostgresql Returns this object to allow chaining.
  465. *
  466. * @since 11.3
  467. */
  468. public function returning($pkCol)
  469. {
  470. if (is_null($this->returning))
  471. {
  472. $this->returning = new JDatabaseQueryElement('RETURNING', $pkCol);
  473. }
  474. return $this;
  475. }
  476. /**
  477. * Sets the offset and limit for the result set, if the database driver supports it.
  478. *
  479. * Usage:
  480. * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
  481. * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
  482. *
  483. * @param integer $limit The limit for the result set
  484. * @param integer $offset The offset for the result set
  485. *
  486. * @return JDatabaseQueryPostgresql Returns this object to allow chaining.
  487. *
  488. * @since 12.1
  489. */
  490. public function setLimit($limit = 0, $offset = 0)
  491. {
  492. $this->limit = (int) $limit;
  493. $this->offset = (int) $offset;
  494. return $this;
  495. }
  496. /**
  497. * Method to modify a query already in string format with the needed
  498. * additions to make the query limited to a particular number of
  499. * results, or start at a particular offset.
  500. *
  501. * @param string $query The query in string format
  502. * @param integer $limit The limit for the result set
  503. * @param integer $offset The offset for the result set
  504. *
  505. * @return string
  506. *
  507. * @since 12.1
  508. */
  509. public function processLimit($query, $limit, $offset = 0)
  510. {
  511. if ($limit > 0)
  512. {
  513. $query .= ' LIMIT ' . $limit;
  514. }
  515. if ($offset > 0)
  516. {
  517. $query .= ' OFFSET ' . $offset;
  518. }
  519. return $query;
  520. }
  521. /**
  522. * Add to the current date and time in Postgresql.
  523. * Usage:
  524. * $query->select($query->dateAdd());
  525. * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  526. *
  527. * @param datetime $date The date to add to
  528. * @param string $interval The string representation of the appropriate number of units
  529. * @param string $datePart The part of the date to perform the addition on
  530. *
  531. * @return string The string with the appropriate sql for addition of dates
  532. *
  533. * @since 13.1
  534. * @note Not all drivers support all units. Check appropriate references
  535. * @link http://www.postgresql.org/docs/9.0/static/functions-datetime.html.
  536. */
  537. public function dateAdd($date, $interval, $datePart)
  538. {
  539. if (substr($interval, 0, 1) != '-')
  540. {
  541. return "timestamp '" . $date . "' + interval '" . $interval . " " . $datePart . "'";
  542. }
  543. else
  544. {
  545. return "timestamp '" . $date . "' - interval '" . ltrim($interval, '-') . " " . $datePart . "'";
  546. }
  547. }
  548. /**
  549. * Return correct regexp operator for Postgresql.
  550. *
  551. * Ensure that the regexp operator is Postgresql compatible.
  552. *
  553. * Usage:
  554. * $query->where('field ' . $query->regexp($search));
  555. *
  556. * @param string $value The regex pattern.
  557. *
  558. * @return string Returns the regex operator.
  559. *
  560. * @since 11.3
  561. */
  562. public function regexp($value)
  563. {
  564. return ' ~* ' . $value;
  565. }
  566. }