11<?php
22
33/**
4- * 根据时间同构分表的查询类
5- * @package app\common\library
4+ * 分表数据查询类
65 * @author LIUJIAN <coder.keda@gmail.com>
76 */
87class ShardingQuery
@@ -28,6 +27,30 @@ class ShardingQuery
2827 */
2928 public $ field ;
3029
30+ /**
31+ * join
32+ * @var array
33+ */
34+ public $ innerJoin ;
35+
36+ /**
37+ * join
38+ * @var array
39+ */
40+ public $ leftJoin ;
41+
42+ /**
43+ * join
44+ * @var array
45+ */
46+ public $ rightJoin ;
47+
48+ /**
49+ * join
50+ * @var array
51+ */
52+ public $ fullJoin ;
53+
3154 /**
3255 * 条件
3356 * @var string
@@ -58,6 +81,12 @@ class ShardingQuery
5881 */
5982 protected $ stats ;
6083
84+ /**
85+ * 追踪
86+ * @var array
87+ */
88+ protected $ trace ;
89+
6190 /**
6291 * ShardingQuery constructor.
6392 * @param array $config
@@ -84,6 +113,30 @@ public function __construct(array $config)
84113 }
85114 $ this ->$ key = $ value ;
86115 break ;
116+ case 'innerJoin ' :
117+ if (!is_array ($ value )) {
118+ throw new \RuntimeException ("'innerJoin' is not a array type. " );
119+ }
120+ $ this ->$ key = $ value ;
121+ break ;
122+ case 'leftJoin ' :
123+ if (!is_array ($ value )) {
124+ throw new \RuntimeException ("'leftJoin' is not a array type. " );
125+ }
126+ $ this ->$ key = $ value ;
127+ break ;
128+ case 'rightJoin ' :
129+ if (!is_array ($ value )) {
130+ throw new \RuntimeException ("'rightJoin' is not a array type. " );
131+ }
132+ $ this ->$ key = $ value ;
133+ break ;
134+ case 'fullJoin ' :
135+ if (!is_array ($ value )) {
136+ throw new \RuntimeException ("'fullJoin' is not a array type. " );
137+ }
138+ $ this ->$ key = $ value ;
139+ break ;
87140 case 'where ' :
88141 if (!is_string ($ value )) {
89142 throw new \RuntimeException ("'where' is not a string type. " );
@@ -120,18 +173,17 @@ public function select()
120173 {
121174 $ this ->stats = $ this ->stats ($ this ->table );
122175 $ range = $ this ->range ($ this ->stats );
176+ $ sql = $ this ->sql ();
123177 $ data = [];
124178 foreach ($ range as $ tableName => $ item ) {
125- $ sql = "SELECT {$ this ->field } FROM ` {$ tableName }` " ;
126- if (!empty ($ this ->where )) {
127- $ sql .= " WHERE {$ this ->where }" ;
128- }
129- if (!empty ($ this ->order )) {
130- $ sql .= " ORDER BY {$ this ->order }" ;
131- }
132- $ sql = "{$ sql } LIMIT {$ item ['limit ' ]} OFFSET {$ item ['offset ' ]}" ;
133- $ result = call_user_func ($ this ->callback , $ sql );
134- $ data = array_merge ($ data , $ result );
179+ $ sql = str_replace ('{table} ' , $ tableName , $ sql );
180+ $ sql = "{$ sql } LIMIT {$ item ['limit ' ]} OFFSET {$ item ['offset ' ]}" ;
181+ $ result = call_user_func ($ this ->callback , $ sql );
182+ $ data = array_merge ($ data , $ result );
183+ $ this ->trace [] = [
184+ 'sql ' => $ sql ,
185+ 'rowCount ' => count ($ result ),
186+ ];
135187 }
136188 return $ data ;
137189 }
@@ -214,6 +266,42 @@ protected function range($stats)
214266 return $ tables ;
215267 }
216268
269+ /**
270+ * 生成sql
271+ * @return string
272+ */
273+ protected function sql ()
274+ {
275+ $ sql = "SELECT {$ this ->field } FROM `{table}` " ;
276+ if (!empty ($ this ->innerJoin )) {
277+ foreach ($ this ->innerJoin as $ item ) {
278+ $ sql .= " INNER JOIN {$ item }" ;
279+ }
280+ }
281+ if (!empty ($ this ->leftJoin )) {
282+ foreach ($ this ->leftJoin as $ item ) {
283+ $ sql .= " LEFT JOIN {$ item }" ;
284+ }
285+ }
286+ if (!empty ($ this ->rightJoin )) {
287+ foreach ($ this ->rightJoin as $ item ) {
288+ $ sql .= " RIGHT JOIN {$ item }" ;
289+ }
290+ }
291+ if (!empty ($ this ->fullJoin )) {
292+ foreach ($ this ->fullJoin as $ item ) {
293+ $ sql .= " FULL JOIN {$ item }" ;
294+ }
295+ }
296+ if (!empty ($ this ->where )) {
297+ $ sql .= " WHERE {$ this ->where }" ;
298+ }
299+ if (!empty ($ this ->order )) {
300+ $ sql .= " ORDER BY {$ this ->order }" ;
301+ }
302+ return $ sql ;
303+ }
304+
217305 /**
218306 * 获取数据总数
219307 * @return int
@@ -226,4 +314,13 @@ public function count()
226314 return $ number ?: 0 ;
227315 }
228316
317+ /**
318+ * 获取追踪数据
319+ * @return array
320+ */
321+ public function trace ()
322+ {
323+ return $ this ->trace ;
324+ }
325+
229326}
0 commit comments