|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Inspector\Database\Db; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
|
8
|
|
|
use Yiisoft\Db\Query\Query; |
|
9
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaInterface; |
|
10
|
|
|
use Yiisoft\Db\Schema\TableSchemaInterface; |
|
11
|
|
|
use Yiisoft\Yii\Debug\Api\Inspector\Database\SchemaProviderInterface; |
|
12
|
|
|
|
|
13
|
|
|
class DbSchemaProvider implements SchemaProviderInterface |
|
14
|
|
|
{ |
|
15
|
2 |
|
public function __construct(private ConnectionInterface $db) |
|
16
|
|
|
{ |
|
17
|
2 |
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
public function getTables(): array |
|
20
|
|
|
{ |
|
21
|
1 |
|
$quoter = $this->db->getQuoter(); |
|
22
|
|
|
/** @var TableSchemaInterface[] $tableSchemas */ |
|
23
|
1 |
|
$tableSchemas = $this->db->getSchema()->getTableSchemas(); |
|
24
|
1 |
|
$tables = []; |
|
25
|
|
|
|
|
26
|
1 |
|
foreach ($tableSchemas as $schema) { |
|
27
|
1 |
|
$tables[] = [ |
|
28
|
1 |
|
'table' => $quoter->unquoteSimpleTableName($schema->getName()), |
|
29
|
1 |
|
'primaryKeys' => $schema->getPrimaryKey(), |
|
30
|
1 |
|
'columns' => $this->serializeARColumnsSchemas($schema->getColumns()), |
|
31
|
1 |
|
'records' => (new Query($this->db))->from($schema->getName())->count(), |
|
32
|
1 |
|
]; |
|
33
|
|
|
} |
|
34
|
1 |
|
return $tables; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function getTable(string $tableName): array |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var TableSchemaInterface[] $tableSchemas */ |
|
40
|
1 |
|
$schema = $this->db->getSchema()->getTableSchema($tableName); |
|
41
|
1 |
|
$records = (new Query($this->db))->from($schema->getName())->all(); |
|
42
|
1 |
|
$data = []; |
|
43
|
|
|
|
|
44
|
|
|
// TODO: add pagination |
|
45
|
1 |
|
foreach ($records as $r => $record) { |
|
46
|
1 |
|
foreach ($record as $n => $attribute) { |
|
47
|
1 |
|
$data[$r][$n] = $attribute; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
return [ |
|
52
|
1 |
|
'table' => $schema->getName(), |
|
53
|
1 |
|
'primaryKeys' => $schema->getPrimaryKey(), |
|
54
|
1 |
|
'columns' => $this->serializeARColumnsSchemas($schema->getColumns()), |
|
55
|
1 |
|
'records' => $data, |
|
56
|
1 |
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param ColumnSchemaInterface[] $columns |
|
61
|
|
|
*/ |
|
62
|
2 |
|
private function serializeARColumnsSchemas(array $columns): array |
|
63
|
|
|
{ |
|
64
|
2 |
|
$result = []; |
|
65
|
2 |
|
foreach ($columns as $columnSchema) { |
|
66
|
2 |
|
$result[] = [ |
|
67
|
2 |
|
'name' => $columnSchema->getName(), |
|
68
|
2 |
|
'size' => $columnSchema->getSize(), |
|
69
|
2 |
|
'type' => $columnSchema->getType(), |
|
70
|
2 |
|
'dbType' => $columnSchema->getDbType(), |
|
71
|
2 |
|
'defaultValue' => $columnSchema->getDefaultValue(), |
|
72
|
2 |
|
'comment' => $columnSchema->getComment(), |
|
73
|
2 |
|
'allowNull' => $columnSchema->isAllowNull(), |
|
74
|
2 |
|
]; |
|
75
|
|
|
} |
|
76
|
2 |
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|