123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
-
-
-
- namespace Doctrine\DBAL;
-
- use Doctrine\DBAL\Connection;
-
-
- class SQLParserUtils
- {
-
-
- static public function getPlaceholderPositions($statement, $isPositional = true)
- {
- $match = ($isPositional) ? '?' : ':';
- if (strpos($statement, $match) === false) {
- return array();
- }
-
- $count = 0;
- $inLiteral = false;
- $stmtLen = strlen($statement);
- $paramMap = array();
- for ($i = 0; $i < $stmtLen; $i++) {
- if ($statement[$i] == $match && !$inLiteral) {
-
- if ($isPositional) {
- $paramMap[$count] = $i;
- } else {
- $name = "";
-
- for ($j = $i; ($j < $stmtLen && preg_match('(([:a-zA-Z0-9]{1}))', $statement[$j])); $j++) {
- $name .= $statement[$j];
- }
- $paramMap[$name][] = $i;
- $i = $j;
- }
- ++$count;
- } else if ($statement[$i] == "'" || $statement[$i] == '"') {
- $inLiteral = ! $inLiteral;
- }
- }
-
- return $paramMap;
- }
-
-
-
- static public function expandListParameters($query, $params, $types)
- {
- $isPositional = is_int(key($params));
- $arrayPositions = array();
- $bindIndex = -1;
- foreach ($types AS $name => $type) {
- ++$bindIndex;
- if ($type === Connection::PARAM_INT_ARRAY || $type == Connection::PARAM_STR_ARRAY) {
- if ($isPositional) {
- $name = $bindIndex;
- }
-
- $arrayPositions[$name] = false;
- }
- }
-
- if (!$arrayPositions || count($params) != count($types)) {
- return array($query, $params, $types);
- }
-
- $paramPos = self::getPlaceholderPositions($query, $isPositional);
- if ($isPositional) {
- $paramOffset = 0;
- $queryOffset = 0;
- foreach ($paramPos AS $needle => $needlePos) {
- if (!isset($arrayPositions[$needle])) {
- continue;
- }
-
- $needle += $paramOffset;
- $needlePos += $queryOffset;
- $len = count($params[$needle]);
-
- $params = array_merge(
- array_slice($params, 0, $needle),
- $params[$needle],
- array_slice($params, $needle + 1)
- );
-
- $types = array_merge(
- array_slice($types, 0, $needle),
- array_fill(0, $len, $types[$needle] - Connection::ARRAY_PARAM_OFFSET),
- array_slice($types, $needle + 1)
- );
-
- $expandStr = implode(", ", array_fill(0, $len, "?"));
- $query = substr($query, 0, $needlePos) . $expandStr . substr($query, $needlePos + 1);
-
- $paramOffset += ($len - 1);
- $queryOffset += (strlen($expandStr) - 1);
- }
- } else {
- throw new DBALException("Array parameters are not supported for named placeholders.");
- }
-
- return array($query, $params, $types);
- }
- }
|