77use Closure ;
88use MongoDB \Collection ;
99use MongoDB \Driver \Exception \ServerException ;
10+ use MongoDB \Laravel \Connection ;
1011use MongoDB \Model \CollectionInfo ;
1112use MongoDB \Model \IndexInfo ;
1213
1617use function array_keys ;
1718use function array_map ;
1819use function array_merge ;
20+ use function array_values ;
1921use function assert ;
2022use function count ;
2123use function current ;
2224use function implode ;
2325use function in_array ;
26+ use function is_array ;
27+ use function is_string ;
2428use function iterator_to_array ;
2529use function sort ;
2630use function sprintf ;
2731use function str_ends_with ;
2832use function substr ;
2933use function usort ;
3034
35+ /** @property Connection $connection */
3136class Builder extends \Illuminate \Database \Schema \Builder
3237{
3338 /**
@@ -137,9 +142,10 @@ public function dropAllTables()
137142 }
138143 }
139144
140- public function getTables ()
145+ /** @param string|null $schema Database name */
146+ public function getTables ($ schema = null )
141147 {
142- $ db = $ this ->connection ->getDatabase ();
148+ $ db = $ this ->connection ->getDatabase ($ schema );
143149 $ collections = [];
144150
145151 foreach ($ db ->listCollectionNames () as $ collectionName ) {
@@ -150,7 +156,8 @@ public function getTables()
150156
151157 $ collections [] = [
152158 'name ' => $ collectionName ,
153- 'schema ' => null ,
159+ 'schema ' => $ db ->getDatabaseName (),
160+ 'schema_qualified_name ' => $ db ->getDatabaseName () . '. ' . $ collectionName ,
154161 'size ' => $ stats [0 ]?->storageStats?->totalSize ?? null ,
155162 'comment ' => null ,
156163 'collation ' => null ,
@@ -165,9 +172,29 @@ public function getTables()
165172 return $ collections ;
166173 }
167174
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 )
169182 {
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 ));
171198
172199 sort ($ collections );
173200
0 commit comments