diff options
Diffstat (limited to 'services/app/src/Sql/Internal/InsertFromSelect.php')
| -rw-r--r-- | services/app/src/Sql/Internal/InsertFromSelect.php | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/services/app/src/Sql/Internal/InsertFromSelect.php b/services/app/src/Sql/Internal/InsertFromSelect.php new file mode 100644 index 0000000..c003aa4 --- /dev/null +++ b/services/app/src/Sql/Internal/InsertFromSelect.php @@ -0,0 +1,78 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Albatross\Sql\Internal; + +use Nsfisis\Albatross\Exceptions\InvalidSqlException; +use Nsfisis\Albatross\Sql\QueryBuilder; + +final class InsertFromSelect +{ + /** + * @var ?list<string> + */ + private ?array $fields; + + private ?Select $from; + + /** + * @internal + */ + public function __construct( + private readonly QueryBuilder $sql, + private readonly string $table, + ) { + } + + /** + * @param list<string> $fields + */ + public function fields(array $fields): self + { + $this->fields = $fields; + return $this; + } + + public function from(Select $from): self + { + $this->from = $from; + return $this; + } + + /** + * @param array<string, string|int> $params + */ + public function execute(array $params = []): void + { + $this->sql->_executeInsertFromSelect($this, $params); + } + + /** + * @internal + */ + public function _getTable(): string + { + return $this->table; + } + + /** + * @internal + * @return list<string> + */ + public function _getFields(): array + { + if (!isset($this->fields)) { + throw new InvalidSqlException('INSERT SELECT: $fields must be set before calling execute()'); + } + return $this->fields; + } + + public function _getFrom(): Select + { + if (!isset($this->from)) { + throw new InvalidSqlException('INSERT SELECT: $from must be set before calling execute()'); + } + return $this->from; + } +} |
