← All changes
|
vendor/wpfluent/framework/src/WPFluent/Database/BaseGrammar.php
+85
-41
1.0.91
→
2.11.0
View file →
| @@ -1,9 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\Framework\Database; |
| 4 | 4 | |
| 5 | +use RuntimeException; | |
| 5 | 6 | use FluentCommunity\Framework\Support\Helper; |
| 7 | +use FluentCommunity\Framework\Database\Schema; | |
| 6 | 8 | use FluentCommunity\Framework\Support\MacroableTrait; |
| 7 | 9 | use FluentCommunity\Framework\Database\Query\Expression; |
| 8 | 10 | |
| 9 | 11 | abstract class BaseGrammar |
| @@ -10,11 +12,18 @@ | ||
| 10 | 12 | { |
| 11 | 13 | use MacroableTrait; |
| 12 | 14 | |
| 13 | 15 | /** |
| 16 | + * Cache of aliased table names. | |
| 17 | + * | |
| 18 | + * @var array | |
| 19 | + */ | |
| 20 | + protected $alias = []; | |
| 21 | + | |
| 22 | + /** | |
| 14 | 23 | * The connection used for escaping values. |
| 15 | 24 | * |
| 16 | - * @var \FluentCommunity\Framework\Database\Connection | |
| 25 | + * @var \FluentCommunity\Framework\Database\ConnectionInterface | |
| 17 | 26 | */ |
| 18 | 27 | protected $connection; |
| 19 | 28 | |
| 20 | 29 | /** |
| @@ -31,8 +40,19 @@ | ||
| 31 | 40 | */ |
| 32 | 41 | protected $tablePrefix = ''; |
| 33 | 42 | |
| 34 | 43 | /** |
| 44 | + * Check if the given table is a built-in table. | |
| 45 | + * | |
| 46 | + * @param string $table | |
| 47 | + * @return boolean | |
| 48 | + */ | |
| 49 | + public function isAliasedTable($table) | |
| 50 | + { | |
| 51 | + return in_array("`$table`", $this->alias); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 35 | 55 | * Wrap an array of values. |
| 36 | 56 | * |
| 37 | 57 | * @param array $values |
| 38 | 58 | * @return array |
| @@ -42,8 +62,37 @@ | ||
| 42 | 62 | return array_map([$this, 'wrap'], $values); |
| 43 | 63 | } |
| 44 | 64 | |
| 45 | 65 | /** |
| 66 | + * Wrap a value in keyword identifiers. | |
| 67 | + * | |
| 68 | + * @param \FluentCommunity\Framework\Database\Query\Expression|string $value | |
| 69 | + * @return string | |
| 70 | + */ | |
| 71 | + public function wrap($value) | |
| 72 | + { | |
| 73 | + if ($this->isExpression($value)) { | |
| 74 | + return $this->getValue($value); | |
| 75 | + } | |
| 76 | + | |
| 77 | + // 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 | |
| 78 | + // expression on its own, and then join these both | |
| 79 | + // back together using the "as" connector. | |
| 80 | + if (stripos($value, ' as ') !== false) { | |
| 81 | + return $this->wrapAliasedValue($value); | |
| 82 | + } | |
| 83 | + | |
| 84 | + // If the given value is a JSON selector we will wrap it differently than a | |
| 85 | + // traditional value. We will need to split this path and wrap each part | |
| 86 | + // wrapped, etc. Otherwise, we will simply wrap the value as a string. | |
| 87 | + if ($this->isJsonSelector($value)) { | |
| 88 | + return $this->wrapJsonSelector($value); | |
| 89 | + } | |
| 90 | + | |
| 91 | + return $this->wrapSegments(explode('.', $value)); | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 46 | 95 | * Wrap a table in keyword identifiers. |
| 47 | 96 | * |
| 48 | 97 | * @param \FluentCommunity\Framework\Database\Query\Expression|string $table |
| 49 | 98 | * @return string |
| @@ -67,9 +116,9 @@ | ||
| 67 | 116 | // prefix the last segment as the table name then wrap each segment alone |
| 68 | 117 | // and eventually join them both back together using the dot connector. |
| 69 | 118 | if (str_contains($table, '.')) { |
| 70 | 119 | $table = substr_replace( |
| 71 | - $table, '.'.$tablePrefix, strrpos($table, '.'), 1 | |
| 120 | + $table, '.' . $tablePrefix, strrpos($table, '.'), 1 | |
| 72 | 121 | ); |
| 73 | 122 | |
| 74 | 123 | return Helper::collect(explode('.', $table)) |
| 75 | 124 | ->map(function($value) { |
| @@ -77,8 +126,12 @@ | ||
| 77 | 126 | }) |
| 78 | 127 | ->implode('.'); |
| 79 | 128 | } |
| 80 | 129 | |
| 130 | + if ($this->isAliasedTable($table)) { | |
| 131 | + return $table; | |
| 132 | + } | |
| 133 | + | |
| 81 | 134 | return $this->wrapValue($tablePrefix.$table); |
| 82 | 135 | } |
| 83 | 136 | |
| 84 | 137 | /** |
| @@ -88,8 +141,12 @@ | ||
| 88 | 141 | * @return string |
| 89 | 142 | */ |
| 90 | 143 | protected function resolveTablePrefix($table) |
| 91 | 144 | { |
| 145 | + if ($this->isAliasedTable($table)) { | |
| 146 | + return ''; | |
| 147 | + } | |
| 148 | + | |
| 92 | 149 | if (!is_multisite()) { |
| 93 | 150 | return $this->tablePrefix; |
| 94 | 151 | } |
| 95 | 152 | |
| @@ -109,37 +166,8 @@ | ||
| 109 | 166 | ) ? $this->basePrefix : $this->tablePrefix; |
| 110 | 167 | } |
| 111 | 168 | |
| 112 | 169 | /** |
| 113 | - * Wrap a value in keyword identifiers. | |
| 114 | - * | |
| 115 | - * @param \FluentCommunity\Framework\Database\Query\Expression|string $value | |
| 116 | - * @return string | |
| 117 | - */ | |
| 118 | - public function wrap($value) | |
| 119 | - { | |
| 120 | - if ($this->isExpression($value)) { | |
| 121 | - return $this->getValue($value); | |
| 122 | - } | |
| 123 | - | |
| 124 | - // If the value being wrapped has a column alias we will need to separate out | |
| 125 | - // the pieces so we can wrap each of the segments of the expression on its | |
| 126 | - // own, and then join these both back together using the "as" connector. | |
| 127 | - if (stripos($value, ' as ') !== false) { | |
| 128 | - return $this->wrapAliasedValue($value); | |
| 129 | - } | |
| 130 | - | |
| 131 | - // If the given value is a JSON selector we will wrap it differently than a | |
| 132 | - // traditional value. We will need to split this path and wrap each part | |
| 133 | - // wrapped, etc. Otherwise, we will simply wrap the value as a string. | |
| 134 | - if ($this->isJsonSelector($value)) { | |
| 135 | - return $this->wrapJsonSelector($value); | |
| 136 | - } | |
| 137 | - | |
| 138 | - return $this->wrapSegments(explode('.', $value)); | |
| 139 | - } | |
| 140 | - | |
| 141 | - /** | |
| 142 | 170 | * Wrap a value that has an alias. |
| 143 | 171 | * |
| 144 | 172 | * @param string $value |
| 145 | 173 | * @return string |
| @@ -147,9 +175,11 @@ | ||
| 147 | 175 | protected function wrapAliasedValue($value) |
| 148 | 176 | { |
| 149 | 177 | $segments = preg_split('/\s+as\s+/i', $value); |
| 150 | 178 | |
| 151 | - return $this->wrap($segments[0]).' as '.$this->wrapValue($segments[1]); | |
| 179 | + return $this->wrap( | |
| 180 | + $segments[0] | |
| 181 | + ) . ' as ' . $this->wrapValue($segments[1]); | |
| 152 | 182 | } |
| 153 | 183 | |
| 154 | 184 | /** |
| 155 | 185 | * Wrap a table that has an alias. |
| @@ -160,13 +190,11 @@ | ||
| 160 | 190 | protected function wrapAliasedTable($value) |
| 161 | 191 | { |
| 162 | 192 | $segments = preg_split('/\s+as\s+/i', $value); |
| 163 | 193 | |
| 164 | - $tablePrefix = $this->resolveTablePrefix($segments[0]); | |
| 194 | + $this->alias[] = $alias = $this->wrapValue($segments[1]); | |
| 165 | 195 | |
| 166 | - return $this->wrapTable( | |
| 167 | - $tablePrefix.$segments[0] | |
| 168 | - ).' as '.$this->wrapValue($segments[1]); | |
| 196 | + return $this->wrapTable($segments[0]) . ' as ' . $alias; | |
| 169 | 197 | } |
| 170 | 198 | |
| 171 | 199 | /** |
| 172 | 200 | * Wrap the given value segments. |
| @@ -175,9 +203,11 @@ | ||
| 175 | 203 | * @return string |
| 176 | 204 | */ |
| 177 | 205 | protected function wrapSegments($segments) |
| 178 | 206 | { |
| 179 | - return Helper::collect($segments)->map(function ($segment, $key) use ($segments) { | |
| 207 | + return Helper::collect($segments)->map(function ( | |
| 208 | + $segment, $key | |
| 209 | + ) use ($segments) { | |
| 180 | 210 | return $key == 0 && count($segments) > 1 |
| 181 | 211 | ? $this->wrapTable($segment) |
| 182 | 212 | : $this->wrapValue($segment); |
| 183 | 213 | })->implode('.'); |
| @@ -207,9 +237,11 @@ | ||
| 207 | 237 | * @throws \RuntimeException |
| 208 | 238 | */ |
| 209 | 239 | protected function wrapJsonSelector($value) |
| 210 | 240 | { |
| 211 | - throw new RuntimeException('This database engine does not support JSON operations.'); | |
| 241 | + throw new RuntimeException( | |
| 242 | + 'This database engine does not support JSON operations.' | |
| 243 | + ); | |
| 212 | 244 | } |
| 213 | 245 | |
| 214 | 246 | /** |
| 215 | 247 | * Determine if the given string is a JSON selector. |
| @@ -279,9 +311,11 @@ | ||
| 279 | 311 | */ |
| 280 | 312 | public function escape($value, $binary = false) |
| 281 | 313 | { |
| 282 | 314 | if (is_null($this->connection)) { |
| 283 | - throw new RuntimeException("The database driver's grammar implementation does not support escaping values."); | |
| 315 | + throw new RuntimeException( | |
| 316 | + "The database driver's grammar implementation does not support escaping values." | |
| 317 | + ); | |
| 284 | 318 | } |
| 285 | 319 | |
| 286 | 320 | return $this->connection->escape($value, $binary); |
| 287 | 321 | } |
| @@ -334,9 +368,9 @@ | ||
| 334 | 368 | |
| 335 | 369 | /** |
| 336 | 370 | * Set the grammar's table prefix. |
| 337 | 371 | * |
| 338 | - * @param string $prefix | |
| 372 | + * @param object $wpdb WordPress database object | |
| 339 | 373 | * @return $this |
| 340 | 374 | */ |
| 341 | 375 | public function setTablePrefix($wpdb) |
| 342 | 376 | { |
| @@ -349,9 +383,9 @@ | ||
| 349 | 383 | |
| 350 | 384 | /** |
| 351 | 385 | * Set the grammar's database connection. |
| 352 | 386 | * |
| 353 | - * @param \FluentCommunity\Framework\Database\Connection $connection | |
| 387 | + * @param \FluentCommunity\Framework\Database\ConnectionInterface $connection | |
| 354 | 388 | * @return $this |
| 355 | 389 | */ |
| 356 | 390 | public function setConnection($connection) |
| 357 | 391 | { |
| @@ -357,6 +391,16 @@ | ||
| 357 | 391 | { |
| 358 | 392 | $this->connection = $connection; |
| 359 | 393 | |
| 360 | 394 | return $this; |
| 395 | + } | |
| 396 | + | |
| 397 | + /** | |
| 398 | + * Track table aliases. | |
| 399 | + * | |
| 400 | + * @param void | |
| 401 | + */ | |
| 402 | + public function addAlias($alias) | |
| 403 | + { | |
| 404 | + $this->alias[] = "`$alias`"; | |
| 361 | 405 | } |
| 362 | 406 | } |