Abstract Class Yiisoft\Db\Syntax\AbstractSqlParser
| Inheritance | Yiisoft\Db\Syntax\AbstractSqlParser |
|---|
SQL parser.
This class provides methods to parse SQL statements and extract placeholders from them.
Protected 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
| 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
| 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
Method Details
| public mixed __construct ( string $sql ) | ||
| $sql | string |
SQL statement to parse. |
public function __construct(protected string $sql)
{
$this->length = strlen($sql);
}
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;
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 => '',
};
}
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;
}
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;
}
}
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;
}
}
}
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);
}
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;
}
}
}
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);
}
Signup or Login in order to comment.