-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Pgsql\Builder; | ||
|
||
use Yiisoft\Db\Expression\AbstractExpressionBuilder; | ||
use Yiisoft\Db\Pgsql\SqlParser; | ||
|
||
final class ExpressionBuilder extends AbstractExpressionBuilder | ||
{ | ||
protected function createSqlParser(string $sql): SqlParser | ||
{ | ||
return new SqlParser($sql); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Pgsql; | ||
|
||
use Yiisoft\Db\Syntax\AbstractSqlParser; | ||
|
||
final class SqlParser extends AbstractSqlParser | ||
{ | ||
public function getNextPlaceholder(int|null &$position = null): string|null | ||
{ | ||
$result = null; | ||
$length = $this->length - 1; | ||
|
||
while ($this->position < $length) { | ||
$pos = $this->position++; | ||
|
||
match ($this->sql[$pos]) { | ||
':' => ($word = $this->parseWord()) === '' | ||
? $this->skipChars(':') | ||
: $result = ':' . $word, | ||
'"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), | ||
'e', 'E' => $this->sql[$this->position] === "'" | ||
Check warning on line 24 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
Check warning on line 24 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
|
||
? ++$this->position && $this->skipQuotedWithEscape("'") | ||
Check warning on line 25 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
Check warning on line 25 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
|
||
: $this->skipIdentifier(), | ||
'$' => $this->skipQuotedWithDollar(), | ||
'-' => $this->sql[$this->position] === '-' | ||
? ++$this->position && $this->skipToAfterChar("\n") | ||
Check warning on line 29 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
Check warning on line 29 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
Check warning on line 29 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
|
||
: null, | ||
'/' => $this->sql[$this->position] === '*' | ||
? ++$this->position && $this->skipToAfterString('*/') | ||
Check warning on line 32 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
Check warning on line 32 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
|
||
: null, | ||
// Identifiers can contain dollar sign which can be used for quoting. Skip them. | ||
'_','a', 'b', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', | ||
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', | ||
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' => $this->skipIdentifier(), | ||
default => null, | ||
}; | ||
|
||
if ($result !== null) { | ||
$position = $pos; | ||
|
||
return $result; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Skips dollar-quoted string. | ||
*/ | ||
private function skipQuotedWithDollar(): void | ||
{ | ||
$pos = $this->position; | ||
$identifier = $this->parseIdentifier(); | ||
|
||
if ($this->sql[$this->position] !== '$') { | ||
$this->position = $pos; | ||
return; | ||
} | ||
|
||
++$this->position; | ||
|
||
$this->skipToAfterString('$' . $identifier . '$'); | ||
} | ||
|
||
/** | ||
* Skips an identifier. Equals to `[$\w]+` in regular expressions. | ||
*/ | ||
private function skipIdentifier(): void | ||
{ | ||
$continue = true; | ||
|
||
while ($continue && $this->position < $this->length) { | ||
match ($this->sql[$this->position]) { | ||
'$', '_', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', | ||
'v', 'w', 'x', 'y', 'z', | ||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', | ||
'V', 'W', 'X', 'Y', 'Z' => ++$this->position, | ||
default => $continue = false, | ||
}; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Pgsql\Tests\Provider; | ||
|
||
class SqlParserProvider extends \Yiisoft\Db\Tests\Provider\SqlParserProvider | ||
{ | ||
public static function getNextPlaceholder(): array | ||
{ | ||
return [ | ||
...parent::getNextPlaceholder(), | ||
[ | ||
"name = e':name' AND age = :age", | ||
':age', | ||
26, | ||
], | ||
[ | ||
"name = E':name' AND age = :age", | ||
':age', | ||
26, | ||
], | ||
[ | ||
"name = E':name' AND age = :age", | ||
':age', | ||
26, | ||
], | ||
[ | ||
'name = $$:name$$ AND age = :age', | ||
':age', | ||
27, | ||
], | ||
[ | ||
'name = $q$:name$q$ AND age = :age', | ||
':age', | ||
29, | ||
], | ||
[ | ||
'name = $name AND age = :age', | ||
':age', | ||
23, | ||
], | ||
[ | ||
'name = name$1$ AND age = :age', | ||
':age', | ||
25, | ||
], | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Pgsql\Tests; | ||
|
||
use Yiisoft\Db\Pgsql\SqlParser; | ||
use Yiisoft\Db\Tests\AbstractSqlParserTest; | ||
|
||
/** | ||
* @group pgsql | ||
*/ | ||
final class SqlParserTest extends AbstractSqlParserTest | ||
{ | ||
protected function createSqlParser(string $sql): SqlParser | ||
{ | ||
return new SqlParser($sql); | ||
} | ||
|
||
/** @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\SqlParserProvider::getNextPlaceholder */ | ||
public function testGetNextPlaceholder(string $sql, string|null $expectedPlaceholder, int|null $expectedPosition): void | ||
{ | ||
parent::testGetNextPlaceholder($sql, $expectedPlaceholder, $expectedPosition); | ||
} | ||
} |