Grammar.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Support\Traits\Macroable;
  4. use Illuminate\Database\Query\Expression;
  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 (strpos(strtolower($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(
  74. $segments[0]).' as '.$this->wrapValue($segments[1]
  75. );
  76. }
  77. /**
  78. * Wrap the given value segments.
  79. *
  80. * @param array $segments
  81. * @return string
  82. */
  83. protected function wrapSegments($segments)
  84. {
  85. return collect($segments)->map(function ($segment, $key) use ($segments) {
  86. return $key == 0 && count($segments) > 1
  87. ? $this->wrapTable($segment)
  88. : $this->wrapValue($segment);
  89. })->implode('.');
  90. }
  91. /**
  92. * Wrap a single string in keyword identifiers.
  93. *
  94. * @param string $value
  95. * @return string
  96. */
  97. protected function wrapValue($value)
  98. {
  99. if ($value !== '*') {
  100. return '"'.str_replace('"', '""', $value).'"';
  101. }
  102. return $value;
  103. }
  104. /**
  105. * Convert an array of column names into a delimited string.
  106. *
  107. * @param array $columns
  108. * @return string
  109. */
  110. public function columnize(array $columns)
  111. {
  112. return implode(', ', array_map([$this, 'wrap'], $columns));
  113. }
  114. /**
  115. * Create query parameter place-holders for an array.
  116. *
  117. * @param array $values
  118. * @return string
  119. */
  120. public function parameterize(array $values)
  121. {
  122. return implode(', ', array_map([$this, 'parameter'], $values));
  123. }
  124. /**
  125. * Get the appropriate query parameter place-holder for a value.
  126. *
  127. * @param mixed $value
  128. * @return string
  129. */
  130. public function parameter($value)
  131. {
  132. return $this->isExpression($value) ? $this->getValue($value) : '?';
  133. }
  134. /**
  135. * Quote the given string literal.
  136. *
  137. * @param string|array $value
  138. * @return string
  139. */
  140. public function quoteString($value)
  141. {
  142. if (is_array($value)) {
  143. return implode(', ', array_map([$this, __FUNCTION__], $value));
  144. }
  145. return "'$value'";
  146. }
  147. /**
  148. * Determine if the given value is a raw expression.
  149. *
  150. * @param mixed $value
  151. * @return bool
  152. */
  153. public function isExpression($value)
  154. {
  155. return $value instanceof Expression;
  156. }
  157. /**
  158. * Get the value of a raw expression.
  159. *
  160. * @param \Illuminate\Database\Query\Expression $expression
  161. * @return string
  162. */
  163. public function getValue($expression)
  164. {
  165. return $expression->getValue();
  166. }
  167. /**
  168. * Get the format for database stored dates.
  169. *
  170. * @return string
  171. */
  172. public function getDateFormat()
  173. {
  174. return 'Y-m-d H:i:s';
  175. }
  176. /**
  177. * Get the grammar's table prefix.
  178. *
  179. * @return string
  180. */
  181. public function getTablePrefix()
  182. {
  183. return $this->tablePrefix;
  184. }
  185. /**
  186. * Set the grammar's table prefix.
  187. *
  188. * @param string $prefix
  189. * @return $this
  190. */
  191. public function setTablePrefix($prefix)
  192. {
  193. $this->tablePrefix = $prefix;
  194. return $this;
  195. }
  196. }