77 * @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88 * @copyright Copyright © 2022 Muhammet ŞAFAK
99 * @license ./LICENSE MIT
10- * @version 2.0.5
10+ * @version 2.0.6
1111 * @link https://www.muhammetsafak.com.tr
1212 */
1313
1414namespace InitPHP \Database ;
1515
16+ use InitPHP \Database \Utils \{Pagination ,
17+ Datatables };
1618use \InitPHP \Database \Exceptions \{WritableException ,
1719 ReadableException ,
1820 UpdatableException ,
@@ -83,6 +85,15 @@ public function __construct(array $credentials = [])
8385 $ this ->_validation = new Validation ($ this ->_credentials ['validation ' ]['methods ' ], $ this ->_credentials ['validation ' ]['messages ' ], $ this ->_credentials ['validation ' ]['labels ' ], $ this );
8486 }
8587
88+ public function __call ($ name , $ arguments )
89+ {
90+ if (Helper::str_starts_with ($ name , 'findBy ' ) === FALSE ){
91+ throw new \RuntimeException ('There is no " ' . $ name . '" method. ' );
92+ }
93+ $ this ->where (Helper::camelCaseToSnakeCase (\substr ($ name , 6 )), \current ($ arguments ));
94+ return $ this ;
95+ }
96+
8697 final public function newInstance (array $ credentials = []): Database
8798 {
8899 return new self (empty ($ credentials ) ? $ this ->_credentials : \array_merge ($ this ->_credentials , $ credentials ));
@@ -625,6 +636,31 @@ public function all(int $limit = 100, int $offset = 0)
625636 ->read ();
626637 }
627638
639+ public function group (\Closure $ group , string $ logical = 'AND ' ): self
640+ {
641+ $ logical = \str_replace (['&& ' , '|| ' ], ['AND ' , 'OR ' ], \strtoupper ($ logical ));
642+ if (!\in_array ($ logical , ['AND ' , 'OR ' ], true )){
643+ throw new \InvalidArgumentException ('Logical operator OR, AND, && or || it could be. ' );
644+ }
645+
646+ $ clone = clone $ this ;
647+ $ clone ->reset ();
648+
649+ \call_user_func_array ($ group , [$ clone ]);
650+
651+ $ where = $ clone ->_whereQuery ();
652+ if ($ where !== '' ){
653+ $ this ->_STRUCTURE ['where ' ][$ logical ][] = '( ' . $ where . ') ' ;
654+ }
655+
656+ $ having = $ clone ->_havingQuery ();
657+ if ($ having !== '' ){
658+ $ this ->_STRUCTURE ['having ' ][$ logical ][] = '( ' . $ having . ') ' ;
659+ }
660+ unset($ clone );
661+ return $ this ;
662+ }
663+
628664 public function onlyDeleted (): self
629665 {
630666 $ this ->_isOnlyDeletes = true ;
@@ -637,17 +673,57 @@ public function onlyUndeleted(): self
637673 return $ this ;
638674 }
639675
676+ /**
677+ * QueryBuilder resetlemeden SELECT cümlesi kurar ve satır sayısını döndürür.
678+ *
679+ * @return int
680+ */
681+ public function count (): int
682+ {
683+ $ select = $ this ->_STRUCTURE ['select ' ];
684+ $ this ->_STRUCTURE ['select ' ][] = 'COUNT(*) AS row_count ' ;
685+ $ this ->_deleteFieldBuild (false );
686+ $ parameters = Parameters::get (false );
687+ $ res = $ this ->query ($ this ->_readQuery ());
688+ $ count = $ res ->toArray ()['row_count ' ] ?? 0 ;
689+ unset($ res );
690+ Parameters::merge ($ parameters );
691+ $ this ->_STRUCTURE ['select ' ] = $ select ;
692+ return $ count ;
693+ }
694+
695+ public function pagination (int $ page = 1 , int $ per_page_limit = 10 , string $ link = '?page={page} ' ): Pagination
696+ {
697+ $ total_row = $ this ->count ();
698+ $ this ->offset (($ page - 1 ) * $ per_page_limit )
699+ ->limit ($ per_page_limit );
700+ $ res = $ this ->query ($ this ->_readQuery ());
701+ $ this ->reset ();
702+
703+ return new Pagination ($ res , $ page , $ per_page_limit , $ total_row , $ link );
704+ }
705+
706+ public function datatables (array $ columns , int $ method = Datatables::GET_REQUEST ): string
707+ {
708+ return (new Datatables ($ this , $ columns , $ method ))->__toString ();
709+ }
710+
640711 public function _readQuery (): string
641712 {
642713 if ($ this ->getSchema () !== null ){
643714 $ this ->table ($ this ->getSchema ());
644715 }
645-
716+ $ where = $ this ->_whereQuery ();
717+ if ($ where !== '' ){
718+ $ where = ' WHERE ' . $ where ;
719+ }else {
720+ $ where = ' WHERE 1 ' ;
721+ }
646722 return 'SELECT '
647723 . (empty ($ this ->_STRUCTURE ['select ' ]) ? '* ' : \implode (', ' , $ this ->_STRUCTURE ['select ' ]))
648724 . ' FROM ' . \implode (', ' , $ this ->_STRUCTURE ['table ' ])
649725 . (!empty ($ this ->_STRUCTURE ['join ' ]) ? ' ' . \implode (', ' , $ this ->_STRUCTURE ['join ' ]) : '' )
650- . $ this -> _whereQuery ()
726+ . $ where
651727 . $ this ->_havingQuery ()
652728 . (!empty ($ this ->_STRUCTURE ['group_by ' ]) ? ' GROUP BY ' . \implode (', ' , $ this ->_STRUCTURE ['group_by ' ]) : '' )
653729 . (!empty ($ this ->_STRUCTURE ['order_by ' ]) ? ' ORDER BY ' . \implode (', ' , $ this ->_STRUCTURE ['order_by ' ]) : '' )
@@ -734,11 +810,17 @@ public function _updateQuery(array $data): string
734810 if ($ schemaID !== null && isset ($ data [$ schemaID ])){
735811 $ this ->where ($ schemaID , $ data [$ schemaID ]);
736812 }
813+ $ where = $ this ->_whereQuery ();
814+ if ($ where !== '' ){
815+ $ where = ' WHERE ' . $ where ;
816+ }else {
817+ $ where = ' WHERE 1 ' ;
818+ }
737819 return 'UPDATE '
738820 . (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
739821 . ' SET '
740822 . \implode (', ' , $ update )
741- . $ this -> _whereQuery ()
823+ . $ where
742824 . $ this ->_havingQuery ()
743825 . $ this ->_limitQuery ();
744826 }
@@ -787,22 +869,33 @@ public function _updateBatchQuery(array $data, $referenceColumn): string
787869 $ update [] = $ syntax ;
788870 }
789871 $ this ->in ($ referenceColumn , $ where );
790-
872+ $ where = $ this ->_whereQuery ();
873+ if ($ where !== '' ){
874+ $ where = ' WHERE ' . $ where ;
875+ }else {
876+ $ where = ' WHERE 1 ' ;
877+ }
791878 return 'UPDATE '
792879 . (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
793880 . ' SET '
794881 . \implode (', ' , $ update )
795- . $ this -> _whereQuery ()
882+ . $ where
796883 . $ this ->_havingQuery ()
797884 . $ this ->_limitQuery ();
798885 }
799886
800887 public function _deleteQuery (): string
801888 {
889+ $ where = $ this ->_whereQuery ();
890+ if ($ where !== '' ){
891+ $ where = ' WHERE ' . $ where ;
892+ }else {
893+ $ where = ' WHERE 1 ' ;
894+ }
802895 return 'DELETE FROM '
803896 . ' '
804897 . (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
805- . $ this -> _whereQuery ()
898+ . $ where
806899 . $ this ->_havingQuery ()
807900 . $ this ->_limitQuery ();
808901 }
@@ -840,10 +933,9 @@ private function _whereQuery(): string
840933 $ isAndEmpty = empty ($ this ->_STRUCTURE ['where ' ]['AND ' ]);
841934 $ isOrEmpty = empty ($ this ->_STRUCTURE ['where ' ]['OR ' ]);
842935 if ($ isAndEmpty && $ isOrEmpty ){
843- return ' WHERE 1 ' ;
936+ return '' ;
844937 }
845- return ' WHERE '
846- . (!$ isAndEmpty ? \implode (' AND ' , $ this ->_STRUCTURE ['where ' ]['AND ' ]) : '' )
938+ return (!$ isAndEmpty ? \implode (' AND ' , $ this ->_STRUCTURE ['where ' ]['AND ' ]) : '' )
847939 . (!$ isAndEmpty && !$ isOrEmpty ? ' AND ' : '' )
848940 . (!$ isOrEmpty ? \implode (' OR ' , $ this ->_STRUCTURE ['where ' ]['OR ' ]) : '' );
849941 }
0 commit comments