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