@@ -2961,9 +2961,8 @@ 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 ): 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 , string $ method = '' ): array
29652965 {
2966- $ attributes = $ collection ->getAttribute ('attributes ' , []);
29672966 $ collection = $ collection ->getId ();
29682967 $ name = $ this ->filter ($ collection );
29692968 $ roles = $ this ->authorization ->getRoles ();
@@ -3103,16 +3102,30 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
31033102
31043103 $ selections = $ this ->getAttributeSelections ($ queries );
31053104
3106- $ sql = "
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 = "
31073117 SELECT {$ this ->getAttributeProjection ($ selections , $ alias )}
31083118 FROM {$ this ->getSQLTable ($ name )} AS {$ this ->quote ($ alias )}
31093119 {$ sqlWhere }
31103120 {$ sqlOrder }
31113121 {$ sqlLimit };
31123122 " ;
3123+ }
31133124
31143125 $ sql = $ this ->trigger (Database::EVENT_DOCUMENT_FIND , $ sql );
31153126
3127+ var_dump ($ sql );
3128+ var_dump ($ binds );
31163129 try {
31173130 $ stmt = $ this ->getPDO ()->prepare ($ sql );
31183131
@@ -3168,102 +3181,6 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
31683181 return $ results ;
31693182 }
31703183
3171- /**
3172- * Count Documents
3173- *
3174- * @param Document $collection
3175- * @param array<Query> $queries
3176- * @param int|null $max
3177- * @return int
3178- * @throws Exception
3179- * @throws PDOException
3180- */
3181- public function count (Document $ collection , array $ queries = [], ?int $ max = null ): int
3182- {
3183- $ attributes = $ collection ->getAttribute ("attributes " , []);
3184- $ collection = $ collection ->getId ();
3185- $ name = $ this ->filter ($ collection );
3186- $ roles = $ this ->authorization ->getRoles ();
3187- $ binds = [];
3188- $ where = [];
3189- $ alias = Query::DEFAULT_ALIAS ;
3190-
3191- $ limit = '' ;
3192- if (! \is_null ($ max )) {
3193- $ binds [':limit ' ] = $ max ;
3194- $ limit = 'LIMIT :limit ' ;
3195- }
3196-
3197- $ queries = array_map (fn ($ query ) => clone $ query , $ queries );
3198-
3199- // Extract vector queries (used for ORDER BY) and keep non-vector for WHERE
3200- $ vectorQueries = [];
3201- $ otherQueries = [];
3202- foreach ($ queries as $ query ) {
3203- if (in_array ($ query ->getMethod (), Query::VECTOR_TYPES )) {
3204- $ vectorQueries [] = $ query ;
3205- } else {
3206- $ otherQueries [] = $ query ;
3207- }
3208- }
3209-
3210- $ conditions = $ this ->getSQLConditions ($ otherQueries , $ binds );
3211- if (!empty ($ conditions )) {
3212- $ where [] = $ conditions ;
3213- }
3214-
3215- if ($ this ->authorization ->getStatus ()) {
3216- $ where [] = $ this ->getSQLPermissionsCondition ($ name , $ roles , $ alias );
3217- }
3218-
3219- if ($ this ->sharedTables ) {
3220- $ binds [':_tenant ' ] = $ this ->tenant ;
3221- $ where [] = "{$ this ->getTenantQuery ($ collection , $ alias , condition: '' )}" ;
3222- }
3223-
3224- $ sqlWhere = !empty ($ where )
3225- ? 'WHERE ' . \implode (' AND ' , $ where )
3226- : '' ;
3227-
3228- // Add vector distance calculations to ORDER BY (similarity-aware LIMIT)
3229- $ vectorOrders = [];
3230- foreach ($ vectorQueries as $ query ) {
3231- $ vectorOrder = $ this ->getVectorDistanceOrder ($ query , $ binds , $ alias );
3232- if ($ vectorOrder ) {
3233- $ vectorOrders [] = $ vectorOrder ;
3234- }
3235- }
3236- $ sqlOrder = !empty ($ vectorOrders ) ? 'ORDER BY ' . implode (', ' , $ vectorOrders ) : '' ;
3237-
3238- $ sql = "
3239- SELECT COUNT(1) as sum FROM (
3240- SELECT 1
3241- FROM {$ this ->getSQLTable ($ name )} AS {$ this ->quote ($ alias )}
3242- {$ sqlWhere }
3243- {$ sqlOrder }
3244- {$ limit }
3245- ) table_count
3246- " ;
3247-
3248- $ sql = $ this ->trigger (Database::EVENT_DOCUMENT_COUNT , $ sql );
3249-
3250- $ stmt = $ this ->getPDO ()->prepare ($ sql );
3251-
3252- foreach ($ binds as $ key => $ value ) {
3253- $ stmt ->bindValue ($ key , $ value , $ this ->getPDOType ($ value ));
3254- }
3255-
3256- $ this ->execute ($ stmt );
3257-
3258- $ result = $ stmt ->fetchAll ();
3259- $ stmt ->closeCursor ();
3260- if (!empty ($ result )) {
3261- $ result = $ result [0 ];
3262- }
3263-
3264- return $ result ['sum ' ] ?? 0 ;
3265- }
3266-
32673184 /**
32683185 * Sum an Attribute
32693186 *
0 commit comments