Skip to content

Commit 930734a

Browse files
czoIgrobert.kubac
andauthored
fix #361 (#362)
Co-authored-by: robert.kubac <robert.kubac@farmaprom.pl>
1 parent d78dacf commit 930734a

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/PHPSQLParser/positions/PositionCalculator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ protected function findPositionWithinString($sql, $value, $expr_type) {
148148
}
149149

150150
// if we have a quoted string, we every character is allowed after it
151-
// see issue 137
152-
$quoted = ($sql[$pos + strlen($value) - 1] === '`');
151+
// see issues 137 and 361
152+
$quotedBefore = in_array($sql[$pos], array('`', '('), true);
153+
$quotedAfter = in_array($sql[$pos + strlen($value) - 1], array('`', ')'), true);
153154
$after = "";
154155
if (isset($sql[$pos + strlen($value)])) {
155156
$after = $sql[$pos + strlen($value)];
@@ -178,10 +179,11 @@ protected function findPositionWithinString($sql, $value, $expr_type) {
178179
// in all other cases we accept
179180
// whitespace, comma, operators, parenthesis and end_of_string
180181

181-
$ok = ($before === "" || in_array($before, self::$allowedOnOther, true));
182+
$ok = ($before === "" || in_array($before, self::$allowedOnOther, true)
183+
|| ($quotedBefore && (strtolower($before) >= 'a' && strtolower($before) <= 'z')));
182184
$ok = $ok
183185
&& ($after === "" || in_array($after, self::$allowedOnOther, true)
184-
|| ($quoted && (strtolower($after) >= 'a' && strtolower($after) <= 'z')));
186+
|| ($quotedAfter && (strtolower($after) >= 'a' && strtolower($after) <= 'z')));
185187

186188
if ($ok) {
187189
break;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace PHPSQLParser\Test\Creator;
4+
use PHPSQLParser\PHPSQLParser;
5+
use PHPSQLParser\PHPSQLCreator;
6+
7+
class issue361Test extends \PHPUnit\Framework\TestCase
8+
{
9+
public function testIssue361()
10+
{
11+
$sql = 'SELECT IF(status = 1,1,0)FROM users INNER JOIN names ON(users.name = names.name)WHERE(users.id IN(123, 456))AND names.name = "adam"';
12+
$createdSql = 'SELECT IF(status = 1,1,0) FROM users INNER JOIN names ON (users.name = names.name) '
13+
. 'WHERE (users.id IN (123, 456)) AND names.name = "adam"';
14+
15+
$parser = new PHPSQLParser();
16+
$creator = new PHPSQLCreator();
17+
18+
$parser->parse($sql, true);
19+
20+
$this->assertEquals($createdSql, $creator->create($parser->parsed));
21+
}
22+
}

0 commit comments

Comments
 (0)