DBDiff\DB\Schema\TableSchema::getSchema PHP Method

getSchema() public method

public getSchema ( $connection, $table )
    public function getSchema($connection, $table)
    {
        // collation & engine
        $status = $this->{$connection}->select("show table status like '{$table}'");
        $engine = $status[0]['Engine'];
        $collation = $status[0]['Collation'];
        $schema = $this->{$connection}->select("SHOW CREATE TABLE `{$table}`")[0]['Create Table'];
        $lines = array_map(function ($el) {
            return trim($el);
        }, explode("\n", $schema));
        $lines = array_slice($lines, 1, -1);
        $columns = [];
        $keys = [];
        $constraints = [];
        foreach ($lines as $line) {
            preg_match("/`([^`]+)`/", $line, $matches);
            $name = $matches[1];
            $line = trim($line, ',');
            if (starts_with($line, '`')) {
                // column
                $columns[$name] = $line;
            } else {
                if (starts_with($line, 'CONSTRAINT')) {
                    // constraint
                    $constraints[$name] = $line;
                } else {
                    // keys
                    $keys[$name] = $line;
                }
            }
        }
        return ['engine' => $engine, 'collation' => $collation, 'columns' => $columns, 'keys' => $keys, 'constraints' => $constraints];
    }