0 follower

Final Class Yiisoft\Db\Sqlite\SqlToken

InheritanceYiisoft\Db\Sqlite\SqlToken
ImplementsArrayAccess, Stringable

Represents SQL tokens produced by {@see SqlTokenizer} or its child classes.

Public Methods

Hide inherited methods

Method Description Defined By
__toString() Returns the SQL code representing the token. Yiisoft\Db\Sqlite\SqlToken
content() Set token content. Yiisoft\Db\Sqlite\SqlToken
endOffset() Set original SQL token end position. Yiisoft\Db\Sqlite\SqlToken
getChildren() Returns child tokens. Yiisoft\Db\Sqlite\SqlToken
getContent() Yiisoft\Db\Sqlite\SqlToken
getHasChildren() Returns whether the token represents a collection of tokens and has a non-zero number of children. Yiisoft\Db\Sqlite\SqlToken
getIsCollection() Returns whether the token represents a collection of tokens. Yiisoft\Db\Sqlite\SqlToken
getSql() Returns the SQL code representing the token. Yiisoft\Db\Sqlite\SqlToken
getType() Yiisoft\Db\Sqlite\SqlToken
matches() Returns whether this token (including its children) matches the specified "pattern" SQL code. Yiisoft\Db\Sqlite\SqlToken
offsetExists() Returns whether there is a child token at the specified offset. Yiisoft\Db\Sqlite\SqlToken
offsetGet() Returns a child token at the specified offset. Yiisoft\Db\Sqlite\SqlToken
offsetSet() Adds a child token to the token. Yiisoft\Db\Sqlite\SqlToken
offsetUnset() Removes a child token at the specified offset. Yiisoft\Db\Sqlite\SqlToken
parent() Set parent token. Yiisoft\Db\Sqlite\SqlToken
setChildren() Sets a list of child tokens. Yiisoft\Db\Sqlite\SqlToken
startOffset() Set original SQL token start position. Yiisoft\Db\Sqlite\SqlToken
type() Set token type. Yiisoft\Db\Sqlite\SqlToken

Constants

Hide inherited constants

Constant Value Description Defined By
TYPE_CODE 0 Yiisoft\Db\Sqlite\SqlToken
TYPE_IDENTIFIER 6 Yiisoft\Db\Sqlite\SqlToken
TYPE_KEYWORD 4 Yiisoft\Db\Sqlite\SqlToken
TYPE_OPERATOR 5 Yiisoft\Db\Sqlite\SqlToken
TYPE_PARENTHESIS 3 Yiisoft\Db\Sqlite\SqlToken
TYPE_STATEMENT 1 Yiisoft\Db\Sqlite\SqlToken
TYPE_STRING_LITERAL 7 Yiisoft\Db\Sqlite\SqlToken
TYPE_TOKEN 2 Yiisoft\Db\Sqlite\SqlToken

Method Details

Hide inherited methods

__toString() public method

Returns the SQL code representing the token.

public string __toString ( )
return string

SQL code.

                public function __toString(): string
{
    return $this->getSql();
}

            
content() public method

Set token content.

public self content ( string|null $value )
$value string|null

                public function content(?string $value): self
{
    $this->content = $value;
    return $this;
}

            
endOffset() public method

Set original SQL token end position.

public self endOffset ( integer $value )
$value integer

Original SQL token end position.

                public function endOffset(int $value): self
{
    $this->endOffset = $value;
    return $this;
}

            
getChildren() public method

Returns child tokens.

public Yiisoft\Db\Sqlite\SqlToken[] getChildren ( )
return Yiisoft\Db\Sqlite\SqlToken[]

Child tokens.

                public function getChildren(): array
{
    return $this->children;
}

            
getContent() public method

public string|null getContent ( )
return string|null

The token content.

                public function getContent(): ?string
{
    return $this->content;
}

            
getHasChildren() public method

Returns whether the token represents a collection of tokens and has a non-zero number of children.

public boolean getHasChildren ( )
return boolean

Whether the token has children.

                public function getHasChildren(): bool
{
    return $this->getIsCollection() && !empty($this->children);
}

            
getIsCollection() public method

Returns whether the token represents a collection of tokens.

public boolean getIsCollection ( )
return boolean

Whether the token represents a collection of tokens.

                public function getIsCollection(): bool
{
    return in_array($this->type, [self::TYPE_CODE, self::TYPE_STATEMENT, self::TYPE_PARENTHESIS], true);
}

            
getSql() public method

Returns the SQL code representing the token.

public string getSql ( )
return string

SQL code.

                public function getSql(): string
{
    $sql = '';
    $code = $this;
    while ($code->parent !== null) {
        $code = $code->parent;
    }
    if ($code->content !== null) {
        $sql = mb_substr(
            $code->content,
            (int) $this->startOffset,
            (int) $this->endOffset - (int) $this->startOffset,
            'UTF-8',
        );
    }
    return $sql;
}

            
getType() public method

public integer getType ( )
return integer

The type of the token.

                public function getType(): int
{
    return $this->type;
}

            
matches() public method

Returns whether this token (including its children) matches the specified "pattern" SQL code.

Usage Example:

$patternToken = (new \Yiisoft\Db\Sqlite\SqlTokenizer('SELECT any FROM any'))->tokenize();
if ($sqlToken->matches($patternToken, 0, $firstMatchIndex, $lastMatchIndex)) {
    // ...
}
public boolean matches ( Yiisoft\Db\Sqlite\SqlToken $patternToken, integer $offset 0, integer|null &$firstMatchIndex null, integer|null &$lastMatchIndex null )
$patternToken Yiisoft\Db\Sqlite\SqlToken

Tokenized SQL codes to match. In addition to regular SQL, the any keyword is supported which will match any number of keywords, identifiers, whitespaces.

$offset integer

Token children offset to start lookup with.

$firstMatchIndex integer|null

Token children offset where a successful match begins.

$lastMatchIndex integer|null

Token children offset where a successful match ends.

return boolean

Whether this token matches the pattern SQL code.

                public function matches(
    self $patternToken,
    int $offset = 0,
    ?int &$firstMatchIndex = null,
    ?int &$lastMatchIndex = null,
): bool {
    $result = false;
    if ($patternToken->getHasChildren() && ($patternToken[0] instanceof self)) {
        $result = $this->tokensMatch($patternToken[0], $this, $offset, $firstMatchIndex, $lastMatchIndex);
    }
    return $result;
}

            
offsetExists() public method

Returns whether there is a child token at the specified offset.

This method is required by the SPL {@see \ArrayAccess} interface.

It's implicitly called when you use something like isset($token[$offset]).

public boolean offsetExists ( integer $offset )
$offset integer

The child token offset.

return boolean

Whether the token exists.

                public function offsetExists($offset): bool
{
    return isset($this->children[$this->calculateOffset($offset)]);
}

            
offsetGet() public method

Returns a child token at the specified offset.

This method is required by the SPL {@see \ArrayAccess} interface.

It's implicitly called when you use something like $child = $token[$offset];.

public Yiisoft\Db\Sqlite\SqlToken|null offsetGet ( integer $offset )
$offset integer

The child token offset.

return Yiisoft\Db\Sqlite\SqlToken|null

The child token at the specified offset, null if there's no token.

                public function offsetGet($offset): ?self
{
    $offset = $this->calculateOffset($offset);
    return $this->children[$offset] ?? null;
}

            
offsetSet() public method

Adds a child token to the token.

This method is required by the SPL {@see \ArrayAccess} interface.

It's implicitly called when you use something like $token[$offset] = $child;.

public void offsetSet ( mixed $offset, mixed $value )
$offset mixed

The child token offset.

$value mixed

Token to add.

                public function offsetSet(mixed $offset, mixed $value): void
{
    if ($value instanceof self) {
        $value->parent = $this;
    }
    if ($offset === null) {
        $this->children[] = $value;
    } else {
        $this->children[$this->calculateOffset((int) $offset)] = $value;
    }
    $this->updateCollectionOffsets();
}

            
offsetUnset() public method

Removes a child token at the specified offset.

This method is required by the SPL {@see \ArrayAccess} interface.

It's implicitly called when you use something like unset($token[$offset]).

public void offsetUnset ( integer $offset )
$offset integer

Child token offset.

                public function offsetUnset($offset): void
{
    $offset = $this->calculateOffset($offset);
    if (isset($this->children[$offset])) {
        array_splice($this->children, $offset, 1);
    }
    $this->updateCollectionOffsets();
}

            
parent() public method

Set parent token.

public self parent ( Yiisoft\Db\Sqlite\SqlToken $value )
$value Yiisoft\Db\Sqlite\SqlToken

The parent token.

                public function parent(self $value): self
{
    $this->parent = $value;
    return $this;
}

            
setChildren() public method

Sets a list of child tokens.

public void setChildren ( Yiisoft\Db\Sqlite\SqlToken[] $children )
$children Yiisoft\Db\Sqlite\SqlToken[]

Child tokens.

                public function setChildren(array $children): void
{
    $this->children = [];
    foreach ($children as $child) {
        $child->parent = $this;
        $this->children[] = $child;
    }
    $this->updateCollectionOffsets();
}

            
startOffset() public method

Set original SQL token start position.

public self startOffset ( integer $value )
$value integer

Original SQL token start position.

                public function startOffset(int $value): self
{
    $this->startOffset = $value;
    return $this;
}

            
type() public method

Set token type.

public self type ( integer $value )
$value integer

Token type. It has to be one of the following constants:

  • {@see \Yiisoft\Db\Sqlite\TYPE_CODE}
  • {@see \Yiisoft\Db\Sqlite\TYPE_STATEMENT}
  • {@see \Yiisoft\Db\Sqlite\TYPE_TOKEN}
  • {@see \Yiisoft\Db\Sqlite\TYPE_PARENTHESIS}
  • {@see \Yiisoft\Db\Sqlite\TYPE_KEYWORD}
  • {@see \Yiisoft\Db\Sqlite\TYPE_OPERATOR}
  • {@see \Yiisoft\Db\Sqlite\TYPE_IDENTIFIER}
  • {@see \Yiisoft\Db\Sqlite\TYPE_STRING_LITERAL}

                public function type(int $value): self
{
    $this->type = $value;
    return $this;
}