@@ -51,11 +51,66 @@ public function make(
51
51
return $ key ;
52
52
}
53
53
54
+ protected function getBindingsSlug () : string
55
+ {
56
+ if (! method_exists ($ this ->model , 'query ' )) {
57
+ return '' ;
58
+ }
59
+
60
+ return Arr::query ($ this ->model ->query ()->getBindings ());
61
+ }
62
+
63
+ protected function getColumnClauses (array $ where ) : string
64
+ {
65
+ if ($ where ["type " ] !== "Column " ) {
66
+ return "" ;
67
+ }
68
+
69
+ return "- {$ where ["boolean " ]}_ {$ where ["first " ]}_ {$ where ["operator " ]}_ {$ where ["second " ]}" ;
70
+ }
71
+
72
+ protected function getCurrentBinding (string $ type , string $ bindingFallback ): string
73
+ {
74
+ return data_get ($ this ->query ->bindings , "{$ type }. {$ this ->currentBinding }" , $ bindingFallback );
75
+ }
76
+
54
77
protected function getIdColumn (string $ idColumn ) : string
55
78
{
56
79
return $ idColumn ? "_ {$ idColumn }" : "" ;
57
80
}
58
81
82
+ protected function getInAndNotInClauses (array $ where ) : string
83
+ {
84
+ if (! in_array ($ where ["type " ], ["In " , "NotIn " , "InRaw " ])) {
85
+ return "" ;
86
+ }
87
+
88
+ $ type = strtolower ($ where ["type " ]);
89
+ $ subquery = $ this ->getValuesFromWhere ($ where );
90
+ $ values = collect ($ this ->getCurrentBinding ('where ' ) ?? []);
91
+
92
+ if (Str::startsWith ($ subquery , $ values ->first ())) {
93
+ $ this ->currentBinding += count ($ where ["values " ]);
94
+ }
95
+
96
+ if (! is_numeric ($ subquery ) && ! is_numeric (str_replace ("_ " , "" , $ subquery ))) {
97
+ try {
98
+ $ subquery = Uuid::fromBytes ($ subquery );
99
+ $ values = $ this ->recursiveImplode ([$ subquery ], "_ " );
100
+
101
+ return "- {$ where ["column " ]}_ {$ type }{$ values }" ;
102
+ } catch (Exception $ exception ) {
103
+ // do nothing
104
+ }
105
+ }
106
+
107
+ $ subquery = preg_replace ('/\?(?=(?:[^"]*"[^"]*")*[^"]*\Z)/m ' , "_??_ " , $ subquery );
108
+ $ subquery = collect (vsprintf (str_replace ("_??_ " , "%s " , $ subquery ), $ values ->toArray ()));
109
+ $ values = $ this ->recursiveImplode ($ subquery ->toArray (), "_ " );
110
+
111
+ return "- {$ where ["column " ]}_ {$ type }{$ values }" ;
112
+ }
113
+
59
114
protected function getLimitClause () : string
60
115
{
61
116
if (! property_exists ($ this ->query , "limit " )
@@ -67,15 +122,18 @@ protected function getLimitClause() : string
67
122
return "-limit_ {$ this ->query ->limit }" ;
68
123
}
69
124
70
- protected function getTableSlug () : string
125
+ protected function getModelSlug () : string
71
126
{
72
- return (new Str )->slug ($ this ->query ->from )
73
- . ": " ;
127
+ return (new Str )->slug (get_class ($ this ->model ));
74
128
}
75
129
76
- protected function getModelSlug ( ) : string
130
+ protected function getNestedClauses ( array $ where ) : string
77
131
{
78
- return (new Str )->slug (get_class ($ this ->model ));
132
+ if (! in_array ($ where ["type " ], ["Exists " , "Nested " , "NotExists " ])) {
133
+ return "" ;
134
+ }
135
+
136
+ return "- " . strtolower ($ where ["type " ]) . $ this ->getWhereClauses ($ where ["query " ]->wheres );
79
137
}
80
138
81
139
protected function getOffsetClause () : string
@@ -110,6 +168,18 @@ protected function getOrderByClauses() : string
110
168
?: "" ;
111
169
}
112
170
171
+ protected function getOtherClauses (array $ where ) : string
172
+ {
173
+ if (in_array ($ where ["type " ], ["Exists " , "Nested " , "NotExists " , "Column " , "raw " , "In " , "NotIn " , "InRaw " ])) {
174
+ return "" ;
175
+ }
176
+
177
+ $ value = $ this ->getTypeClause ($ where );
178
+ $ value .= $ this ->getValuesClause ($ where );
179
+
180
+ return "- {$ where ["column " ]}_ {$ value }" ;
181
+ }
182
+
113
183
protected function getQueryColumns (array $ columns ) : string
114
184
{
115
185
if (($ columns === ["* " ]
@@ -129,6 +199,36 @@ protected function getQueryColumns(array $columns) : string
129
199
return "_ " . implode ("_ " , $ columns );
130
200
}
131
201
202
+ protected function getRawClauses (array $ where ) : string
203
+ {
204
+ if (! in_array ($ where ["type " ], ["raw " ])) {
205
+ return "" ;
206
+ }
207
+
208
+ $ queryParts = explode ("? " , $ where ["sql " ]);
209
+ $ clause = "_ {$ where ["boolean " ]}" ;
210
+
211
+ while (count ($ queryParts ) > 1 ) {
212
+ $ clause .= "_ " . array_shift ($ queryParts );
213
+ $ clause .= $ this ->getCurrentBinding ('where ' );
214
+ $ this ->currentBinding ++;
215
+ }
216
+
217
+ $ lastPart = array_shift ($ queryParts );
218
+
219
+ if ($ lastPart ) {
220
+ $ clause .= "_ " . $ lastPart ;
221
+ }
222
+
223
+ return "- " . str_replace (" " , "_ " , $ clause );
224
+ }
225
+
226
+ protected function getTableSlug () : string
227
+ {
228
+ return (new Str )->slug ($ this ->query ->from )
229
+ . ": " ;
230
+ }
231
+
132
232
protected function getTypeClause ($ where ) : string
133
233
{
134
234
$ type = in_array ($ where ["type " ], ["InRaw " , "In " , "NotIn " , "Null " , "NotNull " , "between " , "NotInSub " , "InSub " , "JsonContains " ])
@@ -174,9 +274,8 @@ protected function getValuesFromWhere(array $where) : string
174
274
175
275
protected function getValuesFromBindings (array $ where , string $ values ) : string
176
276
{
177
- // Fallback to this when the current binding does not exist in the bindings array
178
277
$ bindingFallback = __CLASS__ . ':UNKNOWN_BINDING ' ;
179
- $ currentBinding = $ this ->getCurrentBinding ('where ' ) ?? $ bindingFallback ;
278
+ $ currentBinding = $ this ->getCurrentBinding ('where ' , $ bindingFallback) ;
180
279
181
280
if ($ currentBinding !== $ bindingFallback ) {
182
281
$ values = $ currentBinding ;
@@ -211,57 +310,41 @@ protected function getWhereClauses(array $wheres = []) : string
211
310
return $ value ;
212
311
});
213
312
}
214
-
215
- protected function getNestedClauses (array $ where ) : string
313
+
314
+ protected function getWheres (array $ wheres ) : Collection
216
315
{
217
- if (! in_array ($ where ["type " ], ["Exists " , "Nested " , "NotExists " ])) {
218
- return "" ;
219
- }
220
-
221
- return "- " . strtolower ($ where ["type " ]) . $ this ->getWhereClauses ($ where ["query " ]->wheres );
222
- }
316
+ $ wheres = collect ($ wheres );
223
317
224
- protected function getColumnClauses ( array $ where ) : string
225
- {
226
- if ( $ where [ " type " ] !== " Column " ) {
227
- return "" ;
318
+ if ( $ wheres -> isEmpty ()
319
+ && property_exists ( $ this -> query , " wheres " )
320
+ ) {
321
+ $ wheres = collect ( $ this -> query -> wheres ) ;
228
322
}
229
323
230
- return " - { $ where [ " boolean " ]} _ { $ where [ " first " ]} _ { $ where [ " operator " ]} _ { $ where [ " second " ]}" ;
324
+ return $ wheres ;
231
325
}
232
326
233
- protected function getInAndNotInClauses ( array $ where ) : string
327
+ protected function getWithModels ( ) : string
234
328
{
235
- if (! in_array ($ where ["type " ], ["In " , "NotIn " , "InRaw " ])) {
236
- return "" ;
237
- }
238
-
239
- $ type = strtolower ($ where ["type " ]);
240
- $ subquery = $ this ->getValuesFromWhere ($ where );
241
- $ values = collect ($ this ->getCurrentBinding ('where ' ) ?? []);
329
+ $ eagerLoads = collect ($ this ->eagerLoad );
242
330
243
- if (Str:: startsWith ( $ subquery , $ values -> first () )) {
244
- $ this -> currentBinding += count ( $ where [ " values " ]) ;
331
+ if ($ eagerLoads -> isEmpty ( )) {
332
+ return "" ;
245
333
}
246
334
247
- if (! is_numeric ($ subquery ) && ! is_numeric (str_replace ("_ " , "" , $ subquery ))) {
248
- try {
249
- $ subquery = Uuid::fromBytes ($ subquery );
250
- $ values = $ this ->recursiveImplode ([$ subquery ], "_ " );
251
-
252
- return "- {$ where ["column " ]}_ {$ type }{$ values }" ;
253
- } catch (Exception $ exception ) {
254
- // do nothing
335
+ return $ eagerLoads ->keys ()->reduce (function ($ carry , $ related ) {
336
+ if (! method_exists ($ this ->model , $ related )) {
337
+ return "{$ carry }- {$ related }" ;
255
338
}
256
- }
257
339
258
- $ subquery = preg_replace ( ' /\?(?=(?:[^"]*"[^"]*")*[^"]*\Z)/m ' , " _??_ " , $ subquery );
259
- $ subquery = collect ( vsprintf ( str_replace ( " _??_ " , " %s " , $ subquery ), $ values -> toArray ()) );
260
- $ values = $ this -> recursiveImplode ( $ subquery -> toArray (), " _ " );
340
+ $ relatedModel = $ this -> model -> $ related ()-> getRelated ( );
341
+ $ relatedConnection = $ relatedModel -> getConnection ()-> getName ( );
342
+ $ relatedDatabase = $ relatedModel -> getConnection ()-> getDatabaseName ( );
261
343
262
- return "- {$ where ["column " ]}_ {$ type }{$ values }" ;
344
+ return "{$ carry }- {$ relatedConnection }: {$ relatedDatabase }: {$ related }" ;
345
+ });
263
346
}
264
-
347
+
265
348
protected function recursiveImplode (array $ items , string $ glue = ", " ) : string
266
349
{
267
350
$ result = "" ;
@@ -287,89 +370,4 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
287
370
288
371
return $ result ;
289
372
}
290
-
291
- protected function getRawClauses (array $ where ) : string
292
- {
293
- if (! in_array ($ where ["type " ], ["raw " ])) {
294
- return "" ;
295
- }
296
-
297
- $ queryParts = explode ("? " , $ where ["sql " ]);
298
- $ clause = "_ {$ where ["boolean " ]}" ;
299
-
300
- while (count ($ queryParts ) > 1 ) {
301
- $ clause .= "_ " . array_shift ($ queryParts );
302
- $ clause .= $ this ->getCurrentBinding ('where ' );
303
- $ this ->currentBinding ++;
304
- }
305
-
306
- $ lastPart = array_shift ($ queryParts );
307
-
308
- if ($ lastPart ) {
309
- $ clause .= "_ " . $ lastPart ;
310
- }
311
-
312
- return "- " . str_replace (" " , "_ " , $ clause );
313
- }
314
-
315
- protected function getOtherClauses (array $ where ) : string
316
- {
317
- if (in_array ($ where ["type " ], ["Exists " , "Nested " , "NotExists " , "Column " , "raw " , "In " , "NotIn " , "InRaw " ])) {
318
- return "" ;
319
- }
320
-
321
- $ value = $ this ->getTypeClause ($ where );
322
- $ value .= $ this ->getValuesClause ($ where );
323
-
324
- return "- {$ where ["column " ]}_ {$ value }" ;
325
- }
326
-
327
- protected function getWheres (array $ wheres ) : Collection
328
- {
329
- $ wheres = collect ($ wheres );
330
-
331
- if ($ wheres ->isEmpty ()
332
- && property_exists ($ this ->query , "wheres " )
333
- ) {
334
- $ wheres = collect ($ this ->query ->wheres );
335
- }
336
-
337
- return $ wheres ;
338
- }
339
-
340
- protected function getWithModels () : string
341
- {
342
- $ eagerLoads = collect ($ this ->eagerLoad );
343
-
344
- if ($ eagerLoads ->isEmpty ()) {
345
- return "" ;
346
- }
347
-
348
- return $ eagerLoads ->keys ()->reduce (function ($ carry , $ related ) {
349
- if (! method_exists ($ this ->model , $ related )) {
350
- return "{$ carry }- {$ related }" ;
351
- }
352
-
353
- $ relatedModel = $ this ->model ->$ related ()->getRelated ();
354
- $ relatedConnection = $ relatedModel ->getConnection ()->getName ();
355
- $ relatedDatabase = $ relatedModel ->getConnection ()->getDatabaseName ();
356
-
357
- return "{$ carry }- {$ relatedConnection }: {$ relatedDatabase }: {$ related }" ;
358
- });
359
- }
360
-
361
- protected function getBindingsSlug () : string
362
- {
363
- if (! method_exists ($ this ->model , 'query ' )) {
364
- return '' ;
365
- }
366
-
367
- return Arr::query ($ this ->model ->query ()->getBindings ());
368
- }
369
-
370
-
371
- private function getCurrentBinding (string $ type )
372
- {
373
- return data_get ($ this ->query ->bindings , "$ type. $ this ->currentBinding " );
374
- }
375
373
}
0 commit comments