7
7
use Closure ;
8
8
use MongoDB \Collection ;
9
9
use MongoDB \Driver \Exception \ServerException ;
10
+ use MongoDB \Laravel \Connection ;
10
11
use MongoDB \Model \CollectionInfo ;
11
12
use MongoDB \Model \IndexInfo ;
12
13
16
17
use function array_keys ;
17
18
use function array_map ;
18
19
use function array_merge ;
20
+ use function array_values ;
19
21
use function assert ;
20
22
use function count ;
21
23
use function current ;
22
24
use function implode ;
23
25
use function in_array ;
26
+ use function is_array ;
27
+ use function is_string ;
24
28
use function iterator_to_array ;
25
29
use function sort ;
26
30
use function sprintf ;
27
31
use function str_ends_with ;
28
32
use function substr ;
29
33
use function usort ;
30
34
35
+ /** @property Connection $connection */
31
36
class Builder extends \Illuminate \Database \Schema \Builder
32
37
{
33
38
/**
@@ -137,9 +142,10 @@ public function dropAllTables()
137
142
}
138
143
}
139
144
140
- public function getTables ()
145
+ /** @param string|null $schema Database name */
146
+ public function getTables ($ schema = null )
141
147
{
142
- $ db = $ this ->connection ->getDatabase ();
148
+ $ db = $ this ->connection ->getDatabase ($ schema );
143
149
$ collections = [];
144
150
145
151
foreach ($ db ->listCollectionNames () as $ collectionName ) {
@@ -165,9 +171,23 @@ public function getTables()
165
171
return $ collections ;
166
172
}
167
173
168
- public function getTableListing ()
174
+ public function getTableListing ($ schema = null , $ schemaQualified = true )
169
175
{
170
- $ collections = iterator_to_array ($ this ->connection ->getDatabase ()->listCollectionNames ());
176
+ $ collections = [];
177
+
178
+ if ($ schema === null || is_string ($ schema )) {
179
+ $ collections [$ schema ?? '' ] = iterator_to_array ($ this ->connection ->getDatabase ($ schema )->listCollectionNames ());
180
+ } elseif (is_array ($ schema )) {
181
+ foreach ($ schema as $ db ) {
182
+ $ collections [$ db ] = iterator_to_array ($ this ->connection ->getDatabase ($ db )->listCollectionNames ());
183
+ }
184
+ }
185
+
186
+ if ($ schema && $ schemaQualified ) {
187
+ $ collections = array_map (fn ($ db , $ collections ) => array_map (static fn ($ collection ) => $ db . '. ' . $ collection , $ collections ), array_keys ($ collections ), $ collections );
188
+ }
189
+
190
+ $ collections = array_merge (...array_values ($ collections ));
171
191
172
192
sort ($ collections );
173
193
0 commit comments