44
55namespace Tpetry \PostgresqlEnhanced \Support \Helpers ;
66
7- use Illuminate \Database \Connection ;
87use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
98use Illuminate \Database \Query \Builder as QueryBuilder ;
10- use RuntimeException ;
119
1210class Query
1311{
@@ -20,29 +18,8 @@ public static function toSql(EloquentBuilder|QueryBuilder|string $query): string
2018 return $ query ;
2119 }
2220
23- /** @var Connection $connection */
24- $ connection = $ query ->getConnection ();
25-
26- $ bindings = $ connection ->prepareBindings ($ query ->getBindings ());
27- $ sql = preg_replace_callback ('/(?<!\?)\?(?!\?)/ ' , function () use (&$ bindings , $ connection ) {
28- if (0 === \count ($ bindings )) {
29- throw new RuntimeException ('Number of bindings does not match the number of placeholders ' );
30- }
31-
32- $ value = array_shift ($ bindings );
33-
34- return (string ) match (true ) {
35- null === $ value => 'null ' ,
36- \is_bool ($ value ) => $ value ? 'true ' : 'false ' ,
37- is_numeric ($ value ) => $ value ,
38- default => $ connection ->getPdo ()->quote ((string ) $ value ),
39- };
40- }, $ query ->toSql ());
41-
42- if (\count ($ bindings ) > 0 ) {
43- throw new RuntimeException ('Number of bindings does not match the number of placeholders ' );
44- }
45-
46- return $ sql ;
21+ return $ query ->getGrammar ()->substituteBindingsIntoRawSql (
22+ $ query ->toSql (), $ query ->getConnection ()->prepareBindings ($ query ->getBindings ())
23+ );
4724 }
4825}
0 commit comments