-
-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add support for formatting functions to_char
, to_date
, to_number
, to_timestamp
#386
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
martin-georgiev
merged 7 commits into
martin-georgiev:main
from
karpilin:DataTypeFormattingFunctions
Jun 15, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c533b42
feat: add support for formatting functions 'to_char', 'to_date', 'to_…
karpilin 739c919
Adds more tests
karpilin a456d1e
Merge branch 'main' into DataTypeFormattingFunctions
martin-georgiev 800a8e6
Updates documentation URL and adds since tag
karpilin 066acda
further corrections to tests
karpilin e20f28d
Merge branch 'main' into DataTypeFormattingFunctions
martin-georgiev 93b5f8b
Apply suggestions from code review
martin-georgiev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
21 changes: 21 additions & 0 deletions
21
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/ToChar.php
This file contains hidden or 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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
||
/** | ||
* Implementation of PostgreSQL to_char(). | ||
* | ||
* @see https://www.postgresql.org/docs/17/functions-formatting.html | ||
* @since 3.3.0 | ||
*/ | ||
class ToChar extends BaseFunction | ||
{ | ||
protected function customizeFunction(): void | ||
{ | ||
$this->setFunctionPrototype('to_char(%s, %s)'); | ||
$this->addNodeMapping('ArithmeticFactor'); | ||
$this->addNodeMapping('StringPrimary'); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/ToDate.php
This file contains hidden or 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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
||
/** | ||
* Implementation of PostgreSQL to_date(). | ||
* | ||
* @see https://www.postgresql.org/docs/17/functions-formatting.html | ||
* @since 3.3.0 | ||
*/ | ||
class ToDate extends BaseFunction | ||
{ | ||
protected function customizeFunction(): void | ||
{ | ||
$this->setFunctionPrototype('to_date(%s, %s)'); | ||
$this->addNodeMapping('StringPrimary'); | ||
$this->addNodeMapping('StringPrimary'); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/ToNumber.php
This file contains hidden or 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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
||
/** | ||
* Implementation of PostgreSQL to_number(). | ||
* | ||
* @see https://www.postgresql.org/docs/17/functions-formatting.html | ||
* @since 3.3.0 | ||
*/ | ||
class ToNumber extends BaseFunction | ||
{ | ||
protected function customizeFunction(): void | ||
{ | ||
$this->setFunctionPrototype('to_number(%s, %s)'); | ||
$this->addNodeMapping('StringPrimary'); | ||
$this->addNodeMapping('StringPrimary'); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/ToTimestamp.php
This file contains hidden or 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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
||
/** | ||
* Implementation of PostgreSQL to_timestamp(). | ||
* | ||
* @see https://www.postgresql.org/docs/17/functions-formatting.html | ||
* @since 3.3.0 | ||
*/ | ||
class ToTimestamp extends BaseFunction | ||
{ | ||
protected function customizeFunction(): void | ||
{ | ||
$this->setFunctionPrototype('to_timestamp(%s, %s)'); | ||
$this->addNodeMapping('StringPrimary'); | ||
$this->addNodeMapping('StringPrimary'); | ||
} | ||
} |
This file contains hidden or 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
153 changes: 153 additions & 0 deletions
153
tests/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/ToCharTest.php
This file contains hidden or 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,153 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
||
use Doctrine\DBAL\Exception; | ||
use Doctrine\DBAL\Exception\DriverException; | ||
use Doctrine\ORM\Query\QueryException; | ||
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ToChar; | ||
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ToTimestamp; | ||
use Tests\Integration\MartinGeorgiev\TestCase; | ||
|
||
class ToCharTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->createTestSchema(); | ||
$this->createTestTableForDateFixture(); | ||
$this->createTestTableForNumericFixture(); | ||
} | ||
|
||
protected function getStringFunctions(): array | ||
{ | ||
return [ | ||
'to_char' => ToChar::class, | ||
'to_timestamp' => ToTimestamp::class, | ||
]; | ||
} | ||
|
||
public function test_tochar_for_timestamp(): void | ||
{ | ||
$dql = "SELECT to_char(t.datetimetz1, 'HH12:MI:SS') AS result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsDates t WHERE t.id = 1"; | ||
$result = $this->executeDqlQuery($dql); | ||
static::assertSame('10:30:00', $result[0]['result']); | ||
} | ||
|
||
public function test_tochar_for_interval(): void | ||
{ | ||
$dql = "SELECT to_char(t.dateinterval1, 'HH24:MI:SS') AS result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsDates t WHERE t.id = 1"; | ||
$result = $this->executeDqlQuery($dql); | ||
static::assertSame('15:02:12', $result[0]['result']); | ||
} | ||
|
||
public function test_tochar_for_numeric(): void | ||
{ | ||
$dql = "SELECT to_char(t.decimal1, '999D99S') AS result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsNumerics t WHERE t.id = 1"; | ||
$result = $this->executeDqlQuery($dql); | ||
static::assertSame('125.80-', $result[0]['result']); | ||
} | ||
|
||
public function test_tochar_for_numeric_literal(): void | ||
{ | ||
$dql = "SELECT to_char(125.80, '999D99S') AS result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsNumerics t WHERE t.id = 1"; | ||
$result = $this->executeDqlQuery($dql); | ||
static::assertSame('125.80+', $result[0]['result']); | ||
} | ||
|
||
public function test_tochar_for_numeric_literal_negative(): void | ||
{ | ||
$dql = "SELECT to_char(-125.80, '999D99S') AS result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsNumerics t WHERE t.id = 1"; | ||
$result = $this->executeDqlQuery($dql); | ||
static::assertSame('125.80-', $result[0]['result']); | ||
} | ||
|
||
public function test_tochar_with_subfunction(): void | ||
{ | ||
$dql = "SELECT to_char(to_timestamp('05 Dec 2000 at 11:55 and 32 seconds', 'DD Mon YYYY tt HH24:MI ttt SS ttttttt'), 'HH24:MI:SS') AS result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsDates t WHERE t.id = 1"; | ||
$result = $this->executeDqlQuery($dql); | ||
static::assertSame('11:55:32', $result[0]['result']); | ||
} | ||
|
||
public function test_tochar_throws_with_invalid_input_type(): void | ||
{ | ||
$this->expectException(DriverException::class); | ||
$dql = "SELECT to_char('can only be timestamp, interval or numeric, never a string', 'DD Mon YYYY') AS result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsDates t WHERE t.id = 1"; | ||
$this->executeDqlQuery($dql); | ||
} | ||
|
||
public function test_tochar_throws_with_invalid_format(): void | ||
{ | ||
$this->expectException(Exception::class); | ||
$dql = "SELECT to_char(t.decimal1, 'invalid_format') FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsNumerics t WHERE t.id = 1"; | ||
$this->executeDqlQuery($dql); | ||
} | ||
|
||
public function test_tochar_throws_with_unsupported_null_input(): void | ||
{ | ||
$this->expectException(QueryException::class); | ||
$dql = "SELECT to_char(NULL, '999D99S') AS result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsNumerics t WHERE t.id = 1"; | ||
$this->executeDqlQuery($dql); | ||
} | ||
|
||
private function createTestTableForDateFixture(): void | ||
{ | ||
$tableName = 'containsdates'; | ||
|
||
$this->dropTestTableIfItExists($tableName); | ||
|
||
$fullTableName = \sprintf('%s.%s', self::DATABASE_SCHEMA, $tableName); | ||
$sql = \sprintf(' | ||
CREATE TABLE %s ( | ||
id SERIAL PRIMARY KEY, | ||
date1 DATE, | ||
date2 DATE, | ||
datetime1 TIMESTAMP, | ||
datetime2 TIMESTAMP, | ||
time1 TIME, | ||
time2 TIME, | ||
datetimetz1 TIMESTAMPTZ, | ||
datetimetz2 TIMESTAMPTZ, | ||
dateinterval1 INTERVAL | ||
) | ||
', $fullTableName); | ||
|
||
$this->connection->executeStatement($sql); | ||
|
||
$sql = \sprintf(' | ||
INSERT INTO %s.containsdates (date1, date2, datetime1, datetime2, time1, time2, datetimetz1, datetimetz2, dateinterval1) VALUES | ||
(\'2023-06-15\', \'2023-06-16\', \'2023-06-15 10:30:00\', \'2023-06-16 11:45:00\', \'10:30:00\', \'11:45:00\', \'2023-06-15 10:30:00+00\', \'2023-06-16 11:45:00+00\', \'15h 2m 12s\') | ||
', self::DATABASE_SCHEMA); | ||
$this->connection->executeStatement($sql); | ||
} | ||
|
||
private function createTestTableForNumericFixture(): void | ||
{ | ||
$tableName = 'containsnumerics'; | ||
|
||
$this->dropTestTableIfItExists($tableName); | ||
|
||
$fullTableName = \sprintf('%s.%s', self::DATABASE_SCHEMA, $tableName); | ||
$sql = \sprintf(' | ||
CREATE TABLE %s ( | ||
id SERIAL PRIMARY KEY, | ||
integer1 INTEGER, | ||
integer2 INTEGER, | ||
bigint1 BIGINT, | ||
bigint2 BIGINT, | ||
decimal1 DECIMAL, | ||
decimal2 DECIMAL | ||
) | ||
', $fullTableName); | ||
|
||
$this->connection->executeStatement($sql); | ||
|
||
$sql = \sprintf(' | ||
INSERT INTO %s.containsnumerics (integer1, integer2, bigint1, bigint2, decimal1, decimal2) VALUES | ||
(10, 20, 1000, 2000, -125.8, 20.5) | ||
', self::DATABASE_SCHEMA); | ||
$this->connection->executeStatement($sql); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/ToDateTest.php
This file contains hidden or 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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
||
use Doctrine\DBAL\Exception\DriverException; | ||
use Doctrine\ORM\Query\QueryException; | ||
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ToDate; | ||
|
||
class ToDateTest extends TextTestCase | ||
{ | ||
protected function getStringFunctions(): array | ||
{ | ||
return [ | ||
'to_date' => ToDate::class, | ||
]; | ||
} | ||
|
||
public function test_todate(): void | ||
{ | ||
$dql = "SELECT to_date('05 Dec 2000', 'DD Mon YYYY') as result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t WHERE t.id = 1"; | ||
$result = $this->executeDqlQuery($dql); | ||
static::assertSame('2000-12-05', $result[0]['result']); | ||
} | ||
|
||
public function test_todate_throws_with_invalid_input(): void | ||
{ | ||
$this->expectException(DriverException::class); | ||
$dql = "SELECT to_date('invalid_date', 'DD Mon YYYY') as result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t WHERE t.id = 1"; | ||
$this->executeDqlQuery($dql); | ||
} | ||
|
||
public function test_todate_with_invalid_format(): void | ||
{ | ||
$dql = "SELECT to_date('05 Dec 2000', 'invalid_format') as result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t WHERE t.id = 1"; | ||
$result = $this->executeDqlQuery($dql); | ||
static::assertSame('2005-01-01', $result[0]['result']); | ||
} | ||
|
||
public function test_todate_throws_with_unsupported_format_type(): void | ||
{ | ||
$this->expectException(QueryException::class); | ||
$dql = "SELECT to_date('05 Dec 2000', 1) as result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t WHERE t.id = 1"; | ||
$this->executeDqlQuery($dql); | ||
} | ||
|
||
public function test_todate_throws_with_unsupported_null_input(): void | ||
{ | ||
$this->expectException(QueryException::class); | ||
$dql = "SELECT to_date(null, 'DD Mon YYYY') as result FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsTexts t WHERE t.id = 1"; | ||
$this->executeDqlQuery($dql); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.