From deacd0dfc195bca41af631114804d29937337cd8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 17 Jan 2024 02:11:31 +0900 Subject: . --- services/app/src/Sql/Internal/Delete.php | 51 +++++++ services/app/src/Sql/Internal/Insert.php | 62 +++++++++ services/app/src/Sql/Internal/InsertFromSelect.php | 78 +++++++++++ services/app/src/Sql/Internal/Join.php | 18 +++ services/app/src/Sql/Internal/Select.php | 146 +++++++++++++++++++++ services/app/src/Sql/Internal/SelectFirst.php | 26 ++++ services/app/src/Sql/Internal/Update.php | 78 +++++++++++ 7 files changed, 459 insertions(+) create mode 100644 services/app/src/Sql/Internal/Delete.php create mode 100644 services/app/src/Sql/Internal/Insert.php create mode 100644 services/app/src/Sql/Internal/InsertFromSelect.php create mode 100644 services/app/src/Sql/Internal/Join.php create mode 100644 services/app/src/Sql/Internal/Select.php create mode 100644 services/app/src/Sql/Internal/SelectFirst.php create mode 100644 services/app/src/Sql/Internal/Update.php (limited to 'services/app/src/Sql/Internal') diff --git a/services/app/src/Sql/Internal/Delete.php b/services/app/src/Sql/Internal/Delete.php new file mode 100644 index 0000000..c0761a2 --- /dev/null +++ b/services/app/src/Sql/Internal/Delete.php @@ -0,0 +1,51 @@ +where = $where; + return $this; + } + + /** + * @param array $params + */ + public function execute(array $params = []): void + { + $this->sql->_executeDelete($this, $params); + } + + /** + * @internal + */ + public function _getTable(): string + { + return $this->table; + } + + /** + * @internal + */ + public function _getWhere(): string + { + return $this->where; + } +} diff --git a/services/app/src/Sql/Internal/Insert.php b/services/app/src/Sql/Internal/Insert.php new file mode 100644 index 0000000..1bdd06f --- /dev/null +++ b/services/app/src/Sql/Internal/Insert.php @@ -0,0 +1,62 @@ + $values + */ + private ?array $values; + + /** + * @internal + */ + public function __construct( + private readonly QueryBuilder $sql, + private readonly string $table, + ) { + } + + /** + * @param array $values + */ + public function values(array $values): self + { + $this->values = $values; + return $this; + } + + /** + * @return positive-int + */ + public function execute(): int + { + return $this->sql->_executeInsert($this); + } + + /** + * @internal + */ + public function _getTable(): string + { + return $this->table; + } + + /** + * @internal + * @return array + */ + public function _getValues(): array + { + if (!isset($this->values)) { + throw new InvalidSqlException('INSERT: $values must be set before calling execute()'); + } + return $this->values; + } +} 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 @@ + + */ + private ?array $fields; + + private ?Select $from; + + /** + * @internal + */ + public function __construct( + private readonly QueryBuilder $sql, + private readonly string $table, + ) { + } + + /** + * @param list $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 $params + */ + public function execute(array $params = []): void + { + $this->sql->_executeInsertFromSelect($this, $params); + } + + /** + * @internal + */ + public function _getTable(): string + { + return $this->table; + } + + /** + * @internal + * @return list + */ + 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; + } +} diff --git a/services/app/src/Sql/Internal/Join.php b/services/app/src/Sql/Internal/Join.php new file mode 100644 index 0000000..4c85fd8 --- /dev/null +++ b/services/app/src/Sql/Internal/Join.php @@ -0,0 +1,18 @@ + + */ + private ?array $fields; + + private ?Join $join = null; + + private string $where = ''; + + /** + * @var list + */ + private array $orderBy = []; + + /** + * @var ?positive-int + */ + private ?int $limit = null; + + public function __construct( + private readonly QueryBuilder $sql, + private readonly string $table, + ) { + } + + public function leftJoin(string $table, string $on): self + { + $this->join = new Join('LEFT JOIN', $table, $on); + return $this; + } + + /** + * @param list $fields + */ + public function fields(array $fields): self + { + $this->fields = $fields; + return $this; + } + + public function where(string $where): self + { + $this->where = $where; + return $this; + } + + /** + * @param list $orderBy + */ + public function orderBy(array $orderBy): self + { + $this->orderBy = $orderBy; + return $this; + } + + /** + * @param positive-int $limit + */ + public function limit(int $limit): self + { + $this->limit = $limit; + return $this; + } + + public function first(): SelectFirst + { + $this->limit = 1; + return new SelectFirst($this); + } + + /** + * @param array $params + * @return list> + */ + public function execute(array $params = []): array + { + return $this->sql->_executeSelect($this, $params); + } + + /** + * @internal + */ + public function _getTable(): string + { + return $this->table; + } + + /** + * @internal + * @return list + */ + public function _getFields(): array + { + if (!isset($this->fields)) { + throw new InvalidSqlException('SELECT: $fields must be set before calling execute()'); + } + return $this->fields; + } + + /** + * @internal + */ + public function _getJoin(): ?Join + { + return $this->join; + } + + /** + * @internal + */ + public function _getWhere(): string + { + return $this->where; + } + + /** + * @internal + * @return list + */ + public function _getOrderBy(): array + { + return $this->orderBy; + } + + /** + * @return ?positive-int + */ + public function _getLimit(): ?int + { + return $this->limit; + } +} diff --git a/services/app/src/Sql/Internal/SelectFirst.php b/services/app/src/Sql/Internal/SelectFirst.php new file mode 100644 index 0000000..baf5aae --- /dev/null +++ b/services/app/src/Sql/Internal/SelectFirst.php @@ -0,0 +1,26 @@ + $params + * @return ?array + */ + public function execute(array $params = []): ?array + { + $result = $this->inner->execute($params); + return $result[0] ?? null; + } +} diff --git a/services/app/src/Sql/Internal/Update.php b/services/app/src/Sql/Internal/Update.php new file mode 100644 index 0000000..a9e9816 --- /dev/null +++ b/services/app/src/Sql/Internal/Update.php @@ -0,0 +1,78 @@ + + */ + private ?array $set; + + private string $where = ''; + + /** + * @internal + */ + public function __construct( + private readonly QueryBuilder $sql, + private readonly string $table, + ) { + } + + /** + * @param array $set + */ + public function set(array $set): self + { + $this->set = $set; + return $this; + } + + public function where(string $where): self + { + $this->where = $where; + return $this; + } + + /** + * @param array $params + */ + public function execute(array $params = []): void + { + $this->sql->_executeUpdate($this, $params); + } + + /** + * @internal + */ + public function _getTable(): string + { + return $this->table; + } + + /** + * @internal + */ + public function _getWhere(): string + { + return $this->where; + } + + /** + * @internal + * @return array + */ + public function _getSet(): array + { + if (!isset($this->set)) { + throw new InvalidSqlException('UPDATE: $set must be set before calling execute()'); + } + return $this->set; + } +} -- cgit v1.2.3-70-g09d2