可溶解 / 架构
数据库信息架构
0.11.2
2017-08-25 20:49 UTC
Requires
- php: ^5.4.4 || ^7.0
- soluble/dbwrapper: ^1.3.0
- zendframework/zend-config: >=2.1.0,<3.0.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.2.0
- phpunit/phpunit: ^4.8 || ^5.4
README
简介
查询数据库架构,了解您的所有表、列、类型、外键等...
功能
- 检查、查询和发现您的数据库结构。
- 依赖于信息架构表以获取深入和准确的信息。
- 支持数据库扩展信息(原生类型、关系...)
- 快速且可靠的实现(至少尽可能快)。
需求
- PHP引擎5.4+、7.0+或HHVM >= 3.2。
支持的数据库
目前仅支持MySQL和MariaDB。
您可以通过实现Soluble\Schema\Source\SchemaSourceInterface
来创建新的架构源(oracle、postgresql...)。
请参阅贡献指南并发送pull request。
文档
安装
通过composer进行即时安装。
$ composer require soluble/schema:0.*
大多数现代框架都会默认包含Composer,但请确保以下文件已包含:
<?php // include the Composer autoloader require 'vendor/autoload.php';
示例
连接
使用有效的PDO
或mysqli
连接初始化Schema\Source\MysqlInformationSchema
。
<?php use Soluble\Schema; $conn = new \PDO("mysql:host=$hostname", $username, $password, [ \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ]); /* Alternatively, use a \mysqli connection instead of PDO */ // $conn = new \mysqli($hostname,$username,$password,$database); // $conn->set_charset($charset); $schema = new Schema\Source\MysqlInformationSchema($conn); // By default the schema (database) is taken from current connection. // If you wnat to query a different schema, set it in the second parameter. $otherDbSchema = new Schema\Source\MysqlInformationSchema($conn, 'otherDbSchema');
在数据库架构中检索表信息
<?php // Retrieve table names defined in schema $tables = $schema->getTables(); // Retrieve full information of tables defined in schema $infos = $schema->getTablesInformation(); // The resulting array looks like [ ["table_name_1"] => [ ["name"] => (string) 'Table name' ["columns"] => [ // Columns information, // @see AbstractSource::getColumnsInformation() "col name_1" => ["name" => "", "type" => "", ...]', "col name_2" => ["name" => "", "type" => "", ...]' ] ["primary_keys"] => [ // Primary key column(s) or empty "pk_col1", "pk_col2" ], ["unique_keys"] => [ // Uniques constraints or empty if none "unique_index_name_1" => ["col1", "col3"], "unique_index_name_2" => ["col4"] ], ["foreign_keys"] => [ // Foreign keys columns and their references or empty if none "col_1" => [ "referenced_table" => "Referenced table name", "referenced_column" => "Referenced column name", "constraint_name" => "Constraint name i.e. 'FK_6A2CA10CBC21F742'" ], "col_2" => [ // ... ] ], ["references"] => [ // Relations referencing this table "ref_table:ref_column->column1" => [ "column" => "Colum name in this table", "referencing_table" => "Referencing table name", "referencing_column" => "Column name in the referencing table", "constraint_name" => "Constraint name i.e. 'FK_6A2CA10CBC21F742'" ], "ref_table:ref_column->column2" => [ //... ] ] ["indexes"] => [], ["options"] => [ // Specific table creation options "comment" => (string) "Table comment", "collation" => (string) "Table collation, i.e. 'utf8_general_ci'", "type" => (string) "Table type, i.e: 'BASE TABLE'", "engine" => (string) "Engine type if applicable, i.e. 'InnoDB'", ] ], ["table_name_2"] => [ //... ] ] // Test if table exists in schema if ($schema->hasTable($table)) { //... }
获取表列信息
<?php // Retrieve just column names from a table $columns = $schema->getColumns($table); // -> ['column_name_1', 'column_name_2'] // Retrieve full columns information from a tabme $columns = $schema->getColumnsInformation($table); // resulting column array looks like -> [ ["column_name_1"] => [ ["type"] => (string) "Database type, i.e: 'char', 'int', 'bigint', 'decimal'...", ["primary"] => (boolean) "Whether column is (part of) a primary key", ["nullable"] => (boolean) "Whether column is nullable", ["default"] => (string) "Default value for column or null if none", // Specific to primary key(s) columns ["autoincrement"] => (boolean) "Whether the primary key is autoincremented" // Specific to numeric, decimal, boolean... types ["unsigned"] => (boolean) "Whether the column is unsigned", ["precision"] => (int) "Number precision (or maximum length)", // Specific to character oriented types as well as enum, blobs... ["length"] => (int) "Maximum length", ["octet_length"] => (int) "Maximum length in octets (differs from length when using multibyte charsets", // Columns specific ddl information ["options"] => [ // Column specific options "comment" => "Column comment", "definition" => "DDL definition, i.e. varchar(250)", "ordinal_position" => "Column position number", "constraint_type" => "Type of constraint if applicable", "column_key" => "", "charset" => "Column charset, i.e. 'utf8'", "collation" => "Column collation, i.e. 'utf8_unicode_ci'" ], ], ["column_name_2"] => [ //... ] ]
检索表主键
<?php // Get primary key try { $pk = $schema->getPrimaryKey($table); } catch (Schema\Exception\MultiplePrimaryKeyException $e) { //... } catch (Schema\Exception\NoPrimaryKeyException $e) { //... } // Get multiple primary keys try { $pks = $schema->getPrimaryKeys($table); } catch (Schema\Exception\NoPrimaryKeyException $e) { // ... }
检索关于唯一键的信息
<?php $uniques = $schema->getUniqueKeys($table); // The resulting array look like [ "unique_index_name_1" => [ "column_name_1", "column_name_2" ], "unique_index_name_2" => [ "column_name_1" ] ]
获取外键信息
<?php $foreign_keys = $schema->getForeignKeys($table); // The resulting array looks like [ "column_name_1" => [ "referenced_table" => "Referenced table name", "referenced_column" => "Referenced column name", "constraint_name" => "Constraint name i.e. 'FK_6A2CA10CBC21F742'" ], "column_name_2" => [ // ... ] ]
检索引用信息
<?php $references = $schema->getReferences($table); // The resulting array looks like [ "ref_table:ref_column->column1" => [ "column" => "Colum name in this table", "referencing_table" => "Referencing table name", "referencing_column" => "Column name in the referencing table", "constraint_name" => "Constaint name i.e. 'FK_6A2CA10CBC21F742'" ], "ref_table:ref_column->column2" => [ //... ] ]
API方法
一旦初始化了Schema\Source\SchemaSourceInterface
,您就可以访问以下方法
未来增强
- 支持更多来源,如postgres、oracle
- PSR-6缓存实现
贡献
欢迎贡献,请参阅贡献指南