# Class yii\db\SqlToken

Inheritance | yii\db\SqlToken » yii\base\BaseObject
Implements | ArrayAccess, yii\base\Configurable
Available since version | 2.0.13
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/SqlToken.php

SqlToken represents SQL tokens produced by yii\db\SqlTokenizer or its child classes.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$children | yii\db\SqlToken[] | Child tokens. | yii\db\SqlToken
$content | string|null | Token content. | yii\db\SqlToken
$endOffset | integer | Original SQL token end position. | yii\db\SqlToken
$hasChildren | boolean | Whether the token has children. | yii\db\SqlToken
$isCollection | boolean | Whether the token represents a collection of tokens. | yii\db\SqlToken
$parent | yii\db\SqlToken | Parent token. | yii\db\SqlToken
$sql | string | SQL code. | yii\db\SqlToken
$startOffset | integer | Original SQL token start position. | yii\db\SqlToken
$type | integer | Token type. | yii\db\SqlToken

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\BaseObject
__construct() | Constructor. | yii\base\BaseObject
__get() | Returns the value of an object property. | yii\base\BaseObject
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject
__set() | Sets value of an object property. | yii\base\BaseObject
__toString() | Returns the SQL code representing the token. | yii\db\SqlToken
__unset() | Sets an object property to null. | yii\base\BaseObject
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
getChildren() | Returns child tokens. | yii\db\SqlToken
getHasChildren() | Returns whether the token represents a collection of tokens and has non-zero number of children. | yii\db\SqlToken
getIsCollection() | Returns whether the token represents a collection of tokens. | yii\db\SqlToken
getSql() | Returns the SQL code representing the token. | yii\db\SqlToken
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject
init() | Initializes the object. | yii\base\BaseObject
matches() | Returns whether this token (including its children) matches the specified "pattern" SQL code. | yii\db\SqlToken
offsetExists() | Returns whether there is a child token at the specified offset. | yii\db\SqlToken
offsetGet() | Returns a child token at the specified offset. | yii\db\SqlToken
offsetSet() | Adds a child token to the token. | yii\db\SqlToken
offsetUnset() | Removes a child token at the specified offset. | yii\db\SqlToken
setChildren() | Sets a list of child tokens. | yii\db\SqlToken

## Constants

Hide inherited constants

Constant | Value | Description | Defined By
--- | --- | --- | ---
TYPE_CODE | 0 |  | yii\db\SqlToken
TYPE_IDENTIFIER | 6 |  | yii\db\SqlToken
TYPE_KEYWORD | 4 |  | yii\db\SqlToken
TYPE_OPERATOR | 5 |  | yii\db\SqlToken
TYPE_PARENTHESIS | 3 |  | yii\db\SqlToken
TYPE_STATEMENT | 1 |  | yii\db\SqlToken
TYPE_STRING_LITERAL | 7 |  | yii\db\SqlToken
TYPE_TOKEN | 2 |  | yii\db\SqlToken

## Property Details

Hide inherited properties

$children public property

Child tokens.

public yii\db\SqlToken[] $children = null
$content public property

Token content.

public string|null $content = null
$endOffset public property

Original SQL token end position.

public integer $endOffset = null
$hasChildren public property

Whether the token has children.

public boolean $hasChildren = null
$isCollection public property

Whether the token represents a collection of tokens.

public boolean $isCollection = null
$parent public property

Parent token.

public yii\db\SqlToken $parent = null
$sql public property

SQL code.

public string $sql = null
$startOffset public property

Original SQL token start position.

public integer $startOffset = null
$type public property

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

- TYPE_CODE
- TYPE_STATEMENT
- TYPE_TOKEN
- TYPE_PARENTHESIS
- TYPE_KEYWORD
- TYPE_OPERATOR
- TYPE_IDENTIFIER
- TYPE_STRING_LITERAL

public integer $type = self::TYPE_TOKEN

## Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name | string | The method name
$params | array | Method parameters
return | mixed | The method return value
throws | yii\base\UnknownMethodException | when calling unknown method

Source code

                    public function __call($name, $params)
    {
        throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
    }

__construct() public method

Defined in: yii\base\BaseObject::__construct()

Constructor.

The default implementation does two things:

- Initializes the object with the given configuration `$config`.
- Call init().

If this method is overridden in a child class, it is recommended that

- the last parameter of the constructor is a configuration array, like `$config` here.
- call the parent implementation at the end of the constructor.

public void __construct ( $config = [] )
$config | array | Name-value pairs that will be used to initialize the object properties

Source code

                    public function __construct($config = [])
    {
        if (!empty($config)) {
            Yii::configure($this, $config);
        }
        $this->init();
    }

__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing `$value = $object->property;`.

See also __set().

public mixed __get ( $name )
$name | string | The property name
return | mixed | The property value
throws | yii\base\UnknownPropertyException | if the property is not defined
throws | yii\base\InvalidCallException | if the property is write-only

Source code

                    public function __get($name)
    {
        $getter = 'get' . $name;
        if (method_exists($this, $getter)) {
            return $this->$getter();
        } elseif (method_exists($this, 'set' . $name)) {
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
        }
        throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
    }

__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing `isset($object->property)`.

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name | string | The property name or the event name
return | boolean | Whether the named property is set (not null).

Source code

                    public function __isset($name)
    {
        $getter = 'get' . $name;
        if (method_exists($this, $getter)) {
            return $this->$getter() !== null;
        }
        return false;
    }

__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing `$object->property = $value;`.

See also __get().

public void __set ( $name, $value )
$name | string | The property name or the event name
$value | mixed | The property value
throws | yii\base\UnknownPropertyException | if the property is not defined
throws | yii\base\InvalidCallException | if the property is read-only

Source code

                    public function __set($name, $value)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter($value);
        } elseif (method_exists($this, 'get' . $name)) {
            throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
        } else {
            throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
        }
    }

__toString() public method

Returns the SQL code representing the token.

public string __toString ( )
return | string | SQL code.

Source code

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

__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing `unset($object->property)`.

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name | string | The property name
throws | yii\base\InvalidCallException | if the property is read only.

Source code

                    public function __unset($name)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter(null);
        } elseif (method_exists($this, 'get' . $name)) {
            throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
        }
    }

canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when `$checkVars` is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars = true )
$name | string | The property name
$checkVars | boolean | Whether to treat member variables as properties
return | boolean | Whether the property can be read

Source code

                    public function canGetProperty($name, $checkVars = true)
    {
        return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
    }

canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when `$checkVars` is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars = true )
$name | string | The property name
$checkVars | boolean | Whether to treat member variables as properties
return | boolean | Whether the property can be written

Source code

                    public function canSetProperty($name, $checkVars = true)
    {
        return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
    }

className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use `::class` instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return | string | The fully qualified name of this class.

Source code

                    public static function className()
    {
        return get_called_class();
    }

getChildren() public method

Returns child tokens.

public yii\db\SqlToken[] getChildren ( )
return | yii\db\SqlToken[] | Child tokens.

Source code

                    public function getChildren()
    {
        return $this->_children;
    }

getHasChildren() public method

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

public boolean getHasChildren ( )
return | boolean | Whether the token has children.

Source code

                    public function getHasChildren()
    {
        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.

Source code

                    public function getIsCollection()
    {
        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.

Source code

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

hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function `method_exists()`. You may override this method when you implemented the php magic method `__call()`.

public boolean hasMethod ( $name )
$name | string | The method name
return | boolean | Whether the method is defined

Source code

                    public function hasMethod($name)
    {
        return method_exists($this, $name);
    }

hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when `$checkVars` is true);

See also:

- canGetProperty()
- canSetProperty()

public boolean hasProperty ( $name, $checkVars = true )
$name | string | The property name
$checkVars | boolean | Whether to treat member variables as properties
return | boolean | Whether the property is defined

Source code

                    public function hasProperty($name, $checkVars = true)
    {
        return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
    }

init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

Source code

                    public function init()
    {
    }

matches() public method

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

Usage Example:

    $patternToken = (new \yii\db\sqlite\SqlTokenizer('SELECT any FROM any'))->tokenize();
    if ($sqlToken->matches($patternToken, 0, $firstMatchIndex, $lastMatchIndex)) {
        // ...
    }

public boolean matches ( yii\db\SqlToken $patternToken, $offset = 0, &$firstMatchIndex = null, &$lastMatchIndex = null )
$patternToken | yii\db\SqlToken | Tokenized SQL code to match against. In addition to normal 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.

Source code

                    public function matches(SqlToken $patternToken, $offset = 0, &$firstMatchIndex = null, &$lastMatchIndex = null)
    {
        if (!$patternToken->getHasChildren()) {
            return false;
        }
        $patternToken = $patternToken[0];
        return $this->tokensMatch($patternToken, $this, $offset, $firstMatchIndex, $lastMatchIndex);
    }

offsetExists() public method

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

This method is required by the SPL ArrayAccess interface. It is implicitly called when you use something like `isset($token[$offset])`.

public boolean offsetExists ( $offset )
$offset | integer | Child token offset.
return | boolean | Whether the token exists.

Source code

                    #[\ReturnTypeWillChange]
    public function offsetExists($offset)
    {
        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 ArrayAccess interface. It is implicitly called when you use something like `$child = $token[$offset];`.

public yii\db\SqlToken|null offsetGet ( $offset )
$offset | integer | Child token offset.
return | yii\db\SqlToken|null | The child token at the specified offset, `null` if there's no token.

Source code

                    #[\ReturnTypeWillChange]
    public function offsetGet($offset)
    {
        $offset = $this->calculateOffset($offset);
        return isset($this->_children[$offset]) ? $this->_children[$offset] : null;
    }

offsetSet() public method

Adds a child token to the token.

This method is required by the SPL ArrayAccess interface. It is implicitly called when you use something like `$token[$offset] = $child;`.

public void offsetSet ( $offset, $token )
$offset | integer|null | Child token offset.
$token | yii\db\SqlToken | Token to be added.

Source code

                    #[\ReturnTypeWillChange]
    public function offsetSet($offset, $token)
    {
        $token->parent = $this;
        if ($offset === null) {
            $this->_children[] = $token;
        } else {
            $this->_children[$this->calculateOffset($offset)] = $token;
        }
        $this->updateCollectionOffsets();
    }

offsetUnset() public method

Removes a child token at the specified offset.

This method is required by the SPL ArrayAccess interface. It is implicitly called when you use something like `unset($token[$offset])`.

public void offsetUnset ( $offset )
$offset | integer | Child token offset.

Source code

                    #[\ReturnTypeWillChange]
    public function offsetUnset($offset)
    {
        $offset = $this->calculateOffset($offset);
        if (isset($this->_children[$offset])) {
            array_splice($this->_children, $offset, 1);
        }
        $this->updateCollectionOffsets();
    }

setChildren() public method

Sets a list of child tokens.

public void setChildren ( $children )
$children | yii\db\SqlToken[] | Child tokens.

Source code

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