4
4
5
5
namespace Tpetry \PostgresqlEnhanced \Support \Helpers ;
6
6
7
- use Illuminate \Database \Connection ;
8
7
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
9
8
use Illuminate \Database \Query \Builder as QueryBuilder ;
10
- use RuntimeException ;
11
9
12
10
class Query
13
11
{
@@ -20,29 +18,8 @@ public static function toSql(EloquentBuilder|QueryBuilder|string $query): string
20
18
return $ query ;
21
19
}
22
20
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
+ );
47
24
}
48
25
}
0 commit comments