diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 2cd1dae..843f620 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -17,6 +17,7 @@ namespace Colopl\Spanner\Query; +use Closure; use Colopl\Spanner\Connection; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Query\Builder as BaseBuilder; @@ -61,13 +62,23 @@ public function update(array $values) /** * @inheritDoc */ - public function updateOrInsert(array $attributes, array $values = []) + public function updateOrInsert(array $attributes, array|callable $values = []) { - if (! $this->where($attributes)->exists()) { + $exists = $this->where($attributes)->exists(); + + if ($values instanceof Closure) { + $values = $values($exists); + } + + if (! $exists) { return $this->insert(array_merge($attributes, $values)); } - return (bool) $this->take(1)->update(Arr::except($values, array_keys($attributes))); + if (empty($values)) { + return true; + } + + return (bool) $this->limit(1)->update(Arr::except($values, array_keys($attributes))); } /**