dfba / schema
在Laravel或纯PHP中读取数据库模式、表和列元数据。
0.1.1
2016-09-30 14:33 UTC
Requires
- nesbot/carbon: ~1.20
This package is not auto-updated.
Last update: 2024-09-23 12:51:45 UTC
README
...一个非常轻量级的工具。无需查找特定数据库系统的正确查询语法,即可遍历所有数据库、表和列。
composer require dfba/schema
示例
如果您正在使用Laravel,这将非常简单
将以下行添加到您的app.php
配置文件的'providers'
部分
Dfba\Schema\Laravel\SchemaServiceProvider::class,
从现在开始,您可以将Dfba\Schema\Schema
注入到您的应用程序中。例如
<?php namespace App\Http\Controllers; use Illuminate\Routing\Controller as BaseController; use Dfba\Schema\Schema; // Very crucial line class ExampleController extends BaseController { public function test(Schema $schema) { // --------------^ HERE // Some demo code: echo "<b>". $schema->getName() ."</b><br>"; foreach ($schema->getTables() as $table) { echo "__ <b>". $table->getName() ."</b><br>"; foreach ($table->getColumns() as $column) { echo "__ __ <b>". $column->getName() ."</b><br>"; echo "__ __ __ <i>dataType:</i> ". $column->getDataType() ."<br>"; echo "__ __ __ <i>unsigned:</i> ". $column->getUnsigned() ."<br>"; echo "__ __ __ <i>zerofill:</i> ". $column->getZerofill() ."<br>"; echo "__ __ __ <i>nullable:</i> ". $column->getNullable() ."<br>"; echo "__ __ __ <i>defaultValue:</i> ". $column->getDefaultValue() ."<br>"; echo "__ __ __ <i>options:</i> ". implode(', ', $column->getOptions() ?: []) ."<br>"; echo "__ __ __ <i>autoIncrement:</i> ". $column->getAutoIncrement() ."<br>"; echo "__ __ __ <i>maximumLength:</i> ". $column->getMaximumLength() ."<br>"; echo "__ __ __ <i>minimumValue:</i> ". $column->getMinimumValue() ."<br>"; echo "__ __ __ <i>maximumValue:</i> ". $column->getMaximumValue() ."<br>"; echo "__ __ __ <i>precision:</i> ". $column->getPrecision() ."<br>"; echo "__ __ __ <i>scale:</i> ". $column->getScale() ."<br>"; echo "__ __ __ <i>characterSet:</i> ". $column->getCharacterSet() ."<br>"; echo "__ __ __ <i>collation:</i> ". $column->getCollation() ."<br>"; echo "__ __ __ <i>comment:</i> ". $column->getComment() ."<br>"; } } } }
在您的代码中注入Dfba\Schema\Schema
将检索当前配置的数据库的模式。这意味着您需要设置并配置数据库 ;)
注意:您可以读取任何开放连接的数据库元数据,而不仅仅是默认的Laravel连接。只要您提供PDO连接和数据库名称,Dfba\Schema\Manager
将愉快地输出Schema
。
<?php namespace App\Http\Controllers; use Illuminate\Routing\Controller as BaseController; use Dfba\Schema\Manager; // Very crucial line class ExampleController extends BaseController { public function test(Manager $manager) { $anyRandomPdo = \DB::connection()->getReadPdo(); $someSchemaName = 'example'; $schema = $manager->getSchema($anyRandomPdo, $someSchemaName); var_dump($schema); } }
纯PHP(或其他框架)
我没有忘记你们
$schemaManager = new Dfba\Schema\Manager(); $anyRandomPdo = new PDO("mysql:host=localhost;dbname=example", "username", "password"); $someSchemaName = 'example'; $schema = $schemaManager->getSchema($anyRandomPdo, $someSchemaName); var_dump($schema);
请注意,Dfba\Schema\Manager
会缓存检索到的模式,以防止多次检索相同的数据。如果您反复创建新的Dfba\Schema\Manager
实例,您将无法从缓存中受益。
Postgres、SQL Server等?
哦,是的。目前仅实现了MySQL和SQLite数据库。不过,我已经将所有数据库特定代码提取到其自身的文件中。您想实现其他数据库吗?可以打开问题,或者最好是将src/MySqlSchemaFactory.php复制过来自己实现。这并不难!您能行! :)