PMA\libraries\plugins\export\ExportSql::getTableDefStandIn PHP Method

getTableDefStandIn() public method

Returns a stand-in CREATE definition to resolve view dependencies
public getTableDefStandIn ( string $db, string $view, string $crlf, array $aliases = [] ) : string
$db string the database name
$view string the view name
$crlf string the end of line sequence
$aliases array Aliases of db/table/columns
return string resulting definition
    public function getTableDefStandIn($db, $view, $crlf, $aliases = array())
    {
        $db_alias = $db;
        $view_alias = $view;
        $this->initAlias($aliases, $db_alias, $view_alias);
        $create_query = '';
        if (!empty($GLOBALS['sql_drop_table'])) {
            $create_query .= 'DROP VIEW IF EXISTS ' . Util::backquote($view_alias) . ';' . $crlf;
        }
        $create_query .= 'CREATE TABLE ';
        if (isset($GLOBALS['sql_if_not_exists']) && $GLOBALS['sql_if_not_exists']) {
            $create_query .= 'IF NOT EXISTS ';
        }
        $create_query .= Util::backquote($view_alias) . ' (' . $crlf;
        $tmp = array();
        $columns = $GLOBALS['dbi']->getColumnsFull($db, $view);
        foreach ($columns as $column_name => $definition) {
            $col_alias = $column_name;
            if (!empty($aliases[$db]['tables'][$view]['columns'][$col_alias])) {
                $col_alias = $aliases[$db]['tables'][$view]['columns'][$col_alias];
            }
            $tmp[] = Util::backquote($col_alias) . ' ' . $definition['Type'] . $crlf;
        }
        $create_query .= implode(',', $tmp) . ');' . $crlf;
        return $create_query;
    }

Usage Example

/**
 * Get views as an array and create SQL view stand-in
 *
 * @param array     $tables_full       array of all tables in given db or dbs
 * @param ExportSql $export_sql_plugin export plugin instance
 * @param string    $db                database name
 *
 * @return array $views
 */
function PMA_getViewsAndCreateSqlViewStandIn($tables_full, $export_sql_plugin, $db)
{
    $views = array();
    foreach ($tables_full as $each_table => $tmp) {
        // to be able to rename a db containing views,
        // first all the views are collected and a stand-in is created
        // the real views are created after the tables
        if ($GLOBALS['dbi']->getTable($db, $each_table)->isView()) {
            // If view exists, and 'add drop view' is selected: Drop it!
            if ($_REQUEST['what'] != 'nocopy' && isset($_REQUEST['drop_if_exists']) && $_REQUEST['drop_if_exists'] == 'true') {
                $drop_query = 'DROP VIEW IF EXISTS ' . PMA\libraries\Util::backquote($_REQUEST['newname']) . '.' . PMA\libraries\Util::backquote($each_table);
                $GLOBALS['dbi']->query($drop_query);
                $GLOBALS['sql_query'] .= "\n" . $drop_query . ';';
            }
            $views[] = $each_table;
            // Create stand-in definition to resolve view dependencies
            $sql_view_standin = $export_sql_plugin->getTableDefStandIn($db, $each_table, "\n");
            $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
            $GLOBALS['dbi']->query($sql_view_standin);
            $GLOBALS['sql_query'] .= "\n" . $sql_view_standin;
        }
    }
    return $views;
}