Skip to content

Commit

Permalink
Add GeneratedField class; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Feb 7, 2024
1 parent ec0df10 commit 48300ea
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/Command/Database/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Cycle\ORM\Command\Traits\MapperTrait;
use Cycle\ORM\Heap\State;
use Cycle\ORM\MapperInterface;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\ORM\SchemaInterface;

/**
Expand Down Expand Up @@ -87,7 +88,7 @@ public function execute(): void
}
// unset db-generated fields if they are null
foreach ($this->generated as $column => $mode) {
if (($mode & SchemaInterface::GENERATED_DB) === 0x0) {
if (($mode & GeneratedField::DB_INSERT) === 0x0) {
continue;
}

Expand Down Expand Up @@ -164,7 +165,7 @@ private function hasGeneratedFields(): bool
$data = $this->state->getData();

foreach ($this->generated as $field => $mode) {
if (($mode & (SchemaInterface::GENERATED_DB | SchemaInterface::GENERATED_PHP_INSERT)) === 0x0) {
if (($mode & (GeneratedField::DB_INSERT | GeneratedField::PHP_INSERT)) === 0x0) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Heap/Traits/WaitFieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function waitField(string $key, bool $required = true): void

public function getWaitingFields(bool $requiredOnly = false): array
{
return array_keys($requiredOnly ? array_filter($this->waitingFields) : $this->waitingFields);
return \array_keys($requiredOnly ? \array_filter($this->waitingFields) : $this->waitingFields);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private function checkNullValuePossibility(Tuple $tuple): bool
}

if ($tuple->status < Tuple::STATUS_PREPROCESSED
&& array_intersect($this->innerKeys, $tuple->state->getWaitingFields(false)) !== []
&& \array_intersect($this->innerKeys, $tuple->state->getWaitingFields(false)) !== []
) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Relation/RefersTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function queue(Pool $pool, Tuple $tuple): void
if ($rTuple->status === Tuple::STATUS_PROCESSED
|| ($rTuple->status > Tuple::STATUS_PREPARING
&& $rTuple->state->getStatus() !== node::NEW
&& array_intersect($this->outerKeys, $rTuple->state->getWaitingFields()) === [])
&& \array_intersect($this->outerKeys, $rTuple->state->getWaitingFields()) === [])
) {
$this->pullValues($tuple->state, $rTuple->state);
$node->setRelation($this->getName(), $related);
Expand Down
38 changes: 38 additions & 0 deletions src/Schema/GeneratedField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Schema;

/**
* @see SchemaInterface::GENERATED_FIELDS
*/
final class GeneratedField
{
/**
* Field value is generated in the user space before transaction running.
*/
public const DEFAULT = 0;

/**
* Field value is generated by the Database.
* It is: autoincrement fields, sequences, timestamps, etc.
*/
public const DB_INSERT = 1;

/**
* Field value is generated by PHP code before Insert command running
* like with `CreatedAt` or `Uuid` Entity Behaviors
*
* @link https://github.com/cycle/entity-behavior
* @link https://github.com/cycle/entity-behavior-uuid
*/
public const PHP_INSERT = 2;

/**
* Field value is generated by PHP code before Update command running like with `UpdatedAt` Entity Behavior.
*
* @link https://github.com/cycle/entity-behavior
*/
public const PHP_UPDATE = 4;
}
5 changes: 0 additions & 5 deletions src/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ interface SchemaInterface
public const TYPECAST_HANDLER = 19; // Typecast handler definition that implements TypecastInterface
public const GENERATED_FIELDS = 20; // List of generated fields [field => generating type]


public const GENERATED_DB = 1; // sequences and others
public const GENERATED_PHP_INSERT = 2; // generated by PHP code on insert like uuid
public const GENERATED_PHP_UPDATE = 4; // generated by PHP code on update like time

/**
* Return all roles defined within the schema.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Transaction/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function complete(): void
{
if ($this->mode === self::MODE_OPEN_TRANSACTION) {
// Commit all of the open and normalized database transactions
foreach (array_reverse($this->drivers) as $driver) {
foreach (\array_reverse($this->drivers) as $driver) {
/** @var DriverInterface $driver */
$driver->commitTransaction();
}
Expand All @@ -94,14 +94,14 @@ public function rollback(): void
{
if ($this->mode === self::MODE_OPEN_TRANSACTION) {
// Close all open and normalized database transactions
foreach (array_reverse($this->drivers) as $driver) {
foreach (\array_reverse($this->drivers) as $driver) {
/** @var DriverInterface $driver */
$driver->rollbackTransaction();
}
}

// Close all of external types of transactions (revert changes)
foreach (array_reverse($this->executed) as $command) {
foreach (\array_reverse($this->executed) as $command) {
if ($command instanceof RollbackMethodInterface) {
$command->rollBack();
}
Expand Down Expand Up @@ -145,7 +145,7 @@ private function prepareTransaction(DriverInterface $driver): void

if ($this->mode === self::MODE_CONTINUE_TRANSACTION) {
if ($driver->getTransactionLevel() === 0) {
throw new RunnerException(sprintf(
throw new RunnerException(\sprintf(
'The `%s` driver connection has no opened transaction.',
$driver->getType()
));
Expand Down
18 changes: 8 additions & 10 deletions src/Transaction/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int
if (!$map->hasSlaves()) {
return self::RELATIONS_RESOLVED;
}
$changedFields = array_keys($tuple->state->getChanges());
$changedFields = \array_keys($tuple->state->getChanges());

// Attach children to pool
$transactData = $tuple->state->getTransactionData();
Expand All @@ -281,15 +281,15 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int
}

$innerKeys = $relation->getInnerKeys();
$isWaitingKeys = array_intersect($innerKeys, $tuple->state->getWaitingFields(true)) !== [];
$hasChangedKeys = array_intersect($innerKeys, $changedFields) !== [];
$isWaitingKeys = \array_intersect($innerKeys, $tuple->state->getWaitingFields(true)) !== [];
$hasChangedKeys = \array_intersect($innerKeys, $changedFields) !== [];
if ($relationStatus === RelationInterface::STATUS_PREPARE) {
$relData ??= $tuple->mapper->fetchRelations($tuple->entity);
$relation->prepare(
$this->pool,
$tuple,
$relData[$name] ?? null,
$isWaitingKeys || $hasChangedKeys
$isWaitingKeys || $hasChangedKeys,
);
$relationStatus = $tuple->state->getRelationStatus($relation->getName());
}
Expand All @@ -298,9 +298,9 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int
&& $relationStatus !== RelationInterface::STATUS_RESOLVED
&& !$isWaitingKeys
&& !$hasChangedKeys
&& \count(array_intersect($innerKeys, array_keys($transactData))) === \count($innerKeys)
&& \count(\array_intersect($innerKeys, \array_keys($transactData))) === \count($innerKeys)
) {
$child ??= $tuple->state->getRelation($name);
// $child ??= $tuple->state->getRelation($name);
$relation->queue($this->pool, $tuple);
$relationStatus = $tuple->state->getRelationStatus($relation->getName());
}
Expand All @@ -316,7 +316,7 @@ private function resolveSelfWithEmbedded(Tuple $tuple, RelationMap $map, bool $h
if (!$map->hasEmbedded() && !$tuple->state->hasChanges()) {
$tuple->status = !$hasDeferredRelations
? Tuple::STATUS_PROCESSED
: max(Tuple::STATUS_DEFERRED, $tuple->status);
: \max(Tuple::STATUS_DEFERRED, $tuple->status);

return;
}
Expand Down Expand Up @@ -358,7 +358,6 @@ private function resolveRelations(Tuple $tuple): void
{
$map = $this->orm->getRelationMap($tuple->node->getRole());

// Dependency relations
$result = $tuple->task === Tuple::TASK_STORE
? $this->resolveMasterRelations($tuple, $map)
: $this->resolveSlaveRelations($tuple, $map);
Expand All @@ -368,8 +367,8 @@ private function resolveRelations(Tuple $tuple): void
// Self
if ($deferred && $tuple->status < Tuple::STATUS_PROPOSED) {
$tuple->status = Tuple::STATUS_DEFERRED;
// $this->pool->attachTuple($tuple);
}

if ($isDependenciesResolved) {
if ($tuple->task === Tuple::TASK_STORE) {
$this->resolveSelfWithEmbedded($tuple, $map, $deferred);
Expand All @@ -383,7 +382,6 @@ private function resolveRelations(Tuple $tuple): void
}

if ($tuple->cascade) {
// Slave relations relations
$tuple->task === Tuple::TASK_STORE
? $this->resolveSlaveRelations($tuple, $map)
: $this->resolveMasterRelations($tuple, $map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\ORM\SchemaInterface as Schema;
use Cycle\ORM\Select\Source;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case321\Entity\User1;
Expand All @@ -27,7 +28,7 @@
],
Schema::SCHEMA => [],
Schema::GENERATED_FIELDS => [
'id' => Schema::GENERATED_DB, // autoincrement
'id' => GeneratedField::DB_INSERT, // autoincrement
],
],
'user2' => [
Expand All @@ -48,7 +49,7 @@
],
Schema::SCHEMA => [],
Schema::GENERATED_FIELDS => [
'id' => Schema::GENERATED_DB, // autoincrement
'id' => GeneratedField::DB_INSERT, // autoincrement
],
],
];
13 changes: 7 additions & 6 deletions tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Schema;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Select;
use Cycle\ORM\Tests\Fixtures\CyclicRef2\Document;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function setUp(): void
SchemaInterface::SCHEMA => [],
SchemaInterface::RELATIONS => [],
SchemaInterface::GENERATED_FIELDS => [
'balance' => SchemaInterface::GENERATED_DB, // sequence
'balance' => GeneratedField::DB_INSERT, // sequence
],
],
Document::class => [
Expand All @@ -81,10 +82,10 @@ public function setUp(): void
'updated_at' => 'datetime',
],
SchemaInterface::GENERATED_FIELDS => [
'id' => SchemaInterface::GENERATED_DB,
'body' => SchemaInterface::GENERATED_DB,
'created_at' => SchemaInterface::GENERATED_DB,
'updated_at' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE,
'id' => GeneratedField::DB_INSERT,
'body' => GeneratedField::DB_INSERT,
'created_at' => GeneratedField::DB_INSERT,
'updated_at' => GeneratedField::PHP_INSERT | GeneratedField::PHP_UPDATE,
],
],
]));
Expand Down Expand Up @@ -134,7 +135,7 @@ public function testPersistMultipleSerial(): void

protected function getCommandGenerator(): ?Transaction\CommandGeneratorInterface
{
return new class extends Transaction\CommandGenerator {
return new class () extends Transaction\CommandGenerator {
protected function storeEntity(ORMInterface $orm, Transaction\Tuple $tuple, bool $isNew): ?CommandInterface
{
/** @var CommandInterface|null $command */
Expand Down

0 comments on commit 48300ea

Please sign in to comment.