-
-
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\Oracle\Builder; | ||
|
||
use Yiisoft\Db\Expression\AbstractExpressionBuilder; | ||
use Yiisoft\Db\Oracle\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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle; | ||
|
||
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->skipToAfterChar('"'), | ||
"'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), | ||
'q', 'Q' => $this->sql[$this->position] === "'" | ||
? $this->skipQuotedWithQ() | ||
: null, | ||
'-' => $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
Check warning on line 32 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
|
||
: null, | ||
default => null, | ||
}; | ||
|
||
if ($result !== null) { | ||
$position = $pos; | ||
|
||
return $result; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Skips quoted string with Q-operator. | ||
*/ | ||
private function skipQuotedWithQ(): void | ||
{ | ||
$endChar = match ($this->sql[++$this->position]) { | ||
Check warning on line 52 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
Check warning on line 52 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
Check warning on line 52 in src/SqlParser.php GitHub Actions / PHP 8.1-ubuntu-latest
|
||
'[' => ']', | ||
'<' => '>', | ||
'{' => '}', | ||
'(' => ')', | ||
default => $this->sql[$this->position], | ||
}; | ||
|
||
++$this->position; | ||
|
||
$this->skipToAfterString("$endChar'"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle\Tests\Provider; | ||
|
||
class SqlParserProvider extends \Yiisoft\Db\Tests\Provider\SqlParserProvider | ||
{ | ||
public static function getNextPlaceholder(): array | ||
{ | ||
return [ | ||
...parent::getNextPlaceholder(), | ||
[ | ||
"name = q'':name'' AND age = :age", | ||
':age', | ||
28, | ||
], | ||
[ | ||
"name = Q'':name'' AND age = :age", | ||
':age', | ||
28, | ||
], | ||
[ | ||
"name = Q'[:name]' AND age = :age", | ||
':age', | ||
28, | ||
], | ||
[ | ||
"name = Q'!':name'!' AND age = :age", | ||
':age', | ||
30, | ||
], | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle\Tests; | ||
|
||
use Yiisoft\Db\Oracle\SqlParser; | ||
use Yiisoft\Db\Tests\AbstractSqlParserTest; | ||
|
||
/** | ||
* @group oracle | ||
*/ | ||
final class SqlParserTest extends AbstractSqlParserTest | ||
{ | ||
protected function createSqlParser(string $sql): SqlParser | ||
{ | ||
return new SqlParser($sql); | ||
} | ||
|
||
/** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\SqlParserProvider::getNextPlaceholder */ | ||
public function testGetNextPlaceholder(string $sql, string|null $expectedPlaceholder, int|null $expectedPosition): void | ||
{ | ||
parent::testGetNextPlaceholder($sql, $expectedPlaceholder, $expectedPosition); | ||
} | ||
} |