@@ -117,52 +117,77 @@ public function dropAllTables()
117
117
$ this ->drop ($ collection );
118
118
}
119
119
}
120
-
121
- public function getTables ()
122
- {
123
- $ db = $ this ->connection ->getMongoDB ();
124
- $ collections = [];
125
-
126
- foreach ($ db ->listCollectionNames () as $ collectionName ) {
127
- // Skip system collections
128
- if (str_starts_with ($ collectionName , 'system. ' )) {
129
- continue ;
130
- }
131
-
132
- $ stats = $ db ->selectCollection ($ collectionName )->aggregate ([
133
- ['$collStats ' => ['storageStats ' => ['scale ' => 1 ]]],
134
- ['$project ' => ['storageStats.totalSize ' => 1 ]],
135
- ])->toArray ();
136
-
137
- $ collections [] = [
138
- 'name ' => $ collectionName ,
139
- 'schema ' => null ,
140
- 'size ' => $ stats [0 ]?->storageStats?->totalSize ?? null ,
141
- 'comment ' => null ,
142
- 'collation ' => null ,
143
- 'engine ' => null ,
144
- ];
145
- }
146
-
147
- usort ($ collections , function ($ a , $ b ) {
148
- return $ a ['name ' ] <=> $ b ['name ' ];
149
- });
150
-
151
- return $ collections ;
152
- }
153
-
154
- public function getTableListing ()
155
- {
156
- $ collections = array_filter (
157
- iterator_to_array ($ this ->connection ->getMongoDB ()->listCollectionNames ()),
158
- // Skip system collections
159
- fn ($ name ) => !str_starts_with ($ name , 'system. ' )
160
- );
161
-
162
- sort ($ collections );
163
-
164
- return $ collections ;
165
- }
120
+ /** @param string|null $schema Database name */
121
+ public function getTables ($ schema = null )
122
+ {
123
+ $ db = $ this ->connection ->getDatabase ($ schema );
124
+ $ collections = [];
125
+
126
+ foreach ($ db ->listCollections () as $ collectionInfo ) {
127
+ $ collectionName = $ collectionInfo ->getName ();
128
+
129
+ // Skip system collections
130
+ if (str_starts_with ($ collectionName , 'system. ' )) {
131
+ continue ;
132
+ }
133
+ // Skip views it doesnt suport aggregate
134
+ $ isView = ($ collectionInfo ['type ' ] ?? '' ) === 'view ' ;
135
+ $ stats = null ;
136
+
137
+ if (! $ isView ) {
138
+ // Only run aggregation if it's a normal collection
139
+ $ stats = $ db ->selectCollection ($ collectionName )->aggregate ([
140
+ ['$collStats ' => ['storageStats ' => ['scale ' => 1 ]]],
141
+ ['$project ' => ['storageStats.totalSize ' => 1 ]],
142
+ ])->toArray ();
143
+ }
144
+
145
+ $ collections [] = [
146
+ 'name ' => $ collectionName ,
147
+ 'schema ' => $ db ->getDatabaseName (),
148
+ 'schema_qualified_name ' => $ db ->getDatabaseName ().'. ' .$ collectionName ,
149
+ 'size ' => $ stats [0 ]?->storageStats?->totalSize ?? null ,
150
+ 'comment ' => null ,
151
+ 'collation ' => null ,
152
+ 'engine ' => $ isView ? 'view ' : 'collection ' ,
153
+ ];
154
+ }
155
+
156
+ usort ($ collections , fn ($ a , $ b ) => $ a ['name ' ] <=> $ b ['name ' ]);
157
+
158
+ return $ collections ;
159
+ }
160
+
161
+ /**
162
+ * @param string|null $schema
163
+ * @param bool $schemaQualified If a schema is provided, prefix the collection names with the schema name
164
+ * @return array
165
+ */
166
+ public function getTableListing ($ schema = null , $ schemaQualified = false )
167
+ {
168
+ $ collections = [];
169
+
170
+ if ($ schema === null || is_string ($ schema )) {
171
+ $ collections [$ schema ?? 0 ] = iterator_to_array ($ this ->connection ->getDatabase ($ schema )->listCollectionNames ());
172
+ } elseif (is_array ($ schema )) {
173
+ foreach ($ schema as $ db ) {
174
+ $ collections [$ db ] = iterator_to_array ($ this ->connection ->getDatabase ($ db )->listCollectionNames ());
175
+ }
176
+ }
177
+
178
+ if ($ schema && $ schemaQualified ) {
179
+ $ collections = array_map (fn ($ db , $ collections ) => array_map (static fn ($ collection ) => $ db .'. ' .$ collection , $ collections ), array_keys ($ collections ), $ collections );
180
+ }
181
+
182
+ $ collections = array_merge (...array_values ($ collections ));
183
+
184
+ // Exclude system collections before sorting
185
+ $ collections = array_filter ($ collections , fn ($ name ) => ! str_starts_with ($ name , 'system. ' ));
186
+
187
+ sort ($ collections );
188
+
189
+ return $ collections ;
190
+ }
166
191
167
192
public function getColumns ($ table )
168
193
{
0 commit comments