@@ -51,21 +51,27 @@ public function transaction(Closure $callback, $attempts = 1)
5151 if ($ this ->getDriverName () === 'sqlsrv ' ) {
5252 return parent ::transaction ($ callback );
5353 }
54+
5455 $ this ->pdo ->exec ('BEGIN TRAN ' );
56+
5557 // We'll simply execute the given callback within a try / catch block
5658 // and if we catch any exception we can rollback the transaction
5759 // so that none of the changes are persisted to the database.
5860 try {
5961 $ result = $ callback ($ this );
62+
6063 $ this ->pdo ->exec ('COMMIT TRAN ' );
6164 }
65+
6266 // If we catch an exception, we will roll back so nothing gets messed
6367 // up in the database. Then we'll re-throw the exception so it can
6468 // be handled how the developer sees fit for their applications.
6569 catch (Exception $ e ) {
6670 $ this ->pdo ->exec ('ROLLBACK TRAN ' );
71+
6772 throw $ e ;
6873 }
74+
6975 return $ result ;
7076 }
7177
@@ -118,30 +124,39 @@ protected function getDoctrineDriver()
118124 */
119125 private function compileForSelect (Builder $ builder , $ bindings ) {
120126 $ arrTables = [];
127+
121128 array_push ($ arrTables , $ builder ->from );
129+
122130 if (!empty ($ builder ->joins )) {
123131 foreach ($ builder ->joins as $ join ) {
124132 array_push ($ arrTables , $ join ->table );
125133 }
126134 }
135+
127136 $ newFormat = [];
137+
128138 foreach ($ arrTables as $ tables ) {
129139 preg_match (
130140 "/(?:(?'table'.*)(?: as )(?'alias'.*))|(?'tables'.*)/ " ,
131141 $ tables ,
132142 $ alias
133143 );
144+
134145 if (empty ($ alias ['alias ' ])) {
135146 $ tables = $ alias ['tables ' ];
136147 } else {
137148 $ tables = $ alias ['table ' ];
138149 }
150+
139151 $ queryString = $ this ->queryStringForSelect ($ tables );
152+
140153 $ queryRes = $ this ->getPdo ()->query ($ queryString );
154+
141155 $ types [$ tables ] = $ queryRes ->fetchAll (PDO ::FETCH_NAMED );
142156
143157 foreach ($ types [$ tables ] as &$ row ) {
144158 $ tipos [strtolower ($ row ['name ' ])] = $ row ['type ' ];
159+
145160 $ tipos [strtolower ($ tables . '. ' . $ row ['name ' ])] = $ row ['type ' ];
146161
147162 if (!empty ($ alias ['alias ' ])) {
@@ -165,6 +180,7 @@ private function compileForSelect(Builder $builder, $bindings) {
165180 }
166181
167182 $ i = 0 ;
183+
168184 $ wheresCount = count ($ wheres );
169185
170186 for ($ ind = 0 ; $ ind < $ wheresCount ; $ ind ++) {
@@ -189,6 +205,7 @@ private function compileForSelect(Builder $builder, $bindings) {
189205 } else {
190206 $ newBinds [$ i ] = (string ) $ bindings [$ i ];
191207 }
208+
192209 $ i ++;
193210 }
194211 }
@@ -198,7 +215,9 @@ private function compileForSelect(Builder $builder, $bindings) {
198215 }
199216
200217 $ wheres = (array ) $ builder ->wheres ;
218+
201219 $ i = 0 ;
220+
202221 $ wheresCount = count ($ wheres );
203222
204223 for ($ ind = 0 ; $ ind < $ wheresCount ; $ ind ++) {
@@ -220,6 +239,7 @@ private function compileForSelect(Builder $builder, $bindings) {
220239 } else {
221240 $ newBinds [$ i ] = (string ) $ bindings [$ i ];
222241 }
242+
223243 $ i ++;
224244 }
225245 }
@@ -237,6 +257,7 @@ private function compileForSelect(Builder $builder, $bindings) {
237257 private function queryStringForSelect ($ tables )
238258 {
239259 $ explicitDB = explode ('.. ' , $ tables );
260+
240261 if (isset ($ explicitDB [1 ])) {
241262 return "
242263 SELECT
@@ -304,6 +325,7 @@ private function compileBindings($query, $bindings)
304325 }
305326
306327 $ bindings = $ this ->prepareBindings ($ bindings );
328+
307329 $ newFormat = [];
308330
309331 switch (explode (' ' , $ query )[0 ]) {
@@ -348,22 +370,29 @@ private function compileBindings($query, $bindings)
348370 if (is_array ($ desQuery ['tables ' ])) {
349371 $ desQuery ['tables ' ] = implode ($ desQuery ['tables ' ], ' ' );
350372 }
373+
351374 if (is_array ($ desQuery ['attributes ' ])) {
352375 $ desQuery ['attributes ' ] = implode ($ desQuery ['attributes ' ], ' ' );
353376 }
354377
355378 unset($ matches );
379+
356380 unset($ queryType );
381+
357382 preg_match_all ("/\[([^\]]*)\]/ " , $ desQuery ['attributes ' ], $ arrQuery );
383+
358384 preg_match_all (
359385 "/\[([^\]]*)\]/ " ,
360386 str_replace ("].[].[ " , '.. ' , $ desQuery ['tables ' ]),
361387 $ arrTables
362388 );
363389
364390 $ arrQuery = $ arrQuery [1 ];
391+
365392 $ arrTables = $ arrTables [1 ];
393+
366394 $ ind = 0 ;
395+
367396 $ numTables = count ($ arrTables );
368397
369398 if ($ numTables == 1 ) {
@@ -379,17 +408,22 @@ private function compileBindings($query, $bindings)
379408 if ($ numTables > 1 ) {
380409 $ table = $ campos ;
381410 }
411+
382412 if (!array_key_exists ($ table , $ newFormat )) {
383413 $ queryRes = $ this ->getPdo ()->query (
384414 $ this ->queryStringForCompileBindings ($ table )
385415 );
416+
386417 $ types [$ table ] = $ queryRes ->fetchAll (PDO ::FETCH_ASSOC );
418+
387419 for ($ k = 0 ; $ k < count ($ types [$ table ]); $ k ++) {
388420 $ types [$ table ][
389421 $ types [$ table ][$ k ]['name ' ]
390422 ] = $ types [$ table ][$ k ];
423+
391424 unset($ types [$ table ][$ k ]);
392425 }
426+
393427 $ newFormat [$ table ] = [];
394428 }
395429 }
@@ -402,6 +436,7 @@ private function compileBindings($query, $bindings)
402436 'binding ' => $ ind
403437 ]
404438 );
439+
405440 if (
406441 in_array (
407442 strtolower ($ types [$ table ][$ campos ]['type ' ]),
@@ -419,6 +454,7 @@ private function compileBindings($query, $bindings)
419454 } else {
420455 array_push ($ newFormat [$ table ], ['campo ' => $ campos ]);
421456 }
457+
422458 $ ind ++;
423459 }
424460 }
@@ -435,6 +471,7 @@ private function compileBindings($query, $bindings)
435471 private function queryStringForCompileBindings ($ table )
436472 {
437473 $ explicitDB = explode ('.. ' , $ table );
474+
438475 if (isset ($ explicitDB [1 ])) {
439476 return "
440477 SELECT
@@ -503,13 +540,18 @@ private function queryStringForCompileBindings($table)
503540 private function compileNewQuery ($ query , $ bindings )
504541 {
505542 $ newQuery = '' ;
543+
506544 $ bindings = $ this ->compileBindings ($ query , $ bindings );
545+
507546 $ partQuery = explode ("? " , $ query );
547+
508548 for ($ i = 0 ; $ i < count ($ partQuery ); $ i ++) {
509549 $ newQuery .= $ partQuery [$ i ];
550+
510551 if ($ i < count ($ bindings )) {
511552 if (is_string ($ bindings [$ i ])) {
512553 $ bindings [$ i ] = str_replace ("' " , "'' " , $ bindings [$ i ]);
554+
513555 $ newQuery .= "' " . $ bindings [$ i ] . "' " ;
514556 } else {
515557 if (!is_null ($ bindings [$ i ])) {
@@ -520,7 +562,9 @@ private function compileNewQuery($query, $bindings)
520562 }
521563 }
522564 }
565+
523566 $ newQuery = str_replace ( '[] ' , '' , $ newQuery );
567+
524568 return $ newQuery ;
525569 }
526570
@@ -536,38 +580,50 @@ private function compileNewQuery($query, $bindings)
536580 public function compileOffset ($ offset , $ query , $ bindings = [], $ me )
537581 {
538582 $ limit = $ this ->queryGrammar ->getBuilder ()->limit ;
583+
539584 $ from = explode (' ' , $ this ->queryGrammar ->getBuilder ()->from )[0 ];
585+
540586 if (!isset ($ limit )) {
541587 $ limit = 999999999999999999999999999 ;
542588 }
589+
543590 $ queryString = $ this ->queryStringForIdentity ($ from );
591+
544592 $ identity = $ this ->getPdo ()->query ($ queryString )->fetchAll (
545593 $ me ->getFetchMode ()
546594 )[0 ];
547595
548596 if (count ((array )$ identity ) === 0 ) {
549597 $ queryString = $ this ->queryStringForPrimaries ($ from );
598+
550599 $ primaries = $ this ->getPdo ()->query ($ queryString )->fetchAll (
551600 $ me ->getFetchMode ()
552601 );
602+
553603 foreach ($ primaries as $ primary ) {
554604 $ newArr [] = $ primary ->primary_key . '+0 AS ' .
555605 $ primary ->primary_key ;
606+
556607 $ whereArr [] = "#tmpPaginate. " . $ primary ->primary_key .
557608 ' = #tmpTable. ' . $ primary ->primary_key ;
558609 }
610+
559611 $ resPrimaries = implode (', ' , $ newArr );
612+
560613 $ wherePrimaries = implode (' AND ' , $ whereArr );
561614 } else {
562615 $ resPrimaries = $ identity ->column . '+0 AS ' . $ identity ->column ;
616+
563617 $ wherePrimaries = "#tmpPaginate. " . $ identity ->column .
564618 ' = #tmpTable. ' . $ identity ->column ;
619+
565620 // Offset operation
566621 $ this ->getPdo ()->query (str_replace (
567622 ' from ' ,
568623 " into #tmpPaginate from " ,
569624 $ this ->compileNewQuery ($ query , $ bindings )
570625 ));
626+
571627 $ this ->getPdo ()->query ("
572628 SELECT
573629 " . $ resPrimaries . ",
@@ -576,6 +632,7 @@ public function compileOffset($offset, $query, $bindings = [], $me)
576632 #tmpTable
577633 FROM
578634 #tmpPaginate " );
635+
579636 return $ this ->getPdo ()->query ("
580637 SELECT
581638 #tmpPaginate.*,
@@ -603,6 +660,7 @@ public function compileOffset($offset, $query, $bindings = [], $me)
603660 private function queryStringForIdentity ($ from )
604661 {
605662 $ explicitDB = explode ('.. ' , $ from );
663+
606664 if (isset ($ explicitDB [1 ])) {
607665 return "
608666 SELECT
@@ -637,6 +695,7 @@ private function queryStringForIdentity($from)
637695 private function queryStringForPrimaries ($ from )
638696 {
639697 $ explicitDB = explode ('.. ' , $ from );
698+
640699 if (isset ($ explicitDB [1 ])) {
641700 return "
642701 SELECT
@@ -698,13 +757,16 @@ public function select($query, $bindings = [], $useReadPdo = true)
698757 return $ this ->compileOffset ($ offset , $ query , $ bindings , $ this );
699758 } else {
700759 $ result = [];
760+
701761 $ statement = $ this ->getPdo ()->query ($ this ->compileNewQuery (
702762 $ query ,
703763 $ bindings
704764 ));
765+
705766 do {
706767 $ result += $ statement ->fetchAll ($ this ->getFetchMode ());
707768 } while ($ statement ->nextRowset ());
769+
708770 return $ result ;
709771 }
710772 });
@@ -723,6 +785,7 @@ public function statement($query, $bindings = [])
723785 if ($ this ->pretending ()) {
724786 return true ;
725787 }
788+
726789 return $ this ->getPdo ()->query ($ this ->compileNewQuery (
727790 $ query ,
728791 $ bindings
@@ -743,6 +806,7 @@ public function affectingStatement($query, $bindings = [])
743806 if ($ this ->pretending ()) {
744807 return 0 ;
745808 }
809+
746810 return $ this ->getPdo ()->query ($ this ->compileNewQuery (
747811 $ query ,
748812 $ bindings
@@ -770,10 +834,13 @@ public function getSchemaBuilder()
770834 if (is_null ($ this ->schemaGrammar )) {
771835 $ this ->useDefaultSchemaGrammar ();
772836 }
837+
773838 $ builder = new Builder ($ this );
839+
774840 $ builder ->blueprintResolver (function ($ table , $ callback ) {
775841 return new Blueprint ($ table , $ callback );
776842 });
843+
777844 return $ builder ;
778845 }
779846}
0 commit comments