|
19 | 19 | use OCP\DB\Schema\ITable; |
20 | 20 | use OCP\IDBConnection; |
21 | 21 | use OCP\Server; |
22 | | -use OCP\Snowflake\ISnowflakeGenerator; |
23 | 22 |
|
24 | 23 | final class EntityManager { |
25 | 24 | public function __construct( |
@@ -79,26 +78,26 @@ public function insert(object $entity): object { |
79 | 78 | if ($propertyAttributes->id !== null && $propertyAttributes->column !== null) { |
80 | 79 | $generatorClass = $propertyAttributes->id->generatorClass; |
81 | 80 | if ($generatorClass) { |
82 | | - if ($generatorClass === ISnowflakeGenerator::class) { |
83 | | - $generator = Server::get($generatorClass); |
84 | | - $values[$propertyAttributes->column->name] = $generator->nextId(); |
85 | | - $property->setValue($entity, $insert->createNamedParameter($values[$propertyAttributes->column->name])); |
86 | | - } |
87 | | - |
| 81 | + $generator = Server::get($generatorClass); |
| 82 | + $value = $generator->nextId(); |
| 83 | + $type = $this->getParameterType($propertyAttributes->column->type, false); |
| 84 | + $values[$propertyAttributes->column->name] = $insert->createNamedParameter($value, $type); |
| 85 | + $property->setValue($entity, $value); |
88 | 86 | continue; |
89 | 87 | } |
90 | 88 |
|
91 | 89 | if ($isComposite) { |
92 | 90 | // A composite primary key can't rely on a single autoincrement column: every |
93 | 91 | // part must already be set on the entity (e.g. a foreign key id, or a value |
94 | 92 | // assigned by the caller) before insert() is called. |
| 93 | + /** @psalm-suppress MixedAssignment */ |
95 | 94 | $value = $property->getValue($entity); |
96 | 95 | if ($value === null) { |
97 | 96 | throw new \LogicException($entity::class . '::' . $property->getName() . ' is part of a composite primary key and must be set before insert(); it cannot rely on DB autoincrement.'); |
98 | 97 | } |
99 | 98 |
|
100 | 99 | if (!is_string($value) && !is_int($value)) { |
101 | | - throw new \LogicException($entity::class . '::' . $property->getName() . ' is part of a composite primary key and must be set to a int or string before insert();.'); |
| 100 | + throw new \LogicException($entity::class . '::' . $property->getName() . ' is part of a composite primary key and must be set to an int or string before insert().'); |
102 | 101 | } |
103 | 102 |
|
104 | 103 | $type = $this->getParameterType($propertyAttributes->column->type, false); |
@@ -222,10 +221,14 @@ public function delete(object $entity): void { |
222 | 221 | foreach ($entityInfo->propertiesAttributes as $propertyAttributes) { |
223 | 222 | if ($propertyAttributes->id !== null && $propertyAttributes->column !== null) { |
224 | 223 | $property = $propertyAttributes->property; |
225 | | - /** @var int|string $value */ |
| 224 | + /** @var int|string|null $value */ |
226 | 225 | $value = $property->getValue($entity); |
| 226 | + if ($value === null) { |
| 227 | + throw new \LogicException('Trying to delete an entity with no primary key set.'); |
| 228 | + } |
227 | 229 |
|
228 | | - $delete->andWhere($delete->expr()->eq($propertyAttributes->column->name, $delete->createNamedParameter($value))); |
| 230 | + $type = $this->getParameterType($propertyAttributes->column->type, false); |
| 231 | + $delete->andWhere($delete->expr()->eq($propertyAttributes->column->name, $delete->createNamedParameter($value, $type))); |
229 | 232 | $foundId = true; |
230 | 233 | }; |
231 | 234 | } |
|
0 commit comments