1
0
Fork 0
mirror of synced 2024-07-05 06:31:08 +12:00

Select Validator

This commit is contained in:
fogelito 2023-03-22 23:10:47 +02:00
parent 319698dab4
commit 60c54e2d10
8 changed files with 138 additions and 4 deletions

View file

@ -1,5 +1,7 @@
<?php <?php
use Appwrite\Utopia\Database\Validator\Query\Filter;
use Appwrite\Utopia\Database\Validator\Query\Select;
use Utopia\App; use Utopia\App;
use Appwrite\Event\Delete; use Appwrite\Event\Delete;
use Appwrite\Extend\Exception; use Appwrite\Extend\Exception;
@ -2900,10 +2902,13 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
->param('databaseId', '', new UID(), 'Database ID.') ->param('databaseId', '', new UID(), 'Database ID.')
->param('collectionId', '', new UID(), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).') ->param('collectionId', '', new UID(), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).')
->param('documentId', '', new UID(), 'Document ID.') ->param('documentId', '', new UID(), 'Document ID.')
->param('queries', [], new Queries(new Select()), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Only supported methods are limit and offset', true)
->inject('response') ->inject('response')
->inject('dbForProject') ->inject('dbForProject')
->inject('mode') ->inject('mode')
->action(function (string $databaseId, string $collectionId, string $documentId, Response $response, Database $dbForProject, string $mode) { ->action(function (string $databaseId, string $collectionId, string $documentId, array $queries, Response $response, Database $dbForProject, string $mode) {
var_dump($queries);
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
@ -2927,9 +2932,9 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
} }
if ($documentSecurity && !$valid) { if ($documentSecurity && !$valid) {
$document = $dbForProject->getDocument('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $documentId); $document = $dbForProject->getDocument('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $documentId, $queries);
} else { } else {
$document = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $documentId)); $document = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $documentId, $queries));
} }
if ($document->isEmpty()) { if ($document->isEmpty()) {

View file

@ -57,7 +57,7 @@ class Queries extends Validator
if (!$query instanceof Query) { if (!$query instanceof Query) {
try { try {
$query = Query::parse($query); $query = Query::parse($query);
if (\str_contains($query->getAttribute(), '.')) { if (\str_contains($query->getAttribute(), '.')) { // todo: double check!
return true; return true;
} }
} catch (\Throwable $th) { } catch (\Throwable $th) {
@ -69,6 +69,9 @@ class Queries extends Validator
$method = $query->getMethod(); $method = $query->getMethod();
$methodType = ''; $methodType = '';
switch ($method) { switch ($method) {
case Query::TYPE_SELECT:
$methodType = Base::METHOD_TYPE_SELECT;
break;
case Query::TYPE_LIMIT: case Query::TYPE_LIMIT:
$methodType = Base::METHOD_TYPE_LIMIT; $methodType = Base::METHOD_TYPE_LIMIT;
break; break;

View file

@ -8,6 +8,7 @@ use Appwrite\Utopia\Database\Validator\Query\Offset;
use Appwrite\Utopia\Database\Validator\Query\Cursor; use Appwrite\Utopia\Database\Validator\Query\Cursor;
use Appwrite\Utopia\Database\Validator\Query\Filter; use Appwrite\Utopia\Database\Validator\Query\Filter;
use Appwrite\Utopia\Database\Validator\Query\Order; use Appwrite\Utopia\Database\Validator\Query\Order;
use Appwrite\Utopia\Database\Validator\Query\Select;
use Utopia\Config\Config; use Utopia\Config\Config;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
@ -60,6 +61,7 @@ class Base extends Queries
]); ]);
$validators = [ $validators = [
new Select(),
new Limit(), new Limit(),
new Offset(), new Offset(),
new Cursor(), new Cursor(),

View file

@ -8,6 +8,7 @@ use Appwrite\Utopia\Database\Validator\Query\Filter;
use Appwrite\Utopia\Database\Validator\Query\Limit; use Appwrite\Utopia\Database\Validator\Query\Limit;
use Appwrite\Utopia\Database\Validator\Query\Offset; use Appwrite\Utopia\Database\Validator\Query\Offset;
use Appwrite\Utopia\Database\Validator\Query\Order; use Appwrite\Utopia\Database\Validator\Query\Order;
use Appwrite\Utopia\Database\Validator\Query\Select;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
@ -38,6 +39,7 @@ class Documents extends Queries
]); ]);
$validators = [ $validators = [
new Select(),
new Limit(), new Limit(),
new Offset(), new Offset(),
new Cursor(), new Cursor(),

View file

@ -12,6 +12,7 @@ abstract class Base extends Validator
public const METHOD_TYPE_CURSOR = 'cursor'; public const METHOD_TYPE_CURSOR = 'cursor';
public const METHOD_TYPE_ORDER = 'order'; public const METHOD_TYPE_ORDER = 'order';
public const METHOD_TYPE_FILTER = 'filter'; public const METHOD_TYPE_FILTER = 'filter';
public const METHOD_TYPE_SELECT = 'select';
/** /**
* @var string * @var string

View file

@ -0,0 +1,56 @@
<?php
namespace Appwrite\Utopia\Database\Validator\Query;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
class Select extends Base
{
/**
* @var array
*/
protected $schema = [];
/**
* Query constructor
*
*/
public function __construct(array $attributes = [])
{
foreach ($attributes as $attribute) {
//$this->schema[$attribute->getAttribute('key')] = $attribute->getArrayCopy();
}
}
/**
* Is valid.
*
* Returns true if method is TYPE_SELECT selections are valid
*
* Otherwise, returns false
*
* @param $query
* @return bool
*/
public function isValid($query): bool
{
/* @var $query Query */
if ($query->getMethod() === Query::TYPE_SELECT) {
foreach ($query->getValues() as $attr) {
var_dump($attr);
// todo: Do some validations
return true;
}
}
return false;
}
public function getMethodType(): string
{
return self::METHOD_TYPE_SELECT;
}
}

View file

@ -1381,6 +1381,37 @@ trait DatabasesBase
} }
} }
/**
* @depends testListDocuments
*/
public function testGetDocumentWithQueries(array $data): void
{
$databaseId = $data['databaseId'];
foreach ($data['documents'] as $document) {
$response = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $document['$collectionId'] . '/documents/' . $document['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'queries' => ['select("title","releaseYear")'],
]);
var_dump($response);
//Query::select(['string', 'integer']),
die;
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($response['body']['$id'], $document['$id']);
$this->assertEquals($document['$collectionId'], $response['body']['$collectionId']);
$this->assertArrayNotHasKey('$collection', $response['body']);
$this->assertEquals($document['$databaseId'], $response['body']['$databaseId']);
$this->assertEquals($response['body']['title'], $document['title']);
$this->assertEquals($response['body']['releaseYear'], $document['releaseYear']);
$this->assertEquals($response['body']['$permissions'], $document['$permissions']);
$this->assertEquals($response['body']['birthDay'], $document['birthDay']);
$this->assertFalse(array_key_exists('$internalId', $response['body']));
}
}
/** /**
* @depends testCreateDocument * @depends testCreateDocument
*/ */

View file

@ -0,0 +1,34 @@
<?php
namespace Tests\Unit\Utopia\Database\Validator\Query;
use Appwrite\Utopia\Database\Validator\Query\Base;
use Appwrite\Utopia\Database\Validator\Query\Select;
use Utopia\Database\Query;
use PHPUnit\Framework\TestCase;
class SelectTest extends TestCase
{
/**
* @var Base
*/
protected $validator = null;
public function setUp(): void
{
$this->validator = new Select();
}
public function tearDown(): void
{
}
public function testValue(): void
{
// Test for Success
$this->assertEquals($this->validator->isValid(Query::select(['*', 'attr1', 'attr2', 'collection.id'])), true, $this->validator->getDescription());
// Test for Failure
$this->assertEquals($this->validator->isValid(Query::limit(1)), false, $this->validator->getDescription());
}
}