Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! feat: extend Entity and adjust QBMapper to support Snowflake IDs
  • Loading branch information
miaulalala committed Dec 23, 2025
commit 9c46c777e6f98d9ae3cb0784616e1c6cfe15cfda
2 changes: 1 addition & 1 deletion apps/files_versions/lib/Db/VersionEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct() {

public function jsonSerialize(): array {
return [
'id' => $this->id,
'id' => $this->getId(),
'file_id' => $this->fileId,
'timestamp' => $this->timestamp,
'size' => $this->size,
Expand Down
22 changes: 13 additions & 9 deletions lib/public/AppFramework/Db/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@
use function substr;

/**
* @method int getId()
* @method void setId(int $id)
* @since 7.0.0
* @psalm-consistent-constructor
*/
abstract class Entity {
/** @var int $id */
public $id = null;

private ?int $id = null;
private array $_updatedFields = [];
/** @var array<string, \OCP\DB\Types::*> */
/** @psalm-param $_fieldTypes array<string, Types::*> */
private array $_fieldTypes = ['id' => 'integer'];

public function setId($id): void {
$this->id = $id;
}

public function getId(): ?int {
return $this->id;
}

/**
* Simple alternative constructor for building entities from a request
* @param array $params the array which was obtained via $this->params('key')
Expand Down Expand Up @@ -64,7 +68,7 @@ public static function fromRow(array $row): static {


/**
* @return array<string, \OCP\DB\Types::*> with attribute and type
* @return array<string, Types::*> with attribute and type
* @since 7.0.0
*/
public function getFieldTypes(): array {
Expand Down Expand Up @@ -266,8 +270,8 @@ public function getUpdatedFields(): array {
* that value once its being returned from the database
*
* @param string $fieldName the name of the attribute
* @param \OCP\DB\Types::* $type the type which will be used to match a cast
* @since 31.0.0 Parameter $type is now restricted to {@see \OCP\DB\Types} constants. The formerly accidentally supported types 'int'|'bool'|'double' are mapped to Types::INTEGER|Types::BOOLEAN|Types::FLOAT accordingly.
* @param Types::* $type the type which will be used to match a cast
* @since 31.0.0 Parameter $type is now restricted to {@see Types} constants. The formerly accidentally supported types 'int'|'bool'|'double' are mapped to Types::INTEGER|Types::BOOLEAN|Types::FLOAT accordingly.
* @since 7.0.0
*/
protected function addType(string $fieldName, string $type): void {
Expand Down
2 changes: 1 addition & 1 deletion lib/public/AppFramework/Db/QBMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function insert(Entity $entity): Entity {

if ($entity instanceof SnowflakeAwareEntity) {
/** @psalm-suppress DocblockTypeContradiction */
if ($entity->id === null) {
if ($entity->getId() === null) {
$entity->setId();
}
$qb->executeStatement();
Expand Down
17 changes: 4 additions & 13 deletions lib/public/AppFramework/Db/SnowflakeAwareEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,23 @@
*/
#[Consumable(since: '33.0.0')]
abstract class SnowflakeAwareEntity extends Entity {
/** @var string $id */
public $id;

private ?string $id = null;
protected ?Snowflake $snowflake = null;

/** @var array<string, \OCP\DB\Types::*> */
/** @psalm-param $_fieldTypes array<string, Types::*> */
private array $_fieldTypes = ['id' => Types::STRING];

/**
* Automatically creates a snowflake ID
*/
public function setId(): void {
#[\Override]
public function setId($id = null): void {
if ($this->id === null) {
$this->id = Server::get(ISnowflakeGenerator::class)->nextId();
$this->markFieldUpdated('id');
}
}

/**
* @psalm-suppress InvalidReturnStatement
* @psalm-suppress InvalidReturnType
*/
public function getId(): string {
return $this->id;
}

public function getCreatedAt(): ?\DateTimeImmutable {
return $this->getSnowflake()?->getCreatedAt();
}
Expand Down