|
19 | 19 | use EventEngine\DocumentStore\Postgres\Exception\InvalidArgumentException;
|
20 | 20 | use EventEngine\DocumentStore\Postgres\Exception\RuntimeException;
|
21 | 21 | use EventEngine\Util\VariableType;
|
| 22 | + |
22 | 23 | use function implode;
|
23 | 24 | use function is_string;
|
24 | 25 | use function json_decode;
|
@@ -433,6 +434,80 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
|
433 | 434 | }
|
434 | 435 | }
|
435 | 436 |
|
| 437 | + /** |
| 438 | + * @param string $collectionName |
| 439 | + * @param string $docId |
| 440 | + * @param array $doc |
| 441 | + * @throws \Throwable if updating did not succeed |
| 442 | + */ |
| 443 | + public function replaceDoc(string $collectionName, string $docId, array $doc): void |
| 444 | + { |
| 445 | + $metadataStr = ''; |
| 446 | + $metadata = []; |
| 447 | + |
| 448 | + if($this->useMetadataColumns && array_key_exists('metadata', $doc)) { |
| 449 | + $metadata = $doc['metadata']; |
| 450 | + unset($doc['metadata']); |
| 451 | + |
| 452 | + |
| 453 | + foreach ($metadata as $k => $v) { |
| 454 | + $metadataStr .= ', '.$k.' = :'.$k; |
| 455 | + } |
| 456 | + } |
| 457 | + |
| 458 | + $cmd = <<<EOT |
| 459 | +UPDATE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)} |
| 460 | +SET doc = :doc{$metadataStr} |
| 461 | +WHERE id = :id |
| 462 | +; |
| 463 | +EOT; |
| 464 | + $this->transactional(function () use ($cmd, $docId, $doc, $metadata) { |
| 465 | + $this->connection->prepare($cmd)->execute(array_merge([ |
| 466 | + 'id' => $docId, |
| 467 | + 'doc' => json_encode($doc) |
| 468 | + ], $metadata)); |
| 469 | + }); |
| 470 | + } |
| 471 | + |
| 472 | + /** |
| 473 | + * @param string $collectionName |
| 474 | + * @param Filter $filter |
| 475 | + * @param array $set |
| 476 | + * @throws \Throwable in case of connection error or other issues |
| 477 | + */ |
| 478 | + public function replaceMany(string $collectionName, Filter $filter, array $set): void |
| 479 | + { |
| 480 | + [$filterStr, $args] = $this->filterToWhereClause($filter); |
| 481 | + |
| 482 | + $where = $filterStr? "WHERE $filterStr" : ''; |
| 483 | + |
| 484 | + $metadataStr = ''; |
| 485 | + $metadata = []; |
| 486 | + |
| 487 | + if($this->useMetadataColumns && array_key_exists('metadata', $set)) { |
| 488 | + $metadata = $set['metadata']; |
| 489 | + unset($set['metadata']); |
| 490 | + |
| 491 | + |
| 492 | + foreach ($metadata as $k => $v) { |
| 493 | + $metadataStr .= ', '.$k.' = :'.$k; |
| 494 | + } |
| 495 | + } |
| 496 | + |
| 497 | + $cmd = <<<EOT |
| 498 | +UPDATE {$this->schemaName($collectionName)}.{$this->tableName($collectionName)} |
| 499 | +SET doc = :doc{$metadataStr} |
| 500 | +$where; |
| 501 | +EOT; |
| 502 | + |
| 503 | + $args['doc'] = json_encode($set); |
| 504 | + $args = array_merge($args, $metadata); |
| 505 | + |
| 506 | + $this->transactional(function () use ($cmd, $args) { |
| 507 | + $this->connection->prepare($cmd)->execute($args); |
| 508 | + }); |
| 509 | + } |
| 510 | + |
436 | 511 | /**
|
437 | 512 | * @param string $collectionName
|
438 | 513 | * @param string $docId
|
|
0 commit comments