@@ -317,4 +317,119 @@ public function bulkDelete(string $table, array $ids): array
317317 'deleted ' => $ stmt ->rowCount ()
318318 ];
319319 }
320+
321+ /**
322+ * Count records with optional filtering
323+ */
324+ public function count (string $ table , array $ opts = []): array
325+ {
326+ $ columns = $ this ->inspector ->getColumns ($ table );
327+ $ colNames = array_column ($ columns , 'Field ' );
328+
329+ // --- Filtering (same as list method) ---
330+ $ where = [];
331+ $ params = [];
332+ $ paramCounter = 0 ;
333+ if (!empty ($ opts ['filter ' ])) {
334+ $ filters = explode (', ' , $ opts ['filter ' ]);
335+ foreach ($ filters as $ f ) {
336+ $ parts = explode (': ' , $ f , 3 );
337+ if (count ($ parts ) === 2 ) {
338+ $ col = $ parts [0 ];
339+ $ val = $ parts [1 ];
340+ if (in_array ($ col , $ colNames , true )) {
341+ if (str_contains ($ val , '% ' )) {
342+ $ paramKey = "{$ col }_ {$ paramCounter }" ;
343+ $ where [] = "` $ col` LIKE : $ paramKey " ;
344+ $ params [$ paramKey ] = $ val ;
345+ $ paramCounter ++;
346+ } else {
347+ $ paramKey = "{$ col }_ {$ paramCounter }" ;
348+ $ where [] = "` $ col` = : $ paramKey " ;
349+ $ params [$ paramKey ] = $ val ;
350+ $ paramCounter ++;
351+ }
352+ }
353+ } elseif (count ($ parts ) === 3 && in_array ($ parts [0 ], $ colNames , true )) {
354+ $ col = $ parts [0 ];
355+ $ operator = strtolower ($ parts [1 ]);
356+ $ val = $ parts [2 ];
357+ $ paramKey = "{$ col }_ {$ paramCounter }" ;
358+
359+ switch ($ operator ) {
360+ case 'eq ' :
361+ $ where [] = "` $ col` = : $ paramKey " ;
362+ $ params [$ paramKey ] = $ val ;
363+ break ;
364+ case 'neq ' :
365+ case 'ne ' :
366+ $ where [] = "` $ col` != : $ paramKey " ;
367+ $ params [$ paramKey ] = $ val ;
368+ break ;
369+ case 'gt ' :
370+ $ where [] = "` $ col` > : $ paramKey " ;
371+ $ params [$ paramKey ] = $ val ;
372+ break ;
373+ case 'gte ' :
374+ case 'ge ' :
375+ $ where [] = "` $ col` >= : $ paramKey " ;
376+ $ params [$ paramKey ] = $ val ;
377+ break ;
378+ case 'lt ' :
379+ $ where [] = "` $ col` < : $ paramKey " ;
380+ $ params [$ paramKey ] = $ val ;
381+ break ;
382+ case 'lte ' :
383+ case 'le ' :
384+ $ where [] = "` $ col` <= : $ paramKey " ;
385+ $ params [$ paramKey ] = $ val ;
386+ break ;
387+ case 'like ' :
388+ $ where [] = "` $ col` LIKE : $ paramKey " ;
389+ $ params [$ paramKey ] = $ val ;
390+ break ;
391+ case 'in ' :
392+ $ values = explode ('| ' , $ val );
393+ $ placeholders = [];
394+ foreach ($ values as $ i => $ v ) {
395+ $ inParamKey = "{$ paramKey }_in_ {$ i }" ;
396+ $ placeholders [] = ": $ inParamKey " ;
397+ $ params [$ inParamKey ] = $ v ;
398+ }
399+ $ where [] = "` $ col` IN ( " . implode (', ' , $ placeholders ) . ") " ;
400+ break ;
401+ case 'notin ' :
402+ case 'nin ' :
403+ $ values = explode ('| ' , $ val );
404+ $ placeholders = [];
405+ foreach ($ values as $ i => $ v ) {
406+ $ inParamKey = "{$ paramKey }_nin_ {$ i }" ;
407+ $ placeholders [] = ": $ inParamKey " ;
408+ $ params [$ inParamKey ] = $ v ;
409+ }
410+ $ where [] = "` $ col` NOT IN ( " . implode (', ' , $ placeholders ) . ") " ;
411+ break ;
412+ case 'null ' :
413+ $ where [] = "` $ col` IS NULL " ;
414+ break ;
415+ case 'notnull ' :
416+ $ where [] = "` $ col` IS NOT NULL " ;
417+ break ;
418+ }
419+ $ paramCounter ++;
420+ }
421+ }
422+ }
423+
424+ $ sql = "SELECT COUNT(*) FROM ` $ table` " ;
425+ if ($ where ) {
426+ $ sql .= ' WHERE ' . implode (' AND ' , $ where );
427+ }
428+
429+ $ stmt = $ this ->pdo ->prepare ($ sql );
430+ $ stmt ->execute ($ params );
431+ $ count = (int )$ stmt ->fetchColumn ();
432+
433+ return ['count ' => $ count ];
434+ }
320435}
0 commit comments