PostgresCompiler.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.0.3
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Database;
  17. use Cake\Database\Expression\FunctionExpression;
  18. /**
  19. * Responsible for compiling a Query object into its SQL representation
  20. * for Postgres
  21. *
  22. * @internal
  23. */
  24. class PostgresCompiler extends QueryCompiler
  25. {
  26. /**
  27. * Always quote aliases in SELECT clause.
  28. *
  29. * Postgres auto converts unquoted identifiers to lower case.
  30. *
  31. * @var bool
  32. */
  33. protected bool $_quotedSelectAliases = true;
  34. /**
  35. * {@inheritDoc}
  36. *
  37. * @var array<string, string>
  38. */
  39. protected array $_templates = [
  40. 'delete' => 'DELETE',
  41. 'where' => ' WHERE %s',
  42. 'group' => ' GROUP BY %s',
  43. 'order' => ' %s',
  44. 'limit' => ' LIMIT %s',
  45. 'offset' => ' OFFSET %s',
  46. 'epilog' => ' %s',
  47. 'comment' => '/* %s */ ',
  48. ];
  49. /**
  50. * Helper function used to build the string representation of a HAVING clause,
  51. * it constructs the field list taking care of aliasing and
  52. * converting expression objects to string.
  53. *
  54. * @param array $parts list of fields to be transformed to string
  55. * @param \Cake\Database\Query $query The query that is being compiled
  56. * @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
  57. * @return string
  58. */
  59. protected function _buildHavingPart(array $parts, Query $query, ValueBinder $binder): string
  60. {
  61. $selectParts = $query->clause('select');
  62. foreach ($selectParts as $selectKey => $selectPart) {
  63. if (!$selectPart instanceof FunctionExpression) {
  64. continue;
  65. }
  66. foreach ($parts as $k => $p) {
  67. if (!is_string($p)) {
  68. continue;
  69. }
  70. preg_match_all(
  71. '/\b' . trim($selectKey, '"') . '\b/i',
  72. $p,
  73. $matches
  74. );
  75. if (empty($matches[0])) {
  76. continue;
  77. }
  78. $parts[$k] = preg_replace(
  79. ['/"/', '/\b' . trim($selectKey, '"') . '\b/i'],
  80. ['', $selectPart->sql($binder)],
  81. $p
  82. );
  83. }
  84. }
  85. return sprintf(' HAVING %s', implode(', ', $parts));
  86. }
  87. }