SqlToken.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. use yii\base\BaseObject;
  9. /**
  10. * SqlToken represents SQL tokens produced by [[SqlTokenizer]] or its child classes.
  11. *
  12. * @property SqlToken[] $children Child tokens.
  13. * @property bool $hasChildren Whether the token has children. This property is read-only.
  14. * @property bool $isCollection Whether the token represents a collection of tokens. This property is
  15. * read-only.
  16. * @property string $sql SQL code. This property is read-only.
  17. *
  18. * @author Sergey Makinen <sergey@makinen.ru>
  19. * @since 2.0.13
  20. */
  21. class SqlToken extends BaseObject implements \ArrayAccess
  22. {
  23. const TYPE_CODE = 0;
  24. const TYPE_STATEMENT = 1;
  25. const TYPE_TOKEN = 2;
  26. const TYPE_PARENTHESIS = 3;
  27. const TYPE_KEYWORD = 4;
  28. const TYPE_OPERATOR = 5;
  29. const TYPE_IDENTIFIER = 6;
  30. const TYPE_STRING_LITERAL = 7;
  31. /**
  32. * @var int token type. It has to be one of the following constants:
  33. *
  34. * - [[TYPE_CODE]]
  35. * - [[TYPE_STATEMENT]]
  36. * - [[TYPE_TOKEN]]
  37. * - [[TYPE_PARENTHESIS]]
  38. * - [[TYPE_KEYWORD]]
  39. * - [[TYPE_OPERATOR]]
  40. * - [[TYPE_IDENTIFIER]]
  41. * - [[TYPE_STRING_LITERAL]]
  42. */
  43. public $type = self::TYPE_TOKEN;
  44. /**
  45. * @var string|null token content.
  46. */
  47. public $content;
  48. /**
  49. * @var int original SQL token start position.
  50. */
  51. public $startOffset;
  52. /**
  53. * @var int original SQL token end position.
  54. */
  55. public $endOffset;
  56. /**
  57. * @var SqlToken parent token.
  58. */
  59. public $parent;
  60. /**
  61. * @var SqlToken[] token children.
  62. */
  63. private $_children = [];
  64. /**
  65. * Returns the SQL code representing the token.
  66. * @return string SQL code.
  67. */
  68. public function __toString()
  69. {
  70. return $this->getSql();
  71. }
  72. /**
  73. * Returns whether there is a child token at the specified offset.
  74. * This method is required by the SPL [[\ArrayAccess]] interface.
  75. * It is implicitly called when you use something like `isset($token[$offset])`.
  76. * @param int $offset child token offset.
  77. * @return bool whether the token exists.
  78. */
  79. public function offsetExists($offset)
  80. {
  81. return isset($this->_children[$this->calculateOffset($offset)]);
  82. }
  83. /**
  84. * Returns a child token at the specified offset.
  85. * This method is required by the SPL [[\ArrayAccess]] interface.
  86. * It is implicitly called when you use something like `$child = $token[$offset];`.
  87. * @param int $offset child token offset.
  88. * @return SqlToken|null the child token at the specified offset, `null` if there's no token.
  89. */
  90. public function offsetGet($offset)
  91. {
  92. $offset = $this->calculateOffset($offset);
  93. return isset($this->_children[$offset]) ? $this->_children[$offset] : null;
  94. }
  95. /**
  96. * Adds a child token to the token.
  97. * This method is required by the SPL [[\ArrayAccess]] interface.
  98. * It is implicitly called when you use something like `$token[$offset] = $child;`.
  99. * @param int|null $offset child token offset.
  100. * @param SqlToken $token token to be added.
  101. */
  102. public function offsetSet($offset, $token)
  103. {
  104. $token->parent = $this;
  105. if ($offset === null) {
  106. $this->_children[] = $token;
  107. } else {
  108. $this->_children[$this->calculateOffset($offset)] = $token;
  109. }
  110. $this->updateCollectionOffsets();
  111. }
  112. /**
  113. * Removes a child token at the specified offset.
  114. * This method is required by the SPL [[\ArrayAccess]] interface.
  115. * It is implicitly called when you use something like `unset($token[$offset])`.
  116. * @param int $offset child token offset.
  117. */
  118. public function offsetUnset($offset)
  119. {
  120. $offset = $this->calculateOffset($offset);
  121. if (isset($this->_children[$offset])) {
  122. array_splice($this->_children, $offset, 1);
  123. }
  124. $this->updateCollectionOffsets();
  125. }
  126. /**
  127. * Returns child tokens.
  128. * @return SqlToken[] child tokens.
  129. */
  130. public function getChildren()
  131. {
  132. return $this->_children;
  133. }
  134. /**
  135. * Sets a list of child tokens.
  136. * @param SqlToken[] $children child tokens.
  137. */
  138. public function setChildren($children)
  139. {
  140. $this->_children = [];
  141. foreach ($children as $child) {
  142. $child->parent = $this;
  143. $this->_children[] = $child;
  144. }
  145. $this->updateCollectionOffsets();
  146. }
  147. /**
  148. * Returns whether the token represents a collection of tokens.
  149. * @return bool whether the token represents a collection of tokens.
  150. */
  151. public function getIsCollection()
  152. {
  153. return in_array($this->type, [
  154. self::TYPE_CODE,
  155. self::TYPE_STATEMENT,
  156. self::TYPE_PARENTHESIS,
  157. ], true);
  158. }
  159. /**
  160. * Returns whether the token represents a collection of tokens and has non-zero number of children.
  161. * @return bool whether the token has children.
  162. */
  163. public function getHasChildren()
  164. {
  165. return $this->getIsCollection() && !empty($this->_children);
  166. }
  167. /**
  168. * Returns the SQL code representing the token.
  169. * @return string SQL code.
  170. */
  171. public function getSql()
  172. {
  173. $code = $this;
  174. while ($code->parent !== null) {
  175. $code = $code->parent;
  176. }
  177. return mb_substr($code->content, $this->startOffset, $this->endOffset - $this->startOffset, 'UTF-8');
  178. }
  179. /**
  180. * Returns whether this token (including its children) matches the specified "pattern" SQL code.
  181. *
  182. * Usage Example:
  183. *
  184. * ```php
  185. * $patternToken = (new \yii\db\sqlite\SqlTokenizer('SELECT any FROM any'))->tokenize();
  186. * if ($sqlToken->matches($patternToken, 0, $firstMatchIndex, $lastMatchIndex)) {
  187. * // ...
  188. * }
  189. * ```
  190. *
  191. * @param SqlToken $patternToken tokenized SQL code to match against. In addition to normal SQL, the
  192. * `any` keyword is supported which will match any number of keywords, identifiers, whitespaces.
  193. * @param int $offset token children offset to start lookup with.
  194. * @param int|null $firstMatchIndex token children offset where a successful match begins.
  195. * @param int|null $lastMatchIndex token children offset where a successful match ends.
  196. * @return bool whether this token matches the pattern SQL code.
  197. */
  198. public function matches(SqlToken $patternToken, $offset = 0, &$firstMatchIndex = null, &$lastMatchIndex = null)
  199. {
  200. if (!$patternToken->getHasChildren()) {
  201. return false;
  202. }
  203. $patternToken = $patternToken[0];
  204. return $this->tokensMatch($patternToken, $this, $offset, $firstMatchIndex, $lastMatchIndex);
  205. }
  206. /**
  207. * Tests the given token to match the specified pattern token.
  208. * @param SqlToken $patternToken
  209. * @param SqlToken $token
  210. * @param int $offset
  211. * @param int|null $firstMatchIndex
  212. * @param int|null $lastMatchIndex
  213. * @return bool
  214. */
  215. private function tokensMatch(SqlToken $patternToken, SqlToken $token, $offset = 0, &$firstMatchIndex = null, &$lastMatchIndex = null)
  216. {
  217. if (
  218. $patternToken->getIsCollection() !== $token->getIsCollection()
  219. || (!$patternToken->getIsCollection() && $patternToken->content !== $token->content)
  220. ) {
  221. return false;
  222. }
  223. if ($patternToken->children === $token->children) {
  224. $firstMatchIndex = $lastMatchIndex = $offset;
  225. return true;
  226. }
  227. $firstMatchIndex = $lastMatchIndex = null;
  228. $wildcard = false;
  229. for ($index = 0, $count = count($patternToken->children); $index < $count; $index++) {
  230. // Here we iterate token by token with an exception of "any" that toggles
  231. // an iteration until we matched with a next pattern token or EOF.
  232. if ($patternToken[$index]->content === 'any') {
  233. $wildcard = true;
  234. continue;
  235. }
  236. for ($limit = $wildcard ? count($token->children) : $offset + 1; $offset < $limit; $offset++) {
  237. if (!$wildcard && !isset($token[$offset])) {
  238. break;
  239. }
  240. if (!$this->tokensMatch($patternToken[$index], $token[$offset])) {
  241. continue;
  242. }
  243. if ($firstMatchIndex === null) {
  244. $firstMatchIndex = $offset;
  245. $lastMatchIndex = $offset;
  246. } else {
  247. $lastMatchIndex = $offset;
  248. }
  249. $wildcard = false;
  250. $offset++;
  251. continue 2;
  252. }
  253. return false;
  254. }
  255. return true;
  256. }
  257. /**
  258. * Returns an absolute offset in the children array.
  259. * @param int $offset
  260. * @return int
  261. */
  262. private function calculateOffset($offset)
  263. {
  264. if ($offset >= 0) {
  265. return $offset;
  266. }
  267. return count($this->_children) + $offset;
  268. }
  269. /**
  270. * Updates token SQL code start and end offsets based on its children.
  271. */
  272. private function updateCollectionOffsets()
  273. {
  274. if (!empty($this->_children)) {
  275. $this->startOffset = reset($this->_children)->startOffset;
  276. $this->endOffset = end($this->_children)->endOffset;
  277. }
  278. if ($this->parent !== null) {
  279. $this->parent->updateCollectionOffsets();
  280. }
  281. }
  282. }