Skip to content

Commit

Permalink
Update test according to main PR (#321)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
  • Loading branch information
Tigrov and vjik committed Apr 12, 2024
1 parent b4642c6 commit df7cf71
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
@@ -1,8 +1,8 @@
# MySQL driver for Yii Database Change Log

## 1.2.1 under development
## 2.0.0 under development

- no changes in this release.
- Enh #321: Implement `SqlParser` and `ExpressionBuilder` driver classes (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
16 changes: 16 additions & 0 deletions src/Builder/ExpressionBuilder.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Builder;

use Yiisoft\Db\Expression\AbstractExpressionBuilder;
use Yiisoft\Db\Mysql\SqlParser;

final class ExpressionBuilder extends AbstractExpressionBuilder
{
protected function createSqlParser(string $sql): SqlParser
{
return new SqlParser($sql);
}
}
4 changes: 3 additions & 1 deletion src/DQLQueryBuilder.php
Expand Up @@ -4,9 +4,10 @@

namespace Yiisoft\Db\Mysql;

use Yiisoft\Db\Expression\ExpressionBuilder;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Mysql\Builder\ExpressionBuilder;
use Yiisoft\Db\Mysql\Builder\JsonExpressionBuilder;
use Yiisoft\Db\QueryBuilder\AbstractDQLQueryBuilder;

Expand Down Expand Up @@ -83,6 +84,7 @@ protected function defaultExpressionBuilders(): array
parent::defaultExpressionBuilders(),
[
JsonExpression::class => JsonExpressionBuilder::class,
Expression::class => ExpressionBuilder::class,
]
);
}
Expand Down
42 changes: 42 additions & 0 deletions src/SqlParser.php
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql;

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->skipQuotedWithEscape($this->sql[$pos]),
'-' => $this->sql[$this->position] === '-'
? ++$this->position && $this->skipToAfterChar("\n")

Check warning on line 25 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Increment": --- Original +++ New @@ @@ match ($this->sql[$pos]) { ':' => ($word = $this->parseWord()) === '' ? $this->skipChars(':') : ($result = ':' . $word), '"', "'", '`' => $this->skipQuotedWithEscape($this->sql[$pos]), - '-' => $this->sql[$this->position] === '-' ? ++$this->position && $this->skipToAfterChar("\n") : null, + '-' => $this->sql[$this->position] === '-' ? --$this->position && $this->skipToAfterChar("\n") : null, '/' => $this->sql[$this->position] === '*' ? ++$this->position && $this->skipToAfterString('*/') : null, default => null, };

Check warning on line 25 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ match ($this->sql[$pos]) { ':' => ($word = $this->parseWord()) === '' ? $this->skipChars(':') : ($result = ':' . $word), '"', "'", '`' => $this->skipQuotedWithEscape($this->sql[$pos]), - '-' => $this->sql[$this->position] === '-' ? ++$this->position && $this->skipToAfterChar("\n") : null, + '-' => $this->sql[$this->position] === '-' ? !(++$this->position && $this->skipToAfterChar("\n")) : null, '/' => $this->sql[$this->position] === '*' ? ++$this->position && $this->skipToAfterString('*/') : null, default => null, };

Check warning on line 25 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ match ($this->sql[$pos]) { ':' => ($word = $this->parseWord()) === '' ? $this->skipChars(':') : ($result = ':' . $word), '"', "'", '`' => $this->skipQuotedWithEscape($this->sql[$pos]), - '-' => $this->sql[$this->position] === '-' ? ++$this->position && $this->skipToAfterChar("\n") : null, + '-' => $this->sql[$this->position] === '-' ? ++$this->position && !$this->skipToAfterChar("\n") : null, '/' => $this->sql[$this->position] === '*' ? ++$this->position && $this->skipToAfterString('*/') : null, default => null, };
: null,
'/' => $this->sql[$this->position] === '*'
? ++$this->position && $this->skipToAfterString('*/')

Check warning on line 28 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Increment": --- Original +++ New @@ @@ ':' => ($word = $this->parseWord()) === '' ? $this->skipChars(':') : ($result = ':' . $word), '"', "'", '`' => $this->skipQuotedWithEscape($this->sql[$pos]), '-' => $this->sql[$this->position] === '-' ? ++$this->position && $this->skipToAfterChar("\n") : null, - '/' => $this->sql[$this->position] === '*' ? ++$this->position && $this->skipToAfterString('*/') : null, + '/' => $this->sql[$this->position] === '*' ? --$this->position && $this->skipToAfterString('*/') : null, default => null, }; if ($result !== null) {

Check warning on line 28 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ ':' => ($word = $this->parseWord()) === '' ? $this->skipChars(':') : ($result = ':' . $word), '"', "'", '`' => $this->skipQuotedWithEscape($this->sql[$pos]), '-' => $this->sql[$this->position] === '-' ? ++$this->position && $this->skipToAfterChar("\n") : null, - '/' => $this->sql[$this->position] === '*' ? ++$this->position && $this->skipToAfterString('*/') : null, + '/' => $this->sql[$this->position] === '*' ? !(++$this->position && $this->skipToAfterString('*/')) : null, default => null, }; if ($result !== null) {

Check warning on line 28 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ ':' => ($word = $this->parseWord()) === '' ? $this->skipChars(':') : ($result = ':' . $word), '"', "'", '`' => $this->skipQuotedWithEscape($this->sql[$pos]), '-' => $this->sql[$this->position] === '-' ? ++$this->position && $this->skipToAfterChar("\n") : null, - '/' => $this->sql[$this->position] === '*' ? ++$this->position && $this->skipToAfterString('*/') : null, + '/' => $this->sql[$this->position] === '*' ? ++$this->position && !$this->skipToAfterString('*/') : null, default => null, }; if ($result !== null) {
: null,
default => null,
};

if ($result !== null) {
$position = $pos;

return $result;
}
}

return null;
}
}
5 changes: 3 additions & 2 deletions tests/CommandTest.php
Expand Up @@ -120,9 +120,10 @@ public function testUpdate(
array $columns,
array|string $conditions,
array $params,
string $expected
array $expectedValues,
int $expectedCount,
): void {
parent::testUpdate($table, $columns, $conditions, $params, $expected);
parent::testUpdate($table, $columns, $conditions, $params, $expectedValues, $expectedCount);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Provider/SqlParserProvider.php
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Tests\Provider;

class SqlParserProvider extends \Yiisoft\Db\Tests\Provider\SqlParserProvider
{
public static function getNextPlaceholder(): array
{
return [
...parent::getNextPlaceholder(),
[
'`:field` = :name AND age = :age',
':name',
11,
],
[
'`:fie\\`ld` = :name AND age = :age',
':name',
13,
],
[
'":fie\\"ld" = :name AND age = :age',
':name',
13,
],
[
"name = ':na\\'me' AND age = :age",
':age',
27,
],
];
}
}
7 changes: 4 additions & 3 deletions tests/QueryBuilderTest.php
Expand Up @@ -602,10 +602,11 @@ public function testUpdate(
string $table,
array $columns,
array|string $condition,
string $expectedSQL,
array $expectedParams
array $params,
string $expectedSql,
array $expectedParams,
): void {
parent::testUpdate($table, $columns, $condition, $expectedSQL, $expectedParams);
parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/SqlParserTest.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Tests;

use Yiisoft\Db\Mysql\SqlParser;
use Yiisoft\Db\Tests\AbstractSqlParserTest;

/**
* @group mysql
*/
final class SqlParserTest extends AbstractSqlParserTest
{
protected function createSqlParser(string $sql): SqlParser
{
return new SqlParser($sql);
}

/** @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\SqlParserProvider::getNextPlaceholder */
public function testGetNextPlaceholder(string $sql, string|null $expectedPlaceholder, int|null $expectedPosition): void
{
parent::testGetNextPlaceholder($sql, $expectedPlaceholder, $expectedPosition);
}
}

0 comments on commit df7cf71

Please sign in to comment.