예제 #1
0
function getDbStructure($schema, $model_name = null, $verbose_output = true)
{
    //set active connection to schema and stop if there is no matching schema
    if (!DinklyDataConfig::setActiveConnection($schema)) {
        echo "\nNo such schema in config/db.yml\n";
        return false;
    }
    //Connect to database
    $db = DinklyDataConnector::fetchDB();
    $creds = DinklyDataConfig::getDBCreds();
    $db_name = $creds["name"];
    $db_name = DinklyBuilder::sanitize($db, $db_name);
    $model_name = DinklyBuilder::sanitize($db, $model_name);
    //get columns from specified table or all tables
    if ($model_name) {
        $stmt = $db->prepare("SHOW COLUMNS FROM " . $model_name . "");
        $stmt->execute();
        $table_schema = $stmt->fetchAll();
        echo "\nRead table " . $model_name . " columns...\n";
        sqlToYml($table_schema, $model_name, $db_name);
    } else {
        $stmt = $db->prepare("SHOW TABLES");
        $stmt->execute();
        $table_names = $stmt->fetchAll();
        foreach ($table_names as $table_array) {
            $table_name = $table_array[0];
            $stmt = $db->prepare("SHOW COLUMNS FROM " . $table_name . "");
            $stmt->execute();
            $table_schema = $stmt->fetchAll();
            echo "\nRead table " . $table_name . " columns...\n";
            sqlToYml($table_schema, $table_name, $db_name);
        }
    }
}