Skip to content

feat: add support for dbal v4 #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
"require": {
"php": "^8.1",
"ext-pdo": "*",
"doctrine/dbal": "^3.0",
"doctrine/dbal": "^3 || ^4",
"doctrine/lexer": "^2.0 || ^3.0",
"doctrine/orm": "^2.14"
"doctrine/orm": "^2.19 || ^3"
},
"require-dev": {
"doctrine/annotations": "^1.13",
"doctrine/coding-standard": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"phpunit/phpunit": "^8.0 || ^9.0 || ^10.0",
"doctrine/coding-standard": "^11.0",
"phpunit/phpunit": "^10.0",
"psalm/plugin-phpunit": "^0.18",
"slevomat/coding-standard": "^8.4",
"vimeo/psalm": "^5.2",
Expand Down
4 changes: 3 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

<!-- Directories to be checked -->
<config name="php_version" value="80100"/>

<!-- Directories to be checked -->
<file>src</file>
<file>tests</file>

Expand Down
25 changes: 11 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="DoctrineJsonFunctions Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="DoctrineJsonFunctions Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
26 changes: 26 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.21.1@8c473e2437be8b6a8fd8f630f0f11a16b114c494">
<file src="tests/Mocks/DbalDriverCompatibility.php">
<DuplicateClass>
<code>DbalDriverCompatibility</code>
</DuplicateClass>
</file>
<file src="tests/Mocks/DbalPlatformCompatibility.php">
<DuplicateClass>
<code>DbalPlatformCompatibility</code>
</DuplicateClass>
<UndefinedClass>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
<code>NotSupported</code>
</UndefinedClass>
</file>
</files>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
allowNamedArgumentCalls="false"
findUnusedBaselineEntry="true"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
Expand Down
38 changes: 38 additions & 0 deletions src/DBALCompatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Scienta\DoctrineJsonFunctions;

use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Platforms\Exception\NotSupported;
use Exception;

use function class_exists;

/**
* @internal
*/
final class DBALCompatibility
{
public static function notSupportedPlatformException(string $method): Exception
{
if (class_exists('Doctrine\DBAL\Platforms\Exception\NotSupported')) {
return NotSupported::new($method);
}

/**
* @psalm-suppress UndefinedClass
*/
return DBALException::notSupported($method);
}

public static function sqlLitePlatform(): string
{
if (class_exists('Doctrine\DBAL\Platforms\SQLitePlatform')) {
return 'Doctrine\DBAL\Platforms\SQLitePlatform';
}

return 'Doctrine\DBAL\Platforms\SqlitePlatform';
}
}
25 changes: 13 additions & 12 deletions src/Query/AST/Functions/AbstractJsonFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TokenType;

abstract class AbstractJsonFunctionNode extends FunctionNode
{
Expand Down Expand Up @@ -40,16 +41,16 @@ abstract class AbstractJsonFunctionNode extends FunctionNode
*/
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);

$argumentParsed = $this->parseArguments($parser, $this->requiredArgumentTypes);

if (!empty($this->optionalArgumentTypes)) {
$this->parseOptionalArguments($parser, $argumentParsed);
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
}

/**
Expand All @@ -59,10 +60,10 @@ public function parse(Parser $parser): void
*/
protected function parseOptionalArguments(Parser $parser, bool $argumentParsed): void
{
$continueParsing = !$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS);
$continueParsing = !$parser->getLexer()->isNextToken(TokenType::T_CLOSE_PARENTHESIS);
while ($continueParsing) {
$argumentParsed = $this->parseArguments($parser, $this->optionalArgumentTypes, $argumentParsed);
$continueParsing = $this->allowOptionalArgumentRepeat && $parser->getLexer()->isNextToken(Lexer::T_COMMA);
$continueParsing = $this->allowOptionalArgumentRepeat && $parser->getLexer()->isNextToken(TokenType::T_COMMA);
}
}

Expand All @@ -77,7 +78,7 @@ protected function parseArguments(Parser $parser, array $argumentTypes, bool $ar
{
foreach ($argumentTypes as $argType) {
if ($argumentParsed) {
$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);
} else {
$argumentParsed = true;
}
Expand Down Expand Up @@ -113,7 +114,7 @@ protected function parseStringLiteral(Parser $parser): Literal
$lexer = $parser->getLexer();
$lookaheadType = $lexer->lookahead->type;

if ($lookaheadType !== Lexer::T_STRING) {
if ($lookaheadType !== TokenType::T_STRING) {
$parser->syntaxError('string');
}

Expand All @@ -131,12 +132,12 @@ protected function parseAlphaNumericLiteral(Parser $parser): Literal
$lookaheadType = $lexer->lookahead->type;

switch ($lookaheadType) {
case Lexer::T_STRING:
case TokenType::T_STRING:
return $this->matchStringLiteral($parser, $lexer);
case Lexer::T_INTEGER:
case Lexer::T_FLOAT:
case TokenType::T_INTEGER:
case TokenType::T_FLOAT:
$parser->match(
$lexer->isNextToken(Lexer::T_INTEGER) ? Lexer::T_INTEGER : Lexer::T_FLOAT
$lexer->isNextToken(TokenType::T_INTEGER) ? TokenType::T_INTEGER : TokenType::T_FLOAT
);

return new Literal(Literal::NUMERIC, $lexer->token->value);
Expand All @@ -147,7 +148,7 @@ protected function parseAlphaNumericLiteral(Parser $parser): Literal

private function matchStringLiteral(Parser $parser, Lexer $lexer): Literal
{
$parser->match(Lexer::T_STRING);
$parser->match(TokenType::T_STRING);
return new Literal(Literal::STRING, $lexer->token->value);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Query/AST/Functions/Mariadb/MariadbJsonFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\ORM\Query\SqlWalker;
use Scienta\DoctrineJsonFunctions\DBALCompatibility;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\AbstractJsonFunctionNode;

abstract class MariadbJsonFunctionNode extends AbstractJsonFunctionNode
Expand All @@ -18,7 +19,7 @@ abstract class MariadbJsonFunctionNode extends AbstractJsonFunctionNode
protected function validatePlatform(SqlWalker $sqlWalker): void
{
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
throw Exception::notSupported(static::FUNCTION_NAME);
throw DBALCompatibility::notSupportedPlatformException(static::FUNCTION_NAME);
}
}
}
10 changes: 5 additions & 5 deletions src/Query/AST/Functions/Mysql/JsonContainsPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql;

use Doctrine\DBAL\Exception;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\TokenType;

/**
* "JSON_CONTAINS_PATH" "(" StringPrimary "," ["one" | "all"] {"," StringPrimary }* ")"
Expand All @@ -22,17 +22,17 @@ class JsonContainsPath extends JsonSearch
*/
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);

$this->parsedArguments[] = $parser->StringPrimary();

$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);

$this->parsedArguments[] = $this->parsePathMode($parser);

$this->parseOptionalArguments($parser, true);

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
}
}
21 changes: 12 additions & 9 deletions src/Query/AST/Functions/Mysql/JsonSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql;

use Doctrine\DBAL\Exception;
use Exception;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\TokenType;
use Scienta\DoctrineJsonFunctions\DBALCompatibility;

/**
* "JSON_SEARCH" "(" StringPrimary "," ["one" | "all"] "," StringPrimary {"," NewValue { "," StringPrimary }* } ")"
Expand Down Expand Up @@ -38,27 +39,27 @@ class JsonSearch extends MysqlJsonFunctionNode
*/
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);

$this->parsedArguments[] = $parser->StringPrimary();

$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);

$this->parsedArguments[] = $this->parsePathMode($parser);

$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);

$this->parsedArguments[] = $parser->StringPrimary();

$continueParsing = !$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS);
$continueParsing = !$parser->getLexer()->isNextToken(TokenType::T_CLOSE_PARENTHESIS);
if ($continueParsing) {
$this->parseArguments($parser, [self::VALUE_ARG, self::STRING_PRIMARY_ARG], true);
}

$this->parseOptionalArguments($parser, true);

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
}

/**
Expand All @@ -80,6 +81,8 @@ protected function parsePathMode(Parser $parser)
return $parser->Literal();
}

throw Exception::notSupported("Mode '$value' is not supported by " . static::FUNCTION_NAME . ".");
throw DBALCompatibility::notSupportedPlatformException(
"Mode '$value' is not supported by " . static::FUNCTION_NAME . "."
);
}
}
3 changes: 2 additions & 1 deletion src/Query/AST/Functions/Mysql/MysqlJsonFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\ORM\Query\SqlWalker;
use Scienta\DoctrineJsonFunctions\DBALCompatibility;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\AbstractJsonFunctionNode;

abstract class MysqlJsonFunctionNode extends AbstractJsonFunctionNode
Expand All @@ -18,7 +19,7 @@ abstract class MysqlJsonFunctionNode extends AbstractJsonFunctionNode
protected function validatePlatform(SqlWalker $sqlWalker): void
{
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
throw Exception::notSupported(static::FUNCTION_NAME);
throw DBALCompatibility::notSupportedPlatformException(static::FUNCTION_NAME);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Query\SqlWalker;
use Scienta\DoctrineJsonFunctions\DBALCompatibility;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\AbstractJsonFunctionNode;

abstract class PostgresqlJsonFunctionNode extends AbstractJsonFunctionNode
Expand All @@ -18,7 +19,7 @@ abstract class PostgresqlJsonFunctionNode extends AbstractJsonFunctionNode
protected function validatePlatform(SqlWalker $sqlWalker): void
{
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof PostgreSQLPlatform) {
throw Exception::notSupported(static::FUNCTION_NAME);
throw DBALCompatibility::notSupportedPlatformException(static::FUNCTION_NAME);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Query\SqlWalker;
use Scienta\DoctrineJsonFunctions\DBALCompatibility;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\AbstractJsonOperatorFunctionNode;

abstract class PostgresqlJsonOperatorFunctionNode extends AbstractJsonOperatorFunctionNode
Expand All @@ -20,7 +21,7 @@ abstract class PostgresqlJsonOperatorFunctionNode extends AbstractJsonOperatorFu
protected function validatePlatform(SqlWalker $sqlWalker): void
{
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof PostgreSQLPlatform) {
throw Exception::notSupported(static::FUNCTION_NAME);
throw DBALCompatibility::notSupportedPlatformException(static::FUNCTION_NAME);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Query/AST/Functions/Sqlite/SqliteJsonFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Sqlite;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\ORM\Query\SqlWalker;
use Scienta\DoctrineJsonFunctions\DBALCompatibility;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\AbstractJsonFunctionNode;

abstract class SqliteJsonFunctionNode extends AbstractJsonFunctionNode
Expand All @@ -17,8 +17,8 @@ abstract class SqliteJsonFunctionNode extends AbstractJsonFunctionNode
*/
protected function validatePlatform(SqlWalker $sqlWalker): void
{
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) {
throw Exception::notSupported(static::FUNCTION_NAME);
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof (DBALCompatibility::sqlLitePlatform())) {
throw DBALCompatibility::notSupportedPlatformException(static::FUNCTION_NAME);
}
}
}
Loading