Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
bohanyang committed Sep 6, 2023
1 parent 5508003 commit 95e7a07
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
60 changes: 43 additions & 17 deletions packages/mango/Doctrine/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ private function getResultAlias(): string

public function selectFrom(string|array $from, ?string ...$selects): self
{
$this->addSelects($this->addFrom([$this->builder, 'from'], $from), $selects);

return $this;
return $this->addFrom([$this->builder, 'from'], $from)->select(...$selects);
}

private function addSelectTable(string $alias, Table $table): void
Expand All @@ -250,8 +248,28 @@ private function addSelectTable(string $alias, Table $table): void
: $alias;
}

private function addJoin(callable $builderCall, string $fromAlias, string|array $join, string|Closure|array $on): string
public function addInnerJoin(string|array $join, string|Closure|array $on, ?string $fromAlias = null): self
{
$this->addJoin('join', $fromAlias, $join, $on);

return $this;
}

public function addLeftJoin(string|array $join, string|Closure|array $on, ?string $fromAlias = null): self
{
$this->addJoin('leftJoin', $fromAlias, $join, $on);

return $this;
}

private function addJoin(string|callable $builderCall, ?string $fromAlias, string|array $join, string|Closure|array $on): string
{
$fromAlias ??= $this->fromAlias;

if (is_string($builderCall)) {
$builderCall = [$this->builder, $builderCall];
}

[$joinTable, $joinAlias] = is_string($join) ? [$join, $join] : $join;

$this->addSelectTable($joinAlias, $table = $this->schema->getTable($joinTable));
Expand All @@ -276,7 +294,7 @@ private function addJoin(callable $builderCall, string $fromAlias, string|array

public function join(string $fromAlias, string|array $join, string|Closure|array $on, ?string ...$selects): self
{
$this->addSelects($this->addJoin([$this->builder, 'join'], $fromAlias, $join, $on), $selects);
$this->addSelects($selects, $this->addJoin([$this->builder, 'join'], $fromAlias, $join, $on));

return $this;
}
Expand All @@ -288,7 +306,7 @@ public function joinOn(string|array $joinTable, string $joinColumn, string $from

public function leftJoin(string $fromAlias, string|array $join, string|Closure|array $on, ?string ...$selects): self
{
$this->addSelects($this->addJoin([$this->builder, 'leftJoin'], $fromAlias, $join, $on), $selects);
$this->addSelects($selects, $this->addJoin([$this->builder, 'leftJoin'], $fromAlias, $join, $on));

return $this;
}
Expand All @@ -300,7 +318,7 @@ public function leftJoinOn(string|array $joinTable, string $joinColumn, string $

public function update(string|array $from, array $data = []): self
{
$fromAlias = $this->addFrom([$this->builder, 'update'], $from);
$this->addFrom([$this->builder, 'update'], $from);

foreach ($data as $column => $value) {
$this->builder->set(...$this->bindUpdate($column, $value));
Expand All @@ -309,11 +327,14 @@ public function update(string|array $from, array $data = []): self
return $this;
}

public function delete(string|array $from): self
public function delete(string ...$from): self
{
$this->addFrom([$this->builder, 'delete'], $from);
return $this->addFrom([$this->builder, 'delete'], $from);
}

return $this;
public function from(string ...$from): self
{
return $this->addFrom([$this->builder, 'from'], $from);
}

public function patch(string|array $from, array $data = []): ?self
Expand All @@ -327,9 +348,10 @@ public function patch(string|array $from, array $data = []): ?self
return $data === [] ? null : $this->update($from, $data);
}

/** @return string The from alias */
private function addFrom(callable $builderCall, string|array $from): string
private function addFrom(callable $builderCall, string|array $from): self
{
$from = is_array($from) && 1 === count($from) ? $from[0] : $from;

[$fromTable, $fromAlias] = is_string($from) ? [$from, null] : $from;

$table = $this->schema->getTable($fromTable);
Expand All @@ -339,7 +361,9 @@ private function addFrom(callable $builderCall, string|array $from): string

$this->addSelectTable($fromAlias, $table);

return $this->fromAlias = $fromAlias;
$this->fromAlias = $fromAlias;

return $this;
}

/** @return Column[] */
Expand All @@ -362,10 +386,12 @@ private function getSelectColumns(Table $table, array $selects): Generator
}
}

private function addSelects(string $tableAlias, array $selects): void
public function select(string ...$selects): self
{
$tableAlias = $this->lastTableAlias;

if (array_key_exists(0, $selects) && $selects[0] === null) {
return;
return $this;
}

$columns = $this->getSelectColumns($this->selectTableMap[$tableAlias], $selects);
Expand All @@ -390,6 +416,8 @@ private function addSelects(string $tableAlias, array $selects): void
)
. ' ' . $this->platform->quoteSingleIdentifier($resultAlias);
}
return $this;
}
public function selectRaw(array $alias, string $type, string|array ...$parts): self
Expand Down Expand Up @@ -417,7 +445,6 @@ public function selectRaw(array $alias, string $type, string|array ...$parts): s
$this->resultTableAliasMap[$resultAlias] = $tableAlias;
$this->resultColumnAliasMap[$resultAlias] = $alias;
$this->selects[] =
$type->convertToPHPValueSQL($sql, $this->platform) . ' '
. $this->platform->quoteSingleIdentifier($resultAlias);
Expand Down Expand Up @@ -654,7 +681,6 @@ public function fetchOne(): mixed
return $this->convertResultToPHPValue('c0', $values[0]);
}


public function fetchOneWithDefault(mixed $value): mixed
{
$values = $this->getQueryResult()->fetchFirstColumn();
Expand Down
4 changes: 1 addition & 3 deletions packages/mango/Doctrine/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public function getColumn(string $name): Column

public function addColumn(string $name, string $typeName, array $options = [], ?string $alias = null): Column
{
$this->columnMap[$alias ?? $name] = $column = $this->table->addColumn($name, $typeName, $options);

return $column;
return $this->columnMap[$alias ?? $name] = $this->table->addColumn($name, $typeName, $options);
}

public function getColumns(): array
Expand Down

0 comments on commit 95e7a07

Please sign in to comment.