Grammar.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Contracts\Database\Query\Expression;
  4. use Illuminate\Support\Traits\Macroable;
  5. use RuntimeException;
  6. abstract class Grammar
  7. {
  8. use Macroable;
  9. /**
  10. * The connection used for escaping values.
  11. *
  12. * @var \Illuminate\Database\Connection
  13. */
  14. protected $connection;
  15. /**
  16. * The grammar table prefix.
  17. *
  18. * @var string
  19. */
  20. protected $tablePrefix = '';
  21. /**
  22. * Wrap an array of values.
  23. *
  24. * @param array $values
  25. * @return array
  26. */
  27. public function wrapArray(array $values)
  28. {
  29. return array_map([$this, 'wrap'], $values);
  30. }
  31. /**
  32. * Wrap a table in keyword identifiers.
  33. *
  34. * @param \Illuminate\Contracts\Database\Query\Expression|string $table
  35. * @return string
  36. */
  37. public function wrapTable($table)
  38. {
  39. if ($this->isExpression($table)) {
  40. return $this->getValue($table);
  41. }
  42. // If the table being wrapped has an alias we'll need to separate the pieces
  43. // so we can prefix the table and then wrap each of the segments on their
  44. // own and then join these both back together using the "as" connector.
  45. if (stripos($table, ' as ') !== false) {
  46. return $this->wrapAliasedTable($table);
  47. }
  48. // If the table being wrapped has a custom schema name specified, we need to
  49. // prefix the last segment as the table name then wrap each segment alone
  50. // and eventually join them both back together using the dot connector.
  51. if (str_contains($table, '.')) {
  52. $table = substr_replace($table, '.'.$this->tablePrefix, strrpos($table, '.'), 1);
  53. return collect(explode('.', $table))
  54. ->map($this->wrapValue(...))
  55. ->implode('.');
  56. }
  57. return $this->wrapValue($this->tablePrefix.$table);
  58. }
  59. /**
  60. * Wrap a value in keyword identifiers.
  61. *
  62. * @param \Illuminate\Contracts\Database\Query\Expression|string $value
  63. * @return string
  64. */
  65. public function wrap($value)
  66. {
  67. if ($this->isExpression($value)) {
  68. return $this->getValue($value);
  69. }
  70. // If the value being wrapped has a column alias we will need to separate out
  71. // the pieces so we can wrap each of the segments of the expression on its
  72. // own, and then join these both back together using the "as" connector.
  73. if (stripos($value, ' as ') !== false) {
  74. return $this->wrapAliasedValue($value);
  75. }
  76. // If the given value is a JSON selector we will wrap it differently than a
  77. // traditional value. We will need to split this path and wrap each part
  78. // wrapped, etc. Otherwise, we will simply wrap the value as a string.
  79. if ($this->isJsonSelector($value)) {
  80. return $this->wrapJsonSelector($value);
  81. }
  82. return $this->wrapSegments(explode('.', $value));
  83. }
  84. /**
  85. * Wrap a value that has an alias.
  86. *
  87. * @param string $value
  88. * @return string
  89. */
  90. protected function wrapAliasedValue($value)
  91. {
  92. $segments = preg_split('/\s+as\s+/i', $value);
  93. return $this->wrap($segments[0]).' as '.$this->wrapValue($segments[1]);
  94. }
  95. /**
  96. * Wrap a table that has an alias.
  97. *
  98. * @param string $value
  99. * @return string
  100. */
  101. protected function wrapAliasedTable($value)
  102. {
  103. $segments = preg_split('/\s+as\s+/i', $value);
  104. return $this->wrapTable($segments[0]).' as '.$this->wrapValue($this->tablePrefix.$segments[1]);
  105. }
  106. /**
  107. * Wrap the given value segments.
  108. *
  109. * @param array $segments
  110. * @return string
  111. */
  112. protected function wrapSegments($segments)
  113. {
  114. return collect($segments)->map(function ($segment, $key) use ($segments) {
  115. return $key == 0 && count($segments) > 1
  116. ? $this->wrapTable($segment)
  117. : $this->wrapValue($segment);
  118. })->implode('.');
  119. }
  120. /**
  121. * Wrap a single string in keyword identifiers.
  122. *
  123. * @param string $value
  124. * @return string
  125. */
  126. protected function wrapValue($value)
  127. {
  128. if ($value !== '*') {
  129. return '"'.str_replace('"', '""', $value).'"';
  130. }
  131. return $value;
  132. }
  133. /**
  134. * Wrap the given JSON selector.
  135. *
  136. * @param string $value
  137. * @return string
  138. *
  139. * @throws \RuntimeException
  140. */
  141. protected function wrapJsonSelector($value)
  142. {
  143. throw new RuntimeException('This database engine does not support JSON operations.');
  144. }
  145. /**
  146. * Determine if the given string is a JSON selector.
  147. *
  148. * @param string $value
  149. * @return bool
  150. */
  151. protected function isJsonSelector($value)
  152. {
  153. return str_contains($value, '->');
  154. }
  155. /**
  156. * Convert an array of column names into a delimited string.
  157. *
  158. * @param array $columns
  159. * @return string
  160. */
  161. public function columnize(array $columns)
  162. {
  163. return implode(', ', array_map([$this, 'wrap'], $columns));
  164. }
  165. /**
  166. * Create query parameter place-holders for an array.
  167. *
  168. * @param array $values
  169. * @return string
  170. */
  171. public function parameterize(array $values)
  172. {
  173. return implode(', ', array_map([$this, 'parameter'], $values));
  174. }
  175. /**
  176. * Get the appropriate query parameter place-holder for a value.
  177. *
  178. * @param mixed $value
  179. * @return string
  180. */
  181. public function parameter($value)
  182. {
  183. return $this->isExpression($value) ? $this->getValue($value) : '?';
  184. }
  185. /**
  186. * Quote the given string literal.
  187. *
  188. * @param string|array $value
  189. * @return string
  190. */
  191. public function quoteString($value)
  192. {
  193. if (is_array($value)) {
  194. return implode(', ', array_map([$this, __FUNCTION__], $value));
  195. }
  196. return "'$value'";
  197. }
  198. /**
  199. * Escapes a value for safe SQL embedding.
  200. *
  201. * @param string|float|int|bool|null $value
  202. * @param bool $binary
  203. * @return string
  204. */
  205. public function escape($value, $binary = false)
  206. {
  207. if (is_null($this->connection)) {
  208. throw new RuntimeException("The database driver's grammar implementation does not support escaping values.");
  209. }
  210. return $this->connection->escape($value, $binary);
  211. }
  212. /**
  213. * Determine if the given value is a raw expression.
  214. *
  215. * @param mixed $value
  216. * @return bool
  217. */
  218. public function isExpression($value)
  219. {
  220. return $value instanceof Expression;
  221. }
  222. /**
  223. * Transforms expressions to their scalar types.
  224. *
  225. * @param \Illuminate\Contracts\Database\Query\Expression|string|int|float $expression
  226. * @return string|int|float
  227. */
  228. public function getValue($expression)
  229. {
  230. if ($this->isExpression($expression)) {
  231. return $this->getValue($expression->getValue($this));
  232. }
  233. return $expression;
  234. }
  235. /**
  236. * Get the format for database stored dates.
  237. *
  238. * @return string
  239. */
  240. public function getDateFormat()
  241. {
  242. return 'Y-m-d H:i:s';
  243. }
  244. /**
  245. * Get the grammar's table prefix.
  246. *
  247. * @return string
  248. */
  249. public function getTablePrefix()
  250. {
  251. return $this->tablePrefix;
  252. }
  253. /**
  254. * Set the grammar's table prefix.
  255. *
  256. * @param string $prefix
  257. * @return $this
  258. */
  259. public function setTablePrefix($prefix)
  260. {
  261. $this->tablePrefix = $prefix;
  262. return $this;
  263. }
  264. /**
  265. * Set the grammar's database connection.
  266. *
  267. * @param \Illuminate\Database\Connection $connection
  268. * @return $this
  269. */
  270. public function setConnection($connection)
  271. {
  272. $this->connection = $connection;
  273. return $this;
  274. }
  275. }