-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Steve Lacey <[email protected]>
- Loading branch information
1 parent
9dd31e4
commit 53a71be
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Query\Mysql; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
|
||
class JsonLength extends FunctionNode | ||
{ | ||
protected $target; | ||
|
||
protected $path; | ||
|
||
public function parse(Parser $parser) | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
|
||
$this->target = $parser->StringPrimary(); | ||
|
||
if ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) { | ||
$parser->match(Lexer::T_COMMA); | ||
|
||
$this->path = $parser->StringPrimary(); | ||
} | ||
|
||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
public function getSql(SqlWalker $sqlWalker) | ||
{ | ||
$target = $sqlWalker->walkStringPrimary($this->target); | ||
|
||
if ($this->path !== null) { | ||
$path = $sqlWalker->walkStringPrimary($this->path); | ||
|
||
return sprintf('JSON_LENGTH(%s, %s)', $target, $path); | ||
} | ||
|
||
return sprintf('JSON_LENGTH(%s)', $target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Tests\Query\Mysql; | ||
|
||
use DoctrineExtensions\Tests\Query\MysqlTestCase; | ||
|
||
class JsonLengthTest extends MysqlTestCase | ||
{ | ||
public function testJsonLengthWithoutPath(): void | ||
{ | ||
$this->assertDqlProducesSql( | ||
"SELECT JSON_LENGTH('{}') from DoctrineExtensions\Tests\Entities\Blank as blank", | ||
"SELECT JSON_LENGTH('{}') AS sclr_0 FROM Blank b0_" | ||
); | ||
} | ||
|
||
public function testJsonLengthWithPathAsParam(): void | ||
{ | ||
$this->assertDqlProducesSql( | ||
"SELECT JSON_LENGTH('{}', :param) from DoctrineExtensions\Tests\Entities\Blank as blank", | ||
"SELECT JSON_LENGTH('{}', ?) AS sclr_0 FROM Blank b0_" | ||
); | ||
} | ||
|
||
public function testJsonLengthWithPathAsExplicit(): void | ||
{ | ||
$this->assertDqlProducesSql( | ||
"SELECT JSON_LENGTH('{}', '$[0]') from DoctrineExtensions\Tests\Entities\Blank as blank", | ||
"SELECT JSON_LENGTH('{}', '$[0]') AS sclr_0 FROM Blank b0_" | ||
); | ||
} | ||
} |