Skip to content
PHP Ecosystem
Explore

Quoter

API reference › yii › db › mssql › Quoter

classyii\db\mssql\Quoter
Since22.0
Sourceframework/db/mssql/Quoter.php

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

Unlike Schema::quoteValue(), the helpers never open a database connection, so they are safe to call while QueryBuilder assembles dynamic SQL ahead of execution.

MethodDescriptionDefined by
escapeLiteralValue() staticEscapes single quotes in a value for embedding as a Transact-SQL string literal.Quoter
extractSimpleIdentifier() staticReturns the trailing single-part identifier of a bracket-quoted qualified name, keeping its brackets.Quoter
isIdentifierBracketQuoted() staticReturns whether an identifier is already wrapped in MSSQL square brackets ([identifier]).Quoter
public static function escapeLiteralValue(string $value): string

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:

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

Parameters

ParameterTypeDescription
$valuestringValue to escape.

Returns

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

public static function extractSimpleIdentifier(string $name): string

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:

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

Parameters

ParameterTypeDescription
$namestringBracket-quoted qualified identifier.

Returns

string: Trailing single-part identifier with brackets preserved.

public static function isIdentifierBracketQuoted(string $identifier): bool

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:

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

Parameters

ParameterTypeDescription
$identifierstringIdentifier to inspect.

Returns

bool: Whether the identifier is already bracket-quoted.