@@ -33,7 +33,10 @@ class SimplePDOQueryBuilder
3333 * @var
3434 */
3535 protected $ alias ;
36-
36+ /**
37+ * @var int
38+ */
39+ protected $ joinOrd = 0 ;
3740 /**
3841 * @var array
3942 */
@@ -127,16 +130,6 @@ public function subQuery($alias)
127130 return $ sq ;
128131 }
129132
130- /**
131- * @param $alias
132- * @return $this
133- */
134- public function setAlias ($ alias )
135- {
136- $ this ->alias = $ alias ;
137- return $ this ;
138- }
139-
140133 /**
141134 * @param $from
142135 * @return $this
@@ -154,6 +147,65 @@ public function from($from)
154147 return $ this ;
155148 }
156149
150+ /**
151+ * @return string
152+ */
153+ public function getSql ()
154+ {
155+ $ query = "\n SELECT " . implode (', ' , $ this ->fields );
156+
157+ if (!empty ($ this ->from )) {
158+ $ query .= "\n FROM " . $ this ->from ;
159+ }
160+ if (!empty ($ this ->joins )) {
161+ $ query .= implode (' ' , $ this ->joins );
162+ }
163+ if (!empty ($ this ->where )) {
164+ $ query .= "\n WHERE " . implode (" AND " , $ this ->where );
165+ }
166+ if (!empty ($ this ->groupBy )) {
167+ $ query .= "\n GROUP BY $ this ->groupBy " ;
168+ }
169+ if (!empty ($ this ->having )) {
170+ $ query .= "\n HAVING " . implode (" AND " , $ this ->having );
171+ }
172+ if (!empty ($ this ->sortBy )) {
173+ $ query .= "\n ORDER BY $ this ->sortBy $ this ->sortOrder " ;
174+ }
175+ if ($ this ->limit ) {
176+ $ query .= "\n LIMIT $ this ->limit " ;
177+ if ($ this ->offset ) {
178+ $ query .= " OFFSET " . $ this ->offset ;
179+ }
180+ }
181+
182+ if (!empty ($ this ->union )) {
183+ $ query .= implode (' ' , $ this ->union );
184+ }
185+
186+ $ query .= "\n" ;
187+
188+ return $ query ;
189+ }
190+
191+ /**
192+ * @return mixed
193+ */
194+ public function getAlias ()
195+ {
196+ return $ this ->alias ;
197+ }
198+
199+ /**
200+ * @param $alias
201+ * @return $this
202+ */
203+ public function setAlias ($ alias )
204+ {
205+ $ this ->alias = $ alias ;
206+ return $ this ;
207+ }
208+
157209 /**
158210 * @param $leftJoin
159211 * @param string $condition
@@ -162,10 +214,14 @@ public function from($from)
162214 public function leftJoin ($ leftJoin , $ condition = '' )
163215 {
164216 if ($ leftJoin instanceof SimplePDOQueryBuilder) {
165- $ this -> leftJoins [] = "\n LEFT JOIN ( {$ leftJoin ->getSql ()} ) as {$ leftJoin ->getAlias ()} ON $ condition " ;
217+ $ join = "\n LEFT JOIN ( {$ leftJoin ->getSql ()} ) as {$ leftJoin ->getAlias ()} ON $ condition " ;
166218 } else {
167- $ this -> leftJoins [] = "\n LEFT JOIN " . $ leftJoin . ($ condition ? ' ON ' . $ condition : '' );
219+ $ join = "\n LEFT JOIN " . $ leftJoin . ($ condition ? ' ON ' . $ condition : '' );
168220 }
221+
222+ $ this ->joinOrd ++;
223+ $ this ->joins [$ this ->joinOrd ] = $ join ;
224+
169225 return $ this ;
170226 }
171227
@@ -177,17 +233,22 @@ public function leftJoin($leftJoin, $condition = '')
177233 public function join ($ join , $ condition = '' )
178234 {
179235 if ($ join instanceof SimplePDOQueryBuilder) {
180- $ this -> joins [] = "\n JOIN ( {$ join ->getSql ()} ) as {$ join ->getAlias ()} ON $ condition " ;
236+ $ join = "\n JOIN ( {$ join ->getSql ()} ) as {$ join ->getAlias ()} ON $ condition " ;
181237 } else {
182- $ this -> joins [] = "\n JOIN " . $ join . ($ condition ? ' ON ' . $ condition : '' );
238+ $ join = "\n JOIN " . $ join . ($ condition ? ' ON ' . $ condition : '' );
183239 }
240+
241+ $ this ->joinOrd ++;
242+ $ this ->joins [$ this ->joinOrd ] = $ join ;
243+
184244 return $ this ;
185245 }
186246
247+
187248 /**
188249 * @param $join
189- * @internal param string $condition
190250 * @return $this
251+ * @internal param string $condition
191252 */
192253 public function union ($ join )
193254 {
@@ -209,21 +270,13 @@ public function where($condition)
209270 return $ this ;
210271 }
211272
212- /**
213- * @return mixed
214- */
215- public function getAlias ()
216- {
217- return $ this ->alias ;
218- }
219-
220273 /**
221274 * @param $having
222275 * @return $this
223276 */
224277 public function having ($ having )
225278 {
226- $ this ->having []= $ having ;
279+ $ this ->having [] = $ having ;
227280 return $ this ;
228281 }
229282
@@ -249,50 +302,6 @@ public function orderBy($sortBy, $sortOrder = 'DESC')
249302 return $ this ;
250303 }
251304
252- /**
253- * @return string
254- */
255- public function getSql ()
256- {
257- $ query = "\n SELECT " . implode (', ' , $ this ->fields );
258-
259- if (!empty ($ this ->from )) {
260- $ query .= "\n FROM " . $ this ->from ;
261- }
262- if (!empty ($ this ->joins )) {
263- $ query .= implode (' ' , $ this ->joins );
264- }
265- if (!empty ($ this ->leftJoins )) {
266- $ query .= implode (' ' , $ this ->leftJoins );
267- }
268- if (!empty ($ this ->where )) {
269- $ query .= "\n WHERE " . implode (" AND " , $ this ->where );
270- }
271- if (!empty ($ this ->groupBy )) {
272- $ query .= "\n GROUP BY $ this ->groupBy " ;
273- }
274- if (!empty ($ this ->having )) {
275- $ query .= "\n HAVING " . implode (" AND " , $ this ->having );
276- }
277- if (!empty ($ this ->sortBy )) {
278- $ query .= "\n ORDER BY $ this ->sortBy $ this ->sortOrder " ;
279- }
280- if ($ this ->limit ) {
281- $ query .= "\n LIMIT $ this ->limit " ;
282- if ($ this ->offset ) {
283- $ query .= " OFFSET " . $ this ->offset ;
284- }
285- }
286-
287- if (!empty ($ this ->union )) {
288- $ query .= implode (' ' , $ this ->union );
289- }
290-
291- $ query .= "\n" ;
292-
293- return $ query ;
294- }
295-
296305 /**
297306 *
298307 */
@@ -306,7 +315,11 @@ public function resetHaving()
306315 */
307316 public function resetLeftJoins ()
308317 {
309- $ this ->leftJoins = [];
318+ if (!empty ($ this ->leftJoins )) {
319+ foreach ($ this ->leftJoins as $ ord ) {
320+ unset($ this ->joins [$ ord ]);
321+ }
322+ }
310323 }
311324
312325 /**
@@ -348,14 +361,6 @@ public function addParameter($k, $v)
348361 $ this ->parameters [$ k ] = $ v ;
349362 }
350363
351- /**
352- * @return array
353- */
354- public function getParameters ()
355- {
356- return $ this ->parameters ;
357- }
358-
359364 public function dump ()
360365 {
361366 $ sql = $ this ->getSql ();
@@ -365,6 +370,14 @@ public function dump()
365370 die ("<pre> " . $ sql . "</pre> " );
366371 }
367372
373+ /**
374+ * @return array
375+ */
376+ public function getParameters ()
377+ {
378+ return $ this ->parameters ;
379+ }
380+
368381 /**
369382 * @return array
370383 */
0 commit comments