SQLParserUtils.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 LGPL. 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) {
  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; ($j < $stmtLen && preg_match('(([:a-zA-Z0-9_]{1}))', $statement[$j])); $j++) {
  60. $name .= $statement[$j];
  61. }
  62. $paramMap[$name][] = $i; // 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
  76. * @param array $params
  77. * @param array $types
  78. */
  79. static public function expandListParameters($query, $params, $types)
  80. {
  81. $isPositional = is_int(key($params));
  82. $arrayPositions = array();
  83. $bindIndex = -1;
  84. foreach ($types AS $name => $type) {
  85. ++$bindIndex;
  86. if ($type === Connection::PARAM_INT_ARRAY || $type === Connection::PARAM_STR_ARRAY) {
  87. if ($isPositional) {
  88. $name = $bindIndex;
  89. }
  90. $arrayPositions[$name] = false;
  91. }
  92. }
  93. if ((!$arrayPositions && $isPositional) || (count($params) != count($types))) {
  94. return array($query, $params, $types);
  95. }
  96. $paramPos = self::getPlaceholderPositions($query, $isPositional);
  97. if ($isPositional) {
  98. $paramOffset = 0;
  99. $queryOffset = 0;
  100. foreach ($paramPos AS $needle => $needlePos) {
  101. if (!isset($arrayPositions[$needle])) {
  102. continue;
  103. }
  104. $needle += $paramOffset;
  105. $needlePos += $queryOffset;
  106. $len = count($params[$needle]);
  107. $params = array_merge(
  108. array_slice($params, 0, $needle),
  109. $params[$needle],
  110. array_slice($params, $needle + 1)
  111. );
  112. $types = array_merge(
  113. array_slice($types, 0, $needle),
  114. array_fill(0, $len, $types[$needle] - Connection::ARRAY_PARAM_OFFSET), // array needles are at PDO::PARAM_* + 100
  115. array_slice($types, $needle + 1)
  116. );
  117. $expandStr = implode(", ", array_fill(0, $len, "?"));
  118. $query = substr($query, 0, $needlePos) . $expandStr . substr($query, $needlePos + 1);
  119. $paramOffset += ($len - 1); // Grows larger by number of parameters minus the replaced needle.
  120. $queryOffset += (strlen($expandStr) - 1);
  121. }
  122. } else {
  123. $queryOffset= 0;
  124. $typesOrd = array();
  125. $paramsOrd = array();
  126. foreach ($paramPos as $needle => $needlePos) {
  127. $paramLen = strlen($needle);
  128. $token = substr($needle,0,1);
  129. $needle = substr($needle,1);
  130. $value = $params[$needle];
  131. if (!isset($arrayPositions[$needle])) {
  132. foreach ($needlePos as $pos) {
  133. $pos += $queryOffset;
  134. $queryOffset -= ($paramLen - 1);
  135. $paramsOrd[] = $value;
  136. $typesOrd[] = $types[$needle];
  137. $query = substr($query, 0, $pos) . '?' . substr($query, ($pos + $paramLen));
  138. }
  139. } else {
  140. $len = count($value);
  141. $expandStr = implode(", ", array_fill(0, $len, "?"));
  142. foreach ($needlePos as $pos) {
  143. foreach ($value as $val) {
  144. $paramsOrd[] = $val;
  145. $typesOrd[] = $types[$needle] - 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. }
  152. }
  153. $types = $typesOrd;
  154. $params = $paramsOrd;
  155. }
  156. return array($query, $params, $types);
  157. }
  158. }