diff --git a/core/src/Repository/DataSource/Sql/DataSource/RawSqlDataSource.php b/core/src/Repository/DataSource/Sql/DataSource/RawSqlDataSource.php index 7cd557b..130bee6 100644 --- a/core/src/Repository/DataSource/Sql/DataSource/RawSqlDataSource.php +++ b/core/src/Repository/DataSource/Sql/DataSource/RawSqlDataSource.php @@ -6,6 +6,9 @@ use Harmony\Core\Repository\DataSource\GetDataSource; use Harmony\Core\Repository\DataSource\PutDataSource; use Harmony\Core\Repository\DataSource\Sql\Helper\SqlBuilder; +use Harmony\Core\Repository\DataSource\Sql\Queries\InsertSqlQuery; +use Harmony\Core\Repository\DataSource\Sql\Queries\PatchSqlQuery; +use Harmony\Core\Repository\DataSource\Sql\Queries\SqlQuery; use Harmony\Core\Repository\Error\DataNotFoundException; use Harmony\Core\Repository\Error\QueryNotSupportedException; use Harmony\Core\Repository\Query\AllQuery; @@ -108,6 +111,10 @@ public function getAll(Query $query): array { * @throws QueryNotSupportedException */ public function put(Query $query, mixed $entity = null): mixed { + if ($query instanceof SqlQuery) { + return $this->queryPut($query); + } + $id = $this->getId($query, $entity); try { @@ -131,6 +138,25 @@ public function put(Query $query, mixed $entity = null): mixed { return $this->get(new IdQuery($id)); } + /** + * @throws QueryNotSupportedException + * @throws DataNotFoundException + */ + public function queryPut(SqlQuery $query): mixed { + switch (true) { + case $query instanceof InsertSqlQuery: + $sql = $this->sqlBuilder->insert(null, $query); + $id = $this->pdo->insert($sql->sql(), $sql->params()); + return $this->get(new IdQuery($id)); + case $query instanceof PatchSqlQuery: + $sql = $this->sqlBuilder->patch($query); + $this->pdo->execute($sql->sql(), $sql->params()); + return $this->get($query); + default: + throw new QueryNotSupportedException(); + } + } + /** * @param Query $query * @param mixed|null $entity diff --git a/core/src/Repository/DataSource/Sql/Helper/SqlBuilder.php b/core/src/Repository/DataSource/Sql/Helper/SqlBuilder.php index 73d1d24..8365a54 100644 --- a/core/src/Repository/DataSource/Sql/Helper/SqlBuilder.php +++ b/core/src/Repository/DataSource/Sql/Helper/SqlBuilder.php @@ -2,6 +2,9 @@ namespace Harmony\Core\Repository\DataSource\Sql\Helper; +use Harmony\Core\Repository\DataSource\Sql\Queries\InsertSqlQuery; +use Harmony\Core\Repository\DataSource\Sql\Queries\PatchSqlQuery; +use Harmony\Core\Repository\DataSource\Sql\Queries\UpdateSqlQuery; use Harmony\Core\Repository\DataSource\Sql\SqlBaseColumn; use Harmony\Core\Repository\DataSource\Sql\SqlOrderDirection; use Harmony\Core\Repository\Query\Composed\ComposedQuery; @@ -109,6 +112,15 @@ public function selectComposed(ComposedQuery $composed): LatitudeQuery { return $query; } + public function patch(PatchSqlQuery $query): LatitudeQuery { + $updates = $query->getValues(); + + $factory = $this->factory->update($this->schema->getTableName(), $updates); + $factory = $this->addWhereConditions($query, $factory); + + return $factory->compile(); + } + public function selectAllComposed(ComposedQuery $composed): LatitudeQuery { $factory = $this->factory->select()->from($this->schema->getTableName()); @@ -141,8 +153,11 @@ public function updateById(mixed $id, mixed $entity): LatitudeQuery { return $query; } - public function insert(mixed $entity): LatitudeQuery { - $values = (array) $entity; + public function insert( + mixed $entity = null, + InsertSqlQuery $query = null, + ): LatitudeQuery { + $values = $entity ? (array) $entity : $query->getValues(); $query = $this->factory ->insert($this->schema->getTableName(), $values) diff --git a/core/src/Repository/DataSource/Sql/Queries/InsertSqlQuery.php b/core/src/Repository/DataSource/Sql/Queries/InsertSqlQuery.php new file mode 100644 index 0000000..5265fe8 --- /dev/null +++ b/core/src/Repository/DataSource/Sql/Queries/InsertSqlQuery.php @@ -0,0 +1,6 @@ + $this->name, + UserSqlSchema::COLUMN_NAME => $this->email, + ]; + } +} diff --git a/core/src/Repository/DataSource/Sql/Queries/PatchSqlQuery.php b/core/src/Repository/DataSource/Sql/Queries/PatchSqlQuery.php new file mode 100644 index 0000000..9869835 --- /dev/null +++ b/core/src/Repository/DataSource/Sql/Queries/PatchSqlQuery.php @@ -0,0 +1,8 @@ + $this->name, + UserSqlSchema::COLUMN_NAME => $this->email, + ]; + } + + public function where(): array { + return [ + (new UserSqlSchema())->getIdColumn() => $this->userId, + ]; + } +} diff --git a/core/src/Repository/DataSource/Sql/Queries/SqlQuery.php b/core/src/Repository/DataSource/Sql/Queries/SqlQuery.php new file mode 100644 index 0000000..80abc65 --- /dev/null +++ b/core/src/Repository/DataSource/Sql/Queries/SqlQuery.php @@ -0,0 +1,9 @@ +