⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sql.php

📁 一个用PHP编写的
💻 PHP
📖 第 1 页 / 共 3 页
字号:
        if (PMA_MYSQL_INT_VERSION > 50100) {            $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddslashes($db,true) . '\';');        } else {            $event_names = array();        }        if ($procedure_names || $function_names || $event_names) {            $text .= $crlf              . 'DELIMITER ' . $delimiter . $crlf;        }        if ($procedure_names) {            $text .=                PMA_exportComment()              . PMA_exportComment($GLOBALS['strProcedures'])              . PMA_exportComment();            foreach($procedure_names as $procedure_name) {                if (! empty($GLOBALS['sql_drop_table'])) {		    $text .= 'DROP PROCEDURE IF EXISTS ' . PMA_backquote($procedure_name) . $delimiter . $crlf;                }                $text .= PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name) . $delimiter . $crlf . $crlf;            }        }        if ($function_names) {            $text .=                PMA_exportComment()              . PMA_exportComment($GLOBALS['strFunctions'])              . PMA_exportComment();            foreach($function_names as $function_name) {                if (! empty($GLOBALS['sql_drop_table'])) {		    $text .= 'DROP FUNCTION IF EXISTS ' . PMA_backquote($function_name) . $delimiter . $crlf;                }                $text .= PMA_DBI_get_definition($db, 'FUNCTION', $function_name) . $delimiter . $crlf . $crlf;            }        }        if ($event_names) {            $text .=                PMA_exportComment()              . PMA_exportComment($GLOBALS['strEvents'])              . PMA_exportComment();            foreach($event_names as $event_name) {                if (! empty($GLOBALS['sql_drop_table'])) {		    $text .= 'DROP EVENT ' . PMA_backquote($event_name) . $delimiter . $crlf;                }                $text .= PMA_DBI_get_definition($db, 'EVENT', $event_name) . $delimiter . $crlf . $crlf;            }        }        if ($procedure_names || $function_names || $event_names) {            $text .= 'DELIMITER ;' . $crlf;        }        if (! empty($text)) {            $result = PMA_exportOutputHandler($text);        }    }    return $result;}/** * Returns a stand-in CREATE definition to resolve view dependencies * * @param   string   the database name * @param   string   the vew name * @param   string   the end of line sequence * * @return  string   resulting definition * * @access  public */function PMA_getTableDefStandIn($db, $view, $crlf) {    $create_query = '';    if (! empty($GLOBALS['sql_drop_table'])) {        $create_query .= 'DROP VIEW IF EXISTS ' . PMA_backquote($view) . ';' . $crlf;    }    $create_query .= 'CREATE TABLE ';    if (isset($GLOBALS['sql_if_not_exists']) && $GLOBALS['sql_if_not_exists']) {        $create_query .= 'IF NOT EXISTS ';    }    $create_query .= PMA_backquote($view) . ' (' . $crlf;    $tmp = array();    $columns = PMA_DBI_get_columns_full($db, $view);    foreach($columns as $column_name => $definition) {        $tmp[] = PMA_backquote($column_name) . ' ' . $definition['Type'] . $crlf;    }    $create_query .= implode(',', $tmp) . ');';    return($create_query);}/** * Returns $table's CREATE definition * * @param   string   the database name * @param   string   the table name * @param   string   the end of line sequence * @param   string   the url to go back in case of error * @param   boolean  whether to include creation/update/check dates * @param   boolean  whether to add semicolon and end-of-line at the end * * @return  string   resulting schema * * @global  boolean  whether to add 'drop' statements or not * @global  boolean  whether to use backquotes to allow the use of special *                   characters in database, table and fields names or not * * @access  public */function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false, $add_semicolon = true){    global $sql_drop_table;    global $sql_backquotes;    global $cfgRelation;    global $sql_constraints;    global $sql_constraints_query; // just the text of the query    $schema_create = '';    $auto_increment = '';    $new_crlf = $crlf;    // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli    $result = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table) . '\'', null, PMA_DBI_QUERY_STORE);    if ($result != FALSE) {        if (PMA_DBI_num_rows($result) > 0) {            $tmpres        = PMA_DBI_fetch_assoc($result);            // Here we optionally add the AUTO_INCREMENT next value,            // but starting with MySQL 5.0.24, the clause is already included            // in SHOW CREATE TABLE so we'll remove it below            if (isset($GLOBALS['sql_auto_increment']) && !empty($tmpres['Auto_increment'])) {                $auto_increment .= ' AUTO_INCREMENT=' . $tmpres['Auto_increment'] . ' ';            }            if ($show_dates && isset($tmpres['Create_time']) && !empty($tmpres['Create_time'])) {                $schema_create .= PMA_exportComment($GLOBALS['strStatCreateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Create_time'])));                $new_crlf = PMA_exportComment() . $crlf;            }            if ($show_dates && isset($tmpres['Update_time']) && !empty($tmpres['Update_time'])) {                $schema_create .= PMA_exportComment($GLOBALS['strStatUpdateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Update_time'])));                $new_crlf = PMA_exportComment() . $crlf;            }            if ($show_dates && isset($tmpres['Check_time']) && !empty($tmpres['Check_time'])) {                $schema_create .= PMA_exportComment($GLOBALS['strStatCheckTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Check_time'])));                $new_crlf = PMA_exportComment() . $crlf;            }        }        PMA_DBI_free_result($result);    }    $schema_create .= $new_crlf;    // no need to generate a DROP VIEW here, it was done earlier    if (! empty($sql_drop_table) && ! PMA_Table::isView($db,$table)) {        $schema_create .= 'DROP TABLE IF EXISTS ' . PMA_backquote($table, $sql_backquotes) . ';' . $crlf;    }    // Steve Alberty's patch for complete table dump,    // Whether to quote table and fields names or not    if ($sql_backquotes) {        PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 1');    } else {        PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 0');    }    // I don't see the reason why this unbuffered query could cause problems,    // because SHOW CREATE TABLE returns only one row, and we free the    // results below. Nonetheless, we got 2 user reports about this    // (see bug 1562533) so I remove the unbuffered mode.    //$result = PMA_DBI_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), null, PMA_DBI_QUERY_UNBUFFERED);    //    // Note: SHOW CREATE TABLE, at least in MySQL 5.1.23, does not    // produce a displayable result for the default value of a BIT    // field, nor does the mysqldump command. See MySQL bug 35796    /*     * We have to select database and not use database name in SHOW CREATE,     * otherwise CREATE statement can include database name.     */    PMA_DBI_select_db($db);    $result = PMA_DBI_try_query('SHOW CREATE TABLE ' . PMA_backquote($table));    // an error can happen, for example the table is crashed    $tmp_error = PMA_DBI_getError();    if ($tmp_error) {        return PMA_exportComment($GLOBALS['strInUse'] . '(' . $tmp_error . ')');    }    if ($result != FALSE && ($row = PMA_DBI_fetch_row($result))) {        $create_query = $row[1];        unset($row);        // Convert end of line chars to one that we want (note that MySQL doesn't return query it will accept in all cases)        if (strpos($create_query, "(\r\n ")) {            $create_query = str_replace("\r\n", $crlf, $create_query);        } elseif (strpos($create_query, "(\n ")) {            $create_query = str_replace("\n", $crlf, $create_query);        } elseif (strpos($create_query, "(\r ")) {            $create_query = str_replace("\r", $crlf, $create_query);        }        // Should we use IF NOT EXISTS?        if (isset($GLOBALS['sql_if_not_exists'])) {            $create_query     = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);        }        // are there any constraints to cut out?        if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $create_query)) {            // Split the query into lines, so we can easily handle it. We know lines are separated by $crlf (done few lines above).            $sql_lines = explode($crlf, $create_query);            $sql_count = count($sql_lines);            // lets find first line with constraints            for ($i = 0; $i < $sql_count; $i++) {                if (preg_match('@^[\s]*(CONSTRAINT|FOREIGN[\s]+KEY)@', $sql_lines[$i])) {                    break;                }            }            // If we really found a constraint            if ($i != $sql_count) {                // remove , from the end of create statement                $sql_lines[$i - 1] = preg_replace('@,$@', '', $sql_lines[$i - 1]);                // prepare variable for constraints                if (!isset($sql_constraints)) {                    if (isset($GLOBALS['no_constraints_comments'])) {                        $sql_constraints = '';                    } else {                        $sql_constraints = $crlf                                         . PMA_exportComment()                                         . PMA_exportComment($GLOBALS['strConstraintsForDumped'])                                         . PMA_exportComment();                    }                }                // comments for current table                if (!isset($GLOBALS['no_constraints_comments'])) {                    $sql_constraints .= $crlf                                     . PMA_exportComment()                                     . PMA_exportComment($GLOBALS['strConstraintsForTable'] . ' ' . PMA_backquote($table))                                     . PMA_exportComment();                }                // let's do the work                $sql_constraints_query .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf;                $sql_constraints .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf;                $first = TRUE;                for ($j = $i; $j < $sql_count; $j++) {                    if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $sql_lines[$j])) {                        if (!$first) {                            $sql_constraints .= $crlf;                        }                        if (strpos($sql_lines[$j], 'CONSTRAINT') === FALSE) {                            $str_tmp = preg_replace('/(FOREIGN[\s]+KEY)/', 'ADD \1', $sql_lines[$j]);                            $sql_constraints_query .= $str_tmp;                            $sql_constraints .= $str_tmp;                        } else {                            $str_tmp = preg_replace('/(CONSTRAINT)/', 'ADD \1', $sql_lines[$j]);                            $sql_constraints_query .= $str_tmp;                            $sql_constraints .= $str_tmp;                        }                        $first = FALSE;                    } else {                        break;                    }                }                $sql_constraints .= ';' . $crlf;                $sql_constraints_query .= ';';                $create_query = implode($crlf, array_slice($sql_lines, 0, $i)) . $crlf . implode($crlf, array_slice($sql_lines, $j, $sql_count - 1));                unset($sql_lines);            }        }        $schema_create .= $create_query;    }    // remove a possible "AUTO_INCREMENT = value" clause    // that could be there starting with MySQL 5.0.24    $schema_create = preg_replace('/AUTO_INCREMENT\s*=\s*([0-9])+/', '', $schema_create);    $schema_create .= $auto_increment;    PMA_DBI_free_result($result);    return $schema_create . ($add_semicolon ? ';' . $crlf : '');} // end of the 'PMA_getTableDef()' function/** * Returns $table's comments, relations etc. * * @param   string   the database name * @param   string   the table name * @param   string   the end of line sequence * @param   boolean  whether to include relation comments * @param   boolean  whether to include mime comments * * @return  string   resulting comments * * @access  public */function PMA_getTableComments($db, $table, $crlf, $do_relation = false,  $do_mime = false){    global $cfgRelation;    global $sql_backquotes;    global $sql_constraints;    $schema_create = '';    // Check if we can use Relations (Mike Beck)    if ($do_relation && !empty($cfgRelation['relation'])) {        // Find which tables are related with the current one and write it in        // an array        $res_rel = PMA_getForeigners($db, $table);        if ($res_rel && count($res_rel) > 0) {            $have_rel = TRUE;        } else {            $have_rel = FALSE;        }    } else {           $have_rel = FALSE;    } // end if    if ($do_mime && $cfgRelation['mimework']) {        if (!($mime_map = PMA_getMIME($db, $table, true))) {            unset($mime_map);        }    }    if (isset($mime_map) && count($mime_map) > 0) {        $schema_create .= $crlf                       . PMA_exportComment()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -