@@ -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 , $ bindingFallback = null )
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,12 +274,15 @@ protected function getValuesFromWhere(array $where) : string
174
274
175
275
protected function getValuesFromBindings (array $ where , string $ values ) : string
176
276
{
177
- if (($ this ->query ->bindings ["where " ][$ this ->currentBinding ] ?? false ) !== false ) {
178
- $ values = $ this ->query ->bindings ["where " ][$ this ->currentBinding ];
277
+ $ bindingFallback = __CLASS__ . ':UNKNOWN_BINDING ' ;
278
+ $ currentBinding = $ this ->getCurrentBinding ("where " , $ bindingFallback );
279
+
280
+ if ($ currentBinding !== $ bindingFallback ) {
281
+ $ values = $ currentBinding ;
179
282
$ this ->currentBinding ++;
180
283
181
284
if ($ where ["type " ] === "between " ) {
182
- $ values .= "_ " . $ this ->query -> bindings [ "where " ][ $ this -> currentBinding ] ;
285
+ $ values .= "_ " . $ this ->getCurrentBinding ( "where " ) ;
183
286
$ this ->currentBinding ++;
184
287
}
185
288
}
@@ -207,57 +310,41 @@ protected function getWhereClauses(array $wheres = []) : string
207
310
return $ value ;
208
311
});
209
312
}
210
-
211
- protected function getNestedClauses (array $ where ) : string
313
+
314
+ protected function getWheres (array $ wheres ) : Collection
212
315
{
213
- if (! in_array ($ where ["type " ], ["Exists " , "Nested " , "NotExists " ])) {
214
- return "" ;
215
- }
216
-
217
- return "- " . strtolower ($ where ["type " ]) . $ this ->getWhereClauses ($ where ["query " ]->wheres );
218
- }
316
+ $ wheres = collect ($ wheres );
219
317
220
- protected function getColumnClauses ( array $ where ) : string
221
- {
222
- if ( $ where [ " type " ] !== " Column " ) {
223
- return "" ;
318
+ if ( $ wheres -> isEmpty ()
319
+ && property_exists ( $ this -> query , " wheres " )
320
+ ) {
321
+ $ wheres = collect ( $ this -> query -> wheres ) ;
224
322
}
225
323
226
- return " - { $ where [ " boolean " ]} _ { $ where [ " first " ]} _ { $ where [ " operator " ]} _ { $ where [ " second " ]}" ;
324
+ return $ wheres ;
227
325
}
228
326
229
- protected function getInAndNotInClauses ( array $ where ) : string
327
+ protected function getWithModels ( ) : string
230
328
{
231
- if (! in_array ($ where ["type " ], ["In " , "NotIn " , "InRaw " ])) {
232
- return "" ;
233
- }
234
-
235
- $ type = strtolower ($ where ["type " ]);
236
- $ subquery = $ this ->getValuesFromWhere ($ where );
237
- $ values = collect ($ this ->query ->bindings ["where " ][$ this ->currentBinding ] ?? []);
329
+ $ eagerLoads = collect ($ this ->eagerLoad );
238
330
239
- if (Str:: startsWith ( $ subquery , $ values -> first () )) {
240
- $ this -> currentBinding += count ( $ where [ " values " ]) ;
331
+ if ($ eagerLoads -> isEmpty ( )) {
332
+ return "" ;
241
333
}
242
334
243
- if (! is_numeric ($ subquery ) && ! is_numeric (str_replace ("_ " , "" , $ subquery ))) {
244
- try {
245
- $ subquery = Uuid::fromBytes ($ subquery );
246
- $ values = $ this ->recursiveImplode ([$ subquery ], "_ " );
247
-
248
- return "- {$ where ["column " ]}_ {$ type }{$ values }" ;
249
- } catch (Exception $ exception ) {
250
- // do nothing
335
+ return $ eagerLoads ->keys ()->reduce (function ($ carry , $ related ) {
336
+ if (! method_exists ($ this ->model , $ related )) {
337
+ return "{$ carry }- {$ related }" ;
251
338
}
252
- }
253
339
254
- $ subquery = preg_replace ( ' /\?(?=(?:[^"]*"[^"]*")*[^"]*\Z)/m ' , " _??_ " , $ subquery );
255
- $ subquery = collect ( vsprintf ( str_replace ( " _??_ " , " %s " , $ subquery ), $ values -> toArray ()) );
256
- $ values = $ this -> recursiveImplode ( $ subquery -> toArray (), " _ " );
340
+ $ relatedModel = $ this -> model -> $ related ()-> getRelated ( );
341
+ $ relatedConnection = $ relatedModel -> getConnection ()-> getName ( );
342
+ $ relatedDatabase = $ relatedModel -> getConnection ()-> getDatabaseName ( );
257
343
258
- return "- {$ where ["column " ]}_ {$ type }{$ values }" ;
344
+ return "{$ carry }- {$ relatedConnection }: {$ relatedDatabase }: {$ related }" ;
345
+ });
259
346
}
260
-
347
+
261
348
protected function recursiveImplode (array $ items , string $ glue = ", " ) : string
262
349
{
263
350
$ result = "" ;
@@ -283,83 +370,4 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
283
370
284
371
return $ result ;
285
372
}
286
-
287
- protected function getRawClauses (array $ where ) : string
288
- {
289
- if (! in_array ($ where ["type " ], ["raw " ])) {
290
- return "" ;
291
- }
292
-
293
- $ queryParts = explode ("? " , $ where ["sql " ]);
294
- $ clause = "_ {$ where ["boolean " ]}" ;
295
-
296
- while (count ($ queryParts ) > 1 ) {
297
- $ clause .= "_ " . array_shift ($ queryParts );
298
- $ clause .= $ this ->query ->bindings ["where " ][$ this ->currentBinding ];
299
- $ this ->currentBinding ++;
300
- }
301
-
302
- $ lastPart = array_shift ($ queryParts );
303
-
304
- if ($ lastPart ) {
305
- $ clause .= "_ " . $ lastPart ;
306
- }
307
-
308
- return "- " . str_replace (" " , "_ " , $ clause );
309
- }
310
-
311
- protected function getOtherClauses (array $ where ) : string
312
- {
313
- if (in_array ($ where ["type " ], ["Exists " , "Nested " , "NotExists " , "Column " , "raw " , "In " , "NotIn " , "InRaw " ])) {
314
- return "" ;
315
- }
316
-
317
- $ value = $ this ->getTypeClause ($ where );
318
- $ value .= $ this ->getValuesClause ($ where );
319
-
320
- return "- {$ where ["column " ]}_ {$ value }" ;
321
- }
322
-
323
- protected function getWheres (array $ wheres ) : Collection
324
- {
325
- $ wheres = collect ($ wheres );
326
-
327
- if ($ wheres ->isEmpty ()
328
- && property_exists ($ this ->query , "wheres " )
329
- ) {
330
- $ wheres = collect ($ this ->query ->wheres );
331
- }
332
-
333
- return $ wheres ;
334
- }
335
-
336
- protected function getWithModels () : string
337
- {
338
- $ eagerLoads = collect ($ this ->eagerLoad );
339
-
340
- if ($ eagerLoads ->isEmpty ()) {
341
- return "" ;
342
- }
343
-
344
- return $ eagerLoads ->keys ()->reduce (function ($ carry , $ related ) {
345
- if (! method_exists ($ this ->model , $ related )) {
346
- return "{$ carry }- {$ related }" ;
347
- }
348
-
349
- $ relatedModel = $ this ->model ->$ related ()->getRelated ();
350
- $ relatedConnection = $ relatedModel ->getConnection ()->getName ();
351
- $ relatedDatabase = $ relatedModel ->getConnection ()->getDatabaseName ();
352
-
353
- return "{$ carry }- {$ relatedConnection }: {$ relatedDatabase }: {$ related }" ;
354
- });
355
- }
356
-
357
- protected function getBindingsSlug () : string
358
- {
359
- if (! method_exists ($ this ->model , 'query ' )) {
360
- return '' ;
361
- }
362
-
363
- return Arr::query ($ this ->model ->query ()->getBindings ());
364
- }
365
373
}
0 commit comments