@@ -61,9 +61,9 @@ public function fetchFirst(mixed ...$bindings): ?array
6161 }
6262
6363 /**
64- * Returns the SQL statement without the bindings.
64+ * Compile the query to a SQL statement without the bindings.
6565 */
66- public function toSql (): ImmutableString
66+ public function compile (): ImmutableString
6767 {
6868 $ sql = $ this ->sql ;
6969 $ dialect = $ this ->dialect ;
@@ -84,87 +84,7 @@ public function toSql(): ImmutableString
8484 */
8585 public function toRawSql (): ImmutableString
8686 {
87- $ sql = $ this ->toSql ();
88- $ resolvedBindings = $ this ->resolveBindingsForDisplay ();
89-
90- if (! array_is_list ($ resolvedBindings )) {
91- return $ this ->replaceNamedBindings ((string ) $ sql , $ resolvedBindings );
92- }
93-
94- return $ this ->replacePositionalBindings ((string ) $ sql , array_values ($ resolvedBindings ));
95- }
96-
97- private function replaceNamedBindings (string $ sql , array $ bindings ): ImmutableString
98- {
99- foreach ($ bindings as $ key => $ value ) {
100- $ placeholder = ': ' . $ key ;
101- $ formattedValue = $ this ->formatValueForSql ($ value );
102- $ sql = str_replace ($ placeholder , $ formattedValue , $ sql );
103- }
104-
105- return new ImmutableString ($ sql );
106- }
107-
108- private function replacePositionalBindings (string $ sql , array $ bindings ): ImmutableString
109- {
110- $ bindingIndex = 0 ;
111- $ result = '' ;
112- $ length = strlen ($ sql );
113-
114- for ($ i = 0 ; $ i < $ length ; $ i ++) {
115- if ($ sql [$ i ] === '? ' && $ bindingIndex < count ($ bindings )) {
116- $ value = $ bindings [$ bindingIndex ];
117- $ result .= $ this ->formatValueForSql ($ value );
118- $ bindingIndex ++;
119- } else {
120- $ result .= $ sql [$ i ];
121- }
122- }
123-
124- return new ImmutableString ($ result );
125- }
126-
127- private function resolveBindingsForDisplay (): array
128- {
129- $ bindings = [];
130-
131- foreach ($ this ->bindings as $ key => $ value ) {
132- if (is_bool ($ value )) {
133- $ value = match ($ this ->dialect ) {
134- DatabaseDialect::POSTGRESQL => $ value ? 'true ' : 'false ' ,
135- default => $ value ? '1 ' : '0 ' ,
136- };
137- }
138-
139- if ($ value instanceof Query) {
140- $ value = '( ' . $ value ->toRawSql () . ') ' ;
141- }
142-
143- $ bindings [$ key ] = $ value ;
144- }
145-
146- return $ bindings ;
147- }
148-
149- private function formatValueForSql (mixed $ value ): string
150- {
151- if ($ value === null ) {
152- return 'NULL ' ;
153- }
154-
155- if (is_string ($ value )) {
156- if (str_starts_with ($ value , '( ' ) && str_ends_with ($ value , ') ' )) {
157- return $ value ;
158- }
159-
160- return "' " . str_replace ("' " , "'' " , $ value ) . "' " ;
161- }
162-
163- if (is_numeric ($ value )) {
164- return (string ) $ value ;
165- }
166-
167- return "' " . str_replace ("' " , "'' " , (string ) $ value ) . "' " ;
87+ return new RawSql ($ this ->dialect , (string ) $ this ->compile (), $ this ->bindings )->toImmutableString ();
16888 }
16989
17090 public function append (string $ append ): self
0 commit comments