@@ -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
0 commit comments