Grammar.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Database\Query\Expression;
  4. abstract class Grammar
  5. {
  6. /**
  7. * The grammar table prefix.
  8. *
  9. * @var string
  10. */
  11. protected $tablePrefix = '';
  12. /**
  13. * Wrap an array of values.
  14. *
  15. * @param array $values
  16. * @return array
  17. */
  18. public function wrapArray(array $values)
  19. {
  20. return array_map([$this, 'wrap'], $values);
  21. }
  22. /**
  23. * Wrap a table in keyword identifiers.
  24. *
  25. * @param \Illuminate\Database\Query\Expression|string $table
  26. * @return string
  27. */
  28. public function wrapTable($table)
  29. {
  30. if (! $this->isExpression($table)) {
  31. return $this->wrap($this->tablePrefix.$table, true);
  32. }
  33. return $this->getValue($table);
  34. }
  35. /**
  36. * Wrap a value in keyword identifiers.
  37. *
  38. * @param \Illuminate\Database\Query\Expression|string $value
  39. * @param bool $prefixAlias
  40. * @return string
  41. */
  42. public function wrap($value, $prefixAlias = false)
  43. {
  44. if ($this->isExpression($value)) {
  45. return $this->getValue($value);
  46. }
  47. // If the value being wrapped has a column alias we will need to separate out
  48. // the pieces so we can wrap each of the segments of the expression on it
  49. // own, and then joins them both back together with the "as" connector.
  50. if (strpos(strtolower($value), ' as ') !== false) {
  51. return $this->wrapAliasedValue($value, $prefixAlias);
  52. }
  53. return $this->wrapSegments(explode('.', $value));
  54. }
  55. /**
  56. * Wrap a value that has an alias.
  57. *
  58. * @param string $value
  59. * @param bool $prefixAlias
  60. * @return string
  61. */
  62. protected function wrapAliasedValue($value, $prefixAlias = false)
  63. {
  64. $segments = preg_split('/\s+as\s+/i', $value);
  65. // If we are wrapping a table we need to prefix the alias with the table prefix
  66. // as well in order to generate proper syntax. If this is a column of course
  67. // no prefix is necessary. The condition will be true when from wrapTable.
  68. if ($prefixAlias) {
  69. $segments[1] = $this->tablePrefix.$segments[1];
  70. }
  71. return $this->wrap(
  72. $segments[0]).' as '.$this->wrapValue($segments[1]
  73. );
  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. * Determine if the given value is a raw expression.
  134. *
  135. * @param mixed $value
  136. * @return bool
  137. */
  138. public function isExpression($value)
  139. {
  140. return $value instanceof Expression;
  141. }
  142. /**
  143. * Get the value of a raw expression.
  144. *
  145. * @param \Illuminate\Database\Query\Expression $expression
  146. * @return string
  147. */
  148. public function getValue($expression)
  149. {
  150. return $expression->getValue();
  151. }
  152. /**
  153. * Get the format for database stored dates.
  154. *
  155. * @return string
  156. */
  157. public function getDateFormat()
  158. {
  159. return 'Y-m-d H:i:s';
  160. }
  161. /**
  162. * Get the grammar's table prefix.
  163. *
  164. * @return string
  165. */
  166. public function getTablePrefix()
  167. {
  168. return $this->tablePrefix;
  169. }
  170. /**
  171. * Set the grammar's table prefix.
  172. *
  173. * @param string $prefix
  174. * @return $this
  175. */
  176. public function setTablePrefix($prefix)
  177. {
  178. $this->tablePrefix = $prefix;
  179. return $this;
  180. }
  181. }