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

replaceWithAliases() public method

replaces db/table/column names with their aliases
public replaceWithAliases ( string $sql_query, array $aliases, string $db, string $table = '', &$flag = null ) : string
$sql_query string SQL query in which aliases are to be substituted
$aliases array Alias information for db/table/column
$db string the database name
$table string the tablename
return string query replaced with aliases
    public function replaceWithAliases($sql_query, $aliases, $db, $table = '', &$flag = null)
    {
        $flag = false;
        /**
         * The parser of this query.
         *
         * @var Parser $parser
         */
        $parser = new Parser($sql_query);
        if (empty($parser->statements[0])) {
            return $sql_query;
        }
        /**
         * The statement that represents the query.
         *
         * @var \SqlParser\Statements\CreateStatement $statement
         */
        $statement = $parser->statements[0];
        /**
         * Old database name.
         *
         * @var string $old_database
         */
        $old_database = $db;
        // Replacing aliases in `CREATE TABLE` statement.
        if ($statement->options->has('TABLE')) {
            // Extracting the name of the old database and table from the
            // statement to make sure the parameters are corect.
            if (!empty($statement->name->database)) {
                $old_database = $statement->name->database;
            }
            /**
             * Old table name.
             *
             * @var string $old_table
             */
            $old_table = $statement->name->table;
            // Finding the aliased database name.
            // The database might be empty so we have to add a few checks.
            $new_database = null;
            if (!empty($statement->name->database)) {
                $new_database = $statement->name->database;
                if (!empty($aliases[$old_database]['alias'])) {
                    $new_database = $aliases[$old_database]['alias'];
                }
            }
            // Finding the aliases table name.
            $new_table = $old_table;
            if (!empty($aliases[$old_database]['tables'][$old_table]['alias'])) {
                $new_table = $aliases[$old_database]['tables'][$old_table]['alias'];
            }
            // Replacing new values.
            if ($statement->name->database !== $new_database || $statement->name->table !== $new_table) {
                $statement->name->database = $new_database;
                $statement->name->table = $new_table;
                $statement->name->expr = null;
                // Force rebuild.
                $flag = true;
            }
            foreach ($statement->fields as $field) {
                // Column name.
                if (!empty($field->type)) {
                    if (!empty($aliases[$old_database]['tables'][$old_table]['columns'][$field->name])) {
                        $field->name = $aliases[$old_database]['tables'][$old_table]['columns'][$field->name];
                        $flag = true;
                    }
                }
                // Key's columns.
                if (!empty($field->key)) {
                    foreach ($field->key->columns as $key => $column) {
                        if (!empty($aliases[$old_database]['tables'][$old_table]['columns'][$column['name']])) {
                            $field->key->columns[$key]['name'] = $aliases[$old_database]['tables'][$old_table]['columns'][$column['name']];
                            $flag = true;
                        }
                    }
                }
                // References.
                if (!empty($field->references)) {
                    $ref_table = $field->references->table->table;
                    // Replacing table.
                    if (!empty($aliases[$old_database]['tables'][$ref_table]['alias'])) {
                        $field->references->table->table = $aliases[$old_database]['tables'][$ref_table]['alias'];
                        $field->references->table->expr = null;
                        $flag = true;
                    }
                    // Replacing column names.
                    foreach ($field->references->columns as $key => $column) {
                        if (!empty($aliases[$old_database]['tables'][$ref_table]['columns'][$column])) {
                            $field->references->columns[$key] = $aliases[$old_database]['tables'][$ref_table]['columns'][$column];
                            $flag = true;
                        }
                    }
                }
            }
        } elseif ($statement->options->has('TRIGGER')) {
            // Extracting the name of the old database and table from the
            // statement to make sure the parameters are corect.
            if (!empty($statement->table->database)) {
                $old_database = $statement->table->database;
            }
            /**
             * Old table name.
             *
             * @var string $old_table
             */
            $old_table = $statement->table->table;
            if (!empty($aliases[$old_database]['tables'][$old_table]['alias'])) {
                $statement->table->table = $aliases[$old_database]['tables'][$old_table]['alias'];
                $statement->table->expr = null;
                // Force rebuild.
                $flag = true;
            }
        }
        if ($statement->options->has('TRIGGER') || $statement->options->has('PROCEDURE') || $statement->options->has('FUNCTION') || $statement->options->has('VIEW')) {
            // Repalcing the body.
            for ($i = 0, $count = count($statement->body); $i < $count; ++$i) {
                /**
                 * Token parsed at this moment.
                 *
                 * @var Token $token
                 */
                $token = $statement->body[$i];
                // Replacing only symbols (that are not variables) and unknown
                // identifiers.
                if ($token->type === Token::TYPE_SYMBOL && !($token->flags & Token::FLAG_SYMBOL_VARIABLE) || ($token->type === Token::TYPE_KEYWORD && !($token->flags & Token::FLAG_KEYWORD_RESERVED) || $token->type === Token::TYPE_NONE)) {
                    $alias = $this->getAlias($aliases, $token->value);
                    if (!empty($alias)) {
                        // Replacing the token.
                        $token->token = Context::escape($alias);
                        $flag = true;
                    }
                }
            }
        }
        return $statement->build();
    }