PostgresDialectTrait.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\OrderByExpression;
  20. use Cake\Database\Expression\UnaryExpression;
  21. use Cake\Database\Query;
  22. use Cake\Database\SqlDialectTrait;
  23. /**
  24. * Contains functions that encapsulates the SQL dialect used by Postgres,
  25. * including query translators and schema introspection.
  26. */
  27. trait PostgresDialectTrait {
  28. use SqlDialectTrait;
  29. /**
  30. * String used to start a database identifier quoting to make it safe
  31. *
  32. * @var string
  33. */
  34. protected $_startQuote = '"';
  35. /**
  36. * String used to end a database identifier quoting to make it safe
  37. *
  38. * @var string
  39. */
  40. protected $_endQuote = '"';
  41. /**
  42. * Returns a query that has been transformed to the specific SQL dialect
  43. * by changing or re-arranging SQL clauses as required.
  44. *
  45. * @param Cake\Database\Query $query
  46. * @return Cake\Database\Query
  47. */
  48. protected function _selectQueryTranslator($query) {
  49. return $query;
  50. }
  51. /**
  52. * Returns an dictionary of expressions to be transformed when compiling a Query
  53. * to SQL. Array keys are method names to be called in this class
  54. *
  55. * @return array
  56. */
  57. protected function _expressionTranslators() {
  58. $namespace = 'Cake\Database\Expression';
  59. return [
  60. $namespace . '\FunctionExpression' => '_transformFunctionExpression'
  61. ];
  62. }
  63. /**
  64. * Receives a FunctionExpression and changes it so that it conforms to this
  65. * SQL dialect.
  66. *
  67. * @param Cake\Database\Expression\FunctionExpression
  68. * @return void
  69. */
  70. protected function _transformFunctionExpression(FunctionExpression $expression) {
  71. switch ($expression->name()) {
  72. case 'CONCAT':
  73. // CONCAT function is expressed as exp1 || exp2
  74. $expression->name('')->type(' ||');
  75. break;
  76. case 'DATEDIFF':
  77. $expression
  78. ->name('')
  79. ->type('-')
  80. ->iterateParts(function($p) {
  81. return new FunctionExpression('DATE', [$p['value']], [$p['type']]);
  82. });
  83. break;
  84. case 'CURRENT_DATE':
  85. $time = new FunctionExpression('LOCALTIMESTAMP', [' 0 ' => 'literal']);
  86. $expression->name('CAST')->type(' AS ')->add([$time, 'date' => 'literal']);
  87. break;
  88. case 'CURRENT_TIME':
  89. $time = new FunctionExpression('LOCALTIMESTAMP', [' 0 ' => 'literal']);
  90. $expression->name('CAST')->type(' AS ')->add([$time, 'time' => 'literal']);
  91. break;
  92. case 'NOW':
  93. $expression->name('LOCALTIMESTAMP')->add([' 0 ' => 'literal']);
  94. break;
  95. }
  96. }
  97. /**
  98. * Get the schema dialect.
  99. *
  100. * Used by Cake\Schema package to reflect schema and
  101. * generate schema.
  102. *
  103. * @return Cake\Database\Schema\PostgresSchema
  104. */
  105. public function schemaDialect() {
  106. return new \Cake\Database\Schema\PostgresSchema($this);
  107. }
  108. }