PostgresDialectTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database\Dialect;
  18. use Cake\Database\Expression\FunctionExpression;
  19. use Cake\Database\Expression\UnaryExpression;
  20. use Cake\Database\Query;
  21. use Cake\Database\SqlDialectTrait;
  22. /**
  23. * Contains functions that encapsulates the SQL dialect used by Postgres,
  24. * including query translators and schema introspection.
  25. */
  26. trait PostgresDialectTrait {
  27. use SqlDialectTrait;
  28. /**
  29. * String used to start a database identifier quoting to make it safe
  30. *
  31. * @var string
  32. */
  33. protected $_startQuote = '"';
  34. /**
  35. * String used to end a database identifier quoting to make it safe
  36. *
  37. * @var string
  38. */
  39. protected $_endQuote = '"';
  40. /**
  41. * Distinct clause needs no transformation
  42. *
  43. * @param Query $query The query to be transformed
  44. * @return Query
  45. */
  46. protected function _transformDistinct($query) {
  47. return $query;
  48. }
  49. /**
  50. * Modifies the original insert query to append a "RETURNING *" epilogue
  51. * so that the latest insert id can be retrieved
  52. *
  53. * @param Cake\Database\Query $query
  54. * @return Cake\Database\Query
  55. */
  56. protected function _insertQueryTranslator($query) {
  57. if (!$query->clause('epilog')) {
  58. $query->epilog('RETURNING *');
  59. }
  60. return $query;
  61. }
  62. /**
  63. * Returns an dictionary of expressions to be transformed when compiling a Query
  64. * to SQL. Array keys are method names to be called in this class
  65. *
  66. * @return array
  67. */
  68. protected function _expressionTranslators() {
  69. $namespace = 'Cake\Database\Expression';
  70. return [
  71. $namespace . '\FunctionExpression' => '_transformFunctionExpression'
  72. ];
  73. }
  74. /**
  75. * Receives a FunctionExpression and changes it so that it conforms to this
  76. * SQL dialect.
  77. *
  78. * @param Cake\Database\Expression\FunctionExpression
  79. * @return void
  80. */
  81. protected function _transformFunctionExpression(FunctionExpression $expression) {
  82. switch ($expression->name()) {
  83. case 'CONCAT':
  84. // CONCAT function is expressed as exp1 || exp2
  85. $expression->name('')->type(' ||');
  86. break;
  87. case 'DATEDIFF':
  88. $expression
  89. ->name('')
  90. ->type('-')
  91. ->iterateParts(function($p) {
  92. return new FunctionExpression('DATE', [$p['value']], [$p['type']]);
  93. });
  94. break;
  95. case 'CURRENT_DATE':
  96. $time = new FunctionExpression('LOCALTIMESTAMP', [' 0 ' => 'literal']);
  97. $expression->name('CAST')->type(' AS ')->add([$time, 'date' => 'literal']);
  98. break;
  99. case 'CURRENT_TIME':
  100. $time = new FunctionExpression('LOCALTIMESTAMP', [' 0 ' => 'literal']);
  101. $expression->name('CAST')->type(' AS ')->add([$time, 'time' => 'literal']);
  102. break;
  103. case 'NOW':
  104. $expression->name('LOCALTIMESTAMP')->add([' 0 ' => 'literal']);
  105. break;
  106. }
  107. }
  108. /**
  109. * Get the schema dialect.
  110. *
  111. * Used by Cake\Schema package to reflect schema and
  112. * generate schema.
  113. *
  114. * @return Cake\Database\Schema\PostgresSchema
  115. */
  116. public function schemaDialect() {
  117. return new \Cake\Database\Schema\PostgresSchema($this);
  118. }
  119. }