33
44class Join
55{
6- public $ join_type , $ primary_field , $ table , $ alias , $ foreign_field , $ wh , $ fields = array (), $ joins = array ();
6+ const INNER = 'INNER JOIN ' ;
7+ const LEFT = 'LEFT JOIN ' ;
8+ const OUTER = 'OUTER JOIN ' ;
9+
10+ protected Table $ table ;
11+
12+ protected $ join_type , $ foreign_field , $ fields = array (), $ joins = array ();
713
8- public function __construct ($ join_type , $ primary_field , $ table , $ alias , $ foreign_field )
14+ /**
15+ * @param String $join_type
16+ * @param Table $table
17+ * @param String $foreign_field
18+ */
19+ public function __construct (String $ join_type , Table $ table , String $ foreign_field )
920 {
1021 $ this ->join_type = trim ($ join_type );
11- $ this ->primary_field = trim ($ primary_field );
12- $ this ->table = trim ($ table );
13- $ this ->alias = trim ($ alias );
22+ $ this ->table = $ table ;
1423 $ this ->foreign_field = trim ($ foreign_field );
1524 }
16-
17- public static function init ( $ join_type , $ primary_field , $ table , $ alias , $ foreign_field )
18- {
19- return new Join ( $ join_type , $ primary_field , $ table , $ alias , $ foreign_field );
20- }
21-
25+
26+ /**
27+ * @param Join $join
28+ *
29+ * @return Join
30+ */
2231 public function join (Join $ join )
2332 {
2433 $ this ->joins [] = $ join ;
2534 return $ this ;
2635 }
2736
37+ /**
38+ * @return QuerySelect
39+ */
2840 public function noField ()
2941 {
3042 $ this ->fields = null ;
3143 return $ this ;
3244 }
3345
34- public function field ($ field , $ alias = "" )
46+ /**
47+ * @param String $field
48+ * @param String $alias
49+ *
50+ * @return Join
51+ */
52+ public function field (String $ field , String $ alias = "" )
3553 {
3654 if (!$ alias )
3755 {
3856 $ alias = $ field ;
3957 }
4058
4159 $ this ->fields [$ alias ] = $ field ;
60+
4261 return $ this ;
4362 }
4463
64+ /**
65+ * @return Array $field_list
66+ */
4567 public function getFields ()
4668 {
4769 $ fields = array ();
48-
49- $ table_alias = $ this ->alias ? $ this ->alias : $ this ->table ;
70+
71+ if ($ this ->table ->alias )
72+ {
73+ $ table_alias = $ this ->table ->alias ;
74+ }
75+ else
76+ {
77+ $ table_alias = "` " . $ this ->table ->name . "` " ;
78+ }
5079
5180 if (is_array ($ this ->fields ))
5281 {
@@ -58,7 +87,7 @@ public function getFields()
5887 {
5988 foreach ($ this ->fields as $ ailas => $ field )
6089 {
61- $ fields [] = $ table_alias . ". " . $ field . " AS " . $ table_alias . " __ " . $ ailas ;
90+ $ fields [] = $ table_alias . ". " . $ field ;
6291 }
6392 }
6493 }
@@ -70,29 +99,45 @@ public function getFields()
7099
71100 return $ fields ;
72101 }
73-
74- public function setWhere (Where $ wh )
75- {
76- $ this ->wh = $ wh ;
77- return $ this ;
78- }
79-
80- public function get ($ table_alias )
81- {
82- if (is_null ($ this ->wh ))
102+
103+ /**
104+ * @param Table $calling_table
105+ *
106+ * @return String
107+ */
108+ public function get (Table $ calling_table )
109+ {
110+ $ q = $ this ->join_type ;
111+
112+ $ join_table_alias = "" ;
113+
114+ if ($ this ->table ->alias )
83115 {
84- $ this ->wh = new Where ("AND " );
116+ $ join_table_alias = $ this ->table ->alias ;
117+
118+ $ q .= " " . "` " . $ this ->table ->name . "` " . " AS " . $ join_table_alias ;
119+ }
120+ else
121+ {
122+ $ join_table_alias = "` " . $ this ->table ->name . "` " ;
123+
124+ $ q .= " " . $ join_table_alias ;
85125 }
86126
87- $ join_table_alias = $ this ->alias ? $ this ->alias : $ this ->table ;
88-
89- $ this ->wh ->add ($ table_alias . ". " . $ this ->primary_field , $ join_table_alias . ". " . $ this ->foreign_field , "= " , "" );
90-
91- $ q = $ this ->join_type . " " . $ this ->table . " AS " . $ join_table_alias . " ON " . $ this ->wh ->get ();
127+ $ q .= " ON " . $ join_table_alias . ". " . $ this ->foreign_field ;
128+
129+ if ($ calling_table ->alias )
130+ {
131+ $ q .= " = " . $ calling_table ->alias . ". " . $ calling_table ->primary_field ;
132+ }
133+ else
134+ {
135+ $ q .= " = " . "` " . $ calling_table ->name . "` " . ". " . $ calling_table ->primary_field ;
136+ }
92137
93138 foreach ($ this ->joins as $ join )
94139 {
95- $ q .= $ join ->get ($ join_table_alias );
140+ $ q .= " " . $ join ->get ($ this -> table );
96141 }
97142
98143 return $ q ;
0 commit comments