/**
  * Update for internal relation
  *
  * @return void
  */
 public function updateForInternalRelationAction()
 {
     $multi_edit_columns_name = isset($_REQUEST['fields_name']) ? $_REQUEST['fields_name'] : null;
     if ($this->upd_query->updateInternalRelations($multi_edit_columns_name, $_POST['destination_db'], $_POST['destination_table'], $_POST['destination_column'], $this->cfgRelation, isset($this->existrel) ? $this->existrel : null)) {
         $this->response->addHTML(Util::getMessage(__('Internal relations were successfully updated.'), '', 'success'));
     }
 }
/**
 * Initiate the column creation statement according to the table creation or
 * add columns to a existing table
 *
 * @param int     $field_cnt     number of columns
 * @param boolean $is_create_tbl true if requirement is to get the statement
 *                               for table creation
 *
 * @return array  $definitions An array of initial sql statements
 *                             according to the request
 */
function PMA_buildColumnCreationStatement($field_cnt, $is_create_tbl = true)
{
    $definitions = array();
    for ($i = 0; $i < $field_cnt; ++$i) {
        // '0' is also empty for php :-(
        if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
            continue;
        }
        $definition = PMA_getStatementPrefix($is_create_tbl) . Table::generateFieldSpec(trim($_REQUEST['field_name'][$i]), $_REQUEST['field_type'][$i], $_REQUEST['field_length'][$i], $_REQUEST['field_attribute'][$i], isset($_REQUEST['field_collation'][$i]) ? $_REQUEST['field_collation'][$i] : '', isset($_REQUEST['field_null'][$i]) ? $_REQUEST['field_null'][$i] : 'NOT NULL', $_REQUEST['field_default_type'][$i], $_REQUEST['field_default_value'][$i], isset($_REQUEST['field_extra'][$i]) ? $_REQUEST['field_extra'][$i] : false, isset($_REQUEST['field_comments'][$i]) ? $_REQUEST['field_comments'][$i] : '', isset($_REQUEST['field_virtuality'][$i]) ? $_REQUEST['field_virtuality'][$i] : '', isset($_REQUEST['field_expression'][$i]) ? $_REQUEST['field_expression'][$i] : '');
        $definition .= PMA_setColumnCreationStatementSuffix($i, $is_create_tbl);
        $definitions[] = $definition;
    }
    // end for
    return $definitions;
}
Esempio n. 3
0
/**
 * Function to calculate new pos if pos is higher than number of rows
 * of displayed table
 *
 * @param String   $db    Database name
 * @param String   $table Table name
 * @param Int|null $pos   Initial position
 *
 * @return Int Number of pos to display last page
 */
function PMA_calculatePosForLastPage($db, $table, $pos)
{
    if (null === $pos) {
        $pos = $_SESSION['tmpval']['pos'];
    }
    $_table = new Table($table, $db);
    $unlim_num_rows = $_table->countRecords(true);
    //If position is higher than number of rows
    if ($unlim_num_rows <= $pos && 0 != $pos) {
        $pos = PMA_getStartPosToDisplayRow($unlim_num_rows);
    }
    return $pos;
}
Esempio n. 4
0
 /**
  * Test for moveCopy
  *
  * @return void
  */
 public function testMoveCopy()
 {
     $source_table = 'PMA_BookMark';
     $source_db = 'PMA';
     $target_table = 'PMA_BookMark_new';
     $target_db = 'PMA_new';
     $what = "dataonly";
     $move = true;
     $mode = "one_table";
     $GLOBALS['dbi']->expects($this->any())->method('getTable')->will($this->returnValue(new Table($target_table, $target_db)));
     $_REQUEST['drop_if_exists'] = true;
     $return = Table::moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move, $mode);
     //successfully
     $expect = true;
     $this->assertEquals($expect, $return);
     $sql_query = "INSERT INTO `PMA_new`.`PMA_BookMark_new` SELECT * FROM " . "`PMA`.`PMA_BookMark`";
     $this->assertContains($sql_query, $GLOBALS['sql_query']);
     $sql_query = "DROP VIEW `PMA`.`PMA_BookMark`";
     $this->assertContains($sql_query, $GLOBALS['sql_query']);
     $return = Table::moveCopy($source_db, $source_table, $target_db, $target_table, $what, false, $mode);
     //successfully
     $expect = true;
     $this->assertEquals($expect, $return);
     $sql_query = "INSERT INTO `PMA_new`.`PMA_BookMark_new` SELECT * FROM " . "`PMA`.`PMA_BookMark`;";
     $this->assertContains($sql_query, $GLOBALS['sql_query']);
     $sql_query = "DROP VIEW `PMA`.`PMA_BookMark`";
     $this->assertNotContains($sql_query, $GLOBALS['sql_query']);
 }
Esempio n. 5
0
 * Various table operations
 *
 * @package PhpMyAdmin
 */
use PMA\libraries\Partition;
use PMA\libraries\Table;
/**
 *
 */
require_once 'libraries/common.inc.php';
/**
 * functions implementation for this script
 */
require_once 'libraries/check_user_privileges.lib.php';
require_once 'libraries/operations.lib.php';
$pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
/**
 * Load JavaScript files
 */
$response = PMA\libraries\Response::getInstance();
$header = $response->getHeader();
$scripts = $header->getScripts();
$scripts->addFile('tbl_operations.js');
/**
 * Runs common work
 */
require 'libraries/tbl_common.inc.php';
$url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
$url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
/**
 * Gets relation settings
Esempio n. 6
0
/**
 * Prints Html For Export Options
 *
 * @param String         $export_type    Selected Export Type
 * @param String         $db             Selected DB
 * @param String         $table          Selected Table
 * @param String         $multi_values   Export selection
 * @param String         $num_tables     number of tables
 * @param ExportPlugin[] $export_list    Export List
 * @param String         $unlim_num_rows Number of Rows
 *
 * @return string
 */
function PMA_getHtmlForExportOptions($export_type, $db, $table, $multi_values, $num_tables, $export_list, $unlim_num_rows)
{
    global $cfg;
    $html = PMA_getHtmlForExportOptionsMethod();
    $html .= PMA_getHtmlForExportOptionsFormatDropdown($export_list);
    $html .= PMA_getHtmlForExportOptionsSelection($export_type, $multi_values);
    $_table = new Table($table, $db);
    if (strlen($table) > 0 && empty($num_tables) && !$_table->isMerge()) {
        $html .= PMA_getHtmlForExportOptionsRows($db, $table, $unlim_num_rows);
    }
    if (isset($cfg['SaveDir']) && !empty($cfg['SaveDir'])) {
        $html .= PMA_getHtmlForExportOptionsQuickExport();
    }
    $html .= PMA_getHtmlForAliasModalDialog($db, $table);
    $html .= PMA_getHtmlForExportOptionsOutput($export_type);
    $html .= PMA_getHtmlForExportOptionsFormat($export_list);
    return $html;
}
Esempio n. 7
0
<?php

/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * View manipulations
 *
 * @package PhpMyAdmin
 */
use PMA\libraries\Table;
use PMA\libraries\Response;
use PMA\libraries\URL;
/**
 *
 */
require_once './libraries/common.inc.php';
$pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
/**
 * functions implementation for this script
 */
require_once 'libraries/operations.lib.php';
/**
 * Load JavaScript files
 */
$response = Response::getInstance();
$header = $response->getHeader();
$scripts = $header->getScripts();
$scripts->addFile('tbl_operations.js');
/**
 * Runs common work
 */
require './libraries/tbl_common.inc.php';
Esempio n. 8
0
 /**
  * Set the content that needs to be shown in message
  *
  * @param string  $sorted_column_message the message for sorted column
  * @param array   $analyzed_sql_results  the analyzed query
  * @param integer $total                 the total number of rows returned by
  *                                       the SQL query without any
  *                                       programmatically appended LIMIT clause
  * @param integer $pos_next              the offset for next page
  * @param string  $pre_count             the string renders before row count
  * @param string  $after_count           the string renders after row count
  *
  * @return Message $message an object of Message
  *
  * @access  private
  *
  * @see     getTable()
  */
 private function _setMessageInformation($sorted_column_message, $analyzed_sql_results, $total, $pos_next, $pre_count, $after_count)
 {
     $unlim_num_rows = $this->__get('unlim_num_rows');
     // To use in isset()
     if (!empty($analyzed_sql_results['statement']->limit)) {
         $first_shown_rec = $analyzed_sql_results['statement']->limit->offset;
         $row_count = $analyzed_sql_results['statement']->limit->rowCount;
         if ($row_count < $total) {
             $last_shown_rec = $first_shown_rec + $row_count - 1;
         } else {
             $last_shown_rec = $first_shown_rec + $total - 1;
         }
     } elseif ($_SESSION['tmpval']['max_rows'] == self::ALL_ROWS || $pos_next > $total) {
         $first_shown_rec = $_SESSION['tmpval']['pos'];
         $last_shown_rec = $total - 1;
     } else {
         $first_shown_rec = $_SESSION['tmpval']['pos'];
         $last_shown_rec = $pos_next - 1;
     }
     $table = new Table($this->__get('table'), $this->__get('db'));
     if ($table->isView() && $total == $GLOBALS['cfg']['MaxExactCountViews']) {
         $message = Message::notice(__('This view has at least this number of rows. ' . 'Please refer to %sdocumentation%s.'));
         $message->addParam('[doc@cfg_MaxExactCount]');
         $message->addParam('[/doc]');
         $message_view_warning = Util::showHint($message);
     } else {
         $message_view_warning = false;
     }
     $message = Message::success(__('Showing rows %1s - %2s'));
     $message->addParam($first_shown_rec);
     if ($message_view_warning !== false) {
         $message->addParam('... ' . $message_view_warning, false);
     } else {
         $message->addParam($last_shown_rec);
     }
     $message->addMessage('(');
     if ($message_view_warning === false) {
         if (isset($unlim_num_rows) && $unlim_num_rows != $total) {
             $message_total = Message::notice($pre_count . __('%1$d total, %2$d in query'));
             $message_total->addParam($total);
             $message_total->addParam($unlim_num_rows);
         } else {
             $message_total = Message::notice($pre_count . __('%d total'));
             $message_total->addParam($total);
         }
         if (!empty($after_count)) {
             $message_total->addMessage($after_count);
         }
         $message->addMessage($message_total, '');
         $message->addMessage(', ', '');
     }
     $message_qt = Message::notice(__('Query took %01.4f seconds.') . ')');
     $message_qt->addParam($this->__get('querytime'));
     $message->addMessage($message_qt, '');
     if (!is_null($sorted_column_message)) {
         $message->addMessage($sorted_column_message, '');
     }
     return $message;
 }
Esempio n. 9
0
/**
 * Export at the table level
 *
 * @param string       $db              the database to export
 * @param string       $table           the table to export
 * @param string       $whatStrucOrData structure or data or both
 * @param ExportPlugin $export_plugin   the selected export plugin
 * @param string       $crlf            end of line character(s)
 * @param string       $err_url         the URL in case of error
 * @param string       $export_type     the export type
 * @param bool         $do_relation     whether to export relation info
 * @param bool         $do_comments     whether to add comments
 * @param bool         $do_mime         whether to add MIME info
 * @param bool         $do_dates        whether to add dates
 * @param string       $allrows         whether "dump all rows" was ticked
 * @param string       $limit_to        upper limit
 * @param string       $limit_from      starting limit
 * @param string       $sql_query       query for which exporting is requested
 * @param array        $aliases         Alias information for db/table/column
 *
 * @return void
 */
function PMA_exportTable($db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $allrows, $limit_to, $limit_from, $sql_query, $aliases)
{
    $db_alias = !empty($aliases[$db]['alias']) ? $aliases[$db]['alias'] : '';
    if (!$export_plugin->exportDBHeader($db, $db_alias)) {
        return;
    }
    if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
        $add_query = ' LIMIT ' . ($limit_from > 0 ? $limit_from . ', ' : '') . $limit_to;
    } else {
        $add_query = '';
    }
    $_table = new Table($table, $db);
    $is_view = $_table->isView();
    if ($whatStrucOrData == 'structure' || $whatStrucOrData == 'structure_and_data') {
        if ($is_view) {
            if (isset($GLOBALS['sql_create_view'])) {
                if (!$export_plugin->exportStructure($db, $table, $crlf, $err_url, 'create_view', $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $aliases)) {
                    return;
                }
            }
        } else {
            if (isset($GLOBALS['sql_create_table'])) {
                if (!$export_plugin->exportStructure($db, $table, $crlf, $err_url, 'create_table', $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $aliases)) {
                    return;
                }
            }
        }
    }
    // If this is an export of a single view, we have to export data;
    // for example, a PDF report
    // if it is a merge table, no data is exported
    if ($whatStrucOrData == 'data' || $whatStrucOrData == 'structure_and_data') {
        if (!empty($sql_query)) {
            // only preg_replace if needed
            if (!empty($add_query)) {
                // remove trailing semicolon before adding a LIMIT
                $sql_query = preg_replace('%;\\s*$%', '', $sql_query);
            }
            $local_query = $sql_query . $add_query;
            $GLOBALS['dbi']->selectDb($db);
        } else {
            // Data is exported only for Non-generated columns
            $tableObj = new PMA\libraries\Table($table, $db);
            $nonGeneratedCols = $tableObj->getNonGeneratedColumns(true);
            $local_query = 'SELECT ' . implode(', ', $nonGeneratedCols) . ' FROM ' . PMA\libraries\Util::backquote($db) . '.' . PMA\libraries\Util::backquote($table) . $add_query;
        }
        if (!$export_plugin->exportData($db, $table, $crlf, $err_url, $local_query, $aliases)) {
            return;
        }
    }
    // now export the triggers (needs to be done after the data because
    // triggers can modify already imported tables)
    if (isset($GLOBALS['sql_create_trigger']) && ($whatStrucOrData == 'structure' || $whatStrucOrData == 'structure_and_data')) {
        if (!$export_plugin->exportStructure($db, $table, $crlf, $err_url, 'triggers', $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $aliases)) {
            return;
        }
    }
    if (!$export_plugin->exportDBFooter($db)) {
        return;
    }
    if (isset($GLOBALS['sql_metadata'])) {
        // Types of metadata to export.
        // In the future these can be allowed to be selected by the user
        $metadataTypes = PMA_getMetadataTypesToExport();
        $export_plugin->exportMetadata($db, $table, $metadataTypes);
    }
}
Esempio n. 10
0
 /**
  * Provides the main table to form the LEFT JOIN clause
  *
  * @param array $search_tables        Tables involved in the search
  * @param array $search_columns       Columns involved in the search
  * @param array $where_clause_columns Columns having criteria where clause
  * @param array $where_clause_tables  Tables having criteria where clause
  *
  * @return string table name
  */
 private function _getMasterTable($search_tables, $search_columns, $where_clause_columns, $where_clause_tables)
 {
     if (count($where_clause_tables) == 1) {
         // If there is exactly one column that has a decent where-clause
         // we will just use this
         $master = key($where_clause_tables);
         return $master;
     }
     // Now let's find out which of the tables has an index
     // (When the control user is the same as the normal user
     // because he is using one of his databases as pmadb,
     // the last db selected is not always the one where we need to work)
     $candidate_columns = $this->_getLeftJoinColumnCandidates($search_tables, $search_columns, $where_clause_columns);
     // Generally, we need to display all the rows of foreign (referenced)
     // table, whether they have any matching row in child table or not.
     // So we select candidate tables which are foreign tables.
     $foreign_tables = array();
     foreach ($candidate_columns as $one_table) {
         $foreigners = PMA_getForeigners($this->_db, $one_table);
         foreach ($foreigners as $key => $foreigner) {
             if ($key != 'foreign_keys_data') {
                 if (in_array($foreigner['foreign_table'], $candidate_columns)) {
                     $foreign_tables[$foreigner['foreign_table']] = $foreigner['foreign_table'];
                 }
                 continue;
             }
             foreach ($foreigner as $one_key) {
                 if (in_array($one_key['ref_table_name'], $candidate_columns)) {
                     $foreign_tables[$one_key['ref_table_name']] = $one_key['ref_table_name'];
                 }
             }
         }
     }
     if (count($foreign_tables)) {
         $candidate_columns = $foreign_tables;
     }
     // If our array of candidates has more than one member we'll just
     // find the smallest table.
     // Of course the actual query would be faster if we check for
     // the Criteria which gives the smallest result set in its table,
     // but it would take too much time to check this
     if (!(count($candidate_columns) > 1)) {
         // Only one single candidate
         return reset($candidate_columns);
     }
     // Of course we only want to check each table once
     $checked_tables = $candidate_columns;
     $tsize = array();
     $csize = array();
     foreach ($candidate_columns as $table) {
         if ($checked_tables[$table] != 1) {
             $_table = new Table($table, $this->_db);
             $tsize[$table] = $_table->countRecords();
             $checked_tables[$table] = 1;
         }
         $csize[$table] = $tsize[$table];
     }
     // Return largest table
     return array_search(max($csize), $csize);
 }
/**
 * Saves the display field for a table.
 *
 * @param string $db    database name
 * @param string $table table name
 * @param string $field display field name
 *
 * @return boolean
 */
function PMA_saveDisplayField($db, $table, $field)
{
    $cfgRelation = PMA_getRelationsParam();
    if (!$cfgRelation['displaywork']) {
        return false;
    }
    $disp = PMA_getDisplayField($db, $table);
    if ($disp && $disp === $field) {
        $field = '';
    }
    $upd_query = new Table($table, $db, $GLOBALS['dbi']);
    $upd_query->updateDisplayField($disp, $field, $cfgRelation);
    return true;
}
Esempio n. 12
0
        $transformation_types = array("input_transformation", "transformation");
        foreach ($mime_map as $transformation) {
            $column_name = $transformation['column_name'];
            foreach ($transformation_types as $type) {
                $file = PMA_securePath($transformation[$type]);
                $extra_data = PMA_transformEditedValues($db, $table, $transformation, $edited_values, $file, $column_name, $extra_data, $type);
            }
        }
        // end of loop for each $mime_map
    }
    // Need to check the inline edited value can be truncated by MySQL
    // without informing while saving
    $column_name = $_REQUEST['fields_name']['multi_edit'][0][0];
    PMA_verifyWhetherValueCanBeTruncatedAndAppendExtraData($db, $table, $column_name, $extra_data);
    /**Get the total row count of the table*/
    $_table = new Table($_REQUEST['table'], $_REQUEST['db']);
    $extra_data['row_count'] = $_table->countRecords();
    $extra_data['sql_query'] = PMA\libraries\Util::getMessage($message, $GLOBALS['display_query']);
    $response = PMA\libraries\Response::getInstance();
    $response->setRequestStatus($message->isSuccess());
    $response->addJSON('message', $message);
    $response->addJSON($extra_data);
    exit;
}
if (!empty($return_to_sql_query)) {
    $disp_query = $GLOBALS['sql_query'];
    $disp_message = $message;
    unset($message);
    $GLOBALS['sql_query'] = $return_to_sql_query;
}
$scripts->addFile('tbl_change.js');
Esempio n. 13
0
/**
 * Builds and executes SQL statements to create the database and tables
 * as necessary, as well as insert all the data.
 *
 * @param string $db_name         Name of the database
 * @param array  &$tables         Array of tables for the specified database
 * @param array  &$analyses       Analyses of the tables
 * @param array  &$additional_sql Additional SQL statements to be executed
 * @param array  $options         Associative array of options
 *
 * @return void
 * @access  public
 *
 * @link http://wiki.phpmyadmin.net/pma/Import
 */
function PMA_buildSQL($db_name, &$tables, &$analyses = null, &$additional_sql = null, $options = null)
{
    /* Take care of the options */
    if (isset($options['db_collation']) && !is_null($options['db_collation'])) {
        $collation = $options['db_collation'];
    } else {
        $collation = "utf8_general_ci";
    }
    if (isset($options['db_charset']) && !is_null($options['db_charset'])) {
        $charset = $options['db_charset'];
    } else {
        $charset = "utf8";
    }
    if (isset($options['create_db'])) {
        $create_db = $options['create_db'];
    } else {
        $create_db = true;
    }
    /* Create SQL code to handle the database */
    $sql = array();
    if ($create_db) {
        $sql[] = "CREATE DATABASE IF NOT EXISTS " . Util::backquote($db_name) . " DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation . ";";
    }
    /**
     * The calling plug-in should include this statement,
     * if necessary, in the $additional_sql parameter
     *
     * $sql[] = "USE " . backquote($db_name);
     */
    /* Execute the SQL statements create above */
    $sql_len = count($sql);
    for ($i = 0; $i < $sql_len; ++$i) {
        PMA_importRunQuery($sql[$i], $sql[$i]);
    }
    /* No longer needed */
    unset($sql);
    /* Run the $additional_sql statements supplied by the caller plug-in */
    if ($additional_sql != null) {
        /* Clean the SQL first */
        $additional_sql_len = count($additional_sql);
        /**
         * Only match tables for now, because CREATE IF NOT EXISTS
         * syntax is lacking or nonexisting for views, triggers,
         * functions, and procedures.
         *
         * See: http://bugs.mysql.com/bug.php?id=15287
         *
         * To the best of my knowledge this is still an issue.
         *
         * $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)';
         */
        $pattern = '/CREATE [^`]*(TABLE)/';
        $replacement = 'CREATE \\1 IF NOT EXISTS';
        /* Change CREATE statements to CREATE IF NOT EXISTS to support
         * inserting into existing structures
         */
        for ($i = 0; $i < $additional_sql_len; ++$i) {
            $additional_sql[$i] = preg_replace($pattern, $replacement, $additional_sql[$i]);
            /* Execute the resulting statements */
            PMA_importRunQuery($additional_sql[$i], $additional_sql[$i]);
        }
    }
    if ($analyses != null) {
        $type_array = array(NONE => "NULL", VARCHAR => "varchar", INT => "int", DECIMAL => "decimal", BIGINT => "bigint", GEOMETRY => 'geometry');
        /* TODO: Do more checking here to make sure they really are matched */
        if (count($tables) != count($analyses)) {
            exit;
        }
        /* Create SQL code to create the tables */
        $num_tables = count($tables);
        for ($i = 0; $i < $num_tables; ++$i) {
            $num_cols = count($tables[$i][COL_NAMES]);
            $tempSQLStr = "CREATE TABLE IF NOT EXISTS " . PMA\libraries\Util::backquote($db_name) . '.' . PMA\libraries\Util::backquote($tables[$i][TBL_NAME]) . " (";
            for ($j = 0; $j < $num_cols; ++$j) {
                $size = $analyses[$i][SIZES][$j];
                if ((int) $size == 0) {
                    $size = 10;
                }
                $tempSQLStr .= PMA\libraries\Util::backquote($tables[$i][COL_NAMES][$j]) . " " . $type_array[$analyses[$i][TYPES][$j]];
                if ($analyses[$i][TYPES][$j] != GEOMETRY) {
                    $tempSQLStr .= "(" . $size . ")";
                }
                if ($j != count($tables[$i][COL_NAMES]) - 1) {
                    $tempSQLStr .= ", ";
                }
            }
            $tempSQLStr .= ") DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation . ";";
            /**
             * Each SQL statement is executed immediately
             * after it is formed so that we don't have
             * to store them in a (possibly large) buffer
             */
            PMA_importRunQuery($tempSQLStr, $tempSQLStr);
        }
    }
    /**
     * Create the SQL statements to insert all the data
     *
     * Only one insert query is formed for each table
     */
    $tempSQLStr = "";
    $col_count = 0;
    $num_tables = count($tables);
    for ($i = 0; $i < $num_tables; ++$i) {
        $num_cols = count($tables[$i][COL_NAMES]);
        $num_rows = count($tables[$i][ROWS]);
        $tempSQLStr = "INSERT INTO " . PMA\libraries\Util::backquote($db_name) . '.' . PMA\libraries\Util::backquote($tables[$i][TBL_NAME]) . " (";
        for ($m = 0; $m < $num_cols; ++$m) {
            $tempSQLStr .= PMA\libraries\Util::backquote($tables[$i][COL_NAMES][$m]);
            if ($m != $num_cols - 1) {
                $tempSQLStr .= ", ";
            }
        }
        $tempSQLStr .= ") VALUES ";
        for ($j = 0; $j < $num_rows; ++$j) {
            $tempSQLStr .= "(";
            for ($k = 0; $k < $num_cols; ++$k) {
                // If fully formatted SQL, no need to enclose
                // with apostrophes, add slashes etc.
                if ($analyses != null && isset($analyses[$i][FORMATTEDSQL][$col_count]) && $analyses[$i][FORMATTEDSQL][$col_count] == true) {
                    $tempSQLStr .= (string) $tables[$i][ROWS][$j][$k];
                } else {
                    if ($analyses != null) {
                        $is_varchar = $analyses[$i][TYPES][$col_count] === VARCHAR;
                    } else {
                        $is_varchar = !is_numeric($tables[$i][ROWS][$j][$k]);
                    }
                    /* Don't put quotes around NULL fields */
                    if (!strcmp($tables[$i][ROWS][$j][$k], 'NULL')) {
                        $is_varchar = false;
                    }
                    $tempSQLStr .= $is_varchar ? "'" : "";
                    $tempSQLStr .= PMA\libraries\Util::sqlAddSlashes((string) $tables[$i][ROWS][$j][$k]);
                    $tempSQLStr .= $is_varchar ? "'" : "";
                }
                if ($k != $num_cols - 1) {
                    $tempSQLStr .= ", ";
                }
                if ($col_count == $num_cols - 1) {
                    $col_count = 0;
                } else {
                    $col_count++;
                }
                /* Delete the cell after we are done with it */
                unset($tables[$i][ROWS][$j][$k]);
            }
            $tempSQLStr .= ")";
            if ($j != $num_rows - 1) {
                $tempSQLStr .= ",\n ";
            }
            $col_count = 0;
            /* Delete the row after we are done with it */
            unset($tables[$i][ROWS][$j]);
        }
        $tempSQLStr .= ";";
        /**
         * Each SQL statement is executed immediately
         * after it is formed so that we don't have
         * to store them in a (possibly large) buffer
         */
        PMA_importRunQuery($tempSQLStr, $tempSQLStr);
    }
    /* No longer needed */
    unset($tempSQLStr);
    /**
     * A work in progress
     */
    /* Add the viewable structures from $additional_sql
     * to $tables so they are also displayed
     */
    $view_pattern = '@VIEW `[^`]+`\\.`([^`]+)@';
    $table_pattern = '@CREATE TABLE IF NOT EXISTS `([^`]+)`@';
    /* Check a third pattern to make sure its not a "USE `db_name`;" statement */
    $regs = array();
    $inTables = false;
    $additional_sql_len = count($additional_sql);
    for ($i = 0; $i < $additional_sql_len; ++$i) {
        preg_match($view_pattern, $additional_sql[$i], $regs);
        if (count($regs) == 0) {
            preg_match($table_pattern, $additional_sql[$i], $regs);
        }
        if (count($regs)) {
            for ($n = 0; $n < $num_tables; ++$n) {
                if (!strcmp($regs[1], $tables[$n][TBL_NAME])) {
                    $inTables = true;
                    break;
                }
            }
            if (!$inTables) {
                $tables[] = array(TBL_NAME => $regs[1]);
            }
        }
        /* Reset the array */
        $regs = array();
        $inTables = false;
    }
    $params = array('db' => (string) $db_name);
    $db_url = 'db_structure.php' . PMA_URL_getCommon($params);
    $db_ops_url = 'db_operations.php' . PMA_URL_getCommon($params);
    $message = '<br /><br />';
    $message .= '<strong>' . __('The following structures have either been created or altered. Here you can:') . '</strong><br />';
    $message .= '<ul><li>' . __("View a structure's contents by clicking on its name.") . '</li>';
    $message .= '<li>' . __('Change any of its settings by clicking the corresponding "Options" link.') . '</li>';
    $message .= '<li>' . __('Edit structure by following the "Structure" link.') . '</li>';
    $message .= sprintf('<br /><li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">' . __('Options') . '</a>)</li>', $db_url, sprintf(__('Go to database: %s'), htmlspecialchars(PMA\libraries\Util::backquote($db_name))), htmlspecialchars($db_name), $db_ops_url, sprintf(__('Edit settings for %s'), htmlspecialchars(PMA\libraries\Util::backquote($db_name))));
    $message .= '<ul>';
    unset($params);
    $num_tables = count($tables);
    for ($i = 0; $i < $num_tables; ++$i) {
        $params = array('db' => (string) $db_name, 'table' => (string) $tables[$i][TBL_NAME]);
        $tbl_url = 'sql.php' . PMA_URL_getCommon($params);
        $tbl_struct_url = 'tbl_structure.php' . PMA_URL_getCommon($params);
        $tbl_ops_url = 'tbl_operations.php' . PMA_URL_getCommon($params);
        unset($params);
        $_table = new Table($tables[$i][TBL_NAME], $db_name);
        if (!$_table->isView()) {
            $message .= sprintf('<li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">' . __('Structure') . '</a>) (<a href="%s" title="%s">' . __('Options') . '</a>)</li>', $tbl_url, sprintf(__('Go to table: %s'), htmlspecialchars(PMA\libraries\Util::backquote($tables[$i][TBL_NAME]))), htmlspecialchars($tables[$i][TBL_NAME]), $tbl_struct_url, sprintf(__('Structure of %s'), htmlspecialchars(PMA\libraries\Util::backquote($tables[$i][TBL_NAME]))), $tbl_ops_url, sprintf(__('Edit settings for %s'), htmlspecialchars(PMA\libraries\Util::backquote($tables[$i][TBL_NAME]))));
        } else {
            $message .= sprintf('<li><a href="%s" title="%s">%s</a></li>', $tbl_url, sprintf(__('Go to view: %s'), htmlspecialchars(PMA\libraries\Util::backquote($tables[$i][TBL_NAME]))), htmlspecialchars($tables[$i][TBL_NAME]));
        }
    }
    $message .= '</ul></ul>';
    global $import_notice;
    $import_notice = $message;
    unset($tables);
}
             $columnMeta['Default'] = $columnMeta['DefaultValue'];
             break;
         case 'NULL':
         case 'CURRENT_TIMESTAMP':
             $columnMeta['Default'] = $columnMeta['DefaultType'];
             break;
     }
     $length = Util\get($_REQUEST, "field_length.{$columnNumber}", $length);
     $submit_attribute = Util\get($_REQUEST, "field_attribute.{$columnNumber}", false);
     $comments_map[$columnMeta['Field']] = Util\get($_REQUEST, "field_comments.{$columnNumber}");
     $mime_map[$columnMeta['Field']] = array_merge($mime_map[$columnMeta['Field']], array('mimetype' => Util\get($_REQUEST, "field_mimetype.{${$columnNumber}}"), 'transformation' => Util\get($_REQUEST, "field_transformation.{${$columnNumber}}"), 'transformation_options' => Util\get($_REQUEST, "field_transformation_options.{${$columnNumber}}")));
 } elseif (isset($fields_meta[$columnNumber])) {
     $columnMeta = $fields_meta[$columnNumber];
     $virtual = array('VIRTUAL', 'PERSISTENT', 'VIRTUAL GENERATED', 'STORED GENERATED');
     if (in_array($columnMeta['Extra'], $virtual)) {
         $tableObj = new Table($GLOBALS['table'], $GLOBALS['db']);
         $expressions = $tableObj->getColumnGenerationExpression($columnMeta['Field']);
         $columnMeta['Expression'] = $expressions[$columnMeta['Field']];
     }
     switch ($columnMeta['Default']) {
         case null:
             if (is_null($columnMeta['Default'])) {
                 // null
                 if ($columnMeta['Null'] == 'YES') {
                     $columnMeta['DefaultType'] = 'NULL';
                     $columnMeta['DefaultValue'] = '';
                 } else {
                     $columnMeta['DefaultType'] = 'NONE';
                     $columnMeta['DefaultValue'] = '';
                 }
             } else {
Esempio n. 15
0
/**
 * Gets all Relations to foreign tables for a given table or
 * optionally a given column in a table
 *
 * @param string $db     the name of the db to check for
 * @param string $table  the name of the table to check for
 * @param string $column the name of the column to check for
 * @param string $source the source for foreign key information
 *
 * @return array    db,table,column
 *
 * @access  public
 */
function PMA_getForeigners($db, $table, $column = '', $source = 'both')
{
    $cfgRelation = PMA_getRelationsParam();
    $foreign = array();
    if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
        $rel_query = '
            SELECT `master_field`,
                `foreign_db`,
                `foreign_table`,
                `foreign_field`
            FROM ' . PMA\libraries\Util::backquote($cfgRelation['db']) . '.' . PMA\libraries\Util::backquote($cfgRelation['relation']) . '
            WHERE `master_db`    = \'' . PMA\libraries\Util::sqlAddSlashes($db) . '\'
                AND `master_table` = \'' . PMA\libraries\Util::sqlAddSlashes($table) . '\' ';
        if (mb_strlen($column)) {
            $rel_query .= ' AND `master_field` = ' . '\'' . PMA\libraries\Util::sqlAddSlashes($column) . '\'';
        }
        $foreign = $GLOBALS['dbi']->fetchResult($rel_query, 'master_field', null, $GLOBALS['controllink']);
    }
    if (($source == 'both' || $source == 'foreign') && mb_strlen($table)) {
        $tableObj = new Table($table, $db);
        $show_create_table = $tableObj->showCreate();
        if ($show_create_table) {
            $parser = new SqlParser\Parser($show_create_table);
            /**
             * @var CreateStatement $stmt
             */
            $stmt = $parser->statements[0];
            $foreign['foreign_keys_data'] = SqlParser\Utils\Table::getForeignKeys($stmt);
        }
    }
    /**
     * Emulating relations for some information_schema tables
     */
    $isInformationSchema = mb_strtolower($db) == 'information_schema';
    $isMysql = mb_strtolower($db) == 'mysql';
    if (($isInformationSchema || $isMysql) && ($source == 'internal' || $source == 'both')) {
        if ($isInformationSchema) {
            $relations_key = 'information_schema_relations';
            include_once './libraries/information_schema_relations.lib.php';
        } else {
            $relations_key = 'mysql_relations';
            include_once './libraries/mysql_relations.lib.php';
        }
        if (isset($GLOBALS[$relations_key][$table])) {
            foreach ($GLOBALS[$relations_key][$table] as $field => $relations) {
                if ((!mb_strlen($column) || $column == $field) && (!isset($foreign[$field]) || !mb_strlen($foreign[$field]))) {
                    $foreign[$field] = $relations;
                }
            }
        }
    }
    return $foreign;
}
 /**
  * Update the table's structure based on $_REQUEST
  *
  * @return boolean $regenerate              true if error occurred
  *
  */
 protected function updateColumns()
 {
     $err_url = 'tbl_structure.php' . PMA_URL_getCommon(array('db' => $this->db, 'table' => $this->table));
     $regenerate = false;
     $field_cnt = count($_REQUEST['field_name']);
     $changes = array();
     $adjust_privileges = array();
     for ($i = 0; $i < $field_cnt; $i++) {
         if (!$this->columnNeedsAlterTable($i)) {
             continue;
         }
         $changes[] = 'CHANGE ' . Table::generateAlter(Util_lib\get($_REQUEST, "field_orig.{$i}", ''), $_REQUEST['field_name'][$i], $_REQUEST['field_type'][$i], $_REQUEST['field_length'][$i], $_REQUEST['field_attribute'][$i], Util_lib\get($_REQUEST, "field_collation.{$i}", ''), Util_lib\get($_REQUEST, "field_null.{$i}", 'NOT NULL'), $_REQUEST['field_default_type'][$i], $_REQUEST['field_default_value'][$i], Util_lib\get($_REQUEST, "field_extra.{$i}", false), Util_lib\get($_REQUEST, "field_comments.{$i}", ''), Util_lib\get($_REQUEST, "field_virtuality.{$i}", ''), Util_lib\get($_REQUEST, "field_expression.{$i}", ''), Util_lib\get($_REQUEST, "field_move_to.{$i}", ''));
         // find the remembered sort expression
         $sorted_col = $this->table_obj->getUiProp(Table::PROP_SORTED_COLUMN);
         // if the old column name is part of the remembered sort expression
         if (mb_strpos($sorted_col, Util::backquote($_REQUEST['field_orig'][$i])) !== false) {
             // delete the whole remembered sort expression
             $this->table_obj->removeUiProp(Table::PROP_SORTED_COLUMN);
         }
         if (isset($_REQUEST['field_adjust_privileges'][$i]) && !empty($_REQUEST['field_adjust_privileges'][$i]) && $_REQUEST['field_orig'][$i] != $_REQUEST['field_name'][$i]) {
             $adjust_privileges[$_REQUEST['field_orig'][$i]] = $_REQUEST['field_name'][$i];
         }
     }
     // end for
     if (count($changes) > 0 || isset($_REQUEST['preview_sql'])) {
         // Builds the primary keys statements and updates the table
         $key_query = '';
         /**
          * this is a little bit more complex
          *
          * @todo if someone selects A_I when altering a column we need to check:
          *  - no other column with A_I
          *  - the column has an index, if not create one
          *
          */
         // To allow replication, we first select the db to use
         // and then run queries on this db.
         if (!$this->dbi->selectDb($this->db)) {
             Util::mysqlDie($this->dbi->getError(), 'USE ' . Util::backquote($this->db) . ';', false, $err_url);
         }
         $sql_query = 'ALTER TABLE ' . Util::backquote($this->table) . ' ';
         $sql_query .= implode(', ', $changes) . $key_query;
         $sql_query .= ';';
         // If there is a request for SQL previewing.
         if (isset($_REQUEST['preview_sql'])) {
             PMA_previewSQL(count($changes) > 0 ? $sql_query : '');
         }
         $columns_with_index = $this->dbi->getTable($this->db, $this->table)->getColumnsWithIndex(PMA_Index::PRIMARY | PMA_Index::UNIQUE | PMA_Index::INDEX | PMA_Index::SPATIAL | PMA_Index::FULLTEXT);
         $changedToBlob = array();
         // While changing the Column Collation
         // First change to BLOB
         for ($i = 0; $i < $field_cnt; $i++) {
             if (isset($_REQUEST['field_collation'][$i]) && isset($_REQUEST['field_collation_orig'][$i]) && $_REQUEST['field_collation'][$i] !== $_REQUEST['field_collation_orig'][$i] && !in_array($_REQUEST['field_orig'][$i], $columns_with_index)) {
                 $secondary_query = 'ALTER TABLE ' . Util::backquote($this->table) . ' CHANGE ' . Util::backquote($_REQUEST['field_orig'][$i]) . ' ' . Util::backquote($_REQUEST['field_orig'][$i]) . ' BLOB;';
                 $this->dbi->query($secondary_query);
                 $changedToBlob[$i] = true;
             } else {
                 $changedToBlob[$i] = false;
             }
         }
         // Then make the requested changes
         $result = $this->dbi->tryQuery($sql_query);
         if ($result !== false) {
             $changed_privileges = $this->adjustColumnPrivileges($adjust_privileges);
             if ($changed_privileges) {
                 $message = Message::success(__('Table %1$s has been altered successfully. Privileges ' . 'have been adjusted.'));
             } else {
                 $message = Message::success(__('Table %1$s has been altered successfully.'));
             }
             $message->addParam($this->table);
             $this->response->addHTML(Util::getMessage($message, $sql_query, 'success'));
         } else {
             // An error happened while inserting/updating a table definition
             // Save the Original Error
             $orig_error = $this->dbi->getError();
             $changes_revert = array();
             // Change back to Original Collation and data type
             for ($i = 0; $i < $field_cnt; $i++) {
                 if ($changedToBlob[$i]) {
                     $changes_revert[] = 'CHANGE ' . Table::generateAlter(Util_lib\get($_REQUEST, "field_orig.{$i}", ''), $_REQUEST['field_name'][$i], $_REQUEST['field_type_orig'][$i], $_REQUEST['field_length_orig'][$i], $_REQUEST['field_attribute_orig'][$i], Util_lib\get($_REQUEST, "field_collation_orig.{$i}", ''), Util_lib\get($_REQUEST, "field_null_orig.{$i}", 'NOT NULL'), $_REQUEST['field_default_type_orig'][$i], $_REQUEST['field_default_value_orig'][$i], Util_lib\get($_REQUEST, "field_extra_orig.{$i}", false), Util_lib\get($_REQUEST, "field_comments_orig.{$i}", ''), Util_lib\get($_REQUEST, "field_virtuality_orig.{$i}", ''), Util_lib\get($_REQUEST, "field_expression_orig.{$i}", ''), Util_lib\get($_REQUEST, "field_move_to_orig.{$i}", ''));
                 }
             }
             $revert_query = 'ALTER TABLE ' . Util::backquote($this->table) . ' ';
             $revert_query .= implode(', ', $changes_revert) . '';
             $revert_query .= ';';
             // Column reverted back to original
             $this->dbi->query($revert_query);
             $this->response->setRequestStatus(false);
             $this->response->addJSON('message', Message::rawError(__('Query error') . ':<br />' . $orig_error));
             $regenerate = true;
         }
     }
     // update field names in relation
     if (isset($_REQUEST['field_orig']) && is_array($_REQUEST['field_orig'])) {
         foreach ($_REQUEST['field_orig'] as $fieldindex => $fieldcontent) {
             if ($_REQUEST['field_name'][$fieldindex] != $fieldcontent) {
                 PMA_REL_renameField($this->db, $this->table, $fieldcontent, $_REQUEST['field_name'][$fieldindex]);
             }
         }
     }
     // update mime types
     if (isset($_REQUEST['field_mimetype']) && is_array($_REQUEST['field_mimetype']) && $GLOBALS['cfg']['BrowseMIME']) {
         foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
             if (isset($_REQUEST['field_name'][$fieldindex]) && mb_strlen($_REQUEST['field_name'][$fieldindex])) {
                 PMA_setMIME($this->db, $this->table, $_REQUEST['field_name'][$fieldindex], $mimetype, $_REQUEST['field_transformation'][$fieldindex], $_REQUEST['field_transformation_options'][$fieldindex], $_REQUEST['field_input_transformation'][$fieldindex], $_REQUEST['field_input_transformation_options'][$fieldindex]);
             }
         }
     }
     return $regenerate;
 }
Esempio n. 17
0
 * Various table operations
 *
 * @package PhpMyAdmin
 */
use PMA\libraries\Partition;
use PMA\libraries\Table;
/**
 *
 */
require_once 'libraries/common.inc.php';
/**
 * functions implementation for this script
 */
require_once 'libraries/check_user_privileges.lib.php';
require_once 'libraries/operations.lib.php';
$pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
/**
 * Load JavaScript files
 */
$response = PMA\libraries\Response::getInstance();
$header = $response->getHeader();
$scripts = $header->getScripts();
$scripts->addFile('tbl_operations.js');
/**
 * Runs common work
 */
require 'libraries/tbl_common.inc.php';
$url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
$url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
/**
 * Gets relation settings
Esempio n. 18
0
 /**
  * Returns the generation expression for virtual columns
  *
  * @param string $column name of the column
  *
  * @return array|boolean associative array of column name and their expressions
  *                       or false on failure
  */
 public function getColumnGenerationExpression($column = null)
 {
     $serverType = Util::getServerType();
     if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION > 50705 && !$GLOBALS['cfg']['Server']['DisableIS']) {
         $sql = "SELECT\n                `COLUMN_NAME` AS `Field`,\n                `GENERATION_EXPRESSION` AS `Expression`\n                FROM\n                `information_schema`.`COLUMNS`\n                WHERE\n                `TABLE_SCHEMA` = '" . Util::sqlAddSlashes($this->_db_name) . "'\n                AND `TABLE_NAME` = '" . Util::sqlAddSlashes($this->_name) . "'";
         if ($column != null) {
             $sql .= " AND  `COLUMN_NAME` = '" . Util::sqlAddSlashes($column) . "'";
         }
         $columns = $this->_dbi->fetchResult($sql, 'Field', 'Expression');
         return $columns;
     }
     $createTable = $this->showCreate();
     if (!$createTable) {
         return false;
     }
     $parser = new Parser($createTable);
     /**
      * @var \SqlParser\Statements\CreateStatement $stmt
      */
     $stmt = $parser->statements[0];
     $fields = Table::getFields($stmt);
     if ($column != null) {
         $expression = isset($fields[$column]['expr']) ? substr($fields[$column]['expr'], 1, -1) : '';
         return array($column => $expression);
     }
     $ret = array();
     foreach ($fields as $field => $options) {
         if (isset($options['expr'])) {
             $ret[$field] = substr($options['expr'], 1, -1);
         }
     }
     return $ret;
 }
/**
 * Move or copy a table
 *
 * @param string $db    current database name
 * @param string $table current table name
 *
 * @return void
 */
function PMA_moveOrCopyTable($db, $table)
{
    /**
     * Selects the database to work with
     */
    $GLOBALS['dbi']->selectDb($db);
    /**
     * $_REQUEST['target_db'] could be empty in case we came from an input field
     * (when there are many databases, no drop-down)
     */
    if (empty($_REQUEST['target_db'])) {
        $_REQUEST['target_db'] = $db;
    }
    /**
     * A target table name has been sent to this script -> do the work
     */
    if (PMA_isValid($_REQUEST['new_name'])) {
        if ($db == $_REQUEST['target_db'] && $table == $_REQUEST['new_name']) {
            if (isset($_REQUEST['submit_move'])) {
                $message = Message::error(__('Can\'t move table to same one!'));
            } else {
                $message = Message::error(__('Can\'t copy table to same one!'));
            }
        } else {
            Table::moveCopy($db, $table, $_REQUEST['target_db'], $_REQUEST['new_name'], $_REQUEST['what'], isset($_REQUEST['submit_move']), 'one_table');
            if (isset($_REQUEST['adjust_privileges']) && !empty($_REQUEST['adjust_privileges'])) {
                if (isset($_REQUEST['submit_move'])) {
                    PMA_AdjustPrivileges_renameOrMoveTable($db, $table, $_REQUEST['target_db'], $_REQUEST['new_name']);
                } else {
                    PMA_AdjustPrivileges_copyTable($db, $table, $_REQUEST['target_db'], $_REQUEST['new_name']);
                }
                if (isset($_REQUEST['submit_move'])) {
                    $message = Message::success(__('Table %s has been moved to %s. Privileges have been ' . 'adjusted.'));
                } else {
                    $message = Message::success(__('Table %s has been copied to %s. Privileges have been ' . 'adjusted.'));
                }
            } else {
                if (isset($_REQUEST['submit_move'])) {
                    $message = Message::success(__('Table %s has been moved to %s.'));
                } else {
                    $message = Message::success(__('Table %s has been copied to %s.'));
                }
            }
            $old = PMA\libraries\Util::backquote($db) . '.' . PMA\libraries\Util::backquote($table);
            $message->addParam($old);
            $new = PMA\libraries\Util::backquote($_REQUEST['target_db']) . '.' . PMA\libraries\Util::backquote($_REQUEST['new_name']);
            $message->addParam($new);
            /* Check: Work on new table or on old table? */
            if (isset($_REQUEST['submit_move']) || PMA_isValid($_REQUEST['switch_to_new'])) {
            }
        }
    } else {
        /**
         * No new name for the table!
         */
        $message = Message::error(__('The table name is empty!'));
    }
    if ($GLOBALS['is_ajax_request'] == true) {
        $response = PMA\libraries\Response::getInstance();
        $response->addJSON('message', $message);
        if ($message->isSuccess()) {
            $response->addJSON('db', $GLOBALS['db']);
        } else {
            $response->setRequestStatus(false);
        }
        exit;
    }
}
Esempio n. 20
0
/**
 * Builds or execute queries for multiple elements, depending on $query_type
 *
 * @param string $query_type  query type
 * @param array  $selected    selected tables
 * @param string $db          db name
 * @param string $table       table name
 * @param array  $views       table views
 * @param string $primary     table primary
 * @param string $from_prefix from prefix original
 * @param string $to_prefix   to prefix original
 *
 * @return array
 */
function PMA_buildOrExecuteQueryForMulti($query_type, $selected, $db, $table, $views, $primary, $from_prefix, $to_prefix)
{
    $rebuild_database_list = false;
    $reload = null;
    $a_query = null;
    $sql_query = '';
    $sql_query_views = null;
    // whether to run query after each pass
    $run_parts = false;
    // whether to execute the query at the end (to display results)
    $execute_query_later = false;
    $result = null;
    if ($query_type == 'drop_tbl') {
        $sql_query_views = '';
    }
    $selected_cnt = count($selected);
    $deletes = false;
    $copy_tbl = false;
    for ($i = 0; $i < $selected_cnt; $i++) {
        switch ($query_type) {
            case 'row_delete':
                $deletes = true;
                $a_query = $selected[$i];
                $run_parts = true;
                break;
            case 'drop_db':
                PMA_relationsCleanupDatabase($selected[$i]);
                $a_query = 'DROP DATABASE ' . PMA\libraries\Util::backquote($selected[$i]);
                $reload = 1;
                $run_parts = true;
                $rebuild_database_list = true;
                break;
            case 'drop_tbl':
                PMA_relationsCleanupTable($db, $selected[$i]);
                $current = $selected[$i];
                if (!empty($views) && in_array($current, $views)) {
                    $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ') . PMA\libraries\Util::backquote($current);
                } else {
                    $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ') . PMA\libraries\Util::backquote($current);
                }
                $reload = 1;
                break;
            case 'check_tbl':
                $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ') . PMA\libraries\Util::backquote($selected[$i]);
                $execute_query_later = true;
                break;
            case 'optimize_tbl':
                $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ') . PMA\libraries\Util::backquote($selected[$i]);
                $execute_query_later = true;
                break;
            case 'analyze_tbl':
                $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ') . PMA\libraries\Util::backquote($selected[$i]);
                $execute_query_later = true;
                break;
            case 'checksum_tbl':
                $sql_query .= (empty($sql_query) ? 'CHECKSUM TABLE ' : ', ') . PMA\libraries\Util::backquote($selected[$i]);
                $execute_query_later = true;
                break;
            case 'repair_tbl':
                $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ') . PMA\libraries\Util::backquote($selected[$i]);
                $execute_query_later = true;
                break;
            case 'empty_tbl':
                $deletes = true;
                $a_query = 'TRUNCATE ';
                $a_query .= PMA\libraries\Util::backquote($selected[$i]);
                $run_parts = true;
                break;
            case 'drop_fld':
                PMA_relationsCleanupColumn($db, $table, $selected[$i]);
                $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table) : ',') . ' DROP ' . PMA\libraries\Util::backquote($selected[$i]) . ($i == $selected_cnt - 1 ? ';' : '');
                break;
            case 'primary_fld':
                $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ') . PMA\libraries\Util::backquote($selected[$i]) . ($i == $selected_cnt - 1 ? ');' : '');
                break;
            case 'index_fld':
                $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table) . ' ADD INDEX( ' : ', ') . PMA\libraries\Util::backquote($selected[$i]) . ($i == $selected_cnt - 1 ? ');' : '');
                break;
            case 'unique_fld':
                $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table) . ' ADD UNIQUE( ' : ', ') . PMA\libraries\Util::backquote($selected[$i]) . ($i == $selected_cnt - 1 ? ');' : '');
                break;
            case 'spatial_fld':
                $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table) . ' ADD SPATIAL( ' : ', ') . PMA\libraries\Util::backquote($selected[$i]) . ($i == $selected_cnt - 1 ? ');' : '');
                break;
            case 'fulltext_fld':
                $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table) . ' ADD FULLTEXT( ' : ', ') . PMA\libraries\Util::backquote($selected[$i]) . ($i == $selected_cnt - 1 ? ');' : '');
                break;
            case 'add_prefix_tbl':
                $newtablename = $_POST['add_prefix'] . $selected[$i];
                // ADD PREFIX TO TABLE NAME
                $a_query = 'ALTER TABLE ' . PMA\libraries\Util::backquote($selected[$i]) . ' RENAME ' . PMA\libraries\Util::backquote($newtablename);
                $run_parts = true;
                break;
            case 'replace_prefix_tbl':
                $current = $selected[$i];
                $subFromPrefix = mb_substr($current, 0, mb_strlen($from_prefix));
                if ($subFromPrefix == $from_prefix) {
                    $newtablename = $to_prefix . mb_substr($current, mb_strlen($from_prefix));
                } else {
                    $newtablename = $current;
                }
                // CHANGE PREFIX PATTERN
                $a_query = 'ALTER TABLE ' . PMA\libraries\Util::backquote($selected[$i]) . ' RENAME ' . PMA\libraries\Util::backquote($newtablename);
                $run_parts = true;
                break;
            case 'copy_tbl_change_prefix':
                $run_parts = true;
                $copy_tbl = true;
                $current = $selected[$i];
                $newtablename = $to_prefix . mb_substr($current, mb_strlen($from_prefix));
                // COPY TABLE AND CHANGE PREFIX PATTERN
                Table::moveCopy($db, $current, $db, $newtablename, 'data', false, 'one_table');
                break;
            case 'copy_tbl':
                $run_parts = true;
                $copy_tbl = true;
                Table::moveCopy($db, $selected[$i], $_POST['target_db'], $selected[$i], $_POST['what'], false, 'one_table');
                if (isset($_POST['adjust_privileges']) && !empty($_POST['adjust_privileges'])) {
                    include_once 'operations.lib.php';
                    PMA_AdjustPrivileges_copyTable($db, $selected[$i], $_POST['target_db'], $selected[$i]);
                }
                break;
        }
        // end switch
        // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
        // statements will be run at once below
        if ($run_parts && !$copy_tbl) {
            $sql_query .= $a_query . ';' . "\n";
            if ($query_type != 'drop_db') {
                $GLOBALS['dbi']->selectDb($db);
            }
            $result = $GLOBALS['dbi']->query($a_query);
            if ($query_type == 'drop_db') {
                PMA_clearTransformations($selected[$i]);
            } elseif ($query_type == 'drop_tbl') {
                PMA_clearTransformations($db, $selected[$i]);
            } else {
                if ($query_type == 'drop_fld') {
                    PMA_clearTransformations($db, $table, $selected[$i]);
                }
            }
        }
        // end if
    }
    // end for
    if ($deletes && !empty($_REQUEST['pos'])) {
        $_REQUEST['pos'] = PMA_calculatePosForLastPage($db, $table, isset($_REQUEST['pos']) ? $_REQUEST['pos'] : null);
    }
    return array($result, $rebuild_database_list, $reload, $run_parts, $execute_query_later, $sql_query, $sql_query_views);
}