7
7
* @author Muhammet ŞAFAK <[email protected] >
8
8
* @copyright Copyright © 2022 Muhammet ŞAFAK
9
9
* @license ./LICENSE MIT
10
- * @version 2.0.5
10
+ * @version 2.0.6
11
11
* @link https://www.muhammetsafak.com.tr
12
12
*/
13
13
14
14
namespace InitPHP \Database ;
15
15
16
+ use InitPHP \Database \Utils \{Pagination ,
17
+ Datatables };
16
18
use \InitPHP \Database \Exceptions \{WritableException ,
17
19
ReadableException ,
18
20
UpdatableException ,
@@ -83,6 +85,15 @@ public function __construct(array $credentials = [])
83
85
$ this ->_validation = new Validation ($ this ->_credentials ['validation ' ]['methods ' ], $ this ->_credentials ['validation ' ]['messages ' ], $ this ->_credentials ['validation ' ]['labels ' ], $ this );
84
86
}
85
87
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
+
86
97
final public function newInstance (array $ credentials = []): Database
87
98
{
88
99
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)
625
636
->read ();
626
637
}
627
638
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
+
628
664
public function onlyDeleted (): self
629
665
{
630
666
$ this ->_isOnlyDeletes = true ;
@@ -637,17 +673,57 @@ public function onlyUndeleted(): self
637
673
return $ this ;
638
674
}
639
675
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
+
640
711
public function _readQuery (): string
641
712
{
642
713
if ($ this ->getSchema () !== null ){
643
714
$ this ->table ($ this ->getSchema ());
644
715
}
645
-
716
+ $ where = $ this ->_whereQuery ();
717
+ if ($ where !== '' ){
718
+ $ where = ' WHERE ' . $ where ;
719
+ }else {
720
+ $ where = ' WHERE 1 ' ;
721
+ }
646
722
return 'SELECT '
647
723
. (empty ($ this ->_STRUCTURE ['select ' ]) ? '* ' : \implode (', ' , $ this ->_STRUCTURE ['select ' ]))
648
724
. ' FROM ' . \implode (', ' , $ this ->_STRUCTURE ['table ' ])
649
725
. (!empty ($ this ->_STRUCTURE ['join ' ]) ? ' ' . \implode (', ' , $ this ->_STRUCTURE ['join ' ]) : '' )
650
- . $ this -> _whereQuery ()
726
+ . $ where
651
727
. $ this ->_havingQuery ()
652
728
. (!empty ($ this ->_STRUCTURE ['group_by ' ]) ? ' GROUP BY ' . \implode (', ' , $ this ->_STRUCTURE ['group_by ' ]) : '' )
653
729
. (!empty ($ this ->_STRUCTURE ['order_by ' ]) ? ' ORDER BY ' . \implode (', ' , $ this ->_STRUCTURE ['order_by ' ]) : '' )
@@ -734,11 +810,17 @@ public function _updateQuery(array $data): string
734
810
if ($ schemaID !== null && isset ($ data [$ schemaID ])){
735
811
$ this ->where ($ schemaID , $ data [$ schemaID ]);
736
812
}
813
+ $ where = $ this ->_whereQuery ();
814
+ if ($ where !== '' ){
815
+ $ where = ' WHERE ' . $ where ;
816
+ }else {
817
+ $ where = ' WHERE 1 ' ;
818
+ }
737
819
return 'UPDATE '
738
820
. (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
739
821
. ' SET '
740
822
. \implode (', ' , $ update )
741
- . $ this -> _whereQuery ()
823
+ . $ where
742
824
. $ this ->_havingQuery ()
743
825
. $ this ->_limitQuery ();
744
826
}
@@ -787,22 +869,33 @@ public function _updateBatchQuery(array $data, $referenceColumn): string
787
869
$ update [] = $ syntax ;
788
870
}
789
871
$ this ->in ($ referenceColumn , $ where );
790
-
872
+ $ where = $ this ->_whereQuery ();
873
+ if ($ where !== '' ){
874
+ $ where = ' WHERE ' . $ where ;
875
+ }else {
876
+ $ where = ' WHERE 1 ' ;
877
+ }
791
878
return 'UPDATE '
792
879
. (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
793
880
. ' SET '
794
881
. \implode (', ' , $ update )
795
- . $ this -> _whereQuery ()
882
+ . $ where
796
883
. $ this ->_havingQuery ()
797
884
. $ this ->_limitQuery ();
798
885
}
799
886
800
887
public function _deleteQuery (): string
801
888
{
889
+ $ where = $ this ->_whereQuery ();
890
+ if ($ where !== '' ){
891
+ $ where = ' WHERE ' . $ where ;
892
+ }else {
893
+ $ where = ' WHERE 1 ' ;
894
+ }
802
895
return 'DELETE FROM '
803
896
. ' '
804
897
. (empty ($ this ->_STRUCTURE ['table ' ]) ? $ this ->getSchema () : end ($ this ->_STRUCTURE ['table ' ]))
805
- . $ this -> _whereQuery ()
898
+ . $ where
806
899
. $ this ->_havingQuery ()
807
900
. $ this ->_limitQuery ();
808
901
}
@@ -840,10 +933,9 @@ private function _whereQuery(): string
840
933
$ isAndEmpty = empty ($ this ->_STRUCTURE ['where ' ]['AND ' ]);
841
934
$ isOrEmpty = empty ($ this ->_STRUCTURE ['where ' ]['OR ' ]);
842
935
if ($ isAndEmpty && $ isOrEmpty ){
843
- return ' WHERE 1 ' ;
936
+ return '' ;
844
937
}
845
- return ' WHERE '
846
- . (!$ isAndEmpty ? \implode (' AND ' , $ this ->_STRUCTURE ['where ' ]['AND ' ]) : '' )
938
+ return (!$ isAndEmpty ? \implode (' AND ' , $ this ->_STRUCTURE ['where ' ]['AND ' ]) : '' )
847
939
. (!$ isAndEmpty && !$ isOrEmpty ? ' AND ' : '' )
848
940
. (!$ isOrEmpty ? \implode (' OR ' , $ this ->_STRUCTURE ['where ' ]['OR ' ]) : '' );
849
941
}
0 commit comments