# SqlToken

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

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › SqlToken

| class | `yii\db\SqlToken` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0.13 |
| Source | [`framework/db/SqlToken.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/SqlToken.php#L26) |

SqlToken represents SQL tokens produced by [`SqlTokenizer`](/docs/api/yii/db/sqltokenizer/index.md) or its child classes.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-children"></span>`$children` | [`SqlToken`](/docs/api/yii/db/sqltoken/index.md)`[]` | Child tokens. | `SqlToken` |
| <span id="property-content"></span>`$content` | `string\|null` | Token content. Default: `null`. | `SqlToken` |
| <span id="property-endOffset"></span>`$endOffset` | `int` | Original SQL token end position. Default: `null`. | `SqlToken` |
| <span id="property-hasChildren"></span>`$hasChildren` read-only | `bool` | Whether the token has children. | `SqlToken` |
| <span id="property-isCollection"></span>`$isCollection` read-only | `bool` | Whether the token represents a collection of tokens. | `SqlToken` |
| <span id="property-parent"></span>`$parent` | [`SqlToken`](/docs/api/yii/db/sqltoken/index.md) | Parent token. Default: `null`. | `SqlToken` |
| <span id="property-sql"></span>`$sql` read-only | `string` | SQL code. | `SqlToken` |
| <span id="property-startOffset"></span>`$startOffset` | `int` | Original SQL token start position. Default: `null`. | `SqlToken` |
| [`$type`](#property-type) | `int` | Token type. Default: `self::TYPE_TOKEN`. | `SqlToken` |

## Methods

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

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-TYPE_CODE"></span>`TYPE_CODE` | `0` |  | `SqlToken` |
| <span id="const-TYPE_STATEMENT"></span>`TYPE_STATEMENT` | `1` |  | `SqlToken` |
| <span id="const-TYPE_TOKEN"></span>`TYPE_TOKEN` | `2` |  | `SqlToken` |
| <span id="const-TYPE_PARENTHESIS"></span>`TYPE_PARENTHESIS` | `3` |  | `SqlToken` |
| <span id="const-TYPE_KEYWORD"></span>`TYPE_KEYWORD` | `4` |  | `SqlToken` |
| <span id="const-TYPE_OPERATOR"></span>`TYPE_OPERATOR` | `5` |  | `SqlToken` |
| <span id="const-TYPE_IDENTIFIER"></span>`TYPE_IDENTIFIER` | `6` |  | `SqlToken` |
| <span id="const-TYPE_STRING_LITERAL"></span>`TYPE_STRING_LITERAL` | `7` |  | `SqlToken` |

## Property details

### $type <span id="property-type"></span>

```php
public int $type = self::TYPE_TOKEN
```

Token type.

It has to be one of the following constants:

- [`TYPE_CODE`](/docs/api/yii/db/sqltoken/index.md#const-TYPE_CODE)
- [`TYPE_STATEMENT`](/docs/api/yii/db/sqltoken/index.md#const-TYPE_STATEMENT)
- [`TYPE_TOKEN`](/docs/api/yii/db/sqltoken/index.md#const-TYPE_TOKEN)
- [`TYPE_PARENTHESIS`](/docs/api/yii/db/sqltoken/index.md#const-TYPE_PARENTHESIS)
- [`TYPE_KEYWORD`](/docs/api/yii/db/sqltoken/index.md#const-TYPE_KEYWORD)
- [`TYPE_OPERATOR`](/docs/api/yii/db/sqltoken/index.md#const-TYPE_OPERATOR)
- [`TYPE_IDENTIFIER`](/docs/api/yii/db/sqltoken/index.md#const-TYPE_IDENTIFIER)
- [`TYPE_STRING_LITERAL`](/docs/api/yii/db/sqltoken/index.md#const-TYPE_STRING_LITERAL)

## Method details

### __toString() <span id="method-__toString"></span>

```php
public function __toString(): string
```

Returns the SQL code representing the token.

**Returns**

`string`: SQL code.

### getChildren() <span id="method-getChildren"></span>

```php
public function getChildren(): yii\db\SqlToken[]
```

Returns child tokens.

**Returns**

[`SqlToken`](/docs/api/yii/db/sqltoken/index.md)`[]`: Child tokens.

### getHasChildren() <span id="method-getHasChildren"></span>

```php
public function getHasChildren(): bool
```

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

**Returns**

`bool`: Whether the token has children.

### getIsCollection() <span id="method-getIsCollection"></span>

```php
public function getIsCollection(): bool
```

Returns whether the token represents a collection of tokens.

**Returns**

`bool`: Whether the token represents a collection of tokens.

### getSql() <span id="method-getSql"></span>

```php
public function getSql(): string
```

Returns the SQL code representing the token.

**Returns**

`string`: SQL code.

### matches() <span id="method-matches"></span>

```php
public function matches(yii\db\SqlToken $patternToken, int $offset = 0, int|null &$firstMatchIndex = null, int|null &$lastMatchIndex = null): bool
```

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)) {
    // ...
}
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$patternToken` | [`SqlToken`](/docs/api/yii/db/sqltoken/index.md) | 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` | `int` | Token children offset to start lookup with. |
| `$firstMatchIndex` | `int\|null` | Token children offset where a successful match begins. |
| `$lastMatchIndex` | `int\|null` | Token children offset where a successful match ends. |

**Returns**

`bool`: Whether this token matches the pattern SQL code.

### offsetExists() <span id="method-offsetExists"></span>

```php
public function offsetExists(int $offset): bool
```

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

This method is required by the SPL [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php) interface.
It is implicitly called when you use something like `isset($token[$offset])`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int` | Child token offset. |

**Returns**

`bool`: Whether the token exists.

### offsetGet() <span id="method-offsetGet"></span>

```php
public function offsetGet(int $offset): yii\db\SqlToken|null
```

Returns a child token at the specified offset.

This method is required by the SPL [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php) interface.
It is implicitly called when you use something like `$child = $token[$offset];`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int` | Child token offset. |

**Returns**

[`SqlToken`](/docs/api/yii/db/sqltoken/index.md)`|null`: The child token at the specified offset, `null` if there's no token.

### offsetSet() <span id="method-offsetSet"></span>

```php
public function offsetSet(int|null $offset, yii\db\SqlToken $token): mixed
```

Adds a child token to the token.

This method is required by the SPL [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php) interface.
It is implicitly called when you use something like `$token[$offset] = $child;`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|null` | Child token offset. |
| `$token` | [`SqlToken`](/docs/api/yii/db/sqltoken/index.md) | Token to be added. |

**Returns**

`mixed`

### offsetUnset() <span id="method-offsetUnset"></span>

```php
public function offsetUnset(int $offset): mixed
```

Removes a child token at the specified offset.

This method is required by the SPL [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php) interface.
It is implicitly called when you use something like `unset($token[$offset])`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int` | Child token offset. |

**Returns**

`mixed`

### setChildren() <span id="method-setChildren"></span>

```php
public function setChildren(yii\db\SqlToken[] $children): mixed
```

Sets a list of child tokens.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$children` | [`SqlToken`](/docs/api/yii/db/sqltoken/index.md)`[]` | Child tokens. |

**Returns**

`mixed`