|
11 | 11 |
|
12 | 12 | namespace Cycle\Database; |
13 | 13 |
|
| 14 | +use Cycle\Database\Driver\BulkSchemaProviderInterface; |
14 | 15 | use Cycle\Database\Driver\Driver; |
15 | 16 | use Cycle\Database\Driver\DriverInterface; |
16 | 17 | use Cycle\Database\Driver\CursorInterface; |
17 | 18 | use Cycle\Database\Driver\CursorOptions; |
18 | 19 | use Cycle\Database\Exception\DriverException; |
| 20 | +use Cycle\Database\Schema\AbstractTable; |
19 | 21 | use Cycle\Database\Query\DeleteQuery; |
20 | 22 | use Cycle\Database\Query\InsertQuery; |
21 | 23 | use Cycle\Database\Query\QueryParameters; |
@@ -117,6 +119,43 @@ public function table(string $name): Table |
117 | 119 | return new Table($this, $name); |
118 | 120 | } |
119 | 121 |
|
| 122 | + /** |
| 123 | + * Introspect several tables at once. When the driver supports batched introspection (currently |
| 124 | + * Postgres and SQL Server) this costs a constant number of queries instead of a full |
| 125 | + * introspection per table; otherwise it falls back to introspecting each table on its own. The |
| 126 | + * observable result is identical to calling {@see Table::getSchema()} for each table. |
| 127 | + * |
| 128 | + * @param non-empty-string[]|null $tables Table names WITHOUT the database prefix. When `null`, |
| 129 | + * every table of the database is introspected (names resolved via the driver, then fed |
| 130 | + * into the batched path — this is the explicit "whole database" entry point). |
| 131 | + * |
| 132 | + * @return array<non-empty-string, AbstractTable> Keyed by the table name. |
| 133 | + */ |
| 134 | + public function getSchemas(?array $tables = null): array |
| 135 | + { |
| 136 | + $handler = $this->getDriver(self::READ)->getSchemaHandler(); |
| 137 | + |
| 138 | + if ($tables === null) { |
| 139 | + $tables = []; |
| 140 | + foreach ($handler->getTableNames($this->prefix) as $table) { |
| 141 | + $tables[] = \str_contains($table, '.') |
| 142 | + ? \str_replace('.' . $this->prefix, '.', $table) |
| 143 | + : \substr($table, \strlen($this->prefix)); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if ($handler instanceof BulkSchemaProviderInterface) { |
| 148 | + return $handler->getSchemas($tables, $this->prefix); |
| 149 | + } |
| 150 | + |
| 151 | + $result = []; |
| 152 | + foreach ($tables as $table) { |
| 153 | + $result[$table] = $handler->getSchema($table, $this->prefix); |
| 154 | + } |
| 155 | + |
| 156 | + return $result; |
| 157 | + } |
| 158 | + |
120 | 159 | /** |
121 | 160 | * @psalm-param non-empty-string $query |
122 | 161 | */ |
|
0 commit comments