Skip to content

Commit

Permalink
Merge branch 'master' into allow_scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Apr 13, 2024
2 parents d3f80e5 + ca6e276 commit 1443e06
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# MSSQL Server driver for Yii Database Change Log

## 1.2.1 under development
## 2.0.0 under development

- no changes in this release.
- Enh #293: 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mssql\Builder;

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

final class ExpressionBuilder extends AbstractExpressionBuilder
{
protected function createSqlParser(string $sql): SqlParser
{
return new SqlParser($sql);
}
}
3 changes: 3 additions & 0 deletions src/DQLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Mssql\Builder\ExpressionBuilder;
use Yiisoft\Db\Mssql\Builder\InConditionBuilder;
use Yiisoft\Db\Mssql\Builder\LikeConditionBuilder;
use Yiisoft\Db\Query\Query;
Expand Down Expand Up @@ -48,6 +50,7 @@ protected function defaultExpressionBuilders(): array
return array_merge(parent::defaultExpressionBuilders(), [
InCondition::class => InConditionBuilder::class,
LikeCondition::class => LikeConditionBuilder::class,
Expression::class => ExpressionBuilder::class,
]);
}

Expand Down
45 changes: 45 additions & 0 deletions src/SqlParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mssql;

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

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->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), - '-' => $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 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->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), - '-' => $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 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->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), - '-' => $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 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->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), - '-' => $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 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->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), - '-' => $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 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->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), - '-' => $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 31 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Increment": --- Original +++ New @@ @@ '"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), '-' => $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 31 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ '"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), '-' => $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 31 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ '"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), '-' => $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 31 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Increment": --- Original +++ New @@ @@ '"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), '-' => $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 31 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ '"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), '-' => $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 31 in src/SqlParser.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ '"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), '[' => $this->sql[$this->position] === '[' ? $this->skipToAfterString(']]') : $this->skipQuotedWithoutEscape(']'), '-' => $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
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,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
25 changes: 25 additions & 0 deletions tests/Provider/SqlParserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mssql\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,
],
[
'[[:field]] = :name AND age = :age',
':name',
13,
],
];
}
}
12 changes: 12 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,18 @@ public function testSelectExists(string $sql, string $expected): void
parent::testSelectExists($sql, $expected);
}

/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\QueryBuilderProvider::update */
public function testUpdate(
string $table,
array $columns,
array|string $condition,
array $params,
string $expectedSql,
array $expectedParams,
): void {
parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams);
}

/**
* @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\QueryBuilderProvider::upsert
*
Expand Down
25 changes: 25 additions & 0 deletions tests/SqlParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mssql\Tests;

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

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

/** @dataProvider \Yiisoft\Db\Mssql\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 1443e06

Please sign in to comment.