SQLParserUtils.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL;
  20. use Doctrine\DBAL\Connection;
  21. /**
  22. * Utility class that parses sql statements with regard to types and parameters.
  23. *
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @link www.doctrine-project.com
  26. * @since 2.0
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. */
  29. class SQLParserUtils
  30. {
  31. const POSITIONAL_TOKEN = '\?';
  32. const NAMED_TOKEN = ':[a-zA-Z_][a-zA-Z0-9_]*';
  33. // Quote characters within string literals can be preceded by a backslash.
  34. const ESCAPED_SINGLE_QUOTED_TEXT = "'(?:[^'\\\\]|\\\\'|\\\\\\\\)*'";
  35. const ESCAPED_DOUBLE_QUOTED_TEXT = '"(?:[^"\\\\]|\\\\"|\\\\\\\\)*"';
  36. /**
  37. * Get an array of the placeholders in an sql statements as keys and their positions in the query string.
  38. *
  39. * Returns an integer => integer pair (indexed from zero) for a positional statement
  40. * and a string => int[] pair for a named statement.
  41. *
  42. * @param string $statement
  43. * @param bool $isPositional
  44. * @return array
  45. */
  46. static public function getPlaceholderPositions($statement, $isPositional = true)
  47. {
  48. $match = ($isPositional) ? '?' : ':';
  49. if (strpos($statement, $match) === false) {
  50. return array();
  51. }
  52. $token = ($isPositional) ? self::POSITIONAL_TOKEN : self::NAMED_TOKEN;
  53. $paramMap = array();
  54. foreach (self::getUnquotedStatementFragments($statement) as $fragment) {
  55. preg_match_all("/$token/", $fragment[0], $matches, PREG_OFFSET_CAPTURE);
  56. foreach ($matches[0] as $placeholder) {
  57. if ($isPositional) {
  58. $paramMap[] = $placeholder[1] + $fragment[1];
  59. } else {
  60. $pos = $placeholder[1] + $fragment[1];
  61. $paramMap[$pos] = substr($placeholder[0], 1, strlen($placeholder[0]));
  62. }
  63. }
  64. }
  65. return $paramMap;
  66. }
  67. /**
  68. * For a positional query this method can rewrite the sql statement with regard to array parameters.
  69. *
  70. * @param string $query The SQL query to execute.
  71. * @param array $params The parameters to bind to the query.
  72. * @param array $types The types the previous parameters are in.
  73. *
  74. * @return array
  75. */
  76. static public function expandListParameters($query, $params, $types)
  77. {
  78. $isPositional = is_int(key($params));
  79. $arrayPositions = array();
  80. $bindIndex = -1;
  81. foreach ($types as $name => $type) {
  82. ++$bindIndex;
  83. if ($type !== Connection::PARAM_INT_ARRAY && $type !== Connection::PARAM_STR_ARRAY) {
  84. continue;
  85. }
  86. if ($isPositional) {
  87. $name = $bindIndex;
  88. }
  89. $arrayPositions[$name] = false;
  90. }
  91. if (( ! $arrayPositions && $isPositional) || (count($params) != count($types))) {
  92. return array($query, $params, $types);
  93. }
  94. $paramPos = self::getPlaceholderPositions($query, $isPositional);
  95. if ($isPositional) {
  96. $paramOffset = 0;
  97. $queryOffset = 0;
  98. foreach ($paramPos as $needle => $needlePos) {
  99. if ( ! isset($arrayPositions[$needle])) {
  100. continue;
  101. }
  102. $needle += $paramOffset;
  103. $needlePos += $queryOffset;
  104. $count = count($params[$needle]);
  105. $params = array_merge(
  106. array_slice($params, 0, $needle),
  107. $params[$needle],
  108. array_slice($params, $needle + 1)
  109. );
  110. $types = array_merge(
  111. array_slice($types, 0, $needle),
  112. array_fill(0, $count, $types[$needle] - Connection::ARRAY_PARAM_OFFSET), // array needles are at PDO::PARAM_* + 100
  113. array_slice($types, $needle + 1)
  114. );
  115. $expandStr = implode(", ", array_fill(0, $count, "?"));
  116. $query = substr($query, 0, $needlePos) . $expandStr . substr($query, $needlePos + 1);
  117. $paramOffset += ($count - 1); // Grows larger by number of parameters minus the replaced needle.
  118. $queryOffset += (strlen($expandStr) - 1);
  119. }
  120. return array($query, $params, $types);
  121. }
  122. $queryOffset = 0;
  123. $typesOrd = array();
  124. $paramsOrd = array();
  125. foreach ($paramPos as $pos => $paramName) {
  126. $paramLen = strlen($paramName) + 1;
  127. $value = $params[$paramName];
  128. if ( ! isset($arrayPositions[$paramName])) {
  129. $pos += $queryOffset;
  130. $queryOffset -= ($paramLen - 1);
  131. $paramsOrd[] = $value;
  132. $typesOrd[] = $types[$paramName];
  133. $query = substr($query, 0, $pos) . '?' . substr($query, ($pos + $paramLen));
  134. continue;
  135. }
  136. $count = count($value);
  137. $expandStr = $count > 0 ? implode(', ', array_fill(0, $count, '?')) : '?';
  138. foreach ($value as $val) {
  139. $paramsOrd[] = $val;
  140. $typesOrd[] = $types[$paramName] - Connection::ARRAY_PARAM_OFFSET;
  141. }
  142. $pos += $queryOffset;
  143. $queryOffset += (strlen($expandStr) - $paramLen);
  144. $query = substr($query, 0, $pos) . $expandStr . substr($query, ($pos + $paramLen));
  145. }
  146. return array($query, $paramsOrd, $typesOrd);
  147. }
  148. /**
  149. * Slice the SQL statement around pairs of quotes and
  150. * return string fragments of SQL outside of quoted literals.
  151. * Each fragment is captured as a 2-element array:
  152. *
  153. * 0 => matched fragment string,
  154. * 1 => offset of fragment in $statement
  155. *
  156. * @param string $statement
  157. * @return array
  158. */
  159. static private function getUnquotedStatementFragments($statement)
  160. {
  161. $literal = self::ESCAPED_SINGLE_QUOTED_TEXT . '|' . self::ESCAPED_DOUBLE_QUOTED_TEXT;
  162. preg_match_all("/([^'\"]+)(?:$literal)?/s", $statement, $fragments, PREG_OFFSET_CAPTURE);
  163. return $fragments[1];
  164. }
  165. }