function table_structure($table)
{
    global $db_driver;
    if ('mysql' == $db_driver) {
        $query = "SHOW CREATE TABLE `{$table}`";
        $row = db_row_num($query);
        echo $row[1] . ';';
        echo "\n\n";
    }
    if ('pgsql' == $db_driver) {
        return '';
    }
}
Example #2
0
function table_structure($table, $type = "table")
{
    // @dump
    // @export
    // @structure
    global $db_driver;
    if ('mysql' == $db_driver) {
        if ("table" == $type) {
            $query = "SHOW CREATE TABLE `{$table}`";
            $row = db_row_num($query);
            echo $row[1] . ';';
            echo "\n\n";
        } else {
            if ("view" == $type) {
                $query = "SHOW CREATE VIEW `{$table}`";
                $row = db_row_num($query);
                echo $row[1] . ';';
                echo "\n\n";
            } else {
                assert(0);
            }
        }
    } else {
        if ('pgsql' == $db_driver) {
            return;
        } else {
            if ("sqlite" == $db_driver) {
                if ("table" == $type) {
                    $sql = PDO_FetchOne("SELECT sql FROM sqlite_master WHERE name = :name AND type='table' ", array(":name" => $table));
                    $sql = str_replace("\r\n", "\n", $sql);
                    // editplus invalid line endings, strange
                    echo "{$sql};\n";
                } else {
                    if ("view" == $type) {
                        $sql = PDO_FetchOne("SELECT sql FROM sqlite_master WHERE name = :name AND type='view' ", array(":name" => $table));
                        $sql = str_replace("\r\n", "\n", $sql);
                        // editplus invalid line endings, strange
                        echo "{$sql};\n";
                    } else {
                        assert(0);
                    }
                }
                unset($sql);
                if ("table" == $type) {
                    $indexes = PDO_FetchAll("SELECT * FROM sqlite_master WHERE tbl_name = :tbl_name AND type='index' ", array(":tbl_name" => $table));
                    foreach ($indexes as $index) {
                        if ($index["sql"]) {
                            // autoindexes have column "sql" empty
                            echo "\n-- INDEX: \"{$index['name']}\"\n\n";
                            echo "{$index['sql']};\n";
                        }
                    }
                }
                echo "\n";
            }
        }
    }
}