PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/parser_sql/positions/PositionCalculator.php

https://gitlab.com/staging06/myproject
PHP | 221 lines | 180 code | 3 blank | 38 comment | 0 complexity | e375eb88e5d56def00ea00820bed0db6 MD5 | raw file
  1. <?php
  2. /**
  3. * position-calculator.php
  4. *
  5. * This file implements the calculator for the position elements of
  6. * the output of the PHPSQLParser.
  7. *
  8. * Copyright (c) 2010-2012, Justin Swanhart
  9. * with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * * Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  23. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  25. * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  27. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31. * DAMAGE.
  32. */
  33. require_once dirname(__FILE__) . '/../utils/PHPSQLParserConstants.php';
  34. require_once dirname(__FILE__) . '/../exceptions/UnableToCalculatePositionException.php';
  35. /**
  36. * This class calculates the positions
  37. * of base_expr within the original SQL statement.
  38. *
  39. * @author arothe <andre.rothe@phosco.info>
  40. *
  41. */
  42. class PositionCalculator {
  43. private static $_allowedOnOperator = array("\t", "\n", "\r", " ", ",", "(", ")", "_", "'", "\"");
  44. private static $_allowedOnOther = array("\t", "\n", "\r", " ", ",", "(", ")", "<", ">", "*", "+", "-", "/", "|",
  45. "&", "=", "!", ";");
  46. private function _printPos($text, $sql, $charPos, $key, $parsed, $backtracking) {
  47. if (!isset($_ENV['DEBUG'])) {
  48. return;
  49. }
  50. $spaces = "";
  51. $caller = debug_backtrace();
  52. $i = 1;
  53. while ($caller[$i]['function'] === 'lookForBaseExpression') {
  54. $spaces .= " ";
  55. $i++;
  56. }
  57. $holdem = substr($sql, 0, $charPos) . "^" . substr($sql, $charPos);
  58. echo $spaces . $text . " key:" . $key . " parsed:" . $parsed . " back:" . serialize($backtracking) . " "
  59. . $holdem . "\n";
  60. }
  61. public function setPositionsWithinSQL($sql, $parsed) {
  62. $charPos = 0;
  63. $backtracking = array();
  64. $this->lookForBaseExpression($sql, $charPos, $parsed, 0, $backtracking);
  65. return $parsed;
  66. }
  67. private function findPositionWithinString($sql, $value, $expr_type) {
  68. $offset = 0;
  69. $ok = false;
  70. while (true) {
  71. $pos = strpos($sql, $value, $offset);
  72. if ($pos === false) {
  73. break;
  74. }
  75. $before = "";
  76. if ($pos > 0) {
  77. $before = $sql[$pos - 1];
  78. }
  79. $after = "";
  80. if (isset($sql[$pos + strlen($value)])) {
  81. $after = $sql[$pos + strlen($value)];
  82. }
  83. // if we have an operator, it should be surrounded by
  84. // whitespace, comma, parenthesis, digit or letter, end_of_string
  85. // an operator should not be surrounded by another operator
  86. if ($expr_type === 'operator') {
  87. $ok = ($before === "" || in_array($before, self::$_allowedOnOperator, true))
  88. || (strtolower($before) >= 'a' && strtolower($before) <= 'z') || ($before >= '0' && $before <= '9');
  89. $ok = $ok
  90. && ($after === "" || in_array($after, self::$_allowedOnOperator, true)
  91. || (strtolower($after) >= 'a' && strtolower($after) <= 'z') || ($after >= '0' && $after <= '9')
  92. || ($after === '?') || ($after === '@'));
  93. if (!$ok) {
  94. $offset = $pos + 1;
  95. continue;
  96. }
  97. break;
  98. }
  99. // in all other cases we accept
  100. // whitespace, comma, operators, parenthesis and end_of_string
  101. $ok = ($before === "" || in_array($before, self::$_allowedOnOther, true));
  102. $ok = $ok && ($after === "" || in_array($after, self::$_allowedOnOther, true));
  103. if ($ok) {
  104. break;
  105. }
  106. $offset = $pos + 1;
  107. }
  108. return $pos;
  109. }
  110. private function lookForBaseExpression($sql, &$charPos, &$parsed, $key, &$backtracking) {
  111. if (!is_numeric($key)) {
  112. if (($key === 'UNION' || $key === 'UNION ALL')
  113. || ($key === 'expr_type' && $parsed === ExpressionType::EXPRESSION)
  114. || ($key === 'expr_type' && $parsed === ExpressionType::SUBQUERY)
  115. || ($key === 'expr_type' && $parsed === ExpressionType::BRACKET_EXPRESSION)
  116. || ($key === 'expr_type' && $parsed === ExpressionType::TABLE_EXPRESSION)
  117. || ($key === 'expr_type' && $parsed === ExpressionType::RECORD)
  118. || ($key === 'expr_type' && $parsed === ExpressionType::IN_LIST)
  119. || ($key === 'expr_type' && $parsed === ExpressionType::MATCH_ARGUMENTS)
  120. || ($key === 'expr_type' && $parsed === ExpressionType::TABLE)
  121. || ($key === 'expr_type' && $parsed === ExpressionType::TEMPORARY_TABLE)
  122. || ($key === 'expr_type' && $parsed === ExpressionType::COLUMN_TYPE)
  123. || ($key === 'expr_type' && $parsed === ExpressionType::COLDEF)
  124. || ($key === 'expr_type' && $parsed === ExpressionType::PRIMARY_KEY)
  125. || ($key === 'expr_type' && $parsed === ExpressionType::CONSTRAINT)
  126. || ($key === 'expr_type' && $parsed === ExpressionType::COLUMN_LIST)
  127. || ($key === 'expr_type' && $parsed === ExpressionType::CHECK)
  128. || ($key === 'expr_type' && $parsed === ExpressionType::COLLATE)
  129. || ($key === 'expr_type' && $parsed === ExpressionType::LIKE)
  130. || ($key === 'expr_type' && $parsed === ExpressionType::INDEX)
  131. || ($key === 'select-option' && $parsed !== false) || ($key === 'alias' && $parsed !== false)) {
  132. // we hold the current position and come back after the next base_expr
  133. // we do this, because the next base_expr contains the complete expression/subquery/record
  134. // and we have to look into it too
  135. $backtracking[] = $charPos;
  136. } elseif (($key === 'ref_clause' || $key === 'columns') && $parsed !== false) {
  137. // we hold the current position and come back after n base_expr(s)
  138. // there is an array of sub-elements before (!) the base_expr clause of the current element
  139. // so we go through the sub-elements and must come at the end
  140. $backtracking[] = $charPos;
  141. for ($i = 1; $i < count($parsed); $i++) {
  142. $backtracking[] = false; // backtracking only after n base_expr!
  143. }
  144. } elseif (($key === 'sub_tree' && $parsed !== false) || ($key === 'options' && $parsed !== false)) {
  145. // we prevent wrong backtracking on subtrees (too much array_pop())
  146. // there is an array of sub-elements after(!) the base_expr clause of the current element
  147. // so we go through the sub-elements and must not come back at the end
  148. for ($i = 1; $i < count($parsed); $i++) {
  149. $backtracking[] = false;
  150. }
  151. } elseif (($key === 'TABLE') || ($key === 'create-def' && $parsed !== false)) {
  152. // do nothing
  153. } else {
  154. // move the current pos after the keyword
  155. // SELECT, WHERE, INSERT etc.
  156. if (PHPSQLParserConstants::isReserved($key)) {
  157. $charPos = stripos($sql, $key, $charPos);
  158. $charPos += strlen($key);
  159. }
  160. }
  161. }
  162. if (!is_array($parsed)) {
  163. return;
  164. }
  165. foreach ($parsed as $key => $value) {
  166. if ($key === 'base_expr') {
  167. //$this->_printPos("0", $sql, $charPos, $key, $value, $backtracking);
  168. $subject = substr($sql, $charPos);
  169. $pos = $this->findPositionWithinString($subject, $value,
  170. isset($parsed['expr_type']) ? $parsed['expr_type'] : 'alias');
  171. if ($pos === false) {
  172. throw new UnableToCalculatePositionException($value, $subject);
  173. }
  174. $parsed['position'] = $charPos + $pos;
  175. $charPos += $pos + strlen($value);
  176. //$this->_printPos("1", $sql, $charPos, $key, $value, $backtracking);
  177. $oldPos = array_pop($backtracking);
  178. if (isset($oldPos) && $oldPos !== false) {
  179. $charPos = $oldPos;
  180. }
  181. //$this->_printPos("2", $sql, $charPos, $key, $value, $backtracking);
  182. } else {
  183. $this->lookForBaseExpression($sql, $charPos, $parsed[$key], $key, $backtracking);
  184. }
  185. }
  186. }
  187. }
  188. ?>