Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Apr 7, 2021
1 parent 4127c01 commit ac0a0f7
Show file tree
Hide file tree
Showing 32 changed files with 273 additions and 318 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setPath(string $path): self

/**
* Set the request content
* @param mixed $content Request content
* @param mixed $content Request content
* @return $this
*/
public function setContent($content): self
Expand Down
43 changes: 31 additions & 12 deletions src/Drivers/EloquentEntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flat3\Lodata\Exception\Protocol\InternalServerErrorException;
use Flat3\Lodata\Facades\Lodata;
use Flat3\Lodata\Helper\PropertyValue;
use Flat3\Lodata\Interfaces\EntitySet\CountInterface;
use Flat3\Lodata\Interfaces\EntitySet\CreateInterface;
use Flat3\Lodata\Interfaces\EntitySet\DeleteInterface;
use Flat3\Lodata\Interfaces\EntitySet\ExpandInterface;
Expand All @@ -29,6 +30,7 @@
use Flat3\Lodata\NavigationProperty;
use Flat3\Lodata\Property;
use Flat3\Lodata\ReferentialConstraint;
use Generator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -46,7 +48,7 @@
* Eloquent Entity Set
* @package Flat3\Lodata\Drivers
*/
class EloquentEntitySet extends EntitySet implements ReadInterface, UpdateInterface, CreateInterface, DeleteInterface, QueryInterface, FilterInterface, SearchInterface, ExpandInterface, OrderByInterface, TransactionInterface
class EloquentEntitySet extends EntitySet implements ReadInterface, UpdateInterface, CreateInterface, DeleteInterface, QueryInterface, FilterInterface, SearchInterface, ExpandInterface, OrderByInterface, TransactionInterface, CountInterface
{
use SQLConnection;
use SQLSearch;
Expand Down Expand Up @@ -268,9 +270,8 @@ public function delete(PropertyValue $key): void

/**
* Query eloquent models
* @return array Entity buffer
*/
public function query(): array
public function query(): Generator
{
/**
* @var Model $instance
Expand Down Expand Up @@ -304,21 +305,17 @@ public function query(): array
}
}

if ($this->top !== PHP_INT_MAX) {
$builder->limit($this->top);
if ($this->getTop()->hasValue()) {
$builder->limit($this->getTop()->getValue());

if ($this->skip) {
$builder->skip($this->skip);
if ($this->getSkip()->hasValue()) {
$builder->skip($this->getSkip()->getValue());
}
}

$results = [];

foreach ($builder->getModels() as $model) {
$results[] = $this->modelToEntity($model);
yield $this->modelToEntity($model);
}

return $results;
}

/**
Expand Down Expand Up @@ -445,4 +442,26 @@ public function commit()
{
DB::commit();
}

/**
* Count the number of records matching the query
* @return int Count
*/
public function count(): int
{
/**
* @var Model $instance
*/
$instance = new $this->model();
$builder = $instance->newQuery();

$this->resetParameters();
$this->generateWhere();

if ($this->where) {
$builder->whereRaw($this->where, $this->parameters);
}

return $builder->count();
}
}
9 changes: 3 additions & 6 deletions src/Drivers/FilesystemEntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flat3\Lodata\Interfaces\EntitySet\ReadInterface;
use Flat3\Lodata\Interfaces\EntitySet\UpdateInterface;
use Flat3\Lodata\Transaction\MediaType;
use Generator;
use GuzzleHttp\Psr7\Uri;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemAdapter;
Expand Down Expand Up @@ -71,18 +72,14 @@ public function getDisk(): FilesystemAdapter

/**
* Query
* @return array
*/
public function query(): array
public function query(): Generator
{
$contents = $this->disk->getDriver()->listContents('', true);
$results = [];

foreach ($contents as $content) {
$results[] = $this->fromMetadata($content);
yield $this->fromMetadata($content);
}

return $results;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/Drivers/RedisEntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Flat3\Lodata\Exception\Protocol\BadRequestException;
use Flat3\Lodata\Exception\Protocol\InternalServerErrorException;
use Flat3\Lodata\Helper\PropertyValue;
use Flat3\Lodata\Interfaces\EntitySet\CountInterface;
use Flat3\Lodata\Interfaces\EntitySet\CreateInterface;
use Flat3\Lodata\Interfaces\EntitySet\DeleteInterface;
use Flat3\Lodata\Interfaces\EntitySet\PaginationInterface;
use Flat3\Lodata\Interfaces\EntitySet\QueryInterface;
use Flat3\Lodata\Interfaces\EntitySet\ReadInterface;
use Flat3\Lodata\Interfaces\EntitySet\UpdateInterface;
use Flat3\Lodata\Type;
use Generator;
use Illuminate\Redis\Connections\Connection;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
Expand All @@ -23,7 +25,7 @@
* Class RedisEntitySet
* @package Flat3\Lodata\Drivers
*/
class RedisEntitySet extends EntitySet implements CreateInterface, UpdateInterface, DeleteInterface, ReadInterface, QueryInterface, PaginationInterface
class RedisEntitySet extends EntitySet implements CreateInterface, UpdateInterface, DeleteInterface, ReadInterface, QueryInterface, PaginationInterface, CountInterface
{
/** @var ?Connection $connection */
protected $connection = null;
Expand Down Expand Up @@ -90,7 +92,7 @@ public function setConnection(Connection $connection): self
* Count records in the database
* @return int|null
*/
public function count()
public function count(): int
{
return $this->getConnection()->dbsize();
}
Expand Down Expand Up @@ -143,14 +145,13 @@ public function update(PropertyValue $key): Entity

/**
* Query the redis database
* @return Entity[] Results
*/
public function query(): array
public function query(): Generator
{
$skipToken = $this->getSkipToken();

if ($skipToken->isPaginationComplete()) {
return [];
return;
}

$config = [];
Expand All @@ -169,13 +170,13 @@ public function query(): array
$skipToken->setValue($redisPage);
}

return array_map(function ($key) {
foreach ($results as $key) {
$keyValue = new PropertyValue();
$keyValue->setProperty($this->getType()->getKey());
$keyValue->setValue(Type\String_::factory(Str::after($key, config('database.redis.options.prefix'))));

return $this->read($keyValue);
}, $results ?: []);
yield $this->read($keyValue);
}
}

/**
Expand Down
17 changes: 5 additions & 12 deletions src/Drivers/SQL/SQLLimits.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@ trait SQLLimits
*/
public function generateLimits(): string
{
$limits = '';

if ($this->top === PHP_INT_MAX) {
return $limits;
if (!$this->getSkip()->hasValue()) {
return '';
}

$limits .= ' LIMIT ?';
$this->addParameter($this->top);

if (!$this->skip) {
return $limits;
}
$limits = ' LIMIT ?,?';

$limits .= ' OFFSET ?';
$this->addParameter($this->skip);
$this->addParameter($this->getSkip()->getValue());
$this->addParameter($this->getTop()->hasValue() ? $this->getTop()->getValue() : PHP_INT_MAX);

return $limits;
}
Expand Down
12 changes: 4 additions & 8 deletions src/Drivers/SQLEntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Flat3\Lodata\NavigationProperty;
use Flat3\Lodata\Property;
use Flat3\Lodata\ReferentialConstraint;
use Generator;
use PDO;
use PDOException;
use PDOStatement;
Expand Down Expand Up @@ -235,19 +236,14 @@ public function getRowCountQueryString(): string

/**
* Run a PDO query and return the results
* @return array Result buffer
*/
public function query(): array
public function query(): Generator
{
$stmt = $this->pdoSelect($this->getSetResultQueryString());

$results = [];

foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$results[] = $this->newEntity()->fromArray($row);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
yield $this->newEntity()->fromArray($row);
}

return $results;
}

/**
Expand Down
29 changes: 23 additions & 6 deletions src/Drivers/StaticEntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,40 @@
namespace Flat3\Lodata\Drivers;

use ArrayAccess;
use Flat3\Lodata\Entity;
use Flat3\Lodata\EntitySet;
use Flat3\Lodata\EntityType;
use Flat3\Lodata\Interfaces\EntitySet\CountInterface;
use Flat3\Lodata\Interfaces\EntitySet\QueryInterface;
use Generator;

/**
* Class StaticEntitySet
* The static entity set is assigned entities and does not query a data source
* @package Flat3\Lodata\Drivers
*/
class StaticEntitySet extends EntitySet implements QueryInterface, ArrayAccess
class StaticEntitySet extends EntitySet implements QueryInterface, ArrayAccess, CountInterface
{
/**
* @var Entity[] $results
*/
protected $results = [];

public function __construct(EntityType $entityType)
{
parent::__construct('unbound', $entityType);

$this->applyQueryOptions = false;
$this->results = [];
}

/**
* Return no more results
* @return array
* Return all results
*/
public function query(): array
public function query(): Generator
{
return [];
foreach ($this->results as $result) {
yield $result;
}
}

public function offsetExists($offset)
Expand Down Expand Up @@ -83,4 +91,13 @@ public function sort(callable $callback): self

return $this;
}

/**
* Count the objects in the array
* @return int
*/
public function count(): int
{
return count($this->results);
}
}
35 changes: 17 additions & 18 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,35 +186,34 @@ public function emitJson(Transaction $transaction): void
/** @var PropertyValue $propertyValue */
$propertyValue = $this->propertyValues->current();

$propertyMetadata = $propertyValue->getMetadata($transaction);

if ($propertyMetadata->hasProperties()) {
if ($propertyValue->shouldEmit($transaction)) {
if ($requiresSeparator) {
$transaction->outputJsonSeparator();
}

$transaction->outputJsonKV($propertyMetadata->getProperties());
}
$transaction->outputJsonKey($propertyValue->getProperty()->getName());

if (!$propertyValue->shouldEmit($transaction)) {
$this->propertyValues->next();
continue;
}
$value = $propertyValue->getValue();
if (null === $value) {
$transaction->sendJson(null);
} else {
$value->emitJson($transaction);
}

if ($requiresSeparator) {
$transaction->outputJsonSeparator();
$requiresSeparator = true;
}

$transaction->outputJsonKey($propertyValue->getProperty()->getName());
$propertyMetadata = $propertyValue->getMetadata($transaction);

if ($propertyMetadata->hasProperties()) {
if ($requiresSeparator) {
$transaction->outputJsonSeparator();
}

$value = $propertyValue->getValue();
if (null === $value) {
$transaction->sendJson(null);
} else {
$value->emitJson($transaction);
$transaction->outputJsonKV($propertyMetadata->getProperties());
$requiresSeparator = true;
}

$requiresSeparator = true;
$this->propertyValues->next();
}

Expand Down
Loading

0 comments on commit ac0a0f7

Please sign in to comment.