Grammar.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database;
  12. use Hyperf\Database\Query\Expression;
  13. use Hyperf\Macroable\Macroable;
  14. use function Hyperf\Collection\collect;
  15. abstract class Grammar
  16. {
  17. use Macroable;
  18. /**
  19. * The grammar table prefix.
  20. */
  21. protected string $tablePrefix = '';
  22. /**
  23. * Wrap an array of values.
  24. */
  25. public function wrapArray(array $values): array
  26. {
  27. return array_map([$this, 'wrap'], $values);
  28. }
  29. /**
  30. * Wrap a table in keyword identifiers.
  31. *
  32. * @param Expression|string $table
  33. * @return string
  34. */
  35. public function wrapTable($table)
  36. {
  37. if (! $this->isExpression($table)) {
  38. return $this->wrap($this->tablePrefix . $table, true);
  39. }
  40. return $this->getValue($table);
  41. }
  42. /**
  43. * Wrap a value in keyword identifiers.
  44. *
  45. * @param bool $prefixAlias
  46. * @return string
  47. */
  48. public function wrap(Expression|string $value, $prefixAlias = false)
  49. {
  50. if ($this->isExpression($value)) {
  51. return $this->getValue($value);
  52. }
  53. // If the value being wrapped has a column alias we will need to separate out
  54. // the pieces so we can wrap each of the segments of the expression on its
  55. // own, and then join these both back together using the "as" connector.
  56. if (stripos($value, ' as ') !== false) {
  57. return $this->wrapAliasedValue($value, $prefixAlias);
  58. }
  59. return $this->wrapSegments(explode('.', $value));
  60. }
  61. /**
  62. * Convert an array of column names into a delimited string.
  63. */
  64. public function columnize(array $columns): string
  65. {
  66. return implode(', ', array_map([$this, 'wrap'], $columns));
  67. }
  68. /**
  69. * Create query parameter place-holders for an array.
  70. */
  71. public function parameterize(array $values): string
  72. {
  73. return implode(', ', array_map([$this, 'parameter'], $values));
  74. }
  75. /**
  76. * Get the appropriate query parameter place-holder for a value.
  77. *
  78. * @return string
  79. */
  80. public function parameter(mixed $value)
  81. {
  82. return $this->isExpression($value) ? $this->getValue($value) : '?';
  83. }
  84. /**
  85. * Quote the given string literal.
  86. *
  87. * @param array|string $value
  88. */
  89. public function quoteString($value): string
  90. {
  91. if (is_array($value)) {
  92. return implode(', ', array_map([$this, __FUNCTION__], $value));
  93. }
  94. return "'{$value}'";
  95. }
  96. /**
  97. * Determine if the given value is a raw expression.
  98. */
  99. public function isExpression(mixed $value): bool
  100. {
  101. return $value instanceof Expression;
  102. }
  103. /**
  104. * Get the value of a raw expression.
  105. *
  106. * @return string
  107. */
  108. public function getValue(Expression $expression)
  109. {
  110. return $expression->getValue();
  111. }
  112. /**
  113. * Get the format for database stored dates.
  114. */
  115. public function getDateFormat(): string
  116. {
  117. return 'Y-m-d H:i:s';
  118. }
  119. /**
  120. * Get the grammar's table prefix.
  121. */
  122. public function getTablePrefix(): string
  123. {
  124. return $this->tablePrefix;
  125. }
  126. /**
  127. * Set the grammar's table prefix.
  128. *
  129. * @return $this
  130. */
  131. public function setTablePrefix(string $prefix): static
  132. {
  133. $this->tablePrefix = $prefix;
  134. return $this;
  135. }
  136. /**
  137. * Wrap a value that has an alias.
  138. *
  139. * @param string $value
  140. * @param bool $prefixAlias
  141. * @return string
  142. */
  143. protected function wrapAliasedValue($value, $prefixAlias = false)
  144. {
  145. $segments = preg_split('/\s+as\s+/i', $value);
  146. // If we are wrapping a table we need to prefix the alias with the table prefix
  147. // as well in order to generate proper syntax. If this is a column of course
  148. // no prefix is necessary. The condition will be true when from wrapTable.
  149. if ($prefixAlias) {
  150. $segments[1] = $this->tablePrefix . $segments[1];
  151. }
  152. return $this->wrap(
  153. $segments[0]
  154. ) . ' as ' . $this->wrapValue(
  155. $segments[1]
  156. );
  157. }
  158. /**
  159. * Wrap the given value segments.
  160. *
  161. * @param array $segments
  162. */
  163. protected function wrapSegments($segments): string
  164. {
  165. return collect($segments)->map(function ($segment, $key) use ($segments) {
  166. return $key == 0 && count($segments) > 1
  167. ? $this->wrapTable($segment)
  168. : $this->wrapValue($segment);
  169. })->implode('.');
  170. }
  171. /**
  172. * Wrap a single string in keyword identifiers.
  173. *
  174. * @param string $value
  175. */
  176. protected function wrapValue($value): string
  177. {
  178. if ($value !== '*') {
  179. return '"' . str_replace('"', '""', $value) . '"';
  180. }
  181. return $value;
  182. }
  183. }