123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
-
- namespace Illuminate\Database;
-
- use Illuminate\Support\Traits\Macroable;
- use Illuminate\Database\Query\Expression;
-
- abstract class Grammar
- {
- use Macroable;
-
- /**
- * The grammar table prefix.
- *
- * @var string
- */
- protected $tablePrefix = '';
-
- /**
- * Wrap an array of values.
- *
- * @param array $values
- * @return array
- */
- public function wrapArray(array $values)
- {
- return array_map([$this, 'wrap'], $values);
- }
-
- /**
- * Wrap a table in keyword identifiers.
- *
- * @param \Illuminate\Database\Query\Expression|string $table
- * @return string
- */
- public function wrapTable($table)
- {
- if (! $this->isExpression($table)) {
- return $this->wrap($this->tablePrefix.$table, true);
- }
-
- return $this->getValue($table);
- }
-
- /**
- * Wrap a value in keyword identifiers.
- *
- * @param \Illuminate\Database\Query\Expression|string $value
- * @param bool $prefixAlias
- * @return string
- */
- public function wrap($value, $prefixAlias = false)
- {
- if ($this->isExpression($value)) {
- return $this->getValue($value);
- }
-
- // If the value being wrapped has a column alias we will need to separate out
- // the pieces so we can wrap each of the segments of the expression on its
- // own, and then join these both back together using the "as" connector.
- if (strpos(strtolower($value), ' as ') !== false) {
- return $this->wrapAliasedValue($value, $prefixAlias);
- }
-
- return $this->wrapSegments(explode('.', $value));
- }
-
- /**
- * Wrap a value that has an alias.
- *
- * @param string $value
- * @param bool $prefixAlias
- * @return string
- */
- protected function wrapAliasedValue($value, $prefixAlias = false)
- {
- $segments = preg_split('/\s+as\s+/i', $value);
-
- // If we are wrapping a table we need to prefix the alias with the table prefix
- // as well in order to generate proper syntax. If this is a column of course
- // no prefix is necessary. The condition will be true when from wrapTable.
- if ($prefixAlias) {
- $segments[1] = $this->tablePrefix.$segments[1];
- }
-
- return $this->wrap(
- $segments[0]).' as '.$this->wrapValue($segments[1]
- );
- }
-
- /**
- * Wrap the given value segments.
- *
- * @param array $segments
- * @return string
- */
- protected function wrapSegments($segments)
- {
- return collect($segments)->map(function ($segment, $key) use ($segments) {
- return $key == 0 && count($segments) > 1
- ? $this->wrapTable($segment)
- : $this->wrapValue($segment);
- })->implode('.');
- }
-
- /**
- * Wrap a single string in keyword identifiers.
- *
- * @param string $value
- * @return string
- */
- protected function wrapValue($value)
- {
- if ($value !== '*') {
- return '"'.str_replace('"', '""', $value).'"';
- }
-
- return $value;
- }
-
- /**
- * Convert an array of column names into a delimited string.
- *
- * @param array $columns
- * @return string
- */
- public function columnize(array $columns)
- {
- return implode(', ', array_map([$this, 'wrap'], $columns));
- }
-
- /**
- * Create query parameter place-holders for an array.
- *
- * @param array $values
- * @return string
- */
- public function parameterize(array $values)
- {
- return implode(', ', array_map([$this, 'parameter'], $values));
- }
-
- /**
- * Get the appropriate query parameter place-holder for a value.
- *
- * @param mixed $value
- * @return string
- */
- public function parameter($value)
- {
- return $this->isExpression($value) ? $this->getValue($value) : '?';
- }
-
- /**
- * Quote the given string literal.
- *
- * @param string|array $value
- * @return string
- */
- public function quoteString($value)
- {
- if (is_array($value)) {
- return implode(', ', array_map([$this, __FUNCTION__], $value));
- }
-
- return "'$value'";
- }
-
- /**
- * Determine if the given value is a raw expression.
- *
- * @param mixed $value
- * @return bool
- */
- public function isExpression($value)
- {
- return $value instanceof Expression;
- }
-
- /**
- * Get the value of a raw expression.
- *
- * @param \Illuminate\Database\Query\Expression $expression
- * @return string
- */
- public function getValue($expression)
- {
- return $expression->getValue();
- }
-
- /**
- * Get the format for database stored dates.
- *
- * @return string
- */
- public function getDateFormat()
- {
- return 'Y-m-d H:i:s';
- }
-
- /**
- * Get the grammar's table prefix.
- *
- * @return string
- */
- public function getTablePrefix()
- {
- return $this->tablePrefix;
- }
-
- /**
- * Set the grammar's table prefix.
- *
- * @param string $prefix
- * @return $this
- */
- public function setTablePrefix($prefix)
- {
- $this->tablePrefix = $prefix;
-
- return $this;
- }
- }
|