2017 © Pedro Peláez
 

library schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

image

milesasylum/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  • Tuesday, January 16, 2018
  • by courtney-miles
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4,366 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 11 Versions
  • 26 % Grown

The README.md

Schnoop: PHP Database Schema Inspector

Tests Coverage Status Latest Stable Version Total Downloads License, (*1)

Schnoop provides a convenient PHP interface for inspecting a MySQL database schema and producing the DDL statements for the schema., (*2)

It is intended to assist with code generation in development environments., (*3)

Disclaimer: It is not advisable to use this package in production environments., (*4)

Examples

Construct the Schnoop object

To construct the Schnoop object, first establish a PDO connection to your database server, then supply the connection to SchnoopFactory::create()., (*5)

<?php
use \MilesAsylum\Schnoop;

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

Get a list of databases

<?php
// ...

$databaseList = $schnoop->getDatabaseList;

print_r($databaseList);

Inspect a database

<?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.";
}

Inspect a table

<?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()}";
}

Inspect a column

<?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()}.";
}

Inspect the column data type

<?php
// ...

$dataType = $column->getDataType();

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

Inspect table indexes

<?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.
    }
}

Inspect table triggers

```php <?php // ..., (*6)

$triggers = $table->getTriggers();, (*7)

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.
}, (*8)

````, (*9)

Todo

  • Add method DataTypeFactoryInterface::getTypeName() to be used with DataTypeFactory::addHandler().
  • Introduced a repository to prevent duplicate resources from being constructed.
  • Add support for Views.

The Versions

16/01 2018

dev-master

9999999-dev https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

database sql mysql ddl

16/01 2018

v0.2.0

0.2.0.0 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

database sql mysql ddl

16/01 2018

dev-imprv/1/Php7Compatibility

dev-imprv/1/Php7Compatibility https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

database sql mysql ddl

22/10 2016

v0.1.3

0.1.3.0 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

database sql mysql ddl

20/10 2016

v0.1.2

0.1.2.0 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

database sql mysql ddl

16/10 2016

0.1.1

0.1.1.0 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

database sql mysql ddl

13/10 2016

0.1.0

0.1.0.0 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

database sql mysql ddl

11/10 2016

0.1.0-alpha.4

0.1.0.0-alpha4 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

28/09 2016

0.1.0-alpha.3

0.1.0.0-alpha3 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Courtney Miles

26/07 2016

0.1.0-alpha.2

0.1.0.0-alpha2 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

  • php ~5.6.0

 

The Development Requires

by Courtney Miles

21/07 2016

0.1.0-alpha.1

0.1.0.0-alpha1 https://github.com/courtney-miles/schnoop

Schnoop provides a convenient PHP interface for inspecting a database schema.

  Sources   Download

MIT

The Requires

  • php ~5.6.0

 

The Development Requires

by Courtney Miles