Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
Fix parseQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
komex committed May 24, 2017
1 parent e13e5a5 commit 3dd68c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/Che/DBAL/Vertica/ODBCStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,18 @@ protected function parseQuery($query)
$counter = 1;

return preg_replace_callback(
'/(?<!\w)(?:(?<!:):[a-z]\w*|\?)/i',
'/(?<=[\s\(\=,])(?:(\?)(?:(?=[\s\),])|;?$)|(?<!:)(:[a-z]\w*))/i',
function (array $match) use (&$counter) {
$name = $match[0];
if ($name === '?') {
if ($name[0] === '?') {
$this->paramMap[] = $counter++;

return $name;
} else {
$this->paramMap[] = substr($name, 1);
}

return '?';
return '?';
}
},
$query
);
Expand Down
29 changes: 26 additions & 3 deletions tests/ODBCStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,34 @@ public function testParseQuery()
$object = $class->newInstanceWithoutConstructor();
$query = $method->invoke(
$object,
'SELECT :date, ? FROM log WHERE log.date = :date::DATE AND log.hour = :hour GROUP BY 1, ?, 3;'
<<<'SQL'
SELECT REGEXP_SUBSTR(s.url, '^(?:https?:)?//(?:www\.)?([^/]+)/', 1, 1, 'i', 1)
FROM s WHERE s.a IN(?,?, ? ,?) AND s.b IN(:a,:b, :c ,:d) AND s.e = ?;
SQL
);
self::assertSame(
<<<'SQL'
SELECT REGEXP_SUBSTR(s.url, '^(?:https?:)?//(?:www\.)?([^/]+)/', 1, 1, 'i', 1)
FROM s WHERE s.a IN(?,?, ? ,?) AND s.b IN(?,?, ? ,?) AND s.e = ?;
SQL
,
$query
);
self::assertSame('SELECT ?, ? FROM log WHERE log.date = ?::DATE AND log.hour = ? GROUP BY 1, ?, 3;', $query);
$map = $class->getProperty('paramMap');
$map->setAccessible(true);
self::assertSame(['date', 1, 'date', 'hour', 2], $map->getValue($object));
self::assertSame(
[
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 'a',
5 => 'b',
6 => 'c',
7 => 'd',
8 => 5,
],
$map->getValue($object)
);
}
}

0 comments on commit 3dd68c8

Please sign in to comment.