Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
bohanyang committed Sep 26, 2023
1 parent ebfcc27 commit bbc4975
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
22 changes: 21 additions & 1 deletion packages/mango/Doctrine/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use function is_string;
use function sprintf;
use function strpos;
use function substr;

/**
* @method $this andWhere($where)
Expand Down Expand Up @@ -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 !== []) {
Expand Down Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/mango/Serializer/NormalizerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
38 changes: 38 additions & 0 deletions packages/mango/Serializer/TogglableNameConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Mango\Serializer;

use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

class TogglableNameConverter implements AdvancedNameConverterInterface
{
public const DISABLE_NAME_CONVERTER = 'disable_name_converter';

public function __construct(
#[AutowireDecorated]
private NameConverterInterface $inner,
) {
}

public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
{
if ($context[self::DISABLE_NAME_CONVERTER] ?? false) {
return $propertyName;
}

return $this->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);
}
}

0 comments on commit bbc4975

Please sign in to comment.