SqlToken
extends BaseObject
in package
implements
ArrayAccess
SqlToken represents SQL tokens produced by [[SqlTokenizer]] or its child classes.
Tags
Table of Contents
Interfaces
- ArrayAccess
Constants
- TYPE_CODE = 0
- TYPE_IDENTIFIER = 6
- TYPE_KEYWORD = 4
- TYPE_OPERATOR = 5
- TYPE_PARENTHESIS = 3
- TYPE_STATEMENT = 1
- TYPE_STRING_LITERAL = 7
- TYPE_TOKEN = 2
Properties
- $children : array<string|int, SqlToken>
- $content : string|null
- $endOffset : int
- $hasChildren : bool
- $isCollection : bool
- $parent : SqlToken
- $sql : string
- $startOffset : int
- $type : int
- $_children : array<string|int, SqlToken>
Methods
- __call() : mixed
- Calls the named method which is not a class method.
- __construct() : mixed
- Constructor.
- __get() : mixed
- Returns the value of an object property.
- __isset() : bool
- Checks if a property is set, i.e. defined and not null.
- __set() : mixed
- Sets value of an object property.
- __toString() : string
- Returns the SQL code representing the token.
- __unset() : mixed
- Sets an object property to null.
- canGetProperty() : bool
- Returns a value indicating whether a property can be read.
- canSetProperty() : bool
- Returns a value indicating whether a property can be set.
- className() : string
- Returns the fully qualified name of this class.
- getChildren() : array<string|int, SqlToken>
- Returns child tokens.
- getHasChildren() : bool
- Returns whether the token represents a collection of tokens and has non-zero number of children.
- getIsCollection() : bool
- Returns whether the token represents a collection of tokens.
- getSql() : string
- Returns the SQL code representing the token.
- hasMethod() : bool
- Returns a value indicating whether a method is defined.
- hasProperty() : bool
- Returns a value indicating whether a property is defined.
- init() : mixed
- Initializes the object.
- matches() : bool
- Returns whether this token (including its children) matches the specified "pattern" SQL code.
- offsetExists() : bool
- Returns whether there is a child token at the specified offset.
- offsetGet() : SqlToken|null
- Returns a child token at the specified offset.
- offsetSet() : mixed
- Adds a child token to the token.
- offsetUnset() : mixed
- Removes a child token at the specified offset.
- setChildren() : mixed
- Sets a list of child tokens.
- calculateOffset() : int
- Returns an absolute offset in the children array.
- tokensMatch() : bool
- Tests the given token to match the specified pattern token.
- updateCollectionOffsets() : mixed
- Updates token SQL code start and end offsets based on its children.
Constants
TYPE_CODE
public
mixed
TYPE_CODE
= 0
TYPE_IDENTIFIER
public
mixed
TYPE_IDENTIFIER
= 6
TYPE_KEYWORD
public
mixed
TYPE_KEYWORD
= 4
TYPE_OPERATOR
public
mixed
TYPE_OPERATOR
= 5
TYPE_PARENTHESIS
public
mixed
TYPE_PARENTHESIS
= 3
TYPE_STATEMENT
public
mixed
TYPE_STATEMENT
= 1
TYPE_STRING_LITERAL
public
mixed
TYPE_STRING_LITERAL
= 7
TYPE_TOKEN
public
mixed
TYPE_TOKEN
= 2
Properties
$children
public
array<string|int, SqlToken>
$children
Child tokens.
$content
public
string|null
$content
token content.
$endOffset
public
int
$endOffset
original SQL token end position.
$hasChildren read-only
public
bool
$hasChildren
Whether the token has children.
$isCollection read-only
public
bool
$isCollection
Whether the token represents a collection of tokens.
$parent
public
SqlToken
$parent
parent token.
$sql read-only
public
string
$sql
SQL code.
$startOffset
public
int
$startOffset
original SQL token start position.
$type
public
int
$type
= self::TYPE_TOKEN
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]]
$_children
private
array<string|int, SqlToken>
$_children
= []
token children.
Methods
__call()
Calls the named method which is not a class method.
public
__call(string $name, array<string|int, mixed> $params) : mixed
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.
Parameters
- $name : string
-
the method name
- $params : array<string|int, mixed>
-
method parameters
Tags
Return values
mixed —the method return value
__construct()
Constructor.
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
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.
Parameters
- $config : array<string|int, mixed> = []
-
name-value pairs that will be used to initialize the object properties
__get()
Returns the value of an object property.
public
__get(string $name) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
Parameters
- $name : string
-
the property name
Tags
Return values
mixed —the property value
__isset()
Checks if a property is set, i.e. defined and not null.
public
__isset(string $name) : bool
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.
Parameters
- $name : string
-
the property name or the event name
Tags
Return values
bool —whether the named property is set (not null).
__set()
Sets value of an object property.
public
__set(string $name, mixed $value) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
Parameters
- $name : string
-
the property name or the event name
- $value : mixed
-
the property value
Tags
__toString()
Returns the SQL code representing the token.
public
__toString() : string
Return values
string —SQL code.
__unset()
Sets an object property to null.
public
__unset(string $name) : mixed
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.
Parameters
- $name : string
-
the property name
Tags
canGetProperty()
Returns a value indicating whether a property can be read.
public
canGetProperty(string $name[, bool $checkVars = true ]) : bool
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);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property can be read
canSetProperty()
Returns a value indicating whether a property can be set.
public
canSetProperty(string $name[, bool $checkVars = true ]) : bool
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);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property can be written
className()
Returns the fully qualified name of this class.
public
static className() : string
Tags
Return values
string —the fully qualified name of this class.
getChildren()
Returns child tokens.
public
getChildren() : array<string|int, SqlToken>
Return values
array<string|int, SqlToken> —child tokens.
getHasChildren()
Returns whether the token represents a collection of tokens and has non-zero number of children.
public
getHasChildren() : bool
Return values
bool —whether the token has children.
getIsCollection()
Returns whether the token represents a collection of tokens.
public
getIsCollection() : bool
Return values
bool —whether the token represents a collection of tokens.
getSql()
Returns the SQL code representing the token.
public
getSql() : string
Return values
string —SQL code.
hasMethod()
Returns a value indicating whether a method is defined.
public
hasMethod(string $name) : bool
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
Parameters
- $name : string
-
the method name
Return values
bool —whether the method is defined
hasProperty()
Returns a value indicating whether a property is defined.
public
hasProperty(string $name[, bool $checkVars = true ]) : bool
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);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property is defined
init()
Initializes the object.
public
init() : mixed
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
matches()
Returns whether this token (including its children) matches the specified "pattern" SQL code.
public
matches(SqlToken $patternToken[, int $offset = 0 ][, int|null &$firstMatchIndex = null ][, int|null &$lastMatchIndex = null ]) : bool
Usage Example:
$patternToken = (new \yii\db\sqlite\SqlTokenizer('SELECT any FROM any'))->tokenize();
if ($sqlToken->matches($patternToken, 0, $firstMatchIndex, $lastMatchIndex)) {
// ...
}
Parameters
- $patternToken : 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 : int = 0
-
token children offset to start lookup with.
- $firstMatchIndex : int|null = null
-
token children offset where a successful match begins.
- $lastMatchIndex : int|null = null
-
token children offset where a successful match ends.
Return values
bool —whether this token matches the pattern SQL code.
offsetExists()
Returns whether there is a child token at the specified offset.
public
offsetExists(int $offset) : bool
This method is required by the SPL [[\ArrayAccess]] interface.
It is implicitly called when you use something like isset($token[$offset])
.
Parameters
- $offset : int
-
child token offset.
Attributes
Return values
bool —whether the token exists.
offsetGet()
Returns a child token at the specified offset.
public
offsetGet(int $offset) : SqlToken|null
This method is required by the SPL [[\ArrayAccess]] interface.
It is implicitly called when you use something like $child = $token[$offset];
.
Parameters
- $offset : int
-
child token offset.
Attributes
Return values
SqlToken|null —the child token at the specified offset, null
if there's no token.
offsetSet()
Adds a child token to the token.
public
offsetSet(int|null $offset, SqlToken $token) : mixed
This method is required by the SPL [[\ArrayAccess]] interface.
It is implicitly called when you use something like $token[$offset] = $child;
.
Parameters
- $offset : int|null
-
child token offset.
- $token : SqlToken
-
token to be added.
Attributes
offsetUnset()
Removes a child token at the specified offset.
public
offsetUnset(int $offset) : mixed
This method is required by the SPL [[\ArrayAccess]] interface.
It is implicitly called when you use something like unset($token[$offset])
.
Parameters
- $offset : int
-
child token offset.
Attributes
setChildren()
Sets a list of child tokens.
public
setChildren(array<string|int, SqlToken> $children) : mixed
Parameters
- $children : array<string|int, SqlToken>
-
child tokens.
calculateOffset()
Returns an absolute offset in the children array.
private
calculateOffset(int $offset) : int
Parameters
- $offset : int
Return values
inttokensMatch()
Tests the given token to match the specified pattern token.
private
tokensMatch(SqlToken $patternToken, SqlToken $token[, int $offset = 0 ][, int|null &$firstMatchIndex = null ][, int|null &$lastMatchIndex = null ]) : bool
Parameters
- $patternToken : SqlToken
- $token : SqlToken
- $offset : int = 0
- $firstMatchIndex : int|null = null
- $lastMatchIndex : int|null = null
Return values
boolupdateCollectionOffsets()
Updates token SQL code start and end offsets based on its children.
private
updateCollectionOffsets() : mixed