Grammar.php 5.2 KB

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