Exemplo n.º 1
0
/**
 * Create relevant index statements
 *
 * @param array   $index         an array of index columns
 * @param string  $index_choice  index choice that which represents
 *                               the index type of $indexed_fields
 * @param boolean $is_create_tbl true if requirement is to get the statement
 *                               for table creation
 *
 * @return array an array of sql statements for indexes
 */
function PMA_buildIndexStatements($index, $index_choice, $is_create_tbl = true)
{
    $statement = array();
    if (!count($index)) {
        return $statement;
    }
    $sql_query = PMA_getStatementPrefix($is_create_tbl) . ' ' . $index_choice;
    if (!empty($index['Key_name']) && $index['Key_name'] != 'PRIMARY') {
        $sql_query .= ' ' . PMA_Util::backquote($index['Key_name']);
    }
    $index_fields = array();
    foreach ($index['columns'] as $key => $column) {
        $index_fields[$key] = PMA_Util::backquote($_REQUEST['field_name'][$column['col_index']]);
        if ($column['size']) {
            $index_fields[$key] .= '(' . $column['size'] . ')';
        }
    }
    // end while
    $sql_query .= ' (' . implode(', ', $index_fields) . ')';
    $keyBlockSizes = $index['Key_block_size'];
    if (!empty($keyBlockSizes)) {
        $sql_query .= " KEY_BLOCK_SIZE = " . PMA_Util::sqlAddSlashes($keyBlockSizes);
    }
    // specifying index type is allowed only for primary, unique and index only
    $type = $index['Index_type'];
    if ($index['Index_choice'] != 'SPATIAL' && $index['Index_choice'] != 'FULLTEXT' && in_array($type, PMA_Index::getIndexTypes())) {
        $sql_query .= ' USING ' . $type;
    }
    $parser = $index['Parser'];
    if ($index['Index_choice'] == 'FULLTEXT' && !empty($parser)) {
        $sql_query .= " WITH PARSER " . PMA_Util::sqlAddSlashes($parser);
    }
    $comment = $index['Index_comment'];
    if (!empty($comment)) {
        $sql_query .= " COMMENT '" . PMA_Util::sqlAddSlashes($comment) . "'";
    }
    $statement[] = $sql_query;
    return $statement;
}
 /**
  * Function to get the sql query for index creation or edit
  *
  * @param PMA_Index $index  current index
  * @param bool      &$error whether error occurred or not
  *
  * @return string
  */
 public function getSqlQueryForIndexCreateOrEdit($index, &$error)
 {
     // $sql_query is the one displayed in the query box
     $sql_query = sprintf('ALTER TABLE %s.%s', PMA_Util::backquote($this->_db_name), PMA_Util::backquote($this->_name));
     // Drops the old index
     if (!empty($_REQUEST['old_index'])) {
         if ($_REQUEST['old_index'] == 'PRIMARY') {
             $sql_query .= ' DROP PRIMARY KEY,';
         } else {
             $sql_query .= sprintf(' DROP INDEX %s,', PMA_Util::backquote($_REQUEST['old_index']));
         }
     }
     // end if
     // Builds the new one
     switch ($index->getChoice()) {
         case 'PRIMARY':
             if ($index->getName() == '') {
                 $index->setName('PRIMARY');
             } elseif ($index->getName() != 'PRIMARY') {
                 $error = PMA_Message::error(__('The name of the primary key must be "PRIMARY"!'));
             }
             $sql_query .= ' ADD PRIMARY KEY';
             break;
         case 'FULLTEXT':
         case 'UNIQUE':
         case 'INDEX':
         case 'SPATIAL':
             if ($index->getName() == 'PRIMARY') {
                 $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!'));
             }
             $sql_query .= sprintf(' ADD %s ', $index->getChoice());
             if ($index->getName()) {
                 $sql_query .= PMA_Util::backquote($index->getName());
             }
             break;
     }
     // end switch
     $index_fields = array();
     foreach ($index->getColumns() as $key => $column) {
         $index_fields[$key] = PMA_Util::backquote($column->getName());
         if ($column->getSubPart()) {
             $index_fields[$key] .= '(' . $column->getSubPart() . ')';
         }
     }
     // end while
     if (empty($index_fields)) {
         $error = PMA_Message::error(__('No index parts defined!'));
     } else {
         $sql_query .= ' (' . implode(', ', $index_fields) . ')';
     }
     $keyBlockSizes = $index->getKeyBlockSize();
     if (!empty($keyBlockSizes)) {
         $sql_query .= sprintf(' KEY_BLOCK_SIZE = ', PMA_Util::sqlAddSlashes($keyBlockSizes));
     }
     // specifying index type is allowed only for primary, unique and index only
     $type = $index->getType();
     if ($index->getChoice() != 'SPATIAL' && $index->getChoice() != 'FULLTEXT' && in_array($type, PMA_Index::getIndexTypes())) {
         $sql_query .= ' USING ' . $type;
     }
     $parser = $index->getParser();
     if ($index->getChoice() == 'FULLTEXT' && !empty($parser)) {
         $sql_query .= ' WITH PARSER ' . PMA_Util::sqlAddSlashes($parser);
     }
     $comment = $index->getComment();
     if (!empty($comment)) {
         $sql_query .= sprintf(" COMMENT '%s'", PMA_Util::sqlAddSlashes($comment));
     }
     $sql_query .= ';';
     return $sql_query;
 }
 /**
  * Returns HTML for the index type selector
  *
  * @return string HTML for the index type selector
  */
 public function generateIndexTypeSelector()
 {
     $types = array("" => "--");
     foreach (PMA_Index::getIndexTypes() as $type) {
         $types[$type] = $type;
     }
     return PMA_Util::getDropdown("index[Index_type]", $types, $this->_type, "select_index_type");
 }