1
0
mirror of https://github.com/dg/dibi.git synced 2025-01-17 06:08:19 +01:00
php-dibi/examples/database-reflection.php

55 lines
1.2 KiB
PHP
Raw Normal View History

2017-07-21 21:34:37 +02:00
<?php
declare(strict_types=1);
?>
2010-08-03 21:06:56 +02:00
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
2018-05-10 21:45:11 +02:00
<h1>Database Reflection | Dibi</h1>
2010-08-03 21:06:56 +02:00
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
2010-08-03 21:06:56 +02:00
2018-04-17 10:03:43 +02:00
$dibi = new Dibi\Connection([
'driver' => 'sqlite',
'database' => 'data/sample.s3db',
2015-10-06 01:39:01 +02:00
]);
2010-08-03 21:06:56 +02:00
// retrieve database reflection
2018-04-17 10:03:43 +02:00
$database = $dibi->getDatabaseInfo();
2010-08-03 21:06:56 +02:00
2015-10-06 13:14:01 +02:00
echo "<h2>Database '{$database->getName()}'</h2>\n";
2010-08-03 21:06:56 +02:00
echo "<ul>\n";
foreach ($database->getTables() as $table) {
2015-10-06 13:14:01 +02:00
echo '<li>', ($table->isView() ? 'view' : 'table') . " {$table->getName()}</li>\n";
2010-08-03 21:06:56 +02:00
}
echo "</ul>\n";
// table reflection
$table = $database->getTable('products');
2015-10-06 13:14:01 +02:00
echo "<h2>Table '{$table->getName()}'</h2>\n";
2010-08-03 21:06:56 +02:00
echo "Columns\n";
echo "<ul>\n";
foreach ($table->getColumns() as $column) {
2015-10-06 13:14:01 +02:00
echo "<li>{$column->getName()} <i>{$column->getNativeType()}</i> <code>{$column->getDefault()}</code></li>\n";
2010-08-03 21:06:56 +02:00
}
echo "</ul>\n";
2015-06-19 03:11:36 +02:00
echo 'Indexes';
2010-08-03 21:06:56 +02:00
echo "<ul>\n";
foreach ($table->getIndexes() as $index) {
2015-10-06 13:14:01 +02:00
echo "<li>{$index->getName()} " . ($index->isPrimary() ? 'primary ' : '') . ($index->isUnique() ? 'unique' : '') . ' (';
2010-08-03 21:06:56 +02:00
foreach ($index->getColumns() as $column) {
2015-10-06 13:14:01 +02:00
echo $column->getName(), ', ';
2010-08-03 21:06:56 +02:00
}
echo ")</li>\n";
}
echo "</ul>\n";