SQLParserUtils.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace Doctrine\DBAL;
  3. use Doctrine\DBAL\Types\Type;
  4. use function array_fill;
  5. use function array_fill_keys;
  6. use function array_key_exists;
  7. use function array_keys;
  8. use function array_merge;
  9. use function array_slice;
  10. use function array_values;
  11. use function count;
  12. use function implode;
  13. use function is_int;
  14. use function key;
  15. use function ksort;
  16. use function preg_match_all;
  17. use function sprintf;
  18. use function strlen;
  19. use function strpos;
  20. use function substr;
  21. use const PREG_OFFSET_CAPTURE;
  22. /**
  23. * Utility class that parses sql statements with regard to types and parameters.
  24. */
  25. class SQLParserUtils
  26. {
  27. /**#@+
  28. *
  29. * @deprecated Will be removed as internal implementation details.
  30. */
  31. public const POSITIONAL_TOKEN = '\?';
  32. public const NAMED_TOKEN = '(?<!:):[a-zA-Z_][a-zA-Z0-9_]*';
  33. // Quote characters within string literals can be preceded by a backslash.
  34. public const ESCAPED_SINGLE_QUOTED_TEXT = "(?:'(?:\\\\)+'|'(?:[^'\\\\]|\\\\'?|'')*')";
  35. public const ESCAPED_DOUBLE_QUOTED_TEXT = '(?:"(?:\\\\)+"|"(?:[^"\\\\]|\\\\"?)*")';
  36. public const ESCAPED_BACKTICK_QUOTED_TEXT = '(?:`(?:\\\\)+`|`(?:[^`\\\\]|\\\\`?)*`)';
  37. /**#@-*/
  38. private const ESCAPED_BRACKET_QUOTED_TEXT = '(?<!\b(?i:ARRAY))\[(?:[^\]])*\]';
  39. /**
  40. * Gets an array of the placeholders in an sql statements as keys and their positions in the query string.
  41. *
  42. * For a statement with positional parameters, returns a zero-indexed list of placeholder position.
  43. * For a statement with named parameters, returns a map of placeholder positions to their parameter names.
  44. *
  45. * @deprecated Will be removed as internal implementation detail.
  46. *
  47. * @param string $statement
  48. * @param bool $isPositional
  49. *
  50. * @return int[]|string[]
  51. */
  52. public static function getPlaceholderPositions($statement, $isPositional = true)
  53. {
  54. return $isPositional
  55. ? self::getPositionalPlaceholderPositions($statement)
  56. : self::getNamedPlaceholderPositions($statement);
  57. }
  58. /**
  59. * Returns a zero-indexed list of placeholder position.
  60. *
  61. * @return list<int>
  62. */
  63. private static function getPositionalPlaceholderPositions(string $statement): array
  64. {
  65. return self::collectPlaceholders(
  66. $statement,
  67. '?',
  68. self::POSITIONAL_TOKEN,
  69. static function (string $_, int $placeholderPosition, int $fragmentPosition, array &$carry): void {
  70. $carry[] = $placeholderPosition + $fragmentPosition;
  71. }
  72. );
  73. }
  74. /**
  75. * Returns a map of placeholder positions to their parameter names.
  76. *
  77. * @return array<int,string>
  78. */
  79. private static function getNamedPlaceholderPositions(string $statement): array
  80. {
  81. return self::collectPlaceholders(
  82. $statement,
  83. ':',
  84. self::NAMED_TOKEN,
  85. static function (
  86. string $placeholder,
  87. int $placeholderPosition,
  88. int $fragmentPosition,
  89. array &$carry
  90. ): void {
  91. $carry[$placeholderPosition + $fragmentPosition] = substr($placeholder, 1);
  92. }
  93. );
  94. }
  95. /**
  96. * @return mixed[]
  97. */
  98. private static function collectPlaceholders(
  99. string $statement,
  100. string $match,
  101. string $token,
  102. callable $collector
  103. ): array {
  104. if (strpos($statement, $match) === false) {
  105. return [];
  106. }
  107. $carry = [];
  108. foreach (self::getUnquotedStatementFragments($statement) as $fragment) {
  109. preg_match_all('/' . $token . '/', $fragment[0], $matches, PREG_OFFSET_CAPTURE);
  110. foreach ($matches[0] as $placeholder) {
  111. $collector($placeholder[0], $placeholder[1], $fragment[1], $carry);
  112. }
  113. }
  114. return $carry;
  115. }
  116. /**
  117. * For a positional query this method can rewrite the sql statement with regard to array parameters.
  118. *
  119. * @param string $query SQL query
  120. * @param mixed[] $params Query parameters
  121. * @param array<int, Type|int|string|null>|array<string, Type|int|string|null> $types Parameter types
  122. *
  123. * @return mixed[]
  124. *
  125. * @throws SQLParserUtilsException
  126. */
  127. public static function expandListParameters($query, $params, $types)
  128. {
  129. $isPositional = is_int(key($params));
  130. $arrayPositions = [];
  131. $bindIndex = -1;
  132. if ($isPositional) {
  133. // make sure that $types has the same keys as $params
  134. // to allow omitting parameters with unspecified types
  135. $types += array_fill_keys(array_keys($params), null);
  136. ksort($params);
  137. ksort($types);
  138. }
  139. foreach ($types as $name => $type) {
  140. ++$bindIndex;
  141. if ($type !== Connection::PARAM_INT_ARRAY && $type !== Connection::PARAM_STR_ARRAY) {
  142. continue;
  143. }
  144. if ($isPositional) {
  145. $name = $bindIndex;
  146. }
  147. $arrayPositions[$name] = false;
  148. }
  149. if (( ! $arrayPositions && $isPositional)) {
  150. return [$query, $params, $types];
  151. }
  152. if ($isPositional) {
  153. $paramOffset = 0;
  154. $queryOffset = 0;
  155. $params = array_values($params);
  156. $types = array_values($types);
  157. $paramPos = self::getPositionalPlaceholderPositions($query);
  158. foreach ($paramPos as $needle => $needlePos) {
  159. if (! isset($arrayPositions[$needle])) {
  160. continue;
  161. }
  162. $needle += $paramOffset;
  163. $needlePos += $queryOffset;
  164. $count = count($params[$needle]);
  165. $params = array_merge(
  166. array_slice($params, 0, $needle),
  167. $params[$needle],
  168. array_slice($params, $needle + 1)
  169. );
  170. $types = array_merge(
  171. array_slice($types, 0, $needle),
  172. $count ?
  173. // array needles are at {@link \Doctrine\DBAL\ParameterType} constants
  174. // + {@link \Doctrine\DBAL\Connection::ARRAY_PARAM_OFFSET}
  175. array_fill(0, $count, $types[$needle] - Connection::ARRAY_PARAM_OFFSET) :
  176. [],
  177. array_slice($types, $needle + 1)
  178. );
  179. $expandStr = $count ? implode(', ', array_fill(0, $count, '?')) : 'NULL';
  180. $query = substr($query, 0, $needlePos) . $expandStr . substr($query, $needlePos + 1);
  181. $paramOffset += $count - 1; // Grows larger by number of parameters minus the replaced needle.
  182. $queryOffset += strlen($expandStr) - 1;
  183. }
  184. return [$query, $params, $types];
  185. }
  186. $queryOffset = 0;
  187. $typesOrd = [];
  188. $paramsOrd = [];
  189. $paramPos = self::getNamedPlaceholderPositions($query);
  190. foreach ($paramPos as $pos => $paramName) {
  191. $paramLen = strlen($paramName) + 1;
  192. $value = static::extractParam($paramName, $params, true);
  193. if (! isset($arrayPositions[$paramName]) && ! isset($arrayPositions[':' . $paramName])) {
  194. $pos += $queryOffset;
  195. $queryOffset -= $paramLen - 1;
  196. $paramsOrd[] = $value;
  197. $typesOrd[] = static::extractParam($paramName, $types, false, ParameterType::STRING);
  198. $query = substr($query, 0, $pos) . '?' . substr($query, $pos + $paramLen);
  199. continue;
  200. }
  201. $count = count($value);
  202. $expandStr = $count > 0 ? implode(', ', array_fill(0, $count, '?')) : 'NULL';
  203. foreach ($value as $val) {
  204. $paramsOrd[] = $val;
  205. $typesOrd[] = static::extractParam($paramName, $types, false) - Connection::ARRAY_PARAM_OFFSET;
  206. }
  207. $pos += $queryOffset;
  208. $queryOffset += strlen($expandStr) - $paramLen;
  209. $query = substr($query, 0, $pos) . $expandStr . substr($query, $pos + $paramLen);
  210. }
  211. return [$query, $paramsOrd, $typesOrd];
  212. }
  213. /**
  214. * Slice the SQL statement around pairs of quotes and
  215. * return string fragments of SQL outside of quoted literals.
  216. * Each fragment is captured as a 2-element array:
  217. *
  218. * 0 => matched fragment string,
  219. * 1 => offset of fragment in $statement
  220. *
  221. * @param string $statement
  222. *
  223. * @return mixed[][]
  224. */
  225. private static function getUnquotedStatementFragments($statement)
  226. {
  227. $literal = self::ESCAPED_SINGLE_QUOTED_TEXT . '|' .
  228. self::ESCAPED_DOUBLE_QUOTED_TEXT . '|' .
  229. self::ESCAPED_BACKTICK_QUOTED_TEXT . '|' .
  230. self::ESCAPED_BRACKET_QUOTED_TEXT;
  231. $expression = sprintf('/((.+(?i:ARRAY)\\[.+\\])|([^\'"`\\[]+))(?:%s)?/s', $literal);
  232. preg_match_all($expression, $statement, $fragments, PREG_OFFSET_CAPTURE);
  233. return $fragments[1];
  234. }
  235. /**
  236. * @param string $paramName The name of the parameter (without a colon in front)
  237. * @param mixed $paramsOrTypes A hash of parameters or types
  238. * @param bool $isParam
  239. * @param mixed $defaultValue An optional default value. If omitted, an exception is thrown
  240. *
  241. * @return mixed
  242. *
  243. * @throws SQLParserUtilsException
  244. */
  245. private static function extractParam($paramName, $paramsOrTypes, $isParam, $defaultValue = null)
  246. {
  247. if (array_key_exists($paramName, $paramsOrTypes)) {
  248. return $paramsOrTypes[$paramName];
  249. }
  250. // Hash keys can be prefixed with a colon for compatibility
  251. if (array_key_exists(':' . $paramName, $paramsOrTypes)) {
  252. return $paramsOrTypes[':' . $paramName];
  253. }
  254. if ($defaultValue !== null) {
  255. return $defaultValue;
  256. }
  257. if ($isParam) {
  258. throw SQLParserUtilsException::missingParam($paramName);
  259. }
  260. throw SQLParserUtilsException::missingType($paramName);
  261. }
  262. }