Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

BACKEND-3197 :: Feature :: Harmony PHP | Partial Updates #70

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions core/src/Repository/DataSource/Sql/DataSource/RawSqlDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
urijp marked this conversation as resolved.
Show resolved Hide resolved
return $this->get($query);
default:
throw new QueryNotSupportedException();
}
}

/**
* @param Query $query
* @param mixed|null $entity
Expand Down
19 changes: 17 additions & 2 deletions core/src/Repository/DataSource/Sql/Helper/SqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions core/src/Repository/DataSource/Sql/Queries/InsertSqlQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace Harmony\Core\Repository\DataSource\Sql\Queries;

interface InsertSqlQuery extends SqlQuery {
}
18 changes: 18 additions & 0 deletions core/src/Repository/DataSource/Sql/Queries/InsertUserSqlQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Harmony\Core\Repository\DataSource\Sql\Queries;

class InsertUserSqlQuery implements InsertSqlQuery {
public function __construct(
private readonly string $name,
private readonly string $email,
) {
}

public function getValues(): array {
return [
UserSqlSchema::COLUMN_EMAIL => $this->name,
UserSqlSchema::COLUMN_NAME => $this->email,
];
}
}
8 changes: 8 additions & 0 deletions core/src/Repository/DataSource/Sql/Queries/PatchSqlQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Harmony\Core\Repository\DataSource\Sql\Queries;

use Harmony\Core\Repository\Query\Composed\WhereQuery;

interface PatchSqlQuery extends SqlQuery, WhereQuery {
}
25 changes: 25 additions & 0 deletions core/src/Repository/DataSource/Sql/Queries/PatchUserSqlQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Harmony\Core\Repository\DataSource\Sql\Queries;

class PatchUserSqlQuery implements PatchSqlQuery {
public function __construct(
private readonly int $userId,
private readonly string $name,
private readonly string $email,
) {
}

public function getValues(): array {
return [
UserSqlSchema::COLUMN_EMAIL => $this->name,
UserSqlSchema::COLUMN_NAME => $this->email,
urijp marked this conversation as resolved.
Show resolved Hide resolved
];
}

public function where(): array {
return [
(new UserSqlSchema())->getIdColumn() => $this->userId,
];
}
}
9 changes: 9 additions & 0 deletions core/src/Repository/DataSource/Sql/Queries/SqlQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Harmony\Core\Repository\DataSource\Sql\Queries;

use Harmony\Core\Repository\Query\Query;

interface SqlQuery extends Query {
public function getValues(): array;
}
14 changes: 14 additions & 0 deletions core/src/Repository/DataSource/Sql/Queries/UserSqlSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Harmony\Core\Repository\DataSource\Sql\Queries;

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";
orioljp marked this conversation as resolved.
Show resolved Hide resolved

public function getTableName(): string {
return "users";
}
}