Skip to content

Commit

Permalink
feat(db): refactored where parsing and added query filter functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hejrobin committed Mar 1, 2024
1 parent 0e8eb7f commit 57ec84c
Show file tree
Hide file tree
Showing 31 changed files with 666 additions and 148 deletions.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
config
};

use function Haku\Spl\Url\path;
use function Haku\Generic\Url\path;
use function Haku\Delegation\delegate;

/* @willResolve Haku\Http\Message */
Expand Down
2 changes: 1 addition & 1 deletion vendor/Haku/Console/Commands/Generators/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* @note Deny direct file access */
if (defined('HAKU_ROOT_PATH') === false) exit;

use function Haku\Spl\Strings\snakeCaseFromCamelCase;
use function Haku\Generic\Strings\snakeCaseFromCamelCase;

class Model extends Generator
{
Expand Down
2 changes: 1 addition & 1 deletion vendor/Haku/Console/Commands/Generators/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function run(): bool
$route = $path;
}

// Split array segments and parameters
// Genericit array segments and parameters
foreach(explode('/', $route) as $segment)
{
if (\str_starts_with($segment, '{'))
Expand Down
2 changes: 1 addition & 1 deletion vendor/Haku/Console/Commands/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Haku\Console\Command;

use function Haku\resolvePath;
use function Haku\Spl\Strings\random;
use function Haku\Generic\Strings\random;

class Init extends Command
{
Expand Down
2 changes: 1 addition & 1 deletion vendor/Haku/Database/Marshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* @note Deny direct file access */
if (defined('HAKU_ROOT_PATH') === false) exit;

use function Haku\Spl\Strings\{
use function Haku\Generic\Strings\{
camelCaseFromSnakeCase,
snakeCaseFromCamelCase,
};
Expand Down
46 changes: 30 additions & 16 deletions vendor/Haku/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Haku\Database\Mixins\Entity;

use function Haku\haku;
use function Haku\Spl\Strings\camelCaseFromSnakeCase;
use function Haku\Generic\Strings\camelCaseFromSnakeCase;

abstract class Model implements JsonSerializable
{
Expand Down Expand Up @@ -58,6 +58,7 @@ public static function findAll(
): ?array
{
$self = new static();
$db = haku('db');

$result = [];

Expand All @@ -67,15 +68,15 @@ public static function findAll(

[$query, $parameters] = Find::all(
tableName: $self->tableName,
tableFields: $self->getRecordFields(),
fields: $self->getRecordFields(),
aggregateFields: $self->getAggregateFields(),
where: $where,
orderBy: $orderBy,
limit: $limit,
offset: $offset
offset: $offset,
);

return haku('db')->fetchAll($query, $parameters);
return $db->fetchAll($query, $parameters);
}

public static function findOne(
Expand All @@ -84,6 +85,7 @@ public static function findOne(
): ?static
{
$self = new static();
$db = haku('db');

$result = [];

Expand All @@ -94,12 +96,12 @@ public static function findOne(

[$query, $parameters] = Find::one(
tableName: $self->tableName,
tableFields: $self->getRecordFields(),
fields: $self->getRecordFields(),
aggregateFields: $self->getAggregateFields(),
where: $where,
);

$result = haku('db')->fetch($query, $parameters);
$result = $db->fetch($query, $parameters);

if (!$result) {
return null;
Expand Down Expand Up @@ -127,16 +129,22 @@ public static function paginate(
): ?array
{
$self = new static();
$db = haku('db');

$result = [];

if ($self->isSoftDeleteable() && !$includeDeleted) {
$where[] = Where::null('deletedAt');
}

[$countQuery, $countParams] = Find::count($self->tableName, $self->primaryKeyName, $where);
[$countQuery, $countParams] = Find::count(
tableName: $self->tableName,
countFieldName: $self->primaryKeyName,
aggregateFields: $self->getAggregateFields(),
where: $where
);

$numRecords = haku('db')->fetchColumn($countQuery, $countParams);
$numRecords = $db->fetchColumn($countQuery, $countParams);
$pageCount = ceil($numRecords / $limit);

$prevPage = $page - 1;
Expand All @@ -161,32 +169,35 @@ public static function paginate(

[$query, $parameters] = Find::all(
tableName: $self->tableName,
tableFields: $self->getRecordFields(),
fields: $self->getRecordFields(),
aggregateFields: $self->getAggregateFields(),
where: $where,
orderBy: $orderBy,
limit: $limit,
offset: $offset,
);

$records = haku('db')->fetchAll($query, $parameters);
$records = $db->fetchAll($query, $parameters);
$records = array_map(fn($record) => static::from($record)->json(), $records);

return [
'pagination' => [
'count' => $numRecords,
'pageCount' => $pageCount,
'page' => $page,
'prevPage' => $prevPage,
'nextPage' => $nextPage,
],
'meta' => [
'numRecords' => $numRecords,
],
'records' => $records,
];
}

public static function delete(int $primaryKey): bool
{
$self = new static();
$db = haku('db');

if ($self->isSoftDeleteable())
{
Expand All @@ -201,20 +212,22 @@ public static function delete(int $primaryKey): bool
]);
}

return haku('db')->execute($query, $parameters);
return $db->execute($query, $parameters);
}

public function restore(): ?static
{
if ($this->isSoftDeleteable() && $this->deletedAt !== null)
{
$db = haku('db');

$primaryKey = $this->getPrimaryKey();

[$query, $parameters] = Write::restore($self->tableName, [
Where::is($self->primaryKeyName, $primaryKey),
]);

$success = haku('db')->execute($query, $parameters);
$success = $db->execute($query, $parameters);

if ($success)
{
Expand Down Expand Up @@ -282,8 +295,9 @@ public function save(bool $ignoreValidationStatus = false): ?static
);
}

$record = $this->getRecord(filterTimestamps: true);
$db = haku('db');

$record = $this->getRecord(filterTimestamps: true);
$record = $this->marshalRecord($record);

$updatedModel = null;
Expand All @@ -307,15 +321,15 @@ public function save(bool $ignoreValidationStatus = false): ?static

try
{
$result = haku('db')->execute($query, $parameters);
$result = $db->execute($query, $parameters);

if ($this->isPersistent())
{
$primaryKey = $this->getPrimaryKey();
}
else
{
$primaryKey = haku('db')->lastInsertId();
$primaryKey = $db->lastInsertId();
}

if ($result && $primaryKey)
Expand Down
49 changes: 49 additions & 0 deletions vendor/Haku/Database/Query/Conditions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace Haku\Database\Query;

/* @note Deny direct file access */
if (defined('HAKU_ROOT_PATH') === false) exit;

use Haku\Generic\Query\{
Params,
Filter,
};

class Conditions
{

public static function fromQueryString(?string $unresolved = ''): array
{
$params = new Params();

if (!$params->has('filters'))
{
return [];
}

$filter = Filter::from($params->get('filters'));

return static::from($filter);
}

public static function from(Filter $filter): array
{
$where = [];

foreach ($filter->getFilters() as $property)
{
foreach ($property->values as $value)
{
$where[] = call_user_func_array(
"Haku\Database\Query\Where::{$property->operator->value}",
[$property->name, $value]
);
}
}

return $where;
}

}
Loading

0 comments on commit 57ec84c

Please sign in to comment.