Skip to content

Commit

Permalink
Update test according to main PR (#289)
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 2389210 commit 4a95c6b
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
@@ -1,8 +1,8 @@
# SQLite driver for Yii Database Change Log

## 1.2.1 under development
## 2.0.0 under development

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

## 1.2.0 March 21, 2024

Expand Down
5 changes: 4 additions & 1 deletion rector.php
Expand Up @@ -11,7 +11,10 @@

$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
/**
* Disabled ./tests directory due to different branches with main package when testing
*/
// __DIR__ . '/tests',
]);

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

declare(strict_types=1);

namespace Yiisoft\Db\Sqlite\Builder;

use Yiisoft\Db\Expression\AbstractExpressionBuilder;
use Yiisoft\Db\Sqlite\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
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Sqlite;

use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\JsonExpression;
Expand All @@ -12,6 +13,7 @@
use Yiisoft\Db\QueryBuilder\AbstractDQLQueryBuilder;
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
use Yiisoft\Db\QueryBuilder\Condition\LikeCondition;
use Yiisoft\Db\Sqlite\Builder\ExpressionBuilder;
use Yiisoft\Db\Sqlite\Builder\InConditionBuilder;
use Yiisoft\Db\Sqlite\Builder\JsonExpressionBuilder;
use Yiisoft\Db\Sqlite\Builder\LikeConditionBuilder;
Expand Down Expand Up @@ -136,6 +138,7 @@ protected function defaultExpressionBuilders(): array
LikeCondition::class => LikeConditionBuilder::class,
InCondition::class => InConditionBuilder::class,
JsonExpression::class => JsonExpressionBuilder::class,
Expression::class => ExpressionBuilder::class,
]);
}
}
45 changes: 45 additions & 0 deletions src/SqlParser.php
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Sqlite;

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")
: null,
'/' => $this->sql[$this->position] === '*'
? ++$this->position && $this->skipToAfterString('*/')
: null,
default => null,
};

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

return $result;
}
}

return null;
}
}
5 changes: 3 additions & 2 deletions tests/CommandTest.php
Expand Up @@ -465,9 +465,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
30 changes: 30 additions & 0 deletions tests/Provider/SqlParserProvider.php
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Sqlite\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',
11,
],
[
'[[:field]] = :name AND age = :age',
':name',
13,
],
];
}
}
7 changes: 4 additions & 3 deletions tests/QueryBuilderTest.php
Expand Up @@ -703,10 +703,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\Sqlite\Tests;

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

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

/** @dataProvider \Yiisoft\Db\Sqlite\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 4a95c6b

Please sign in to comment.