generated from yiisoft/package-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add database inspection * Apply fixes from StyleCI * [ci-review] Apply changes from Rector action. * Support Cycle * Remove var * Add dependencies to dev section * Add abstraction for db * Apply fixes from StyleCI * Fix deps check Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: rector-bot <rector@yiiframework.com>
- Loading branch information
1 parent
bb8b098
commit bad803e
Showing
8 changed files
with
230 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/Inspector/Database/ActiveRecord/ActiveRecordSchemaProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Api\Inspector\ActiveRecord; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecord; | ||
use Yiisoft\ActiveRecord\ActiveRecordFactory; | ||
use Yiisoft\Db\Connection\ConnectionInterface; | ||
use Yiisoft\Db\Schema\ColumnSchemaInterface; | ||
use Yiisoft\Db\Schema\TableSchemaInterface; | ||
use Yiisoft\Yii\Debug\Api\Inspector\Database\SchemaProviderInterface; | ||
|
||
class ActiveRecordSchemaProvider implements SchemaProviderInterface | ||
{ | ||
public function __construct( | ||
private ConnectionInterface $connection, | ||
private ActiveRecordFactory $activeRecordFactory, | ||
) { | ||
} | ||
|
||
public function getTables(): array | ||
{ | ||
/** @var TableSchemaInterface[] $tableSchemas */ | ||
$tableSchemas = $this->connection->getSchema()->getTableSchemas(); | ||
|
||
$tables = []; | ||
foreach ($tableSchemas as $schema) { | ||
$activeQuery = $this->activeRecordFactory->createQueryTo(ActiveRecord::class, $schema->getName()); | ||
|
||
/** | ||
* @var ActiveRecord[] $records | ||
*/ | ||
$records = $activeQuery->count(); | ||
|
||
$tables[] = [ | ||
'table' => $schema->getName(), | ||
'primaryKeys' => $schema->getPrimaryKey(), | ||
'columns' => $this->serializeARColumnsSchemas($schema->getColumns()), | ||
'records' => $records, | ||
]; | ||
} | ||
return $tables; | ||
} | ||
|
||
public function getTable(string $tableName): array | ||
{ | ||
/** @var TableSchemaInterface[] $tableSchemas */ | ||
$schema = $this->connection->getSchema()->getTableSchema($tableName); | ||
|
||
$activeQuery = $this->activeRecordFactory->createQueryTo(ActiveRecord::class, $tableName); | ||
|
||
/** | ||
* @var ActiveRecord[] $records | ||
*/ | ||
$records = $activeQuery->all(); | ||
|
||
$data = []; | ||
// TODO: add pagination | ||
foreach ($records as $n => $record) { | ||
foreach ($record->attributes() as $attribute) { | ||
$data[$n][$attribute] = $record->{$attribute}; | ||
} | ||
} | ||
|
||
return [ | ||
'table' => $schema->getName(), | ||
'primaryKeys' => $schema->getPrimaryKey(), | ||
'columns' => $this->serializeARColumnsSchemas($schema->getColumns()), | ||
'records' => $data, | ||
]; | ||
} | ||
|
||
/** | ||
* @param ColumnSchemaInterface[] $columns | ||
*/ | ||
private function serializeARColumnsSchemas(array $columns): array | ||
{ | ||
$result = []; | ||
foreach ($columns as $columnSchema) { | ||
$result[] = [ | ||
'name' => $columnSchema->getName(), | ||
'size' => $columnSchema->getSize(), | ||
'type' => $columnSchema->getType(), | ||
'dbType' => $columnSchema->getDbType(), | ||
'defaultValue' => $columnSchema->getDefaultValue(), | ||
'comment' => $columnSchema->getComment(), | ||
'allowNull' => $columnSchema->isAllowNull(), | ||
]; | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Api\Inspector\Database\Cycle; | ||
|
||
use Cycle\Database\ColumnInterface; | ||
use Cycle\Database\DatabaseProviderInterface; | ||
use Yiisoft\Yii\Debug\Api\Inspector\Database\SchemaProviderInterface; | ||
|
||
class CycleSchemaProvider implements SchemaProviderInterface | ||
{ | ||
public function __construct(private DatabaseProviderInterface $databaseProvider) | ||
{ | ||
} | ||
|
||
public function getTables(): array | ||
{ | ||
$database = $this->databaseProvider->database(); | ||
$tableSchemas = $database->getTables(); | ||
|
||
$tables = []; | ||
foreach ($tableSchemas as $schema) { | ||
$records = $database->select()->from($schema->getName())->count(); | ||
$tables[] = [ | ||
'table' => $schema->getName(), | ||
'primaryKeys' => $schema->getPrimaryKeys(), | ||
'columns' => $this->serializeCycleColumnsSchemas($schema->getColumns()), | ||
'records' => $records, | ||
]; | ||
} | ||
|
||
return $tables; | ||
} | ||
|
||
public function getTable(string $tableName): array | ||
{ | ||
$database = $this->databaseProvider->database(); | ||
$schema = $database->table($tableName); | ||
|
||
// TODO: add pagination | ||
$records = $database->select()->from($tableName)->fetchAll(); | ||
|
||
return [ | ||
'table' => $schema->getName(), | ||
'primaryKeys' => $schema->getPrimaryKeys(), | ||
'columns' => $this->serializeCycleColumnsSchemas($schema->getColumns()), | ||
'records' => $records, | ||
]; | ||
} | ||
|
||
/** | ||
* @param ColumnInterface[] $columns | ||
*/ | ||
private function serializeCycleColumnsSchemas(array $columns): array | ||
{ | ||
$result = []; | ||
foreach ($columns as $columnSchema) { | ||
$result[] = [ | ||
'name' => $columnSchema->getName(), | ||
'size' => $columnSchema->getSize(), | ||
'type' => $columnSchema->getInternalType(), | ||
'dbType' => $columnSchema->getType(), | ||
'defaultValue' => $columnSchema->getDefaultValue(), | ||
'comment' => null, // unsupported for now | ||
'allowNull' => $columnSchema->isNullable(), | ||
]; | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Api\Inspector\Database; | ||
|
||
interface SchemaProviderInterface | ||
{ | ||
public function getTables(): array; | ||
|
||
public function getTable(string $tableName): array; | ||
} |