Skip to content

Commit

Permalink
🐛 bugfix/doctrine#6261 - Allow (also for DATETIME) dynamic intervals …
Browse files Browse the repository at this point in the history
…in DATE_ADD & DATE_SUB for SQLite

- ✅ Add related functional test
  • Loading branch information
DaedalusDev committed Jan 26, 2024
1 parent 843719c commit 1a09101
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@
<file name="tests/Platforms/DB2PlatformTest.php"/>
<file name="tests/Platforms/OraclePlatformTest.php"/>
<file name="tests/Platforms/SqlitePlatformTest.php"/>
<file name="tests/Functional/Ticket/DBAL6261Test.php"/>

<!-- See https://github.com/doctrine/dbal/pull/3574 -->
<file name="tests/Query/Expression/ExpressionBuilderTest.php"/>
Expand Down
103 changes: 103 additions & 0 deletions tests/Functional/Ticket/DBAL6261Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Doctrine\DBAL\Tests\Functional\Ticket;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;

class DBAL6261Test extends FunctionalTestCase
{
/** @throws Exception */
protected function setUp(): void
{
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
return;
}

self::markTestSkipped('Related to SQLite only');
}

/**
* @throws SchemaException
* @throws Exception
*/
public function testUnsignedIntegerDetection(): void
{
$testTable = 'dbal6261tbl';
$testTableService = $testTable . '_service';
$testTableAgency = $testTable . '_agency';

$tableService = new Table($testTableService);
$tableService->addColumn('id', Types::INTEGER);
$tableService->addColumn('end_at', Types::DATETIME_MUTABLE);
$tableService->addColumn('agency_id', Types::INTEGER);
$tableService->setPrimaryKey(['id']);

$tableAgency = new Table($testTableAgency);
$tableAgency->addColumn('id', Types::INTEGER);
$tableAgency->addColumn('utc_offset', Types::INTEGER);
$tableAgency->setPrimaryKey(['id']);

$this->dropAndCreateTable($tableService);
$this->dropAndCreateTable($tableAgency);

$this->connection->insert($testTableAgency, [
'id' => 1,
'utc_offset' => 120,
]);

$match1 = [
'id' => 1,
'end_at' => '2023-01-05 16:59:59',
'agency_id' => 1,
];
$match2 = [
'id' => 2,
'end_at' => '2023-01-05 17:00:00',
'agency_id' => 1,
];
$wontMatch1 = [
'id' => 3,
'end_at' => '2023-01-05 17:00:01',
'agency_id' => 1,
];

$this->connection->insert($testTableService, $match1);
$this->connection->insert($testTableService, $match2);
$this->connection->insert($testTableService, $wontMatch1);

$platfom = $this->connection->getDatabasePlatform();
$qb = $this->connection->createQueryBuilder()
->select('s.*')
->from($testTableService, 's')
->leftJoin('s', $testTableAgency, 'a', 's.agency_id = a.id')
->where(
's.end_at <= ' . $platfom->getDateAddMinutesExpression(
"DATETIME('2023-01-05 15:00:00')",
'a.utc_offset',
),
);
$sql = $qb
->getSQL();
self::assertEquals(
'SELECT s.* FROM dbal6261tbl_service s '
. 'LEFT JOIN dbal6261tbl_agency a ON s.agency_id = a.id '
. "WHERE s.end_at <= DATETIME(DATETIME('2023-01-05 15:00:00'),'+' || a.utc_offset || ' MINUTE')",
$sql,
);

$result = $qb->fetchAllAssociative();

self::assertEquals(
[
$match1,
$match2,
],
$result,
);
}
}

0 comments on commit 1a09101

Please sign in to comment.