From bbc4975d433465287e18d6fd0365ac13503a0dda Mon Sep 17 00:00:00 2001 From: Bohan Yang Date: Tue, 26 Sep 2023 09:17:12 +0000 Subject: [PATCH] u --- packages/mango/Doctrine/Query.php | 22 ++++++++++- packages/mango/Serializer/NormalizerTrait.php | 4 +- .../Serializer/TogglableNameConverter.php | 38 +++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 packages/mango/Serializer/TogglableNameConverter.php diff --git a/packages/mango/Doctrine/Query.php b/packages/mango/Doctrine/Query.php index db8a769..987daee 100644 --- a/packages/mango/Doctrine/Query.php +++ b/packages/mango/Doctrine/Query.php @@ -37,6 +37,7 @@ use function is_string; use function sprintf; use function strpos; +use function substr; /** * @method $this andWhere($where) @@ -475,6 +476,25 @@ public function queryWithWriteLock(): self return $this; } + public function returning(?string ...$selects): self + { + $selectSql = $this->schema->createQuery() + ->from($this->selectTableMap[$this->fromAlias]->getName()) + ->select(...$selects) + ->getSQL(); + + // strlen('SELECT ') === 7 + $returningSql = ' RETURNING ' . substr($selectSql, 7, strpos($selectSql, 'FROM') - 8); + + $this->result = $this->connection->executeQuery( + $this->getSQL() . $returningSql, + $this->getParameters(), + $this->getParameterTypes(), + ); + + return $this; + } + public function getSQL(): string { if ($this->selects !== []) { @@ -926,7 +946,7 @@ public function where(mixed ...$expressions): self public function onConflictDoNothing(string|array $conflict, ?int $rowsAffected = null): int { - $conflict = array_map(static fn ($n) => $this->quoteColumn($n), (array) $conflict); + $conflict = array_map(fn ($n) => $this->quoteColumn($n), (array) $conflict); return $this->executeStatement($rowsAffected, static function (&$sql) use ($conflict) { $sql .= ' ON CONFLICT (' . implode(', ', $conflict) . ') DO NOTHING'; diff --git a/packages/mango/Serializer/NormalizerTrait.php b/packages/mango/Serializer/NormalizerTrait.php index 0ffc06d..a501e18 100644 --- a/packages/mango/Serializer/NormalizerTrait.php +++ b/packages/mango/Serializer/NormalizerTrait.php @@ -15,12 +15,12 @@ trait NormalizerTrait private function normalize(?object $data, ...$context): ?array { return $data === null ? $data - : $this->normalizer->normalize($data, 'json', $context); + : $this->normalizer->normalize($data, null, $context); } private function denormalize(?array $data, string $type, ...$context): ?object { return $data === null ? $data - : $this->denormalizer->denormalize($data, $type, 'json', $context); + : $this->denormalizer->denormalize($data, $type, null, $context); } } diff --git a/packages/mango/Serializer/TogglableNameConverter.php b/packages/mango/Serializer/TogglableNameConverter.php new file mode 100644 index 0000000..8259e10 --- /dev/null +++ b/packages/mango/Serializer/TogglableNameConverter.php @@ -0,0 +1,38 @@ +inner->normalize($propertyName, $class, $format, $context); + } + + public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string + { + if ($context[self::DISABLE_NAME_CONVERTER] ?? false) { + return $propertyName; + } + + return $this->inner->denormalize($propertyName, $class, $format, $context); + } +}