Browse Source
Merge pull request #47329 from nextcloud/feat/add-datetime-qbmapper-support
Merge pull request #47329 from nextcloud/feat/add-datetime-qbmapper-support
feat(AppFramework): Add full support for date / time / datetime columnspull/48688/head
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
29 changed files with 527 additions and 146 deletions
-
13apps/contactsinteraction/lib/Db/RecentContact.php
-
7apps/dav/lib/CalDAV/Proxy/Proxy.php
-
9apps/dav/lib/Db/Direct.php
-
4apps/federatedfilesharing/lib/FederatedShareProvider.php
-
2apps/files_reminders/lib/Db/ReminderMapper.php
-
11apps/oauth2/lib/Db/AccessToken.php
-
3apps/oauth2/lib/Db/Client.php
-
8apps/sharebymail/lib/ShareByMailProvider.php
-
7apps/user_status/lib/Db/UserStatus.php
-
5core/Db/LoginFlowV2.php
-
15lib/private/Authentication/Token/PublicKeyToken.php
-
20lib/private/Comments/Manager.php
-
24lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php
-
4lib/private/Security/RateLimiting/Backend/DatabaseBackend.php
-
8lib/private/Share20/DefaultShareProvider.php
-
4lib/private/TextToImage/Db/TaskMapper.php
-
3lib/private/Updater/Changes.php
-
56lib/public/AppFramework/Db/Entity.php
-
30lib/public/AppFramework/Db/QBMapper.php
-
52lib/public/DB/QueryBuilder/IQueryBuilder.php
-
77lib/public/DB/Types.php
-
18lib/public/Migration/Attributes/ColumnType.php
-
44tests/lib/AppFramework/Db/EntityTest.php
-
159tests/lib/AppFramework/Db/QBMapperDBTest.php
-
63tests/lib/AppFramework/Db/QBMapperTest.php
-
7tests/lib/Comments/ManagerTest.php
-
16tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php
-
2tests/lib/Share20/DefaultShareProviderTest.php
-
2tests/lib/Share20/ShareByMailProviderTest.php
@ -0,0 +1,159 @@ |
|||
<?php |
|||
/** |
|||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
|||
* SPDX-License-Identifier: AGPL-3.0-or-later |
|||
*/ |
|||
|
|||
namespace Test\AppFramework\Db; |
|||
|
|||
use Doctrine\DBAL\Schema\SchemaException; |
|||
use OCP\AppFramework\Db\Entity; |
|||
use OCP\AppFramework\Db\QBMapper; |
|||
use OCP\DB\QueryBuilder\IQueryBuilder; |
|||
use OCP\DB\Types; |
|||
use OCP\IConfig; |
|||
use OCP\IDBConnection; |
|||
use OCP\Server; |
|||
use Test\TestCase; |
|||
|
|||
/** |
|||
* @method void setTime(?\DateTime $time) |
|||
* @method ?\DateTime getTime() |
|||
* @method void setDatetime(?\DateTimeImmutable $datetime) |
|||
* @method ?\DateTimeImmutable getDatetime() |
|||
*/ |
|||
class QBDBTestEntity extends Entity { |
|||
protected ?\DateTime $time = null; |
|||
protected ?\DateTimeImmutable $datetime = null; |
|||
|
|||
public function __construct() { |
|||
$this->addType('time', Types::TIME); |
|||
$this->addType('datetime', Types::DATETIME_IMMUTABLE); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Class QBDBTestMapper |
|||
* |
|||
* @package Test\AppFramework\Db |
|||
*/ |
|||
class QBDBTestMapper extends QBMapper { |
|||
public function __construct(IDBConnection $db) { |
|||
parent::__construct($db, 'testing', QBDBTestEntity::class); |
|||
} |
|||
|
|||
public function getParameterTypeForPropertyForTest(Entity $entity, string $property) { |
|||
return parent::getParameterTypeForProperty($entity, $property); |
|||
} |
|||
|
|||
public function getById(int $id): QBDBTestEntity { |
|||
$qb = $this->db->getQueryBuilder(); |
|||
$query = $qb |
|||
->select('*') |
|||
->from($this->tableName) |
|||
->where( |
|||
$qb->expr()->eq('id', $qb->createPositionalParameter($id, IQueryBuilder::PARAM_INT)), |
|||
); |
|||
return $this->findEntity($query); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Test real database handling (serialization) |
|||
* @group DB |
|||
*/ |
|||
class QBMapperDBTest extends TestCase { |
|||
/** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */ |
|||
protected $connection; |
|||
protected $schemaSetup = false; |
|||
|
|||
protected function setUp(): void { |
|||
parent::setUp(); |
|||
|
|||
$this->connection = \OCP\Server::get(IDBConnection::class); |
|||
$this->prepareTestingTable(); |
|||
} |
|||
|
|||
public function testInsertDateTime(): void { |
|||
$mapper = new QBDBTestMapper($this->connection); |
|||
$entity = new QBDBTestEntity(); |
|||
$entity->setTime(new \DateTime('2003-01-01 12:34:00')); |
|||
$entity->setDatetime(new \DateTimeImmutable('2000-01-01 23:45:00')); |
|||
|
|||
$result = $mapper->insert($entity); |
|||
$this->assertNotNull($result->getId()); |
|||
} |
|||
|
|||
public function testRetrieveDateTime(): void { |
|||
$time = new \DateTime('2000-01-01 01:01:00'); |
|||
$datetime = new \DateTimeImmutable('2000-01-01 02:02:00'); |
|||
|
|||
$mapper = new QBDBTestMapper($this->connection); |
|||
$entity = new QBDBTestEntity(); |
|||
$entity->setTime($time); |
|||
$entity->setDatetime($datetime); |
|||
|
|||
$result = $mapper->insert($entity); |
|||
$this->assertNotNull($result->getId()); |
|||
|
|||
$dbEntity = $mapper->getById($result->getId()); |
|||
$this->assertEquals($time->format('H:i:s'), $dbEntity->getTime()->format('H:i:s')); |
|||
$this->assertEquals($datetime->format('Y-m-d H:i:s'), $dbEntity->getDatetime()->format('Y-m-d H:i:s')); |
|||
// The date is not saved for "time"
|
|||
$this->assertNotEquals($time->format('Y'), $dbEntity->getTime()->format('Y')); |
|||
} |
|||
|
|||
public function testUpdateDateTime(): void { |
|||
$time = new \DateTime('2000-01-01 01:01:00'); |
|||
$datetime = new \DateTimeImmutable('2000-01-01 02:02:00'); |
|||
|
|||
$mapper = new QBDBTestMapper($this->connection); |
|||
$entity = new QBDBTestEntity(); |
|||
$entity->setTime('now'); |
|||
$entity->setDatetime('now'); |
|||
|
|||
/** @var QBDBTestEntity */ |
|||
$entity = $mapper->insert($entity); |
|||
$this->assertNotNull($entity->getId()); |
|||
|
|||
// Update the values
|
|||
$entity->setTime($time); |
|||
$entity->setDatetime($datetime); |
|||
$mapper->update($entity); |
|||
|
|||
$dbEntity = $mapper->getById($entity->getId()); |
|||
$this->assertEquals($time->format('H:i:s'), $dbEntity->getTime()->format('H:i:s')); |
|||
$this->assertEquals($datetime->format('Y-m-d H:i:s'), $dbEntity->getDatetime()->format('Y-m-d H:i:s')); |
|||
} |
|||
|
|||
protected function prepareTestingTable(): void { |
|||
if ($this->schemaSetup) { |
|||
$this->connection->getQueryBuilder()->delete('testing')->executeStatement(); |
|||
} |
|||
|
|||
$prefix = Server::get(IConfig::class)->getSystemValueString('dbtableprefix', 'oc_'); |
|||
$schema = $this->connection->createSchema(); |
|||
try { |
|||
$schema->getTable($prefix . 'testing'); |
|||
$this->connection->getQueryBuilder()->delete('testing')->executeStatement(); |
|||
} catch (SchemaException $e) { |
|||
$this->schemaSetup = true; |
|||
$table = $schema->createTable($prefix . 'testing'); |
|||
$table->addColumn('id', Types::BIGINT, [ |
|||
'autoincrement' => true, |
|||
'notnull' => true, |
|||
]); |
|||
|
|||
$table->addColumn('time', Types::TIME, [ |
|||
'notnull' => false, |
|||
]); |
|||
|
|||
$table->addColumn('datetime', Types::DATETIME_IMMUTABLE, [ |
|||
'notnull' => false, |
|||
]); |
|||
|
|||
$table->setPrimaryKey(['id']); |
|||
$this->connection->migrateToSchema($schema); |
|||
} |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue