← All changes
|
vendor/wpfluent/framework/src/WPFluent/Database/BaseGrammar.php
+76
-37
1.10.0
→
2.5.0
View file →
| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FluentBooking\Framework\Database; |
| 4 | 4 | |
| 5 | 5 | use RuntimeException; |
| 6 | 6 | use FluentBooking\Framework\Support\Helper; |
| 7 | +use FluentBooking\Framework\Database\Schema; | |
| 7 | 8 | use FluentBooking\Framework\Support\MacroableTrait; |
| 8 | 9 | use FluentBooking\Framework\Database\Query\Expression; |
| 9 | 10 | |
| 10 | 11 | abstract class BaseGrammar |
| @@ -11,8 +12,15 @@ | ||
| 11 | 12 | { |
| 12 | 13 | use MacroableTrait; |
| 13 | 14 | |
| 14 | 15 | /** |
| 16 | + * Cache of aliased table names. | |
| 17 | + * | |
| 18 | + * @var array | |
| 19 | + */ | |
| 20 | + protected $alias = []; | |
| 21 | + | |
| 22 | + /** | |
| 15 | 23 | * The connection used for escaping values. |
| 16 | 24 | * |
| 17 | 25 | * @var \FluentBooking\Framework\Database\ConnectionInterface |
| 18 | 26 | */ |
| @@ -32,8 +40,19 @@ | ||
| 32 | 40 | */ |
| 33 | 41 | protected $tablePrefix = ''; |
| 34 | 42 | |
| 35 | 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 | + /** | |
| 36 | 55 | * Wrap an array of values. |
| 37 | 56 | * |
| 38 | 57 | * @param array $values |
| 39 | 58 | * @return array |
| @@ -43,8 +62,37 @@ | ||
| 43 | 62 | return array_map([$this, 'wrap'], $values); |
| 44 | 63 | } |
| 45 | 64 | |
| 46 | 65 | /** |
| 66 | + * Wrap a value in keyword identifiers. | |
| 67 | + * | |
| 68 | + * @param \FluentBooking\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 | + /** | |
| 47 | 95 | * Wrap a table in keyword identifiers. |
| 48 | 96 | * |
| 49 | 97 | * @param \FluentBooking\Framework\Database\Query\Expression|string $table |
| 50 | 98 | * @return string |
| @@ -68,9 +116,9 @@ | ||
| 68 | 116 | // prefix the last segment as the table name then wrap each segment alone |
| 69 | 117 | // and eventually join them both back together using the dot connector. |
| 70 | 118 | if (str_contains($table, '.')) { |
| 71 | 119 | $table = substr_replace( |
| 72 | - $table, '.'.$tablePrefix, strrpos($table, '.'), 1 | |
| 120 | + $table, '.' . $tablePrefix, strrpos($table, '.'), 1 | |
| 73 | 121 | ); |
| 74 | 122 | |
| 75 | 123 | return Helper::collect(explode('.', $table)) |
| 76 | 124 | ->map(function($value) { |
| @@ -78,8 +126,12 @@ | ||
| 78 | 126 | }) |
| 79 | 127 | ->implode('.'); |
| 80 | 128 | } |
| 81 | 129 | |
| 130 | + if ($this->isAliasedTable($table)) { | |
| 131 | + return $table; | |
| 132 | + } | |
| 133 | + | |
| 82 | 134 | return $this->wrapValue($tablePrefix.$table); |
| 83 | 135 | } |
| 84 | 136 | |
| 85 | 137 | /** |
| @@ -89,8 +141,12 @@ | ||
| 89 | 141 | * @return string |
| 90 | 142 | */ |
| 91 | 143 | protected function resolveTablePrefix($table) |
| 92 | 144 | { |
| 145 | + if ($this->isAliasedTable($table)) { | |
| 146 | + return ''; | |
| 147 | + } | |
| 148 | + | |
| 93 | 149 | if (!is_multisite()) { |
| 94 | 150 | return $this->tablePrefix; |
| 95 | 151 | } |
| 96 | 152 | |
| @@ -110,37 +166,8 @@ | ||
| 110 | 166 | ) ? $this->basePrefix : $this->tablePrefix; |
| 111 | 167 | } |
| 112 | 168 | |
| 113 | 169 | /** |
| 114 | - * Wrap a value in keyword identifiers. | |
| 115 | - * | |
| 116 | - * @param \FluentBooking\Framework\Database\Query\Expression|string $value | |
| 117 | - * @return string | |
| 118 | - */ | |
| 119 | - public function wrap($value) | |
| 120 | - { | |
| 121 | - if ($this->isExpression($value)) { | |
| 122 | - return $this->getValue($value); | |
| 123 | - } | |
| 124 | - | |
| 125 | - // If the value being wrapped has a column alias we will need to separate out | |
| 126 | - // the pieces so we can wrap each of the segments of the expression on its | |
| 127 | - // own, and then join these both back together using the "as" connector. | |
| 128 | - if (stripos($value, ' as ') !== false) { | |
| 129 | - return $this->wrapAliasedValue($value); | |
| 130 | - } | |
| 131 | - | |
| 132 | - // If the given value is a JSON selector we will wrap it differently than a | |
| 133 | - // traditional value. We will need to split this path and wrap each part | |
| 134 | - // wrapped, etc. Otherwise, we will simply wrap the value as a string. | |
| 135 | - if ($this->isJsonSelector($value)) { | |
| 136 | - return $this->wrapJsonSelector($value); | |
| 137 | - } | |
| 138 | - | |
| 139 | - return $this->wrapSegments(explode('.', $value)); | |
| 140 | - } | |
| 141 | - | |
| 142 | - /** | |
| 143 | 170 | * Wrap a value that has an alias. |
| 144 | 171 | * |
| 145 | 172 | * @param string $value |
| 146 | 173 | * @return string |
| @@ -148,9 +175,11 @@ | ||
| 148 | 175 | protected function wrapAliasedValue($value) |
| 149 | 176 | { |
| 150 | 177 | $segments = preg_split('/\s+as\s+/i', $value); |
| 151 | 178 | |
| 152 | - return $this->wrap($segments[0]).' as '.$this->wrapValue($segments[1]); | |
| 179 | + return $this->wrap( | |
| 180 | + $segments[0] | |
| 181 | + ) . ' as ' . $this->wrapValue($segments[1]); | |
| 153 | 182 | } |
| 154 | 183 | |
| 155 | 184 | /** |
| 156 | 185 | * Wrap a table that has an alias. |
| @@ -161,13 +190,11 @@ | ||
| 161 | 190 | protected function wrapAliasedTable($value) |
| 162 | 191 | { |
| 163 | 192 | $segments = preg_split('/\s+as\s+/i', $value); |
| 164 | 193 | |
| 165 | - $tablePrefix = $this->resolveTablePrefix($segments[0]); | |
| 194 | + $this->alias[] = $alias = $this->wrapValue($segments[1]); | |
| 166 | 195 | |
| 167 | - return $this->wrapTable( | |
| 168 | - $tablePrefix.$segments[0] | |
| 169 | - ).' as '.$this->wrapValue($segments[1]); | |
| 196 | + return $this->wrapTable($segments[0]) . ' as ' . $alias; | |
| 170 | 197 | } |
| 171 | 198 | |
| 172 | 199 | /** |
| 173 | 200 | * Wrap the given value segments. |
| @@ -176,9 +203,11 @@ | ||
| 176 | 203 | * @return string |
| 177 | 204 | */ |
| 178 | 205 | protected function wrapSegments($segments) |
| 179 | 206 | { |
| 180 | - return Helper::collect($segments)->map(function ($segment, $key) use ($segments) { | |
| 207 | + return Helper::collect($segments)->map(function ( | |
| 208 | + $segment, $key | |
| 209 | + ) use ($segments) { | |
| 181 | 210 | return $key == 0 && count($segments) > 1 |
| 182 | 211 | ? $this->wrapTable($segment) |
| 183 | 212 | : $this->wrapValue($segment); |
| 184 | 213 | })->implode('.'); |
| @@ -339,9 +368,9 @@ | ||
| 339 | 368 | |
| 340 | 369 | /** |
| 341 | 370 | * Set the grammar's table prefix. |
| 342 | 371 | * |
| 343 | - * @param string $prefix | |
| 372 | + * @param object $wpdb WordPress database object | |
| 344 | 373 | * @return $this |
| 345 | 374 | */ |
| 346 | 375 | public function setTablePrefix($wpdb) |
| 347 | 376 | { |
| @@ -362,6 +391,16 @@ | ||
| 362 | 391 | { |
| 363 | 392 | $this->connection = $connection; |
| 364 | 393 | |
| 365 | 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`"; | |
| 366 | 405 | } |
| 367 | 406 | } |