Skip to content

Added JSON_VALUE for mysql platform #122

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

Merged
merged 2 commits into from
Nov 8, 2024
Merged
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
17 changes: 17 additions & 0 deletions src/DBALCompatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static function notSupportedPlatformException(string $method): Exception
return new Exception("Method $method is not supported for doctrine platform");
}

/**
* @psalm-return class-string
*/
public static function sqlLitePlatform(): string
{
if (class_exists('Doctrine\DBAL\Platforms\SQLitePlatform')) {
Expand All @@ -41,6 +44,9 @@ public static function sqlLitePlatform(): string
return 'Doctrine\DBAL\Platforms\SqlitePlatform';
}

/**
* @psalm-return class-string
*/
public static function mariaDBPlatform(): string
{
if (!class_exists('\Doctrine\DBAL\Platforms\MariaDBPlatform')) {
Expand All @@ -52,6 +58,17 @@ public static function mariaDBPlatform(): string
return '\Doctrine\DBAL\Platforms\MariaDBPlatform';
}

/**
* @psalm-return class-string
*/
public static function mysqlDBPlatform(): string
{
return '\Doctrine\DBAL\Platforms\MySQLPlatform';
}

/**
* @psalm-return class-string
*/
public static function mysqlAndMariaDBSharedPlatform(): string
{
if (!class_exists('\Doctrine\DBAL\Platforms\AbstractMySQLPlatform')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Query/AST/Functions/AbstractJsonFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
abstract class AbstractJsonFunctionNode extends FunctionNode
{
public const FUNCTION_NAME = null;
public const FUNCTION_NAME = '';

protected const ALPHA_NUMERIC = 'alphaNumeric';
protected const STRING_PRIMARY_ARG = 'stringPrimary';
Expand Down
98 changes: 98 additions & 0 deletions src/Query/AST/Functions/Mysql/JsonValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

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

use Doctrine\DBAL\Exception;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TokenType;

use function implode;
use function sprintf;

/**
* "JSON_VALUE" "(" StringPrimary "," StringPrimary "RETURNING" TypeExpression ")"
* @example JSON_VALUE('{"item": "shoes", "price": "49.95"}', '$.price', DECIMAL(4,2))
*/
class JsonValue extends MysqlJsonFunctionNode
{
public const FUNCTION_NAME = 'JSON_VALUE';

/** @var list<Node> */
private array $jsonArguments = [];

private string | null $returningType;

public function parse(Parser $parser): void
{
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);

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

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

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

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

// match complex returning types
$parser->match(TokenType::T_IDENTIFIER);
$this->returningType = $parser->getLexer()->token->value;

if ($parser->getLexer()->isNextToken(TokenType::T_OPEN_PARENTHESIS)) {
$parser->match(TokenType::T_OPEN_PARENTHESIS);
$parameter = $parser->Literal();
$parameters = [$parameter->value];

if ($parser->getLexer()->isNextToken(TokenType::T_COMMA)) {
while ($parser->getLexer()->isNextToken(TokenType::T_COMMA)) {
$parser->match(TokenType::T_COMMA);
$parameter = $parser->Literal();
$parameters[] = $parameter->value;
}
}

$parser->match(TokenType::T_CLOSE_PARENTHESIS);
$this->returningType .= '(' . implode(',', $parameters) . ')';
}
$argumentParsed = $this->parseArguments($parser, $this->requiredArgumentTypes);

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

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

/**
* @param SqlWalker $sqlWalker
* @return string
* @throws Exception
* @throws \Doctrine\ORM\Query\AST\ASTException
*/
public function getSql(SqlWalker $sqlWalker): string
{
$this->validatePlatform($sqlWalker);

/** @var list<string> $jsonStringArguments */
$jsonStringArguments = [];
foreach ($this->jsonArguments as $jsonArgument) {
if ($jsonArgument === null) {
$jsonStringArguments[] = 'NULL';
} else {
$jsonStringArguments[] = $jsonArgument->dispatch($sqlWalker);
}
}

return sprintf(
'%s(%s RETURNING %s)',
$this->getSQLFunction(),
implode(', ', $jsonStringArguments),
$this->returningType,
);
}
}
27 changes: 27 additions & 0 deletions src/Query/AST/Functions/Mysql/MysqlJsonFunctionNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

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

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

/**
* @internal
*/
abstract class MysqlJsonFunctionNode extends AbstractJsonFunctionNode
{
/**
* @param SqlWalker $sqlWalker
* @throws Exception
*/
protected function validatePlatform(SqlWalker $sqlWalker): void
{
if (!$sqlWalker->getConnection()->getDatabasePlatform() instanceof (DBALCompatibility::mysqlDBPlatform())) {
throw DBALCompatibility::notSupportedPlatformException(static::FUNCTION_NAME);
}
}
}
34 changes: 34 additions & 0 deletions tests/Query/Functions/Mysql/JsonValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

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

use Scienta\DoctrineJsonFunctions\Tests\Query\MysqlTestCase;

class JsonValueTest extends MysqlTestCase
{
public function testJsonValueForData()
{
$this->assertDqlProducesSql(
"SELECT JSON_VALUE(j.jsonData, '$.a', UNSIGNED) FROM Scienta\DoctrineJsonFunctions\Tests\Entities\JsonData j",
"SELECT JSON_VALUE(j0_.jsonData, '$.a' RETURNING UNSIGNED) AS sclr_0 FROM JsonData j0_"
);
}

public function testJsonValueReturningDecimal()
{
$this->assertDqlProducesSql(
"SELECT JSON_VALUE('{\"item\": \"shoes\", \"price\": \"49.95\"}', '$.price', DECIMAL(4,2)) from Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b",
"SELECT JSON_VALUE('{\"item\": \"shoes\", \"price\": \"49.95\"}', '$.price' RETURNING DECIMAL(4,2)) AS sclr_0 FROM Blank b0_"
);
}

public function testJsonValueForDataReturningVarchar()
{
$this->assertDqlProducesSql(
"SELECT JSON_VALUE(j.jsonData, '$.something', CHAR(255)) FROM Scienta\DoctrineJsonFunctions\Tests\Entities\JsonData j",
"SELECT JSON_VALUE(j0_.jsonData, '$.something' RETURNING CHAR(255)) AS sclr_0 FROM JsonData j0_"
);
}
}
1 change: 1 addition & 0 deletions tests/Query/MysqlTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ public static function loadDqlFunctions(Configuration $configuration)
$configuration->addCustomStringFunction(DqlFunctions\JsonType::FUNCTION_NAME, DqlFunctions\JsonType::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonUnquote::FUNCTION_NAME, DqlFunctions\JsonUnquote::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonValid::FUNCTION_NAME, DqlFunctions\JsonValid::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonValue::FUNCTION_NAME, DqlFunctions\JsonValue::class);
}
}
Loading