Skip to content

Commit d6e96ed

Browse files
committed
auth + vecotor changes
1 parent 16e7811 commit d6e96ed

4 files changed

Lines changed: 133 additions & 57 deletions

File tree

src/Database/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ abstract public function deleteDocuments(string $collection, array $sequences, a
823823
* @param string $forPermission
824824
* @return array<Document>
825825
*/
826-
abstract public function find(Document $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, string $forPermission = Database::PERMISSION_READ, string $method = ''): array;
826+
abstract public function find(Document $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, string $forPermission = Database::PERMISSION_READ): array;
827827

828828
/**
829829
* Sum an attribute

src/Database/Adapter/SQL.php

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,7 +2961,7 @@ protected function convertArrayToWKT(array $geometry): string
29612961
* @throws TimeoutException
29622962
* @throws Exception
29632963
*/
2964-
public function find(Document $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, string $forPermission = Database::PERMISSION_READ, string $method = ''): array
2964+
public function find(Document $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, string $forPermission = Database::PERMISSION_READ): array
29652965
{
29662966
$collection = $collection->getId();
29672967
$name = $this->filter($collection);
@@ -3102,30 +3102,16 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
31023102

31033103
$selections = $this->getAttributeSelections($queries);
31043104

3105-
if ($method === 'count'){
3106-
$sql = "
3107-
SELECT COUNT(1) as _uid FROM (
3108-
SELECT 1
3109-
FROM {$this->getSQLTable($name)} AS {$this->quote($alias)}
3110-
{$sqlWhere}
3111-
{$sqlLimit}
3112-
) table_count
3113-
";
3114-
}
3115-
else {
3116-
$sql = "
3105+
$sql = "
31173106
SELECT {$this->getAttributeProjection($selections, $alias)}
31183107
FROM {$this->getSQLTable($name)} AS {$this->quote($alias)}
31193108
{$sqlWhere}
31203109
{$sqlOrder}
31213110
{$sqlLimit};
31223111
";
3123-
}
31243112

31253113
$sql = $this->trigger(Database::EVENT_DOCUMENT_FIND, $sql);
31263114

3127-
var_dump($sql);
3128-
var_dump($binds);
31293115
try {
31303116
$stmt = $this->getPDO()->prepare($sql);
31313117

@@ -3181,6 +3167,86 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
31813167
return $results;
31823168
}
31833169

3170+
/**
3171+
* Count Documents
3172+
*
3173+
* @param Document $collection
3174+
* @param array<Query> $queries
3175+
* @param int|null $max
3176+
* @return int
3177+
* @throws Exception
3178+
* @throws PDOException
3179+
*/
3180+
public function count(Document $collection, array $queries = [], ?int $max = null): int
3181+
{
3182+
$collection = $collection->getId();
3183+
$name = $this->filter($collection);
3184+
$roles = $this->authorization->getRoles();
3185+
$binds = [];
3186+
$where = [];
3187+
$alias = Query::DEFAULT_ALIAS;
3188+
3189+
$limit = '';
3190+
if (! \is_null($max)) {
3191+
$binds[':limit'] = $max;
3192+
$limit = 'LIMIT :limit';
3193+
}
3194+
3195+
$queries = array_map(fn ($query) => clone $query, $queries);
3196+
3197+
$otherQueries = [];
3198+
foreach ($queries as $query) {
3199+
if (!in_array($query->getMethod(), Query::VECTOR_TYPES)) {
3200+
$otherQueries[] = $query;
3201+
}
3202+
}
3203+
3204+
$conditions = $this->getSQLConditions($otherQueries, $binds);
3205+
if (!empty($conditions)) {
3206+
$where[] = $conditions;
3207+
}
3208+
3209+
if ($this->authorization->getStatus()) {
3210+
$where[] = $this->getSQLPermissionsCondition($name, $roles, $alias);
3211+
}
3212+
3213+
if ($this->sharedTables) {
3214+
$binds[':_tenant'] = $this->tenant;
3215+
$where[] = "{$this->getTenantQuery($collection, $alias, condition: '')}";
3216+
}
3217+
3218+
$sqlWhere = !empty($where)
3219+
? 'WHERE ' . \implode(' AND ', $where)
3220+
: '';
3221+
3222+
$sql = "
3223+
SELECT COUNT(1) as sum FROM (
3224+
SELECT 1
3225+
FROM {$this->getSQLTable($name)} AS {$this->quote($alias)}
3226+
{$sqlWhere}
3227+
{$limit}
3228+
) table_count
3229+
";
3230+
3231+
$sql = $this->trigger(Database::EVENT_DOCUMENT_COUNT, $sql);
3232+
3233+
$stmt = $this->getPDO()->prepare($sql);
3234+
3235+
foreach ($binds as $key => $value) {
3236+
$stmt->bindValue($key, $value, $this->getPDOType($value));
3237+
}
3238+
3239+
$this->execute($stmt);
3240+
3241+
$result = $stmt->fetchAll();
3242+
$stmt->closeCursor();
3243+
if (!empty($result)) {
3244+
$result = $result[0];
3245+
}
3246+
3247+
return $result['sum'] ?? 0;
3248+
}
3249+
31843250
/**
31853251
* Sum an Attribute
31863252
*
@@ -3194,7 +3260,6 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
31943260
*/
31953261
public function sum(Document $collection, string $attribute, array $queries = [], ?int $max = null): int|float
31963262
{
3197-
$collectionAttributes = $collection->getAttribute("attributes", []);
31983263
$collection = $collection->getId();
31993264
$name = $this->filter($collection);
32003265
$attribute = $this->filter($attribute);

src/Database/Database.php

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7697,7 +7697,7 @@ public function purgeCachedDocument(string $collectionId, ?string $id): bool
76977697
* @throws TimeoutException
76987698
* @throws Exception
76997699
*/
7700-
public function find(string $collection, array $queries = [], string $forPermission = Database::PERMISSION_READ, string $method = ''): array
7700+
public function find(string $collection, array $queries = [], string $forPermission = Database::PERMISSION_READ): array
77017701
{
77027702
$collection = $this->silent(fn () => $this->getCollection($collection));
77037703

@@ -7729,7 +7729,6 @@ public function find(string $collection, array $queries = [], string $forPermiss
77297729
$documentSecurity = $collection->getAttribute('documentSecurity', false);
77307730
$skipAuth = $this->authorization->isValid(new Input($forPermission, $collection->getPermissionsByType($forPermission)));
77317731

7732-
77337732
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
77347733
throw new AuthorizationException($this->authorization->getDescription());
77357734
}
@@ -7810,8 +7809,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
78107809
$orderTypes,
78117810
$cursor,
78127811
$cursorDirection,
7813-
$forPermission,
7814-
$method
7812+
$forPermission
78157813
);
78167814

78177815
$results = $skipAuth ? $this->authorization->skip($getResults) : $getResults();
@@ -7942,25 +7940,57 @@ public function findOne(string $collection, array $queries = []): Document
79427940
*/
79437941
public function count(string $collection, array $queries = [], ?int $max = null): int
79447942
{
7945-
$filters = Query::groupByType($queries)['filters'];
7943+
$collection = $this->silent(fn () => $this->getCollection($collection));
7944+
$attributes = $collection->getAttribute('attributes', []);
7945+
$indexes = $collection->getAttribute('indexes', []);
79467946

7947-
$queries = [];
7948-
foreach ($filters as $query) {
7949-
if (!in_array($query->getMethod(), Query::VECTOR_TYPES)) {
7950-
$queries[] = $query;
7947+
$this->checkQueryTypes($queries);
7948+
7949+
if ($this->validate) {
7950+
$validator = new DocumentsValidator(
7951+
$attributes,
7952+
$indexes,
7953+
$this->adapter->getIdAttributeType(),
7954+
$this->maxQueryValues,
7955+
$this->adapter->getMaxUIDLength(),
7956+
$this->adapter->getMinDateTime(),
7957+
$this->adapter->getMaxDateTime(),
7958+
$this->adapter->getSupportForAttributes()
7959+
);
7960+
if (!$validator->isValid($queries)) {
7961+
throw new QueryException($validator->getDescription());
79517962
}
79527963
}
79537964

7954-
if ($max === null) {
7955-
$max = PHP_INT_MAX;
7965+
$documentSecurity = $collection->getAttribute('documentSecurity', false);
7966+
$skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_READ, $collection->getRead()));
7967+
7968+
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
7969+
throw new AuthorizationException($this->authorization->getDescription());
7970+
}
7971+
7972+
$relationships = \array_filter(
7973+
$collection->getAttribute('attributes', []),
7974+
fn (Document $attribute) => $attribute->getAttribute('type') === self::VAR_RELATIONSHIP
7975+
);
7976+
7977+
$queries = Query::groupByType($queries)['filters'];
7978+
$queries = $this->convertQueries($collection, $queries);
7979+
7980+
$queriesOrNull = $this->convertRelationshipQueries($relationships, $queries);
7981+
7982+
if ($queriesOrNull === null) {
7983+
return 0;
79567984
}
79577985

7958-
$queries[] = Query::limit($max);
7959-
$queries[] = Query::select(['$id']);
7986+
$queries = $queriesOrNull;
7987+
7988+
$getCount = fn () => $this->adapter->count($collection, $queries, $max);
7989+
$count = $skipAuth ? $this->authorization->skip($getCount) : $getCount();
79607990

7961-
$result = $this->find($collection, $queries, method: 'count');
7991+
$this->trigger(self::EVENT_DOCUMENT_COUNT, $count);
79627992

7963-
return $result[0]['$id'] ?? 0;
7993+
return $count;
79647994
}
79657995

79667996
/**
@@ -7978,29 +8008,6 @@ public function count(string $collection, array $queries = [], ?int $max = null)
79788008
*/
79798009
public function sum(string $collection, string $attribute, array $queries = [], ?int $max = null): float|int
79808010
{
7981-
$filters = Query::groupByType($queries)['filters'];
7982-
7983-
$queries = [];
7984-
foreach ($filters as $query) {
7985-
if (!in_array($query->getMethod(), Query::VECTOR_TYPES)) {
7986-
$queries[] = $query;
7987-
}
7988-
}
7989-
7990-
if ($max === null) {
7991-
$max = PHP_INT_MAX;
7992-
}
7993-
7994-
$queries[] = Query::limit($max);
7995-
$queries[] = Query::select(['$id']);
7996-
7997-
$result = $this->find($collection, $queries, method: 'count');
7998-
7999-
return $result[0]['$id'] ?? 0;
8000-
8001-
8002-
8003-
80048011
$collection = $this->silent(fn () => $this->getCollection($collection));
80058012
$attributes = $collection->getAttribute('attributes', []);
80068013
$indexes = $collection->getAttribute('indexes', []);
@@ -8023,8 +8030,13 @@ public function sum(string $collection, string $attribute, array $queries = [],
80238030
}
80248031
}
80258032

8033+
$documentSecurity = $collection->getAttribute('documentSecurity', false);
80268034
$skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_READ, $collection->getRead()));
80278035

8036+
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
8037+
throw new AuthorizationException($this->authorization->getDescription());
8038+
}
8039+
80288040
$relationships = \array_filter(
80298041
$collection->getAttribute('attributes', []),
80308042
fn (Document $attribute) => $attribute->getAttribute('type') === self::VAR_RELATIONSHIP

tests/e2e/Adapter/Scopes/RelationshipTests.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3907,7 +3907,6 @@ public function testRelationshipSpatialQueries(): void
39073907
$this->assertCount(1, $restaurants);
39083908
$this->assertEquals('rest1', $restaurants[0]->getId());
39093909

3910-
var_dump('===============================================');
39113910
// count with spatial relationship query
39123911
$count = $database->count('restaurantsSpatial', [
39133912
Query::distanceLessThan('supplier.warehouseLocation', [-74.0060, 40.7128], 1.0)

0 commit comments

Comments
 (0)