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 ) {
@@ -150,7 +156,8 @@ public function getTables()
150
156
151
157
$ collections [] = [
152
158
'name ' => $ collectionName ,
153
- 'schema ' => null ,
159
+ 'schema ' => $ db ->getDatabaseName (),
160
+ 'schema_qualified_name ' => $ db ->getDatabaseName () . '. ' . $ collectionName ,
154
161
'size ' => $ stats [0 ]?->storageStats?->totalSize ?? null ,
155
162
'comment ' => null ,
156
163
'collation ' => null ,
@@ -165,9 +172,29 @@ public function getTables()
165
172
return $ collections ;
166
173
}
167
174
168
- public function getTableListing ()
175
+ /**
176
+ * @param string|null $schema
177
+ * @param bool $schemaQualified If a schema is provided, prefix the collection names with the schema name
178
+ *
179
+ * @return array
180
+ */
181
+ public function getTableListing ($ schema = null , $ schemaQualified = false )
169
182
{
170
- $ collections = iterator_to_array ($ this ->connection ->getDatabase ()->listCollectionNames ());
183
+ $ collections = [];
184
+
185
+ if ($ schema === null || is_string ($ schema )) {
186
+ $ collections [$ schema ?? 0 ] = iterator_to_array ($ this ->connection ->getDatabase ($ schema )->listCollectionNames ());
187
+ } elseif (is_array ($ schema )) {
188
+ foreach ($ schema as $ db ) {
189
+ $ collections [$ db ] = iterator_to_array ($ this ->connection ->getDatabase ($ db )->listCollectionNames ());
190
+ }
191
+ }
192
+
193
+ if ($ schema && $ schemaQualified ) {
194
+ $ collections = array_map (fn ($ db , $ collections ) => array_map (static fn ($ collection ) => $ db . '. ' . $ collection , $ collections ), array_keys ($ collections ), $ collections );
195
+ }
196
+
197
+ $ collections = array_merge (...array_values ($ collections ));
171
198
172
199
sort ($ collections );
173
200
0 commit comments