Generate the charset query part
static public getCharsetQueryPart ( string $collation, $override = false ) : string | ||
$collation | string | Collation |
return | string |
if ($response->isAjax()) { $response->setRequestStatus(false); $response->addJSON('message', Message::error(__('No databases selected.'))); } else { PMA_sendHeaderLocation($uri); } exit; } } // end if (ensures db exists) /** * Changes database charset if requested by the user */ if (isset($_REQUEST['submitcollation']) && isset($_REQUEST['db_collation']) && !empty($_REQUEST['db_collation'])) { list($db_charset) = explode('_', $_REQUEST['db_collation']); $sql_query = 'ALTER DATABASE ' . PMA\libraries\Util::backquote($db) . ' DEFAULT' . Util::getCharsetQueryPart($_REQUEST['db_collation']); $result = $GLOBALS['dbi']->query($sql_query); $message = Message::success(); unset($db_charset); /** * If we are in an Ajax request, let us stop the execution here. Necessary for * db charset change action on db_operations.php. If this causes a bug on * other pages, we might have to move this to a different location. */ if ($GLOBALS['is_ajax_request'] == true) { $response = PMA\libraries\Response::getInstance(); $response->setRequestStatus($message->isSuccess()); $response->addJSON('message', $message); exit; } }
/** * Get table alters array * * @param Table $pma_table The Table object * @param string $pack_keys pack keys * @param string $checksum value of checksum * @param string $page_checksum value of page checksum * @param string $delay_key_write delay key write * @param string $row_format row format * @param string $newTblStorageEngine table storage engine * @param string $transactional value of transactional * @param string $tbl_collation collation of the table * * @return array $table_alters */ function PMA_getTableAltersArray($pma_table, $pack_keys, $checksum, $page_checksum, $delay_key_write, $row_format, $newTblStorageEngine, $transactional, $tbl_collation) { global $auto_increment; $table_alters = array(); if (isset($_REQUEST['comment']) && urldecode($_REQUEST['prev_comment']) !== $_REQUEST['comment']) { $table_alters[] = 'COMMENT = \'' . $GLOBALS['dbi']->escapeString($_REQUEST['comment']) . '\''; } if (!empty($newTblStorageEngine) && mb_strtolower($newTblStorageEngine) !== mb_strtolower($GLOBALS['tbl_storage_engine'])) { $table_alters[] = 'ENGINE = ' . $newTblStorageEngine; } if (!empty($_REQUEST['tbl_collation']) && $_REQUEST['tbl_collation'] !== $tbl_collation) { $table_alters[] = 'DEFAULT ' . Util::getCharsetQueryPart($_REQUEST['tbl_collation']); } if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'ISAM')) && isset($_REQUEST['new_pack_keys']) && $_REQUEST['new_pack_keys'] != (string) $pack_keys) { $table_alters[] = 'pack_keys = ' . $_REQUEST['new_pack_keys']; } $_REQUEST['new_checksum'] = empty($_REQUEST['new_checksum']) ? '0' : '1'; if ($pma_table->isEngine(array('MYISAM', 'ARIA')) && $_REQUEST['new_checksum'] !== $checksum) { $table_alters[] = 'checksum = ' . $_REQUEST['new_checksum']; } $_REQUEST['new_transactional'] = empty($_REQUEST['new_transactional']) ? '0' : '1'; if ($pma_table->isEngine('ARIA') && $_REQUEST['new_transactional'] !== $transactional) { $table_alters[] = 'TRANSACTIONAL = ' . $_REQUEST['new_transactional']; } $_REQUEST['new_page_checksum'] = empty($_REQUEST['new_page_checksum']) ? '0' : '1'; if ($pma_table->isEngine('ARIA') && $_REQUEST['new_page_checksum'] !== $page_checksum) { $table_alters[] = 'PAGE_CHECKSUM = ' . $_REQUEST['new_page_checksum']; } $_REQUEST['new_delay_key_write'] = empty($_REQUEST['new_delay_key_write']) ? '0' : '1'; if ($pma_table->isEngine(array('MYISAM', 'ARIA')) && $_REQUEST['new_delay_key_write'] !== $delay_key_write) { $table_alters[] = 'delay_key_write = ' . $_REQUEST['new_delay_key_write']; } if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'INNODB', 'PBXT')) && !empty($_REQUEST['new_auto_increment']) && (!isset($auto_increment) || $_REQUEST['new_auto_increment'] !== $auto_increment)) { $table_alters[] = 'auto_increment = ' . $GLOBALS['dbi']->escapeString($_REQUEST['new_auto_increment']); } if (!empty($_REQUEST['new_row_format'])) { $newRowFormat = $_REQUEST['new_row_format']; $newRowFormatLower = mb_strtolower($newRowFormat); if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'INNODB', 'PBXT')) && (strlen($row_format) === 0 || $newRowFormatLower !== mb_strtolower($row_format))) { $table_alters[] = 'ROW_FORMAT = ' . $GLOBALS['dbi']->escapeString($newRowFormat); } } return $table_alters; }
/** * generates column specification for ALTER or CREATE TABLE syntax * * @param string $name name * @param string $type type ('INT', 'VARCHAR', 'BIT', ...) * @param string $length length ('2', '5,2', '', ...) * @param string $attribute attribute * @param string $collation collation * @param bool|string $null with 'NULL' or 'NOT NULL' * @param string $default_type whether default is CURRENT_TIMESTAMP, * NULL, NONE, USER_DEFINED * @param string $default_value default value for USER_DEFINED * default type * @param string $extra 'AUTO_INCREMENT' * @param string $comment field comment * @param string $virtuality virtuality of the column * @param string $expression expression for the virtual column * @param string $move_to new position for column * * @todo move into class PMA_Column * @todo on the interface, some js to clear the default value when the * default current_timestamp is checked * * @return string field specification */ static function generateFieldSpec($name, $type, $length = '', $attribute = '', $collation = '', $null = false, $default_type = 'USER_DEFINED', $default_value = '', $extra = '', $comment = '', $virtuality = '', $expression = '', $move_to = '') { $is_timestamp = mb_strpos(mb_strtoupper($type), 'TIMESTAMP') !== false; $query = Util::backquote($name) . ' ' . $type; // allow the possibility of a length for TIME, DATETIME and TIMESTAMP // (will work on MySQL >= 5.6.4) // // MySQL permits a non-standard syntax for FLOAT and DOUBLE, // see https://dev.mysql.com/doc/refman/5.5/en/floating-point-types.html // $pattern = '@^(DATE|TINYBLOB|TINYTEXT|BLOB|TEXT|' . 'MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN|UUID)$@i'; if (strlen($length) !== 0 && !preg_match($pattern, $type)) { // Note: The variable $length here can contain several other things // besides length - ENUM/SET value or length of DECIMAL (eg. 12,3) // so we can't just convert it to integer $query .= '(' . $length . ')'; } if ($virtuality) { $query .= ' AS (' . $expression . ') ' . $virtuality; } else { if ($attribute != '') { $query .= ' ' . $attribute; if ($is_timestamp && preg_match('/TIMESTAMP/i', $attribute) && strlen($length) !== 0 && $length !== 0) { $query .= '(' . $length . ')'; } } $matches = preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR|ENUM|SET)$@i', $type); if (!empty($collation) && $collation != 'NULL' && $matches) { $query .= Util::getCharsetQueryPart($collation, true); } if ($null !== false) { if ($null == 'NULL') { $query .= ' NULL'; } else { $query .= ' NOT NULL'; } } switch ($default_type) { case 'USER_DEFINED': if ($is_timestamp && $default_value === '0') { // a TIMESTAMP does not accept DEFAULT '0' // but DEFAULT 0 works $query .= ' DEFAULT 0'; } elseif ($type == 'BIT') { $query .= ' DEFAULT b\'' . preg_replace('/[^01]/', '0', $default_value) . '\''; } elseif ($type == 'BOOLEAN') { if (preg_match('/^1|T|TRUE|YES$/i', $default_value)) { $query .= ' DEFAULT TRUE'; } elseif (preg_match('/^0|F|FALSE|NO$/i', $default_value)) { $query .= ' DEFAULT FALSE'; } else { // Invalid BOOLEAN value $query .= ' DEFAULT \'' . $GLOBALS['dbi']->escapeString($default_value) . '\''; } } elseif ($type == 'BINARY' || $type == 'VARBINARY') { $query .= ' DEFAULT 0x' . $default_value; } else { $query .= ' DEFAULT \'' . $GLOBALS['dbi']->escapeString($default_value) . '\''; } break; /** @noinspection PhpMissingBreakStatementInspection */ /** @noinspection PhpMissingBreakStatementInspection */ case 'NULL': // If user uncheck null checkbox and not change default value null, // default value will be ignored. if ($null !== false && $null !== 'NULL') { break; } // else fall-through intended, no break here // else fall-through intended, no break here case 'CURRENT_TIMESTAMP': $query .= ' DEFAULT ' . $default_type; if (strlen($length) !== 0 && $length !== 0 && $is_timestamp && $default_type !== 'NULL') { $query .= '(' . $length . ')'; } break; case 'NONE': default: break; } if (!empty($extra)) { $query .= ' ' . $extra; } } if (!empty($comment)) { $query .= " COMMENT '" . $GLOBALS['dbi']->escapeString($comment) . "'"; } // move column if ($move_to == '-first') { // dash can't appear as part of column name $query .= ' FIRST'; } elseif ($move_to != '') { $query .= ' AFTER ' . Util::backquote($move_to); } return $query; }
/** * Handles creating a new database * * @return void */ public function createDatabaseAction() { /** * Builds and executes the db creation sql query */ $sql_query = 'CREATE DATABASE ' . Util::backquote($_POST['new_db']); if (!empty($_POST['db_collation'])) { list($db_charset) = explode('_', $_POST['db_collation']); $charsets = Charsets::getMySQLCharsets(); $collations = Charsets::getMySQLCollations(); if (in_array($db_charset, $charsets) && in_array($_POST['db_collation'], $collations[$db_charset])) { $sql_query .= ' DEFAULT' . Util::getCharsetQueryPart($_POST['db_collation']); } } $sql_query .= ';'; $result = $GLOBALS['dbi']->tryQuery($sql_query); if (!$result) { // avoid displaying the not-created db name in header or navi panel $GLOBALS['db'] = ''; $message = Message::rawError($GLOBALS['dbi']->getError()); $this->response->setRequestStatus(false); $this->response->addJSON('message', $message); } else { $GLOBALS['db'] = $_POST['new_db']; $message = Message::success(__('Database %1$s has been created.')); $message->addParam($_POST['new_db']); $this->response->addJSON('message', $message); $this->response->addJSON('sql_query', Util::getMessage(null, $sql_query, 'success')); $url_query = URL::getCommon(array('db' => $_POST['new_db'])); $this->response->addJSON('url_query', Util::getScriptNameForOption($GLOBALS['cfg']['DefaultTabDatabase'], 'database') . $url_query . '&db=' . urlencode($_POST['new_db'])); } }
/** * Function to get table creation sql query * * @param string $db database name * @param string $table table name * * @return string */ function PMA_getTableCreationQuery($db, $table) { // get column addition statements $sql_statement = PMA_getColumnCreationStatements(true); // Builds the 'create table' statement $sql_query = 'CREATE TABLE ' . PMA\libraries\Util::backquote($db) . '.' . PMA\libraries\Util::backquote(trim($table)) . ' (' . $sql_statement . ')'; // Adds table type, character set, comments and partition definition if (!empty($_REQUEST['tbl_storage_engine']) && $_REQUEST['tbl_storage_engine'] != 'Default') { $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine']; } if (!empty($_REQUEST['tbl_collation'])) { $sql_query .= Util::getCharsetQueryPart($_REQUEST['tbl_collation']); } if (!empty($_REQUEST['connection']) && !empty($_REQUEST['tbl_storage_engine']) && $_REQUEST['tbl_storage_engine'] == 'FEDERATED') { $sql_query .= " CONNECTION = '" . $GLOBALS['dbi']->escapeString($_REQUEST['connection']) . "'"; } if (!empty($_REQUEST['comment'])) { $sql_query .= ' COMMENT = \'' . $GLOBALS['dbi']->escapeString($_REQUEST['comment']) . '\''; } $sql_query .= PMA_getPartitionsDefinition(); $sql_query .= ';'; return $sql_query; }