@@ -16,26 +16,31 @@ final class Select extends Fetch
16
16
use SubQueryTrait;
17
17
18
18
/**
19
- * @param string $table_name Table name
20
- * @param string[] $columns_name Selected cloumn
21
- * @param MyPDO $PDO MyPDO class
22
- * @param string[] $options Add costume option (eg: query)
19
+ * @param string|InnerQuery $table_name Table name
20
+ * @param string[] $columns_name Selected cloumn
21
+ * @param MyPDO $PDO MyPDO class
22
+ * @param string[] $options Add costume option (eg: query)
23
23
*
24
24
* @return void
25
25
*/
26
- public function __construct (string $ table_name , array $ columns_name , MyPDO $ PDO , ?array $ options = null )
26
+ public function __construct ($ table_name , array $ columns_name , MyPDO $ PDO , ?array $ options = null )
27
27
{
28
- $ this ->_table = $ table_name ;
29
- $ this ->_column = $ columns_name ;
30
- $ this ->PDO = $ PDO ;
28
+ $ this ->_sub_query = $ table_name instanceof InnerQuery ? $ table_name : new InnerQuery (table: $ table_name );
29
+ $ this ->_column = $ columns_name ;
30
+ $ this ->PDO = $ PDO ;
31
+
32
+ // inherit bind from sub query
33
+ if ($ table_name instanceof InnerQuery) {
34
+ $ this ->_binds = $ table_name ->getBind ();
35
+ }
31
36
32
37
// defaul query
33
38
if (count ($ this ->_column ) > 1 ) {
34
39
$ this ->_column = array_map (fn ($ e ) => "` $ e` " , $ this ->_column );
35
40
}
36
41
37
42
$ column = implode (', ' , $ columns_name );
38
- $ this ->_query = $ options ['query ' ] ?? "SELECT $ column FROM ` $ this ->_table ` " ;
43
+ $ this ->_query = $ options ['query ' ] ?? "SELECT { $ column} FROM `{ $ this ->_sub_query } ` " ;
39
44
}
40
45
41
46
public function __toString ()
@@ -67,9 +72,14 @@ public static function from(string $table_name, array $column_name, MyPDO $PDO)
67
72
public function join (AbstractJoin $ ref_table ): self
68
73
{
69
74
// overide master table
70
- $ ref_table ->table ($ this ->_table );
75
+ $ ref_table ->table ($ this ->_sub_query -> getAlias () );
71
76
72
77
$ this ->_join [] = $ ref_table ->stringJoin ();
78
+ $ binds = (fn () => $ this ->{'sub_query ' })->call ($ ref_table );
79
+
80
+ if (null !== $ binds ) {
81
+ $ this ->_binds = array_merge ($ this ->_binds , $ binds ->getBind ());
82
+ }
73
83
74
84
return $ this ;
75
85
}
@@ -161,8 +171,8 @@ public function limitOffset(int $limit, int $offset): self
161
171
public function order (string $ column_name , int $ order_using = MyQuery::ORDER_ASC , ?string $ belong_to = null )
162
172
{
163
173
$ order = 0 === $ order_using ? 'ASC ' : 'DESC ' ;
164
- $ belong_to ??= $ this ->_table ;
165
- $ this ->_sort_order = "ORDER BY ` $ belong_to` .`$ column_name` $ order " ;
174
+ $ belong_to ??= null === $ this ->_sub_query ? " ` { $ this -> _table } ` " : $ this -> _sub_query -> getAlias () ;
175
+ $ this ->_sort_order = "ORDER BY $ belong_to.` $ column_name` $ order " ;
166
176
167
177
return $ this ;
168
178
}
@@ -183,7 +193,7 @@ protected function builder(): string
183
193
184
194
$ condition = implode (' ' , array_filter ($ build , fn ($ item ) => $ item !== '' ));
185
195
186
- return $ this ->_query = "SELECT $ column FROM ` $ this ->_table ` $ condition " ;
196
+ return $ this ->_query = "SELECT { $ column} FROM { $ this ->_sub_query } { $ condition} " ;
187
197
}
188
198
189
199
/**
0 commit comments