1313
1414namespace InitPHP \Database ;
1515
16- use InitPHP \Database \Utils \Pagination ;
16+ use InitPHP \Database \Utils \{Pagination ,
17+ Datatables };
1718use \InitPHP \Database \Exceptions \{WritableException ,
1819 ReadableException ,
1920 UpdatableException ,
@@ -475,10 +476,16 @@ public function createBatch(array $set)
475476 */
476477 public function count (): int
477478 {
479+ $ select = $ this ->_STRUCTURE ['select ' ];
480+ $ this ->_STRUCTURE ['select ' ][] = 'COUNT(*) AS row_count ' ;
478481 $ this ->_deleteFieldBuild (false );
482+ $ parameters = Parameters::get (false );
479483 $ res = $ this ->query ($ this ->_readQuery ());
480-
481- return $ res ->numRows ();
484+ $ count = $ res ->toArray ()['row_count ' ] ?? 0 ;
485+ unset($ res );
486+ Parameters::merge ($ parameters );
487+ $ this ->_STRUCTURE ['select ' ] = $ select ;
488+ return $ count ;
482489 }
483490
484491 public function pagination (int $ page = 1 , int $ per_page_limit = 10 , string $ link = '?page={page} ' ): Pagination
@@ -492,6 +499,11 @@ public function pagination(int $page = 1, int $per_page_limit = 10, string $link
492499 return new Pagination ($ res , $ page , $ per_page_limit , $ total_row , $ link );
493500 }
494501
502+ public function datatables (array $ columns , int $ method = Datatables::GET_REQUEST ): string
503+ {
504+ return (new Datatables ($ this , $ columns , $ method ))->__toString ();
505+ }
506+
495507 /**
496508 * @param array $selector
497509 * @param array $conditions
@@ -650,6 +662,31 @@ public function all(int $limit = 100, int $offset = 0)
650662 ->read ();
651663 }
652664
665+ public function group (\Closure $ group , string $ logical = 'AND ' ): self
666+ {
667+ $ logical = \str_replace (['&& ' , '|| ' ], ['AND ' , 'OR ' ], \strtoupper ($ logical ));
668+ if (!\in_array ($ logical , ['AND ' , 'OR ' ], true )){
669+ throw new \InvalidArgumentException ('Logical operator OR, AND, && or || it could be. ' );
670+ }
671+
672+ $ clone = clone $ this ;
673+ $ clone ->reset ();
674+
675+ \call_user_func_array ($ group , [$ clone ]);
676+
677+ $ where = $ clone ->_whereQuery ();
678+ if ($ where !== '' ){
679+ $ this ->_STRUCTURE ['where ' ][$ logical ][] = '( ' . $ where . ') ' ;
680+ }
681+
682+ $ having = $ clone ->_havingQuery ();
683+ if ($ having !== '' ){
684+ $ this ->_STRUCTURE ['having ' ][$ logical ][] = '( ' . $ having . ') ' ;
685+ }
686+ unset($ clone );
687+ return $ this ;
688+ }
689+
653690 public function onlyDeleted (): self
654691 {
655692 $ this ->_isOnlyDeletes = true ;
@@ -667,12 +704,17 @@ public function _readQuery(): string
667704 if ($ this ->getSchema () !== null ){
668705 $ this ->table ($ this ->getSchema ());
669706 }
670-
707+ $ where = $ this ->_whereQuery ();
708+ if ($ where !== '' ){
709+ $ where = ' WHERE ' . $ where ;
710+ }else {
711+ $ where = ' WHERE 1 ' ;
712+ }
671713 return 'SELECT '
672714 . (empty ($ this ->_STRUCTURE ['select ' ]) ? '* ' : \implode (', ' , $ this ->_STRUCTURE ['select ' ]))
673715 . ' FROM ' . \implode (', ' , $ this ->_STRUCTURE ['table ' ])
674716 . (!empty ($ this ->_STRUCTURE ['join ' ]) ? ' ' . \implode (', ' , $ this ->_STRUCTURE ['join ' ]) : '' )
675- . $ this -> _whereQuery ()
717+ . $ where
676718 . $ this ->_havingQuery ()
677719 . (!empty ($ this ->_STRUCTURE ['group_by ' ]) ? ' GROUP BY ' . \implode (', ' , $ this ->_STRUCTURE ['group_by ' ]) : '' )
678720 . (!empty ($ this ->_STRUCTURE ['order_by ' ]) ? ' ORDER BY ' . \implode (', ' , $ this ->_STRUCTURE ['order_by ' ]) : '' )
@@ -759,11 +801,17 @@ public function _updateQuery(array $data): string
759801 if ($ schemaID !== null && isset ($ data [$ schemaID ])){
760802 $ this ->where ($ schemaID , $ data [$ schemaID ]);
761803 }
804+ $ where = $ this ->_whereQuery ();
805+ if ($ where !== '' ){
806+ $ where = ' WHERE ' . $ where ;
807+ }else {
808+ $ where = ' WHERE 1 ' ;
809+ }
762810 return 'UPDATE '
763811 . (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
764812 . ' SET '
765813 . \implode (', ' , $ update )
766- . $ this -> _whereQuery ()
814+ . $ where
767815 . $ this ->_havingQuery ()
768816 . $ this ->_limitQuery ();
769817 }
@@ -812,22 +860,33 @@ public function _updateBatchQuery(array $data, $referenceColumn): string
812860 $ update [] = $ syntax ;
813861 }
814862 $ this ->in ($ referenceColumn , $ where );
815-
863+ $ where = $ this ->_whereQuery ();
864+ if ($ where !== '' ){
865+ $ where = ' WHERE ' . $ where ;
866+ }else {
867+ $ where = ' WHERE 1 ' ;
868+ }
816869 return 'UPDATE '
817870 . (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
818871 . ' SET '
819872 . \implode (', ' , $ update )
820- . $ this -> _whereQuery ()
873+ . $ where
821874 . $ this ->_havingQuery ()
822875 . $ this ->_limitQuery ();
823876 }
824877
825878 public function _deleteQuery (): string
826879 {
880+ $ where = $ this ->_whereQuery ();
881+ if ($ where !== '' ){
882+ $ where = ' WHERE ' . $ where ;
883+ }else {
884+ $ where = ' WHERE 1 ' ;
885+ }
827886 return 'DELETE FROM '
828887 . ' '
829888 . (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
830- . $ this -> _whereQuery ()
889+ . $ where
831890 . $ this ->_havingQuery ()
832891 . $ this ->_limitQuery ();
833892 }
@@ -865,10 +924,9 @@ private function _whereQuery(): string
865924 $ isAndEmpty = empty ($ this ->_STRUCTURE ['where ' ]['AND ' ]);
866925 $ isOrEmpty = empty ($ this ->_STRUCTURE ['where ' ]['OR ' ]);
867926 if ($ isAndEmpty && $ isOrEmpty ){
868- return ' WHERE 1 ' ;
927+ return '' ;
869928 }
870- return ' WHERE '
871- . (!$ isAndEmpty ? \implode (' AND ' , $ this ->_STRUCTURE ['where ' ]['AND ' ]) : '' )
929+ return (!$ isAndEmpty ? \implode (' AND ' , $ this ->_STRUCTURE ['where ' ]['AND ' ]) : '' )
872930 . (!$ isAndEmpty && !$ isOrEmpty ? ' AND ' : '' )
873931 . (!$ isOrEmpty ? \implode (' OR ' , $ this ->_STRUCTURE ['where ' ]['OR ' ]) : '' );
874932 }
0 commit comments