0

Class yii\db\mssql\Quoter

Inheritanceyii\db\mssql\Quoter
Available since version22.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/mssql/Quoter.php

Provides connection-free helpers for quoting MSSQL values and identifiers when building SQL.

Unlike {@see \yii\db\Schema::quoteValue()}, the helpers never open a database connection, so they are safe to call while {@see \yii\db\mssql\QueryBuilder} assembles dynamic SQL ahead of execution.

Public Methods

Hide inherited methods

Method Description Defined By
escapeLiteralValue() Escapes single quotes in a value for embedding as a Transact-SQL string literal. yii\db\mssql\Quoter
extractSimpleIdentifier() Returns the trailing single-part identifier of a bracket-quoted qualified name, keeping its brackets. yii\db\mssql\Quoter
isIdentifierBracketQuoted() Returns whether an identifier is already wrapped in MSSQL square brackets ([identifier]). yii\db\mssql\Quoter

Method Details

Hide inherited methods

escapeLiteralValue() public static method

Escapes single quotes in a value for embedding as a Transact-SQL string literal.

Doubles every single quote (' becomes '') without adding the surrounding quote characters, so the result can be placed inside '...' or N'...'.

Usage example: `php $literal = \yii\db\mssql\Quoter::escapeLiteralValue("O'Brien"); // O''Brien $sql = "DECLARE @name SYSNAME = N'{$literal}'"; `

public static string escapeLiteralValue ( string $value )
$value string

Value to escape.

return string

Value with single quotes doubled, ready to embed inside a quoted literal.

                public static function escapeLiteralValue(string $value): string
{
    return str_replace("'", "''", $value);
}

            
extractSimpleIdentifier() public static method

Returns the trailing single-part identifier of a bracket-quoted qualified name, keeping its brackets.

Splits on the last ].[ boundary; a name without that boundary is returned unchanged.

Usage example: `php \yii\db\mssql\Quoter::extractSimpleIdentifier('[schema].[table]'); // [table] \yii\db\mssql\Quoter::extractSimpleIdentifier('[table]'); // [table] `

public static string extractSimpleIdentifier ( string $name )
$name string

Bracket-quoted qualified identifier.

return string

Trailing single-part identifier with brackets preserved.

                public static function extractSimpleIdentifier(string $name): string
{
    $pos = strrpos($name, '].[');
    if ($pos !== false) {
        return substr($name, $pos + 2);
    }
    return $name;
}

            
isIdentifierBracketQuoted() public static method

Returns whether an identifier is already wrapped in MSSQL square brackets ([identifier]).

Canonical predicate for an already bracket-quoted identifier; the value starts with [ and ends with ].

Usage example: `php \yii\db\mssql\Quoter::isIdentifierBracketQuoted('[user]'); // true \yii\db\mssql\Quoter::isIdentifierBracketQuoted('user'); // false `

public static boolean isIdentifierBracketQuoted ( string $identifier )
$identifier string

Identifier to inspect.

return boolean

Whether the identifier is already bracket-quoted.

                public static function isIdentifierBracketQuoted(string $identifier): bool
{
    return str_starts_with($identifier, '[') && str_ends_with($identifier, ']');
}