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 SQL statement. Yiisoft\Db\Syntax\AbstractSqlParser

Protected Methods

Hide inherited methods

Method Description Defined By
parseIdentifier() Parses and returns identifier. Equals to [_a-zA-Z]\w+ in regular expressions. Yiisoft\Db\Syntax\AbstractSqlParser
parseWord() Parses and returns word symbols. Equals 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

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 mixed __construct ( string $sql )
$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 SQL statement.

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

Position of the placeholder in 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. Equals to [_a-zA-Z]\w+ in regular expressions.

protected string parseIdentifier ( )
return string

Parsed identifier.

                protected function parseIdentifier(): string
{
    return match ($this->sql[$this->position]) {
        '_',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
        'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
        'V', 'W', 'X', 'Y', 'Z' => $this->sql[$this->position++] . $this->parseWord(),
        default => '',
    };
}

            
parseWord() protected method

Parses and returns word symbols. Equals to \w+ in regular expressions.

protected string parseWord ( )
return string

Parsed word symbols.

                final protected function parseWord(): string
{
    $word = '';
    $continue = true;
    while ($continue && $this->position < $this->length) {
        match ($this->sql[$this->position]) {
            '_', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
            'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z' => $word .= $this->sql[$this->position++],
            default => $continue = false,
        };
    }
    return $word;
}

            
skipChars() protected method

Skips all specified characters.

protected void skipChars ( string $char )
$char string

                final protected function skipChars(string $char): void
{
    while ($this->position < $this->length && $this->sql[$this->position] === $char) {
        ++$this->position;
    }
}

            
skipQuotedWithEscape() protected method

Skips quoted string with escape characters.

protected void skipQuotedWithEscape ( string $endChar )
$endChar string

                final protected function skipQuotedWithEscape(string $endChar): void
{
    for (; $this->position < $this->length; ++$this->position) {
        if ($this->sql[$this->position] === $endChar) {
            ++$this->position;
            return;
        }
        if ($this->sql[$this->position] === '\\') {
            ++$this->position;
        }
    }
}

            
skipQuotedWithoutEscape() protected method

Skips quoted string without escape characters.

protected void skipQuotedWithoutEscape ( string $endChar )
$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 void skipToAfterChar ( string $char )
$char string

                final protected function skipToAfterChar(string $char): void
{
    for (; $this->position < $this->length; ++$this->position) {
        if ($this->sql[$this->position] === $char) {
            ++$this->position;
            return;
        }
    }
}

            
skipToAfterString() protected method

Skips to the character after the specified string.

protected void skipToAfterString ( string $string )
$string string

                final protected function skipToAfterString(string $string): void
{
    $firstChar = $string[0];
    $subString = substr($string, 1);
    $length = strlen($subString);
    do {
        $this->skipToAfterChar($firstChar);
        if (substr($this->sql, $this->position, $length) === $subString) {
            $this->position += $length;
            return;
        }
    } while ($this->position + $length < $this->length);
}