@@ -24,6 +24,7 @@ class Builder
2424 private $ db ;
2525
2626 protected $ table ;
27+ protected $ tableAs ;
2728 protected $ field ;
2829 protected $ orderBy ;
2930 protected $ limit ;
@@ -88,14 +89,23 @@ public function getConnection()
8889 * @param string $tableName 支持 as 例如 user as u
8990 * @return static
9091 */
91- public function table ($ tableName = '' )
92+ public function table ($ tableName = '' , $ asName = null )
9293 {
93- if (!empty ($ tableName )) {
94- $ tableName = self ::addPrefix ($ tableName );
94+ if ($ asName === null ) {
95+ // 兼容as部份写在tableName中
96+ // user as u
97+ // {{user}} as u
98+ // {{%user}} as u
99+ // user u
100+ if (preg_match ('/^(.+?)\s+(as\s+)?(\w+)$/i ' , $ tableName , $ res )) {
101+ $ tableName = $ res [1 ];
102+ $ asName = $ res [3 ];
103+ }
95104 }
96105
97106 $ builder = clone $ this ;
98- $ builder ->table = $ tableName ;
107+ $ builder ->table = self ::addPrefix ($ tableName );
108+ $ builder ->tableAs = $ this ->addTableQuote ($ asName );
99109 return $ builder ;
100110 }
101111
@@ -108,24 +118,31 @@ public function table($tableName = '')
108118 */
109119 public function addPrefix ($ tableName )
110120 {
111- // user as u
112- // {{user}} as u
113- // {{%user}} as u
114- // user u
115- if (preg_match ('/^(.+?)\s+(as\s+)?(\w+)$/i ' , $ tableName , $ res )) {
116- $ tableName = $ res [1 ];
117- $ asName = ' AS ' . $ res [3 ];
118- } else {
119- $ asName = '' ;
121+ if (empty ($ tableName )) {
122+ return $ tableName ;
123+ }
124+ if (strpos ($ tableName , '{{ ' ) === false ) {
125+ return $ this ->addTableQuote ('% ' . $ tableName );
126+ }
127+
128+ return $ tableName ;
129+ }
130+
131+ private function addTableQuote ($ tableName )
132+ {
133+ if (empty ($ tableName )) {
134+ return $ tableName ;
120135 }
121136
122137 if (strpos ($ tableName , '{{ ' ) === false ) {
123- $ tableName = '{{% ' . $ tableName . '}} ' ;
138+ return '{{ ' . $ tableName . '}} ' ;
124139 }
140+
125141 if (!preg_match ('/^\{\{%?[\w\-\.\$]+%?\}\}$/ ' , $ tableName )) {
126- throw new Exception ('表名含有不被允许的字符 ' );
142+ throw new Exception ('表名含有不被允许的字符: ' . $ tableName );
127143 }
128- return $ tableName . $ asName ;
144+
145+ return $ tableName ;
129146 }
130147
131148 /**
@@ -206,7 +223,7 @@ public function findAll($condition = '', $params = array())
206223 {
207224 $ this ->where ($ condition , $ params );
208225
209- $ sql = 'SELECT ' . static ::getFieldString () . ' FROM ' . $ this ->table
226+ $ sql = 'SELECT ' . static ::getFieldString () . ' FROM ' . $ this ->table . $ this -> getTableAs ()
210227 . $ this ->getJoinString ()
211228 . $ this ->getWhereString ()
212229 . $ this ->getGroupByString ()
@@ -218,6 +235,15 @@ public function findAll($condition = '', $params = array())
218235 return static ::findAllBySql ($ sql , $ this ->params );
219236 }
220237
238+ private function getTableAs ()
239+ {
240+ if (empty ($ this ->tableAs )) {
241+ return '' ;
242+ }
243+
244+ return ' AS ' . $ this ->tableAs ;
245+ }
246+
221247 /**
222248 * 拆分查询,用于处理非常多的查询结果,而不会消耗大量内存,建议加上排序字段
223249 *
@@ -564,7 +590,7 @@ public function __call($method, $arguments)
564590
565591 $ method = strtoupper ($ method );
566592
567- $ sql = 'SELECT ' . $ method . '( ' . $ field . ') FROM ' . $ this ->table
593+ $ sql = 'SELECT ' . $ method . '( ' . $ field . ') FROM ' . $ this ->table . $ this -> getTableAs ()
568594 . $ this ->getJoinString ()
569595 . $ this ->getWhereString ();
570596
@@ -894,7 +920,7 @@ public function transaction(Closure $callback)
894920 */
895921 public function toSql ()
896922 {
897- $ sql = 'SELECT ' . static ::getFieldString () . ' FROM ' . $ this ->table
923+ $ sql = 'SELECT ' . static ::getFieldString () . ' FROM ' . $ this ->table . $ this -> getTableAs ()
898924 . $ this ->getJoinString ()
899925 . $ this ->getWhereString ()
900926 . $ this ->getGroupByString ()
@@ -1211,6 +1237,7 @@ public function afterFind($callback)
12111237 protected function reset ()
12121238 {
12131239 $ this ->table = null ;
1240+ $ this ->tableAs = null ;
12141241 $ this ->fetchClass = null ;
12151242 $ this ->orderBy = null ;
12161243 $ this ->field = null ;
0 commit comments