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

Feat array contains #335

Merged
merged 20 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
19 changes: 13 additions & 6 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function createAttribute(string $collection, string $id, string $type, in
$type = $this->getSQLType($type, $size, $signed);

if ($array) {
$type = 'LONGTEXT';
$type = 'JSON';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will have to migrate this on Appwrite

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK
Just saw on MariaDB when trying to create a JSON attribute, It creates this:
genres longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(genres))
While in Mysql it is really a JSON field

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm so we won't need a migration for MariaDB but will for MySQL.. this is a new problem 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can save it as Text + create a key / value Collection for search?

}

return $this->getPDO()
Expand Down Expand Up @@ -246,7 +246,7 @@ public function updateAttribute(string $collection, string $id, string $type, in
$type = $this->getSQLType($type, $size, $signed);

if ($array) {
$type = 'LONGTEXT';
$type = 'JSON';
}

return $this->getPDO()
Expand Down Expand Up @@ -1314,19 +1314,26 @@ protected function getSQLCondition(Query $query): string

switch ($query->getMethod()) {
case Query::TYPE_SEARCH:
return "MATCH(table_main.{$attribute}) AGAINST (:{$placeholder}_0 IN BOOLEAN MODE)";
return "MATCH(`table_main`.{$attribute}) AGAINST (:{$placeholder}_0 IN BOOLEAN MODE)";

case Query::TYPE_BETWEEN:
return "table_main.{$attribute} BETWEEN :{$placeholder}_0 AND :{$placeholder}_1";
return "`table_main`.{$attribute} BETWEEN :{$placeholder}_0 AND :{$placeholder}_1";

case Query::TYPE_IS_NULL:
case Query::TYPE_IS_NOT_NULL:
return "table_main.{$attribute} {$this->getSQLOperator($query->getMethod())}";
return "`table_main`.{$attribute} {$this->getSQLOperator($query->getMethod())}";

case Query::TYPE_CONTAINS:
$conditions = [];
foreach ($query->getValues() as $key => $value) {
$conditions[] = "JSON_CONTAINS(`table_main`.{$attribute}, :{$placeholder}_{$key})";
}
$condition = implode(' OR ', $conditions);
return empty($condition) ? '' : '(' . $condition . ')';
default:
$conditions = [];
foreach ($query->getValues() as $key => $value) {
$conditions[] = $attribute . ' ' . $this->getSQLOperator($query->getMethod()) . ' :' . $placeholder . '_' . $key;
$conditions[] = "{$attribute} {$this->getSQLOperator($query->getMethod())} :{$placeholder}_{$key}";
}
$condition = implode(' OR ', $conditions);
return empty($condition) ? '' : '(' . $condition . ')';
Expand Down
15 changes: 11 additions & 4 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function createAttribute(string $collection, string $id, string $type, in
$type = $this->getSQLType($type, $size, $signed);

if ($array) {
$type = 'TEXT';
$type = 'JSONB';
}

return $this->getPDO()
Expand Down Expand Up @@ -292,7 +292,7 @@ public function updateAttribute(string $collection, string $id, string $type, in
$type = $this->getSQLType($type, $size, $signed);

if ($array) {
$type = 'LONGTEXT';
$type = 'JSONB';
}

if ($type == 'TIMESTAMP(3)') {
Expand Down Expand Up @@ -1045,7 +1045,6 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
$where[] = $this->getSQLCondition($query);
}


if (Authorization::$status) {
$where[] = $this->getSQLPermissionsCondition($name, $roles);
}
Expand Down Expand Up @@ -1321,7 +1320,7 @@ protected function getSQLCondition(Query $query): string
default => $query->getAttribute()
});

$attribute = "\"{$query->getAttribute()}\"" ;
$attribute = "\"{$query->getAttribute()}\"";
$placeholder = $this->getSQLPlaceholder($query);

switch ($query->getMethod()) {
Expand All @@ -1335,6 +1334,14 @@ protected function getSQLCondition(Query $query): string
case Query::TYPE_IS_NOT_NULL:
return "table_main.{$attribute} {$this->getSQLOperator($query->getMethod())}";

case Query::TYPE_CONTAINS:
$conditions = [];
foreach ($query->getValues() as $key => $value) {
$conditions[] = "{$attribute} @> :{$placeholder}_{$key}";
}
$condition = implode(' OR ', $conditions);
return empty($condition) ? '' : '(' . $condition . ')';

default:
$conditions = [];
foreach ($query->getValues() as $key => $value) {
Expand Down
6 changes: 4 additions & 2 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ public function getSupportForCasting(): bool
*/
public function getSupportForQueryContains(): bool
{
return false;
return true;
}

public function getSupportForRelationships(): bool
Expand All @@ -703,10 +703,12 @@ protected function bindConditionValue(mixed $stmt, Query $query): void
Query::TYPE_STARTS_WITH => $this->escapeWildcards($value) . '%',
Query::TYPE_ENDS_WITH => '%' . $this->escapeWildcards($value),
Query::TYPE_SEARCH => $this->getFulltextValue($value),
Query::TYPE_CONTAINS => \json_encode($value),
default => $value
};

$placeholder = $this->getSQLPlaceholder($query).'_'.$key;
$placeholder = $this->getSQLPlaceholder($query) . '_' . $key;

$stmt->bindValue($placeholder, $value, $this->getPDOType($value));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,11 @@ public function getSupportForSchemas(): bool
return false;
}

public function getSupportForQueryContains(): bool
{
return false;
}

/**
* Is fulltext index supported?
*
Expand Down
29 changes: 15 additions & 14 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1483,17 +1483,18 @@ public function testDeleteDocument(Document $document): void
public function testFind(): array
{
Authorization::setRole(Role::any()->toString());

static::getDatabase()->createCollection('movies', permissions: [
Permission::create(Role::any()),
Permission::update(Role::users())
], documentSecurity: true);
]);

$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'name', Database::VAR_STRING, 128, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'director', Database::VAR_STRING, 128, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'year', Database::VAR_INTEGER, 0, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'price', Database::VAR_FLOAT, 0, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'active', Database::VAR_BOOLEAN, 0, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'generes', Database::VAR_STRING, 32, true, null, true, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'genres', Database::VAR_STRING, 32, true, null, true, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'with-dash', Database::VAR_STRING, 128, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'nullable', Database::VAR_STRING, 128, false));

Expand All @@ -1518,7 +1519,7 @@ public function testFind(): array
'year' => 2013,
'price' => 39.50,
'active' => true,
'generes' => ['animation', 'kids'],
'genres' => ['animation', 'kids'],
'with-dash' => 'Works'
]));

Expand All @@ -1542,7 +1543,7 @@ public function testFind(): array
'year' => 2019,
'price' => 39.50,
'active' => true,
'generes' => ['animation', 'kids'],
'genres' => ['animation', 'kids'],
'with-dash' => 'Works'
]));

Expand All @@ -1566,7 +1567,7 @@ public function testFind(): array
'year' => 2011,
'price' => 25.94,
'active' => true,
'generes' => ['science fiction', 'action', 'comics'],
'genres' => ['science fiction', 'action', 'comics'],
'with-dash' => 'Works2'
]));

Expand All @@ -1590,7 +1591,7 @@ public function testFind(): array
'year' => 2019,
'price' => 25.99,
'active' => true,
'generes' => ['science fiction', 'action', 'comics'],
'genres' => ['science fiction', 'action', 'comics'],
'with-dash' => 'Works2'
]));

Expand All @@ -1614,7 +1615,7 @@ public function testFind(): array
'year' => 2025,
'price' => 0.0,
'active' => false,
'generes' => [],
'genres' => [],
'with-dash' => 'Works3'
]));

Expand All @@ -1636,7 +1637,7 @@ public function testFind(): array
'year' => 2026,
'price' => 0.0,
'active' => false,
'generes' => [],
'genres' => [],
'with-dash' => 'Works3',
'nullable' => 'Not null'
]));
Expand Down Expand Up @@ -1665,8 +1666,8 @@ public function testFindBasicChecks(): void
$this->assertIsFloat($documents[0]->getAttribute('price'));
$this->assertEquals(true, $documents[0]->getAttribute('active'));
$this->assertIsBool($documents[0]->getAttribute('active'));
$this->assertEquals(['animation', 'kids'], $documents[0]->getAttribute('generes'));
$this->assertIsArray($documents[0]->getAttribute('generes'));
$this->assertEquals(['animation', 'kids'], $documents[0]->getAttribute('genres'));
$this->assertIsArray($documents[0]->getAttribute('genres'));
$this->assertEquals('Works', $documents[0]->getAttribute('with-dash'));

// Alphabetical order
Expand Down Expand Up @@ -1836,7 +1837,7 @@ public function testFindContains(): void
}

$documents = static::getDatabase()->find('movies', [
Query::contains('generes', ['comics'])
Query::contains('genres', ['comics'])
]);

$this->assertEquals(2, count($documents));
Expand All @@ -1845,7 +1846,7 @@ public function testFindContains(): void
* Array contains OR condition
*/
$documents = static::getDatabase()->find('movies', [
Query::contains('generes', ['comics', 'kids']),
Query::contains('genres', ['comics', 'kids']),
]);

$this->assertEquals(4, count($documents));
Expand Down Expand Up @@ -3786,7 +3787,7 @@ public function testUniqueIndexDuplicate(): void
'year' => 2013,
'price' => 39.50,
'active' => true,
'generes' => ['animation', 'kids'],
'genres' => ['animation', 'kids'],
'with-dash' => 'Works4'
]));
}
Expand Down Expand Up @@ -3818,7 +3819,7 @@ public function testUniqueIndexDuplicateUpdate(): void
'year' => 2013,
'price' => 39.50,
'active' => true,
'generes' => ['animation', 'kids'],
'genres' => ['animation', 'kids'],
'with-dash' => 'Works4'
]));

Expand Down
16 changes: 0 additions & 16 deletions tests/Database/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,22 +375,6 @@ public function testParseV2(): void
$this->assertEquals('"quoted":"colon"', $query->getValue());
}

/*
Tests for aliases if we enable them:
public function testAlias(): void {
$query = Query::parse('eq(1)');
$this->assertEquals(Query::TYPE_EQUAL, $query->getMethod());
$query = Query::parse('lt(1)');
$this->assertEquals(Query::TYPE_LESSER, $query->getMethod());
$query = Query::parse('lte(1)');
$this->assertEquals(Query::TYPE_LESSEREQUAL, $query->getMethod());
$query = Query::parse('gt(1)');
$this->assertEquals(Query::TYPE_GREATER, $query->getMethod());
$query = Query::parse('gte(1)');
$this->assertEquals(Query::TYPE_GREATEREQUAL, $query->getMethod());
}
*/

public function testParseComplex(): void
{
$queries = [
Expand Down
1 change: 1 addition & 0 deletions tests/Database/Validator/Query/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testSuccess(): void
$this->assertTrue($this->validator->isValid(Query::isNull('attr')));
$this->assertTrue($this->validator->isValid(Query::startsWith('attr', 'super')));
$this->assertTrue($this->validator->isValid(Query::endsWith('attr', 'man')));
$this->assertTrue($this->validator->isValid(Query::contains('attr', ['super'])));
}

public function testFailure(): void
Expand Down