Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appwrite validators transfer #264

Merged
merged 80 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 79 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
6430f9c
Queries new classes
fogelito Apr 13, 2023
c80a4b4
Queries Tests
fogelito Apr 13, 2023
9caa559
Queries Tests with document validator
fogelito Apr 13, 2023
3457f52
Query.php changes
fogelito Apr 23, 2023
6043d50
empty values && validate find queries
fogelito Apr 24, 2023
8921aa8
phpstan fixes
fogelito Apr 24, 2023
67b4f09
contains test remove
fogelito Apr 24, 2023
00f473b
comment queries org
fogelito Apr 25, 2023
27e2023
Query test from appwrite
fogelito Apr 25, 2023
94bca64
name changes
fogelito Apr 27, 2023
e56f580
Queries folder
fogelito Apr 27, 2023
52f3512
Merge branch 'main' of github.com:utopia-php/database into appwrite-v…
fogelito Apr 27, 2023
9e19f83
DocumentsQueriesValidator name chage
fogelito Apr 27, 2023
d0a06ec
remove OrderAttributes
fogelito Apr 30, 2023
0d201d5
Test for DocumentQueries
fogelito Apr 30, 2023
ea66257
DocumentsQueries Tests
fogelito Apr 30, 2023
94714c3
remove isArray check since is always true
fogelito Apr 30, 2023
f9e767c
MariaDBTest.php tests
fogelito Apr 30, 2023
0b7a496
Get query by type
fogelito May 1, 2023
3507239
var_dumps for debug
fogelito May 1, 2023
1f05340
fallback for empty key
fogelito May 1, 2023
fa28aac
remove fallback
fogelito May 1, 2023
9b693eb
remove fallback
fogelito May 1, 2023
67cc419
dbg
fogelito May 1, 2023
cd24a59
use $id instead of key
fogelito May 2, 2023
cc0069b
Fallback for key
fogelito May 2, 2023
025a8fd
little green
fogelito May 2, 2023
501dbc0
Select dots failing
fogelito May 2, 2023
d131ad4
Select dots failing
fogelito May 2, 2023
2b2802d
allowing false as value parameter
fogelito May 2, 2023
959bce0
Strict not empty
fogelito May 2, 2023
66df107
Fix empty check
fogelito May 3, 2023
0401b24
remove comment
fogelito May 3, 2023
8ec2ce7
Fix empty queries
fogelito May 3, 2023
46de56c
formatting
fogelito May 3, 2023
3af2d46
dots test
fogelito May 3, 2023
283dfb5
remove NESTING_QUERIES
fogelito May 4, 2023
d7795ca
remove nested queries
fogelito May 4, 2023
852fd1c
typo
fogelito May 4, 2023
44c5da9
remove filtering
fogelito May 7, 2023
be26112
variadic
fogelito May 15, 2023
abe2d33
variadic constructor
fogelito May 15, 2023
a61e77a
Invalid query: single quote
fogelito May 15, 2023
51ff5b0
variadic constructor
fogelito May 15, 2023
b24c481
remove redundant message
fogelito May 15, 2023
28c7597
select validator isset fix
fogelito May 16, 2023
bad239e
Check instanceof Query
fogelito May 16, 2023
a0272d9
Upper case
fogelito May 16, 2023
448f6a4
Uncomment ambiguous syntax
fogelito May 16, 2023
bee4314
testAttributeNamesWithDots
fogelito May 16, 2023
fb7c7f2
remove status
fogelito May 16, 2023
aa72d65
Remove comment + Cursor investigation + lint
fogelito May 16, 2023
0c7aa62
Remove comment + Cursor investigation + lint
fogelito May 16, 2023
3af9a10
Explicit Hint for validator
fogelito May 16, 2023
46fb159
alias DocumentValidator + Invalid query
fogelito May 21, 2023
c08a623
alias DocumentValidator + Invalid query +
fogelito May 21, 2023
0321314
Empty queries values
fogelito May 21, 2023
875851c
Split tests for success and failure
fogelito May 21, 2023
3a6fd3a
Fix OffsetTest.php test
fogelito May 21, 2023
e379b03
Some more tests
fogelito May 21, 2023
a4751bd
Filter parsing tests +
fogelito May 21, 2023
817c60d
stopOnFailure
fogelito May 21, 2023
cccbe00
Merge branch 'main' of github.com:utopia-php/database into appwrite-v…
fogelito May 21, 2023
0db9f8d
lint changes for $schema
fogelito May 22, 2023
0792731
Invalid query
fogelito May 22, 2023
e6db7ef
Merge branch 'main' of github.com:utopia-php/database into appwrite-v…
fogelito May 22, 2023
6b50f76
Merge tracking branch
fogelito May 22, 2023
22be6a2
Merge branch 'main' of github.com:utopia-php/database into appwrite-v…
fogelito May 28, 2023
3a8b861
Invalid query text change
fogelito May 28, 2023
e4c1ec1
Invalid query changes
fogelito May 28, 2023
c488929
Update src/Database/Validator/Query/Filter.php
fogelito May 28, 2023
6da27fc
Invalid query changes
fogelito May 28, 2023
7d95638
Merge remote-tracking branch 'origin/appwrite-validators' into appwri…
fogelito May 28, 2023
c9043db
Filter operators count = 1
fogelito May 28, 2023
75b2d09
Php unit
fogelito May 28, 2023
9bad321
notEqual empty string
fogelito May 28, 2023
f1382bb
Update src/Database/Validator/Query/Filter.php
fogelito May 29, 2023
485d6b4
PHP_INT_MAX
fogelito May 29, 2023
98bd452
testTwoAttributesFulltext
fogelito May 29, 2023
0a1c827
formatting
fogelito May 30, 2023
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
5 changes: 3 additions & 2 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,7 @@ public function sum(string $collection, string $attribute, array $queries = [],
* @param array<string> $selections
* @param string $prefix
* @return mixed
* @throws Exception
*/
protected function getAttributeProjection(array $selections, string $prefix = ''): mixed
{
Expand All @@ -1196,11 +1197,11 @@ protected function getAttributeProjection(array $selections, string $prefix = ''

if (!empty($prefix)) {
foreach ($selections as &$selection) {
$selection = "`{$prefix}`.`{$selection}`";
$selection = "`{$prefix}`.`{$this->filter($selection)}`";
}
} else {
foreach ($selections as &$selection) {
$selection = "`{$selection}`";
$selection = "`{$this->filter($selection)}`";
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ public function sum(string $collection, string $attribute, array $queries = [],
* @param string[] $selections
* @param string $prefix
* @return string
* @throws Exception
*/
protected function getAttributeProjection(array $selections, string $prefix = ''): string
{
Expand All @@ -1203,11 +1204,11 @@ protected function getAttributeProjection(array $selections, string $prefix = ''

if (!empty($prefix)) {
foreach ($selections as &$selection) {
$selection = "\"{$prefix}\".\"{$selection}\"";
$selection = "\"{$prefix}\".\"{$this->filter($selection)}\"";
}
} else {
foreach ($selections as &$selection) {
$selection = "\"{$selection}\"";
$selection = "\"{$this->filter($selection)}\"";
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\Queries\Documents;
use Utopia\Database\Validator\Index as IndexValidator;
use Utopia\Database\Validator\Permissions;
use Utopia\Database\Validator\Structure;
Expand Down Expand Up @@ -3838,6 +3839,13 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu
}

$collection = $this->silent(fn () => $this->getCollection($collection));
$attributes = $collection->getAttribute('attributes', []);
$indexes = $collection->getAttribute('indexes', []);

$validator = new Documents($attributes, $indexes);
if (!$validator->isValid($queries)) {
throw new Exception($validator->getDescription());
}

$authorization = new Authorization(self::PERMISSION_READ);
$skipAuth = $authorization->isValid($collection->getRead());
Expand Down
5 changes: 2 additions & 3 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,10 @@ public static function endsWith(string $attribute, string $value): self
* Filters $queries for $types
*
* @param array<Query> $queries
* @param string ...$types
*
* @param array<string> $types
* @return array<Query>
*/
public static function getByType(array $queries, string ...$types): array
public static function getByType(array $queries, array $types): array
{
$filtered = [];
foreach ($queries as $query) {
Expand Down
103 changes: 103 additions & 0 deletions src/Database/Validator/IndexedQueries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Utopia\Database\Validator;

use Exception;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Validator\Query\Base;

class IndexedQueries extends Queries
{
/**
* @var array<Document>
*/
protected array $attributes = [];

/**
* @var array<Document>
*/
protected array $indexes = [];

/**
* Expression constructor
*
* This Queries Validator filters indexes for only available indexes
*
* @param array<Document> $attributes
* @param array<Document> $indexes
* @param array<Base> $validators
* @throws Exception
*/
public function __construct(array $attributes = [], array $indexes = [], array $validators = [])
{
$this->attributes = $attributes;

$this->indexes[] = new Document([
'type' => Database::INDEX_UNIQUE,
'attributes' => ['$id']
]);

$this->indexes[] = new Document([
'type' => Database::INDEX_KEY,
'attributes' => ['$createdAt']
]);

$this->indexes[] = new Document([
'type' => Database::INDEX_KEY,
'attributes' => ['$updatedAt']
]);

foreach ($indexes as $index) {
$this->indexes[] = $index;
}

parent::__construct($validators);
}

/**
* @param mixed $value
* @return bool
* @throws Exception
*/
public function isValid($value): bool
{
if (!parent::isValid($value)) {
return false;
}
$queries = [];
foreach ($value as $query) {
if (!$query instanceof Query) {
$query = Query::parse($query);
}

$queries[] = $query;
}

$grouped = Query::groupByType($queries);
$filters = $grouped['filters'];

foreach ($filters as $filter) {
if ($filter->getMethod() === Query::TYPE_SEARCH) {
$matched = false;

foreach ($this->indexes as $index) {
if (
$index->getAttribute('type') === Database::INDEX_FULLTEXT
&& $index->getAttribute('attributes') === [$filter->getAttribute()]
) {
$matched = true;
}
}

if (!$matched) {
$this->message = "Searching by attribute \"{$filter->getAttribute()}\" requires a fulltext index.";
return false;
}
}
}

return true;
}
}
192 changes: 0 additions & 192 deletions src/Database/Validator/OrderAttributes.php

This file was deleted.

Loading