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