SQLParserUtils.php 10 KB

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