/** * Charset information */ if (!PMA_DRIZZLE) { include_once './libraries/mysql_charsets.inc.php'; } if (!isset($mysql_charsets)) { $mysql_charsets = array(); $mysql_collations_flat = array(); } /** * Initializes the SQL parsing library. */ include_once SQL_PARSER_AUTOLOAD; // Loads closest context to this version. SqlParser\Context::loadClosest((PMA_DRIZZLE ? 'Drizzle' : 'MySql') . PMA_MYSQL_INT_VERSION); // Sets the default delimiter (if specified). if (!empty($_REQUEST['sql_delimiter'])) { SqlParser\Lexer::$DEFAULT_DELIMITER = $_REQUEST['sql_delimiter']; } // TODO: Set SQL modes too. /** * the PMA_List_Database class */ include_once './libraries/PMA.php'; $pma = new PMA(); $pma->userlink = $userlink; $pma->controllink = $controllink; /** * some resetting has to be done when switching servers */
/** * Adds backquotes on both sides of a database, table or field name. * in compatibility mode * * example: * <code> * echo backquoteCompat('owner`s db'); // `owner``s db` * * </code> * * @param mixed $a_name the database, table or field name to * "backquote" or array of it * @param string $compatibility string compatibility mode (used by dump * functions) * @param boolean $do_it a flag to bypass this function (used by dump * functions) * * @return mixed the "backquoted" database, table or field name * * @access public */ public static function backquoteCompat($a_name, $compatibility = 'MSSQL', $do_it = true) { if (is_array($a_name)) { foreach ($a_name as &$data) { $data = self::backquoteCompat($data, $compatibility, $do_it); } return $a_name; } if (!$do_it) { if (!SqlParser\Context::isKeyword($a_name)) { return $a_name; } } // @todo add more compatibility cases (ORACLE for example) switch ($compatibility) { case 'MSSQL': $quote = '"'; break; default: $quote = "`"; break; } // '0' is also empty for php :-( if (mb_strlen($a_name) && $a_name !== '*') { return $quote . $a_name . $quote; } else { return $a_name; } }
/** * replaces db/table/column names with their aliases * * @param string $sql_query SQL query in which aliases are to be substituted * @param array $aliases Alias information for db/table/column * @param string $db the database name * @param string $table the tablename * @param string &$flag the flag denoting whether any replacement was done * * @return string query replaced with aliases */ public function replaceWithAliases($sql_query, $aliases, $db, $table = '', &$flag = null) { $flag = false; /** * The parser of this query. * @var SqlParser\Parser $parser */ $parser = new SqlParser\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])) { $field->key->columns[$key] = $aliases[$old_database]['tables'][$old_table]['columns'][$column]; $flag = true; } } } // References. if (!empty($field->references)) { $ref_table = $field->references->table; // Replacing table. if (!empty($aliases[$old_database]['tables'][$ref_table]['alias'])) { $field->references->table = $aliases[$old_database]['tables'][$ref_table]['alias']; $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 === SqlParser\Token::TYPE_SYMBOL && !($token->flags & SqlParser\Token::FLAG_SYMBOL_VARIABLE) || ($token->type === SqlParser\Token::TYPE_KEYWORD && !($token->flags & SqlParser\Token::FLAG_KEYWORD_RESERVED) || $token->type === SqlParser\Token::TYPE_NONE)) { $alias = $this->getAlias($aliases, $token->value); if (!empty($alias)) { // Replacing the token. $token->token = SqlParser\Context::escape($alias); $flag = true; } } } } return $statement->build(); }
/** * Get all column names which are MySQL reserved words * * @return array * @access public */ public function getReservedColumnNames() { $columns = $this->getColumns(false); $return = array(); foreach ($columns as $column) { $temp = explode('.', $column); $column_name = $temp[2]; if (SqlParser\Context::isKeyword($column_name, true)) { $return[] = $column_name; } } return $return; }
exit; } /** * handle MySQL reserved words columns check */ if (isset($_REQUEST['reserved_word_check'])) { $response = PMA_Response::getInstance(); if ($GLOBALS['cfg']['ReservedWordDisableWarning'] === false) { $columns_names = $_REQUEST['field_name']; $reserved_keywords_names = array(); foreach ($columns_names as $column) { if (SqlParser\Context::isKeyword(trim($column), true)) { $reserved_keywords_names[] = trim($column); } } if (SqlParser\Context::isKeyword(trim($table), true)) { $reserved_keywords_names[] = trim($table); } if (count($reserved_keywords_names) == 0) { $response->isSuccess(false); } $response->addJSON('message', sprintf(_ngettext('The name \'%s\' is a MySQL reserved keyword.', 'The names \'%s\' are MySQL reserved keywords.', count($reserved_keywords_names)), implode(',', $reserved_keywords_names))); } else { $response->isSuccess(false); } exit; } /** * A click on Change has been made for one column */ if (isset($_REQUEST['change_column'])) {
*/ $GLOBALS['PMA_Types'] = new PMA_Types_MySQL(); /** * Charset information */ include_once './libraries/mysql_charsets.inc.php'; if (!isset($mysql_charsets)) { $mysql_charsets = array(); $mysql_collations_flat = array(); } /** * Initializes the SQL parsing library. */ include_once SQL_PARSER_AUTOLOAD; // Loads closest context to this version. SqlParser\Context::loadClosest('MySql' . PMA_MYSQL_INT_VERSION); // Sets the default delimiter (if specified). if (!empty($_REQUEST['sql_delimiter'])) { SqlParser\Lexer::$DEFAULT_DELIMITER = $_REQUEST['sql_delimiter']; } // TODO: Set SQL modes too. /** * the PMA_List_Database class */ include_once './libraries/PMA.php'; $pma = new PMA(); $pma->userlink = $userlink; $pma->controllink = $controllink; /** * some resetting has to be done when switching servers */