milesasylum/schnoop

Schnoop 提供了一个方便的 PHP 接口,用于检查数据库架构。

v0.4.2 2024-08-26 03:18 UTC

README

Tests Coverage Status Latest Stable Version Total Downloads License

Schnoop 提供了一个方便的 PHP 接口,用于检查 MySQL 数据库架构并生成架构的 DDL 语句。

它旨在帮助开发环境中的代码生成。

免责声明:不建议在生产环境中使用此包。

示例

构建 Schnoop 对象

要构建 Schnoop 对象,首先建立与数据库服务器的 PDO 连接,然后将连接提供给 SchnoopFactory::create()

<?php
use \MilesAsylum\Schnoop;

$conn = new \PDO('mysql:host=localhost', 'root');
$schnoop = Schnoop::createSelf($conn);

获取数据库列表

<?php
// ...

$databaseList = $schnoop->getDatabaseList;

print_r($databaseList);

检查数据库

<?php
// ...

$databaseName = 'acme_db';

if ($schnoop->hasDatabase($databaseName)) {
    $database = $schnoop->getDatabase($databaseName);
    
    echo $database->getName(); // acme_database
    echo $database->getDefaultCollation(); // I.e. utf8mb4_general_ci
    print_r($database->getTableList()); // Array of table names.
} else {
    echo "A database named, $databaseName, cannot be found on the server.";
}

检查表

<?php
// ...

$tableName = 'acme_tbl';

if ($database->hasTable($tableName){
    $table = $database->getTable($tableName);
    
    echo $table->getName(); // acme_tbl
    echo $table->getEngine(); // I.e. InnoDB
    echo $table->getDefaultCollation(); // I.e. utf8mb_general_ci
    echo $table->getRowFormat(); // I.e. dynamic
    print_r($table->getColumnList()); // Array of column names;
    print_r($table->getIndexList()); // Array of index names;
} else {
    echo "A table named, $tableName, cannot be found in {$database->getName()}";
}

检查列

<?php
// ...

$columnName = 'acme_col';

if ($table->hasColumn($columnName) {
    $column = $table->getColumn($columnName);
    
    echo $column->getName(); // The name of the column.
    echo $column->getDefault(); // I.e. The default value of the column.
    var_export($column->isNullable()); // true == NULL, false == NOT NULL.
} else {
    echo "A column named, $columnName, does not exists for table {$table->getName()}.";
}

检查列数据类型

<?php
// ...

$dataType = $column->getDataType();

echo $dataType->getType(); // INT, VARCHAR, TEXT or BLOB, etc.

检查表索引

<?php
// ...

$indexName = 'PRIMARY KEY';

if ($table->hasIndex($indexName)) {
    $index = $table->getIndex($indexName);
    
    echo $index->getConstraintType(); // index, unique, fulltext or spatial.
    echo $index->getIndexType(); // hash, btree, rtree or fulltext.
    
    foreach ($index->getIndexedColumns as $indexedColumn) {
        echo $indexedColumn->getColumnName(); // The name of the column in the index.
        echo $indexedColumn->getLength(); // The index prefix length.
        echo $indexedColumn->getCollation(); // The collation (i.e. Asc) of the index on the column.
    }
}

检查表触发器

<?php
// ...

$triggers = $table->getTriggers();

foreach ($triggers as $trigger)
{
    echo $trigger->getName(); // The trigger name.
    echo $trigger->getEvent(); // INSERT, UPDATE or DELETE.
    echo $trigger->getTiming(); // BEFORE or AFTER.
    echo $trigger->getBody(); // The trigger logic.     
}

待办事项

  • 添加方法 DataTypeFactoryInterface::getTypeName(),用于与 DataTypeFactory::addHandler() 一起使用。
  • 引入了一个存储库,以防止构建重复的资源。
  • 添加对视图的支持。