0 follower

Abstract Class Yiisoft\Db\Syntax\AbstractSqlParser

InheritanceYiisoft\Db\Syntax\AbstractSqlParser

SQL parser.

This class provides methods to parse SQL statements and extract placeholders from them.

Protected Properties

Hide inherited properties

Property Type Description Defined By
$length integer Length of SQL statement. Yiisoft\Db\Syntax\AbstractSqlParser
$position integer Current position in SQL statement. Yiisoft\Db\Syntax\AbstractSqlParser
$sql string Yiisoft\Db\Syntax\AbstractSqlParser

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Db\Syntax\AbstractSqlParser
getNextPlaceholder() Returns the next placeholder from the current position in an SQL statement. Yiisoft\Db\Syntax\AbstractSqlParser

Protected Methods

Hide inherited methods

Method Description Defined By
parseIdentifier() Parses and returns identifier. Equivalent to [_a-zA-Z]\w* in regular expressions. Yiisoft\Db\Syntax\AbstractSqlParser
parseWord() Parses and returns word characters. Equivalent to \w* in regular expressions. Yiisoft\Db\Syntax\AbstractSqlParser
skipChars() Skips all specified characters. Yiisoft\Db\Syntax\AbstractSqlParser
skipQuotedWithEscape() Skips quoted string with escape characters. Yiisoft\Db\Syntax\AbstractSqlParser
skipQuotedWithoutEscape() Skips quoted string without escape characters. Yiisoft\Db\Syntax\AbstractSqlParser
skipToAfterChar() Skips to the character after the specified character. Yiisoft\Db\Syntax\AbstractSqlParser
skipToAfterString() Skips to the character after the specified string. Yiisoft\Db\Syntax\AbstractSqlParser

Constants

Hide inherited constants

Constant Value Description Defined By
LETTER_CHARS '_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' Yiisoft\Db\Syntax\AbstractSqlParser
WORD_CHARS '0123456789' . self::LETTER_CHARS Yiisoft\Db\Syntax\AbstractSqlParser

Property Details

Hide inherited properties

$length protected property

Length of SQL statement.

protected integer $length null
$position protected property

Current position in SQL statement.

protected integer $position 0
$sql protected property
protected string $sql null

Method Details

Hide inherited methods

__construct() public method

public __construct( string $sql ): mixed
$sql string

SQL statement to parse.

                public function __construct(protected string $sql)
{
    $this->length = strlen($sql);
}

            
getNextPlaceholder() public abstract method

Returns the next placeholder from the current position in an SQL statement.

public abstract getNextPlaceholder( integer|null &$position null ): string|null
$position integer|null

Position of the placeholder in an SQL statement.

return string|null

The next placeholder or null if it is not found.

                abstract public function getNextPlaceholder(?int &$position = null): ?string;

            
parseIdentifier() protected method

Parses and returns identifier. Equivalent to [_a-zA-Z]\w* in regular expressions.

protected parseIdentifier( ): string
return string

Parsed identifier.

                protected function parseIdentifier(): string
{
    $length = strspn($this->sql, self::LETTER_CHARS, $this->position);
    if ($length === 0) {
        return '';
    }
    $length += strspn($this->sql, self::WORD_CHARS, $this->position + $length);
    $word = substr($this->sql, $this->position, $length);
    $this->position += $length;
    return $word;
}

            
parseWord() protected method

Parses and returns word characters. Equivalent to \w* in regular expressions.

protected parseWord( ): string
return string

Parsed word characters.

                final protected function parseWord(): string
{
    $length = strspn($this->sql, self::WORD_CHARS, $this->position);
    $word = substr($this->sql, $this->position, $length);
    $this->position += $length;
    return $word;
}

            
skipChars() protected method

Skips all specified characters.

protected skipChars( string $char ): void
$char string

                final protected function skipChars(string $char): void
{
    $length = strspn($this->sql, $char, $this->position);
    $this->position += $length;
}

            
skipQuotedWithEscape() protected method

Skips quoted string with escape characters.

protected skipQuotedWithEscape( string $endChar ): void
$endChar string

                final protected function skipQuotedWithEscape(string $endChar): void
{
    preg_match("/(?>[^$endChar\\\\]+|\\\\.)*/", $this->sql, $matches, 0, $this->position);
    $this->position += strlen($matches[0]) + 1;
}

            
skipQuotedWithoutEscape() protected method

Skips quoted string without escape characters.

protected skipQuotedWithoutEscape( string $endChar ): void
$endChar string

                final protected function skipQuotedWithoutEscape(string $endChar): void
{
    do {
        $this->skipToAfterChar($endChar);
    } while (($this->sql[$this->position] ?? null) === $endChar && ++$this->position);
}

            
skipToAfterChar() protected method

Skips to the character after the specified character.

protected skipToAfterChar( string $char ): void
$char string

                final protected function skipToAfterChar(string $char): void
{
    $length = strcspn($this->sql, $char, $this->position);
    $this->position += $length + 1;
}

            
skipToAfterString() protected method

Skips to the character after the specified string.

protected skipToAfterString( string $string ): void
$string string

                final protected function skipToAfterString(string $string): void
{
    /** @var int $pos */
    $pos = strpos($this->sql, $string, $this->position);
    $this->position = $pos + strlen($string);
}