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