@@ -314,7 +314,7 @@ private function process_sql(&$tokens,$start_at = 0, $stop_at = false) {
314
314
$ token_category = 'SELECT ' ;
315
315
}
316
316
317
- /* If it isn't obvious, when $skip_next is set, then we ignore the next real
317
+ /* If it isn't obvious, when $skip_next is set, then we ignore the next real
318
318
token, that is we ignore whitespace.
319
319
*/
320
320
if ($ skip_next ) {
@@ -323,7 +323,7 @@ private function process_sql(&$tokens,$start_at = 0, $stop_at = false) {
323
323
continue ;
324
324
}
325
325
326
- #to skip the token we replace it with whitespace
326
+ #to skip the token we replace it with whitespace
327
327
$ new_token = "" ;
328
328
$ skip_next = false ;
329
329
}
@@ -440,7 +440,6 @@ private function process_sql(&$tokens,$start_at = 0, $stop_at = false) {
440
440
break ;
441
441
442
442
case 'USING ' :
443
- #case 'ON':
444
443
/* USING in FROM clause is different from USING w/ prepared statement*/
445
444
if ($ token_category == 'EXECUTE ' ) {
446
445
$ token_category =$ upper ;
@@ -545,7 +544,6 @@ private function process_sql(&$tokens,$start_at = 0, $stop_at = false) {
545
544
break ;
546
545
}
547
546
548
- #echo "HERE: $token $token_number\n";
549
547
if ($ prev_category == $ token_category ) {
550
548
$ out [$ token_category ][] = $ token ;
551
549
}
@@ -592,7 +590,7 @@ private function process_set_list($tokens) {
592
590
foreach ($ tokens as $ token ) {
593
591
$ token =trim ($ token );
594
592
if (!$ column ) {
595
- if (! $ token ) continue ;
593
+ if ($ token === false || empty ( $ token ) ) continue ;
596
594
$ column .= $ token ;
597
595
continue ;
598
596
}
@@ -659,7 +657,7 @@ private function process_select(&$tokens) {
659
657
$ expr [] = $ this ->process_select_expr (trim ($ expression ));
660
658
$ expression = "" ;
661
659
} else {
662
- if (! $ token ) $ token =" " ;
660
+ if ($ token === "" || $ token === false ) $ token =" " ;
663
661
$ expression .= $ token ;
664
662
}
665
663
}
@@ -748,7 +746,8 @@ private function process_select_expr($expression) {
748
746
}
749
747
$ processed = false ;
750
748
$ type ='expression ' ;
751
- if (trim ($ base_expr ) == '( ' ) {
749
+
750
+ if (substr (trim ($ base_expr ),0 ,1 ) == '( ' ) {
752
751
$ base_expr = substr ($ expression ,1 ,-1 );
753
752
if (preg_match ('/^sel/i ' , $ base_expr )) {
754
753
$ type ='subquery ' ;
@@ -775,16 +774,19 @@ private function process_from(&$tokens) {
775
774
$ expr = array ();
776
775
$ token_count =0 ;
777
776
$ table = "" ;
778
- $ alias = "" ;
777
+ $ alias = "" ;
779
778
780
779
$ skip_next =false ;
781
780
$ i =0 ;
782
- $ join_type = '' ;
781
+ $ join_type = '' ;
783
782
$ ref_type ="" ;
784
783
$ ref_expr ="" ;
785
784
$ base_expr ="" ;
786
785
$ sub_tree = false ;
787
786
$ subquery = "" ;
787
+
788
+ $ first_join =true ;
789
+
788
790
foreach ($ tokens as $ token ) {
789
791
$ base_expr = false ;
790
792
$ upper = strtoupper (trim ($ token ));
@@ -798,20 +800,49 @@ private function process_from(&$tokens) {
798
800
continue ;
799
801
}
800
802
}
803
+
801
804
if (preg_match ("/^ \\s* \\( \\s*select/i " ,$ token )) {
802
805
$ type = 'subquery ' ;
803
806
$ table = "DEPENDENT-SUBQUERY " ;
804
807
$ sub_tree = $ this ->parse (trim ($ token ,'() ' ));
805
808
$ subquery = $ token ;
806
809
}
807
810
808
- if ($ upper != 'JOIN ' && $ token != ', ' ) {
809
- $ expression .= $ token == '' ? " " : $ token ;
810
- if ($ ref_type ) {
811
- $ ref_expr .= $ token == '' ? " " : $ token ;
812
- }
813
-
814
- }
811
+ switch ($ upper ) {
812
+ case 'OUTER ' :
813
+ continue ;
814
+ break ;
815
+
816
+ case 'LEFT ' :
817
+ case 'RIGHT ' :
818
+ case 'NATURAL ' :
819
+ case 'CROSS ' :
820
+ //if($join_type == "") {
821
+ if ($ first_join ) {
822
+ $ first_join = false ;
823
+ $ join_type = 'JOIN ' ;
824
+ } else {
825
+ $ join_type = " " . $ upper . " " ;
826
+ }
827
+ //} else {
828
+ // $join_type = " " . $upper . " ";
829
+ //}
830
+
831
+ break ;
832
+
833
+ case ', ' :
834
+ case 'JOIN ' :
835
+ $ first_join =false ;
836
+ if ($ join_type == "" ) $ join_type = "JOIN " ;
837
+ break ;
838
+
839
+ default :
840
+ $ expression .= $ token == '' ? " " : $ token ;
841
+ if ($ ref_type ) {
842
+ $ ref_expr .= $ token == '' ? " " : $ token ;
843
+ }
844
+ break ;
845
+ }
815
846
816
847
switch ($ upper ) {
817
848
case 'AS ' :
@@ -827,7 +858,6 @@ private function process_from(&$tokens) {
827
858
break ;
828
859
829
860
case 'INDEX ' :
830
-
831
861
if ($ token_category == 'CREATE ' ) {
832
862
$ token_category = $ upper ;
833
863
continue 2 ;
@@ -855,53 +885,34 @@ private function process_from(&$tokens) {
855
885
case 'LEFT ' :
856
886
case 'RIGHT ' :
857
887
case 'STRAIGHT_JOIN ' :
858
- $ join_type .= ($ token == '' ? ' ' : $ token . " " );
859
- $ ref_expr = "" ;
860
888
continue ;
861
889
break ;
862
-
863
- case 'FOR ' :
890
+
891
+ case 'FOR ' :
864
892
$ token_count ++;
865
893
$ skip_next = true ;
866
894
continue ;
867
895
break ;
868
-
869
- case ', ' :
896
+
897
+ case ', ' :
870
898
case 'JOIN ' :
871
- #any options for the first join (the one after the FROM) are "stuck" to the first
872
- #table. we need to use 'FROM' as the join_type for the first table, and save the
873
- #"stuck" options for the next table
874
- $ save_join_type = "" ;
875
- if (count ($ expr )>0 ) {
876
- $ join_type .= $ token == ', ' ? ' JOIN ' : $ upper ;
877
- } else {
878
- $ save_join_type = $ join_type ;
879
- $ join_type = 'JOIN ' ;
880
- }
899
+
881
900
if (!trim ($ alias )) $ alias = $ table ;
882
-
883
- $ join_type =strtoupper ($ join_type );
884
- /*if(!$base_expr) {
885
- $expr[] = array('table'=>$table, 'alias'=>$alias,'join_type'=>$join_type,'ref_type'=> $ref_type,'ref_clause'=> $ref_expr);
886
- } else {*/
887
- if ($ subquery ) $ base_expr =$ subquery ;
888
- $ expr [] = array ('table ' =>$ table , 'alias ' =>$ alias ,'join_type ' =>$ join_type ,'ref_type ' => $ ref_type ,'ref_clause ' => $ ref_expr , 'base_expr ' => $ base_expr , 'sub_tree ' => $ sub_tree );
889
- $ subquery = "" ;
890
- # }
891
-
892
- if ($ save_join_type ) {
893
- $ join_type = $ save_join_type ;
894
- } else {
895
- $ join_type = "" ;
901
+ if ($ subquery ) {
902
+ $ sub_tree = $ this ->parse (trim ($ subquery ,'() ' ));
903
+ $ base_expr =$ subquery ;
896
904
}
905
+ $ expr [] = array ('table ' =>$ table , 'alias ' =>$ alias ,'join_type ' =>$ join_type ,'ref_type ' => $ ref_type ,'ref_clause ' => $ ref_expr , 'base_expr ' => $ base_expr , 'sub_tree ' => $ sub_tree );
906
+
897
907
$ token_count = 0 ;
898
- $ table = $ alias = $ expression = $ base_expr = $ ref_type = $ ref_expr = "" ;
899
- $ sub_tree = false ;
908
+ $ table = $ alias = $ expression = $ base_expr = $ ref_type = $ ref_expr = "" ;
909
+ $ sub_tree =false ;
910
+ $ subquery = "" ;
900
911
901
912
break ;
902
913
903
914
default :
904
- if (! $ token ) continue ;
915
+ if ($ token === false || empty ( $ token ) || $ token === "" ) continue ;
905
916
906
917
if ($ token_count == 0 ) {
907
918
if (!$ table ) {
@@ -915,13 +926,11 @@ private function process_from(&$tokens) {
915
926
}
916
927
++$ i ;
917
928
}
918
- if ($ join_type == '' ) $ join_type = 'JOIN ' ;
919
- if (!trim ($ alias )) $ alias = $ table ;
920
- /*if(!$base_expr) {
921
- $expr[] = array('table'=>$table, 'alias'=>$alias,'join_type'=>$join_type,'ref_type'=> $ref_type,'ref_clause'=> $ref_expr);
922
- } else { */
923
- $ expr [] = array ('table ' =>$ table , 'alias ' =>$ alias ,'join_type ' =>$ join_type ,'ref_type ' => $ ref_type ,'ref_clause ' => $ ref_expr , 'base_expr ' => $ base_expr , 'sub_tree ' => $ sub_tree );
924
- # }
929
+ if ($ join_type == '' ) $ join_type = 'JOIN ' ;
930
+
931
+ if (!trim ($ alias )) $ alias = $ table ;
932
+ $ expr [] = array ('table ' =>$ table , 'alias ' =>$ alias ,'join_type ' =>$ join_type ,'ref_type ' => $ ref_type ,'ref_clause ' => $ ref_expr , 'base_expr ' => $ base_expr , 'sub_tree ' => $ sub_tree );
933
+
925
934
926
935
return $ expr ;
927
936
}
0 commit comments