SQLParserUtils.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /**
  32. * Get an array of the placeholders in an sql statements as keys and their positions in the query string.
  33. *
  34. * Returns an integer => integer pair (indexed from zero) for a positional statement
  35. * and a string => int[] pair for a named statement.
  36. *
  37. * @param string $statement
  38. * @param bool $isPositional
  39. * @return array
  40. */
  41. static public function getPlaceholderPositions($statement, $isPositional = true)
  42. {
  43. $match = ($isPositional) ? '?' : ':';
  44. if (strpos($statement, $match) === false) {
  45. return array();
  46. }
  47. $count = 0;
  48. $inLiteral = false; // a valid query never starts with quotes
  49. $stmtLen = strlen($statement);
  50. $paramMap = array();
  51. for ($i = 0; $i < $stmtLen; $i++) {
  52. if ($statement[$i] == $match && !$inLiteral && ($isPositional || $statement[$i+1] != '=')) {
  53. // real positional parameter detected
  54. if ($isPositional) {
  55. $paramMap[$count] = $i;
  56. } else {
  57. $name = "";
  58. // TODO: Something faster/better to match this than regex?
  59. for ($j = $i + 1; ($j < $stmtLen && preg_match('(([a-zA-Z0-9_]{1}))', $statement[$j])); $j++) {
  60. $name .= $statement[$j];
  61. }
  62. $paramMap[$i] = $name; // named parameters can be duplicated!
  63. $i = $j;
  64. }
  65. ++$count;
  66. } else if ($statement[$i] == "'" || $statement[$i] == '"') {
  67. $inLiteral = ! $inLiteral; // switch state!
  68. }
  69. }
  70. return $paramMap;
  71. }
  72. /**
  73. * For a positional query this method can rewrite the sql statement with regard to array parameters.
  74. *
  75. * @param string $query The SQL query to execute.
  76. * @param array $params The parameters to bind to the query.
  77. * @param array $types The types the previous parameters are in.
  78. *
  79. * @return array
  80. */
  81. static public function expandListParameters($query, $params, $types)
  82. {
  83. $isPositional = is_int(key($params));
  84. $arrayPositions = array();
  85. $bindIndex = -1;
  86. foreach ($types as $name => $type) {
  87. ++$bindIndex;
  88. if ($type !== Connection::PARAM_INT_ARRAY && $type !== Connection::PARAM_STR_ARRAY) {
  89. continue;
  90. }
  91. if ($isPositional) {
  92. $name = $bindIndex;
  93. }
  94. $arrayPositions[$name] = false;
  95. }
  96. if (( ! $arrayPositions && $isPositional) || (count($params) != count($types))) {
  97. return array($query, $params, $types);
  98. }
  99. $paramPos = self::getPlaceholderPositions($query, $isPositional);
  100. if ($isPositional) {
  101. $paramOffset = 0;
  102. $queryOffset = 0;
  103. foreach ($paramPos as $needle => $needlePos) {
  104. if ( ! isset($arrayPositions[$needle])) {
  105. continue;
  106. }
  107. $needle += $paramOffset;
  108. $needlePos += $queryOffset;
  109. $count = count($params[$needle]);
  110. $params = array_merge(
  111. array_slice($params, 0, $needle),
  112. $params[$needle],
  113. array_slice($params, $needle + 1)
  114. );
  115. $types = array_merge(
  116. array_slice($types, 0, $needle),
  117. array_fill(0, $count, $types[$needle] - Connection::ARRAY_PARAM_OFFSET), // array needles are at PDO::PARAM_* + 100
  118. array_slice($types, $needle + 1)
  119. );
  120. $expandStr = implode(", ", array_fill(0, $count, "?"));
  121. $query = substr($query, 0, $needlePos) . $expandStr . substr($query, $needlePos + 1);
  122. $paramOffset += ($count - 1); // Grows larger by number of parameters minus the replaced needle.
  123. $queryOffset += (strlen($expandStr) - 1);
  124. }
  125. return array($query, $params, $types);
  126. }
  127. $queryOffset = 0;
  128. $typesOrd = array();
  129. $paramsOrd = array();
  130. foreach ($paramPos as $pos => $paramName) {
  131. $paramLen = strlen($paramName) + 1;
  132. $value = $params[$paramName];
  133. if ( ! isset($arrayPositions[$paramName])) {
  134. $pos += $queryOffset;
  135. $queryOffset -= ($paramLen - 1);
  136. $paramsOrd[] = $value;
  137. $typesOrd[] = $types[$paramName];
  138. $query = substr($query, 0, $pos) . '?' . substr($query, ($pos + $paramLen));
  139. continue;
  140. }
  141. $count = count($value);
  142. $expandStr = $count > 0 ? implode(', ', array_fill(0, $count, '?')) : '?';
  143. foreach ($value as $val) {
  144. $paramsOrd[] = $val;
  145. $typesOrd[] = $types[$paramName] - Connection::ARRAY_PARAM_OFFSET;
  146. }
  147. $pos += $queryOffset;
  148. $queryOffset += (strlen($expandStr) - $paramLen);
  149. $query = substr($query, 0, $pos) . $expandStr . substr($query, ($pos + $paramLen));
  150. }
  151. return array($query, $paramsOrd, $typesOrd);
  152. }
  153. }