From 6679953688aa87a6e77c5ce9ff83ecc1e8e42e80 Mon Sep 17 00:00:00 2001 From: oriol Date: Thu, 9 Mar 2023 20:04:09 +0100 Subject: [PATCH 1/2] draft: Sql Queries --- .../Sql/DataSource/RawSqlDataSource.php | 26 +++++++++++++++++++ .../DataSource/Sql/Helper/SqlBuilder.php | 19 ++++++++++++-- .../DataSource/Sql/Queries/InsertSqlQuery.php | 6 +++++ .../Sql/Queries/InsertUserSqlQuery.php | 18 +++++++++++++ .../DataSource/Sql/Queries/PatchSqlQuery.php | 8 ++++++ .../Sql/Queries/PatchUserSqlQuery.php | 25 ++++++++++++++++++ .../DataSource/Sql/Queries/SqlQuery.php | 9 +++++++ .../DataSource/Sql/Queries/UserSqlSchema.php | 14 ++++++++++ 8 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 core/src/Repository/DataSource/Sql/Queries/InsertSqlQuery.php create mode 100644 core/src/Repository/DataSource/Sql/Queries/InsertUserSqlQuery.php create mode 100644 core/src/Repository/DataSource/Sql/Queries/PatchSqlQuery.php create mode 100644 core/src/Repository/DataSource/Sql/Queries/PatchUserSqlQuery.php create mode 100644 core/src/Repository/DataSource/Sql/Queries/SqlQuery.php create mode 100644 core/src/Repository/DataSource/Sql/Queries/UserSqlSchema.php 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 @@ + Date: Thu, 9 Mar 2023 20:20:21 +0100 Subject: [PATCH 2/2] changing names --- core/src/Repository/DataSource/Sql/Queries/UserSqlSchema.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/Repository/DataSource/Sql/Queries/UserSqlSchema.php b/core/src/Repository/DataSource/Sql/Queries/UserSqlSchema.php index 3a7531b..e0a43b3 100644 --- a/core/src/Repository/DataSource/Sql/Queries/UserSqlSchema.php +++ b/core/src/Repository/DataSource/Sql/Queries/UserSqlSchema.php @@ -5,8 +5,8 @@ use Harmony\Core\Repository\DataSource\Sql\Helper\DefaultSqlSchema; class UserSqlSchema extends DefaultSqlSchema { - public const COLUMN_NAME = "landing_page_id"; - public const COLUMN_EMAIL = "use_facebook_ads"; + public const COLUMN_NAME = "name"; + public const COLUMN_EMAIL = "email"; public function getTableName(): string { return "users";