Class Yiisoft\Db\Schema\Quoter
| Inheritance | Yiisoft\Db\Schema\Quoter |
|---|---|
| Implements | Yiisoft\Db\Schema\QuoterInterface |
The Quoter is a class that's used to quote table and column names for use in SQL statements.
It provides a set of methods for quoting different types of names, such as table names, column names, and schema names.
The Quoter class is used by Yiisoft\Db\QueryBuilder\AbstractQueryBuilder to quote names.
It's also used by Yiisoft\Db\Command\AbstractCommand to quote names in SQL statements before passing them to database servers.
Public Methods
Method Details
| public __construct( array|string $columnQuoteCharacter, array|string $tableQuoteCharacter, string $tablePrefix = '' ): mixed | ||
| $columnQuoteCharacter | array|string | |
| $tableQuoteCharacter | array|string | |
| $tablePrefix | string | |
public function __construct(
/** @psalm-var string[]|string */
private readonly array|string $columnQuoteCharacter,
/** @psalm-var string[]|string */
private readonly array|string $tableQuoteCharacter,
private string $tablePrefix = '',
) {}
| public cleanUpTableNames( array $tableNames ): array | ||
| $tableNames | array | |
public function cleanUpTableNames(array $tableNames): array
{
$cleanedUpTableNames = [];
$pattern = <<<PATTERN
~^\s*((?:['"`\[]|{{).*?(?:['"`\]]|}})|\(.*?\)|.*?)(?:\s+(?:as\s+)?((?:['"`\[]|{{).*?(?:['"`\]]|}})|.*?))?\s*$~iux
PATTERN;
/** @psalm-var array<array-key, ExpressionInterface|string> $tableNames */
foreach ($tableNames as $alias => $tableName) {
if (is_string($tableName) && !is_string($alias)) {
if (preg_match($pattern, $tableName, $matches)) {
if (isset($matches[2])) {
[, $tableName, $alias] = $matches;
} else {
$tableName = $alias = $matches[1];
}
}
}
if (!is_string($alias)) {
throw new InvalidArgumentException(
'To use Expression in from() method, pass it in array format with alias.',
);
}
if (is_string($tableName)) {
$cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $this->ensureNameQuoted($tableName);
} elseif ($tableName instanceof ExpressionInterface) {
$cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $tableName;
} else {
throw new InvalidArgumentException(
'Use ExpressionInterface without cast to string as object of tableName',
);
}
}
return $cleanedUpTableNames;
}
| public ensureColumnName( string $name ): string | ||
| $name | string | |
public function ensureColumnName(string $name): string
{
if (($pos = strrpos($name, '.')) !== false) {
$name = substr($name, $pos + 1);
}
/** @var string */
return preg_replace('|^\[\[([\w\- ]+)]]$|', '\1', $name);
}
| public ensureNameQuoted( string $name ): string | ||
| $name | string | |
public function ensureNameQuoted(string $name): string
{
$name = str_replace(["'", '"', '`', '[', ']'], '', $name);
if (empty($name) || str_starts_with($name, '{{')) {
return $name;
}
return '{{' . $name . '}}';
}
| public getRawTableName( string $name ): string | ||
| $name | string | |
public function getRawTableName(string $name): string
{
if (str_contains($name, '{{')) {
/** @var string $name */
$name = preg_replace('/{{(.*?)}}/', '\1', $name);
return str_replace('%', $this->tablePrefix, $name);
}
return $name;
}
| public getTableNameParts( string $name ): array | ||
| $name | string | |
public function getTableNameParts(string $name): array
{
$parts = array_reverse(array_slice(explode('.', $name), -2, 2));
/** @var string[] */
$parts = array_map($this->unquoteSimpleTableName(...), $parts);
if (!isset($parts[1])) {
return ['name' => $parts[0]];
}
return [
'schemaName' => $parts[1],
'name' => $parts[0],
];
}
| public quoteColumnName( string $name ): string | ||
| $name | string | |
public function quoteColumnName(string $name): string
{
if (str_contains($name, '(') || str_contains($name, '[[')) {
return $name;
}
if (($pos = strrpos($name, '.')) !== false) {
$prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.';
$name = substr($name, $pos + 1);
} else {
$prefix = '';
}
if (str_contains($name, '{{')) {
return $name;
}
return $prefix . $this->quoteSimpleColumnName($name);
}
| public quoteSimpleColumnName( string $name ): string | ||
| $name | string | |
public function quoteSimpleColumnName(string $name): string
{
if ($name === '' || $name === '*') {
return $name;
}
if (is_string($this->columnQuoteCharacter)) {
$startingCharacter = $endingCharacter = $this->columnQuoteCharacter;
} else {
[$startingCharacter, $endingCharacter] = $this->columnQuoteCharacter;
}
return $name[0] === $startingCharacter
? $name
: $startingCharacter . $name . $endingCharacter;
}
| public quoteSimpleTableName( string $name ): string | ||
| $name | string | |
public function quoteSimpleTableName(string $name): string
{
if (is_string($this->tableQuoteCharacter)) {
$startingCharacter = $endingCharacter = $this->tableQuoteCharacter;
} else {
[$startingCharacter, $endingCharacter] = $this->tableQuoteCharacter;
}
return ($name[0] ?? '') === $startingCharacter
? $name
: $startingCharacter . $name . $endingCharacter;
}
| public quoteSql( string $sql ): string | ||
| $sql | string | |
public function quoteSql(string $sql): string
{
/** @var string */
return preg_replace_callback(
'/{{(%?[\w\-. ]+)%?}}|\\[\\[([\w\-. ]+)]]/',
function ($matches) {
if (isset($matches[2])) {
return $this->quoteSimpleColumnName($matches[2]);
}
return str_replace('%', $this->tablePrefix, $this->quoteSimpleTableName($matches[1]));
},
$sql,
);
}
| public quoteTableName( string $name ): string | ||
| $name | string | |
public function quoteTableName(string $name): string
{
if ($name[0] === '(' || str_contains($name, '{{')) {
return $name;
}
if (!str_contains($name, '.')) {
return $this->quoteSimpleTableName($name);
}
$parts = $this->getTableNameParts($name);
foreach ($parts as &$part) {
$part = $this->quoteSimpleTableName($part);
}
return implode('.', $parts);
}
| public quoteValue( string $value ): string | ||
| $value | string | |
public function quoteValue(string $value): string
{
return "'" . str_replace("'", "''", addcslashes($value, "\000\032")) . "'";
}
| public setTablePrefix( string $value ): void | ||
| $value | string | |
public function setTablePrefix(string $value): void
{
$this->tablePrefix = $value;
}
| public unquoteSimpleColumnName( string $name ): string | ||
| $name | string | |
public function unquoteSimpleColumnName(string $name): string
{
return ($name[0] ?? '') !== $this->columnQuoteCharacter[0]
? $name
: substr($name, 1, -1);
}
| public unquoteSimpleTableName( string $name ): string | ||
| $name | string | |
public function unquoteSimpleTableName(string $name): string
{
return ($name[0] ?? '') !== $this->tableQuoteCharacter[0]
? $name
: substr($name, 1, -1);
}
Signup or Login in order to comment.