/** * Test for PMA_generateCharsetQueryPart * * @param bool $drizzle Value for PMA_DRIZZLE * @param string $collation Collation * @param string $expected Expected Charset Query * * @return void * @test * @dataProvider charsetQueryData */ public function testGenerateCharsetQueryPart( $drizzle, $collation, $expected ) { if (! PMA_HAS_RUNKIT) { $this->markTestSkipped( 'Cannot redefine constant - missing runkit extension' ); } else { $restoreDrizzle = ''; if (defined('PMA_DRIZZLE')) { $restoreDrizzle = PMA_DRIZZLE; runkit_constant_redefine('PMA_DRIZZLE', $drizzle); } else { $restoreDrizzle = 'PMA_TEST_CONSTANT_REMOVE'; define('PMA_DRIZZLE', $drizzle); } $this->assertEquals( $expected, PMA_generateCharsetQueryPart($collation) ); if ($restoreDrizzle === 'PMA_TEST_CONSTANT_REMOVE') { runkit_constant_remove('PMA_DRIZZLE'); } else { runkit_constant_redefine('PMA_DRIZZLE', $restoreDrizzle); } } }
} if (!isset($newname) || !strlen($newname)) { $message = PMA_Message::error('strDatabaseEmpty'); } else { $sql_query = ''; // in case target db exists $_error = false; if ($move || isset($create_database_before_copying) && $create_database_before_copying) { // lower_case_table_names=1 `DB` becomes `db` $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1); if ($lower_case_table_names === '1') { $newname = strtolower($newname); } $local_query = 'CREATE DATABASE ' . PMA_backquote($newname); if (isset($db_collation)) { $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation); } $local_query .= ';'; $sql_query = $local_query; PMA_DBI_query($local_query); // rebuild the database list because PMA_Table::moveCopy // checks in this list if the target db exists $GLOBALS['pma']->databases->build(); } if (isset($GLOBALS['add_constraints'])) { $GLOBALS['sql_constraints_query_full_db'] = ''; } $tables_full = PMA_DBI_get_tables_full($db); $views = array(); foreach ($tables_full as $each_table => $tmp) { // to be able to rename a db containing views, we
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' . PMA_generateCharsetQueryPart($_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; } }
/** * 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 .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']); } if (!empty($_REQUEST['connection']) && !empty($_REQUEST['tbl_storage_engine']) && $_REQUEST['tbl_storage_engine'] == 'FEDERATED') { $sql_query .= " CONNECTION = '" . PMA\libraries\Util::sqlAddSlashes($_REQUEST['connection']) . "'"; } if (!empty($_REQUEST['comment'])) { $sql_query .= ' COMMENT = \'' . PMA\libraries\Util::sqlAddSlashes($_REQUEST['comment']) . '\''; } $sql_query .= PMA_getPartitionsDefinition(); $sql_query .= ';'; return $sql_query; }
function PMA_generateAlterTable($oldcol, $newcol, $full_field_type, $collation, $null, $default, $default_current_timestamp, $extra, $comment = '') { // $default_current_timestamp has priority over $default // TODO: on the interface, some js to clear the default value // when the default current_timestamp is checked $query = PMA_backquote($oldcol) . ' ' . PMA_backquote($newcol) . ' ' . $full_field_type; if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($collation) && $collation != 'NULL' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR\\(\\d+\\)|CHAR\\(\\d+\\))$@i', $full_field_type)) { $query .= PMA_generateCharsetQueryPart($collation); } if (!empty($null)) { $query .= ' NOT NULL'; } else { $query .= ' NULL'; } if ($default_current_timestamp && strpos(' ' . strtoupper($full_field_type), 'TIMESTAMP') == 1) { $query .= ' DEFAULT CURRENT_TIMESTAMP'; // 0 is empty in PHP } elseif (!empty($default) || $default == '0') { if (strtoupper($default) == 'NULL') { $query .= ' DEFAULT NULL'; } else { $query .= ' DEFAULT \'' . PMA_sqlAddslashes($default) . '\''; } } if (!empty($extra)) { $query .= ' ' . $extra; } if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($comment)) { $query .= " COMMENT '" . PMA_sqlAddslashes($comment) . "'"; } return $query; }
* Updates table comment, type and options if required */ if (isset($submitcomment)) { if (empty($prev_comment) || urldecode($prev_comment) != $comment) { $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\''; $result = PMA_DBI_query($sql_query); $message = $strSuccess; } } if (isset($submittype)) { $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' TYPE = ' . $tbl_type; $result = PMA_DBI_query($sql_query); $message = $strSuccess; } if (isset($submitcollation)) { $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' DEFAULT' . PMA_generateCharsetQueryPart($tbl_collation); $result = PMA_DBI_query($sql_query); $message = $strSuccess; unset($tbl_collation); } if (isset($submitoptions)) { $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . (isset($pack_keys) ? ' pack_keys=1' : ' pack_keys=0') . (isset($checksum) ? ' checksum=1' : ' checksum=0') . (isset($delay_key_write) ? ' delay_key_write=1' : ' delay_key_write=0') . (!empty($auto_increment) ? ' auto_increment=' . PMA_sqlAddslashes($auto_increment) : ''); $result = PMA_DBI_query($sql_query); $message = $strSuccess; } /** * Reordering the table has been requested by the user */ if (isset($submitorderby) && !empty($order_field)) { $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ORDER BY ' . PMA_backquote(urldecode($order_field)); if (isset($order_order) && $order_order == 'desc') {
// Not a valid db name -> back to the welcome page if (strlen($db)) { $is_db = PMA_DBI_select_db($db); } if (! strlen($db) || !$is_db) { PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php?' . PMA_generate_common_url('', '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1'); exit; } } // end if (ensures db exists) /** * Changes database charset if requested by the user */ if (isset($submitcollation) && !empty($db_collation)) { list($db_charset) = explode('_', $db_collation); $sql_query = 'ALTER DATABASE ' . PMA_backquote($db) . ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation); $result = PMA_DBI_query($sql_query); $message = PMA_Message::success(); unset($db_charset, $db_collation); } $GLOBALS['js_include'][] = 'functions.js'; require_once './libraries/header.inc.php'; /** * Set parameters for links */ $url_query = PMA_generate_common_url($db); ?>
if (strlen($fulltext)) { $sql_query .= ', FULLTEXT (' . $fulltext . ')'; $query_cpy .= ',' . "\n" . ' FULLTEXT (' . $fulltext . ')'; } unset($fulltext); // Builds the 'create table' statement $sql_query = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $sql_query . ')'; $query_cpy = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $query_cpy . "\n" . ')'; // Adds table type, character set and comments if (!empty($tbl_type) && $tbl_type != 'Default') { $sql_query .= ' ' . PMA_ENGINE_KEYWORD . ' = ' . $tbl_type; $query_cpy .= "\n" . PMA_ENGINE_KEYWORD . ' = ' . $tbl_type; } if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($tbl_collation)) { $sql_query .= PMA_generateCharsetQueryPart($tbl_collation); $query_cpy .= "\n" . PMA_generateCharsetQueryPart($tbl_collation); } if (!empty($comment)) { $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\''; $query_cpy .= "\n" . 'COMMENT = \'' . PMA_sqlAddslashes($comment) . '\''; } // Executes the query $error_create = false; $result = PMA_DBI_try_query($sql_query) or $error_create = true; if ($error_create == false) { $sql_query = $query_cpy . ';'; unset($query_cpy); $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenCreated; // garvin: If comments were sent, enable relation stuff require_once './libs/relation.lib.php'; require_once './libs/transformations.lib.php';
/** * generates column specification for ALTER or CREATE TABLE syntax * * @param string $name name * @param string $type type ('INT', 'VARCHAR', 'BIT', ...) * @param string $index index * @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 array &$field_primary list of fields for PRIMARY KEY * @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, $index, $length = '', $attribute = '', $collation = '', $null = false, $default_type = 'USER_DEFINED', $default_value = '', $extra = '', $comment = '', &$field_primary = null, $move_to = '') { $is_timestamp = strpos(strtoupper($type), 'TIMESTAMP') !== false; $query = PMA_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 http://dev.mysql.com/doc/refman/5.5/en/floating-point-types.html // if ($length != '' && !preg_match('@^(DATE|TINYBLOB|TINYTEXT|BLOB|TEXT|' . 'MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN|UUID)$@i', $type)) { $query .= '(' . $length . ')'; } if ($attribute != '') { $query .= ' ' . $attribute; } $matches = preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR|ENUM|SET)$@i', $type); if (!empty($collation) && $collation != 'NULL' && $matches) { $query .= PMA_generateCharsetQueryPart($collation); } 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 \'' . PMA_Util::sqlAddSlashes($default_value) . '\''; } } else { $query .= ' DEFAULT \'' . PMA_Util::sqlAddSlashes($default_value) . '\''; } break; case 'NULL': // If user uncheck null checkbox and not change default value null, // default value will be ignored. if ($null !== false && $null != 'NULL') { break; } // otherwise, fall to next case (no break; here) // otherwise, fall to next case (no break; here) case 'CURRENT_TIMESTAMP': $query .= ' DEFAULT ' . $default_type; break; case 'NONE': default: break; } if (!empty($extra)) { $query .= ' ' . $extra; // Force an auto_increment field to be part of the primary key // even if user did not tick the PK box; if ($extra == 'AUTO_INCREMENT') { $primary_cnt = count($field_primary); if (1 == $primary_cnt) { for ($j = 0; $j < $primary_cnt; $j++) { if ($field_primary[$j] == $index) { break; } } if (isset($field_primary[$j]) && $field_primary[$j] == $index) { $query .= ' PRIMARY KEY'; unset($field_primary[$j]); } } else { // but the PK could contain other columns so do not append // a PRIMARY KEY clause, just add a member to $field_primary $found_in_pk = false; for ($j = 0; $j < $primary_cnt; $j++) { if ($field_primary[$j] == $index) { $found_in_pk = true; break; } } // end for if (!$found_in_pk) { $field_primary[] = $index; } } } // end if (auto_increment) } if (!empty($comment)) { $query .= " COMMENT '" . PMA_Util::sqlAddSlashes($comment) . "'"; } // move column if ($move_to == '-first') { // dash can't appear as part of column name $query .= ' FIRST'; } elseif ($move_to != '') { $query .= ' AFTER ' . PMA_Util::backquote($move_to); } return $query; }
} } // end for $fulltext = preg_replace('@, $@', '', $fulltext); if (strlen($fulltext)) { $sql_query .= ', FULLTEXT (' . $fulltext . ')'; } unset($fulltext); // Builds the 'create table' statement $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' (' . $sql_query . ')'; // Adds table type, character set and comments if (!empty($tbl_type) && $tbl_type != 'Default') { $sql_query .= ' ' . PMA_ENGINE_KEYWORD . ' = ' . $tbl_type; } if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($tbl_collation)) { $sql_query .= PMA_generateCharsetQueryPart($tbl_collation); } if (!empty($comment)) { $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\''; } // Executes the query $result = PMA_DBI_try_query($sql_query); if ($result) { // garvin: If comments were sent, enable relation stuff require_once './libraries/relation.lib.php'; require_once './libraries/transformations.lib.php'; $cfgRelation = PMA_getRelationsParam(); // garvin: Update comment table, if a comment was set. if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) { foreach ($field_comments as $fieldindex => $fieldcomment) { if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
/** * 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']); if (in_array($db_charset, $GLOBALS['mysql_charsets']) && in_array($_POST['db_collation'], $GLOBALS['mysql_collations'][$db_charset])) { $sql_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($_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 = PMA_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'])); } }
/** * Test for PMA_generateCharsetQueryPart * * @param string $collation Collation * @param string $expected Expected Charset Query * * @return void * @test * @dataProvider charsetQueryData */ public function testGenerateCharsetQueryPart($collation, $expected) { $this->assertEquals($expected, PMA_generateCharsetQueryPart($collation)); }
} } // Not a valid db name -> back to the welcome page $uri = $cfg['PmaAbsoluteUri'] . 'main.php?' . PMA_generate_common_url('', '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1'; if (!strlen($db) || !$is_db) { PMA_sendHeaderLocation($uri); exit; } } // end if (ensures db exists) /** * Changes database charset if requested by the user */ if (isset($submitcollation) && !empty($db_collation)) { list($db_charset) = explode('_', $db_collation); $sql_query = 'ALTER DATABASE ' . PMA_CommonFunctions::getInstance()->backquote($db) . ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation); $result = PMA_DBI_query($sql_query); $message = PMA_Message::success(); unset($db_charset, $db_collation); /** * 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_Response::getInstance(); $response->isSuccess($message->isSuccess()); $response->addJSON('message', $message); exit; } }
require_once 'libraries/common.inc.php'; require_once 'libraries/mysql_charsets.inc.php'; require_once 'libraries/replication.inc.php'; require 'libraries/build_html_for_db.lib.php'; /** * Defines the url to return to in case of error in a sql statement */ $err_url = 'index.php' . PMA_URL_getCommon(); /** * Builds and executes the db creation sql query */ $sql_query = 'CREATE DATABASE ' . PMA\libraries\Util::backquote($_POST['new_db']); if (!empty($_POST['db_collation'])) { list($db_charset) = explode('_', $_POST['db_collation']); if (in_array($db_charset, $mysql_charsets) && in_array($_POST['db_collation'], $mysql_collations[$db_charset])) { $sql_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($_POST['db_collation']); } $db_collation_for_ajax = $_POST['db_collation']; unset($db_charset); } $sql_query .= ';'; $result = $GLOBALS['dbi']->tryQuery($sql_query); if (!$result) { $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError()); // avoid displaying the not-created db name in header or navi panel $GLOBALS['db'] = ''; $GLOBALS['table'] = ''; /** * If in an Ajax request, just display the message with {@link PMA\libraries\Response} */ if ($GLOBALS['is_ajax_request'] == true) {
/** * 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 = PMA_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 http://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 ($length != '' && !preg_match($pattern, $type)) { $query .= '(' . $length . ')'; } if ($virtuality) { $query .= ' AS (' . $expression . ') ' . $virtuality; } else { if ($attribute != '') { $query .= ' ' . $attribute; } $matches = preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR|ENUM|SET)$@i', $type); if (!empty($collation) && $collation != 'NULL' && $matches) { $query .= PMA_generateCharsetQueryPart($collation); } 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 \'' . PMA_Util::sqlAddSlashes($default_value) . '\''; } } elseif ($type == 'BINARY' || $type == 'VARBINARY') { $query .= ' DEFAULT 0x' . $default_value; } else { $query .= ' DEFAULT \'' . PMA_Util::sqlAddSlashes($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; break; case 'NONE': default: break; } if (!empty($extra)) { $query .= ' ' . $extra; } } if (!empty($comment)) { $query .= " COMMENT '" . PMA_Util::sqlAddSlashes($comment) . "'"; } // move column if ($move_to == '-first') { // dash can't appear as part of column name $query .= ' FIRST'; } elseif ($move_to != '') { $query .= ' AFTER ' . PMA_Util::backquote($move_to); } return $query; }
/** * 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 array &$field_primary list of fields for PRIMARY KEY * @param string $index * * @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 = '', &$field_primary, $index) { $is_timestamp = strpos(strtoupper($type), 'TIMESTAMP') !== false; $query = PMA_backquote($name) . ' ' . $type; if ($length != '' && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|' . 'MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN|UUID)$@i', $type)) { $query .= '(' . $length . ')'; } if ($attribute != '') { $query .= ' ' . $attribute; } if (!empty($collation) && $collation != 'NULL' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR|ENUM|SET)$@i', $type)) { $query .= PMA_generateCharsetQueryPart($collation); } 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 \'' . PMA_sqlAddSlashes($default_value) . '\''; } } else { $query .= ' DEFAULT \'' . PMA_sqlAddSlashes($default_value) . '\''; } break; case 'NULL': //If user uncheck null checkbox and not change default value null, //default value will be ignored. if ($null !== false && $null != 'NULL') { break; } case 'CURRENT_TIMESTAMP': $query .= ' DEFAULT ' . $default_type; break; case 'NONE': default: break; } if (!empty($extra)) { $query .= ' ' . $extra; // Force an auto_increment field to be part of the primary key // even if user did not tick the PK box; if ($extra == 'AUTO_INCREMENT') { $primary_cnt = count($field_primary); if (1 == $primary_cnt) { for ($j = 0; $j < $primary_cnt; $j++) { if ($field_primary[$j] == $index) { break; } } if (isset($field_primary[$j]) && $field_primary[$j] == $index) { $query .= ' PRIMARY KEY'; unset($field_primary[$j]); } } else { // but the PK could contain other columns so do not append // a PRIMARY KEY clause, just add a member to $field_primary $found_in_pk = false; for ($j = 0; $j < $primary_cnt; $j++) { if ($field_primary[$j] == $index) { $found_in_pk = true; break; } } // end for if (!$found_in_pk) { $field_primary[] = $index; } } } // end if (auto_increment) } if (!empty($comment)) { $query .= " COMMENT '" . PMA_sqlAddSlashes($comment) . "'"; } return $query; }
} if (!isset($query)) { $query = ''; } else { $query .= ', CHANGE '; } $query .= PMA_backquote($field_orig[$i]) . ' ' . PMA_backquote($field_name[$i]) . ' ' . $field_type[$i]; // Some field types shouldn't have lengths if ($field_length[$i] != '' && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) { $query .= '(' . $field_length[$i] . ')'; } if ($field_attribute[$i] != '') { $query .= ' ' . $field_attribute[$i]; } else { if (PMA_MYSQL_INT_VERSION >= 40100 && $field_collation[$i] != '' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR)$@i', $field_type[$i])) { $query .= PMA_generateCharsetQueryPart($field_collation[$i]); } } if ($field_default[$i] != '') { if (strtoupper($field_default[$i]) == 'NULL') { $query .= ' DEFAULT NULL'; } else { $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\''; } } if ($field_null[$i] != '') { $query .= ' ' . $field_null[$i]; } if ($field_extra[$i] != '') { $query .= ' ' . $field_extra[$i]; }
/** * generates column/field specification for ALTER or CREATE TABLE syntax * * @todo move into class PMA_Column * @todo on the interface, some js to clear the default value when the default * current_timestamp is checked * @static * @param string $name name * @param string $type type ('INT', 'VARCHAR', 'BIT', ...) * @param string $length length ('2', '5,2', '', ...) * @param string $attribute * @param string $collation * @param string $null with 'NULL' or 'NOT NULL' * @param string $default default value * @param boolean $default_current_timestamp whether default value is * CURRENT_TIMESTAMP or not * this overrides $default value * @param string $extra 'AUTO_INCREMENT' * @param string $comment field comment * @param array &$field_primary list of fields for PRIMARY KEY * @param string $index * @param string $default_orig * @return string field specification */ function generateFieldSpec($name, $type, $length = '', $attribute = '', $collation = '', $null = false, $default = '', $default_current_timestamp = false, $extra = '', $comment = '', &$field_primary, $index, $default_orig = false) { $is_timestamp = strpos(' ' . strtoupper($type), 'TIMESTAMP') == 1; // $default_current_timestamp has priority over $default /** * @todo include db-name */ $query = PMA_backquote($name) . ' ' . $type; if ($length != '' && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $type)) { $query .= '(' . $length . ')'; } if ($attribute != '') { $query .= ' ' . $attribute; } if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($collation) && $collation != 'NULL' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR|ENUM|SET)$@i', $type)) { $query .= PMA_generateCharsetQueryPart($collation); } if ($null !== false) { if (!empty($null)) { $query .= ' NOT NULL'; } else { $query .= ' NULL'; } } if ($default_current_timestamp && $is_timestamp) { $query .= ' DEFAULT CURRENT_TIMESTAMP'; // auto_increment field cannot have a default value } elseif ($extra !== 'AUTO_INCREMENT' && (strlen($default) || $default != $default_orig)) { if (strtoupper($default) == 'NULL') { $query .= ' DEFAULT NULL'; } else { if (strlen($default)) { if ($is_timestamp && $default == '0') { // a TIMESTAMP does not accept DEFAULT '0' // but DEFAULT 0 works $query .= ' DEFAULT ' . PMA_sqlAddslashes($default); } elseif ($default && $type == 'BIT') { $query .= ' DEFAULT b\'' . preg_replace('/[^01]/', '0', $default) . '\''; } else { $query .= ' DEFAULT \'' . PMA_sqlAddslashes($default) . '\''; } } } } if (!empty($extra)) { $query .= ' ' . $extra; // Force an auto_increment field to be part of the primary key // even if user did not tick the PK box; if ($extra == 'AUTO_INCREMENT') { $primary_cnt = count($field_primary); if (1 == $primary_cnt) { for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $index; $j++) { //void } if (isset($field_primary[$j]) && $field_primary[$j] == $index) { $query .= ' PRIMARY KEY'; unset($field_primary[$j]); } // but the PK could contain other columns so do not append // a PRIMARY KEY clause, just add a member to $field_primary } else { $found_in_pk = false; for ($j = 0; $j < $primary_cnt; $j++) { if ($field_primary[$j] == $index) { $found_in_pk = true; break; } } // end for if (!$found_in_pk) { $field_primary[] = $index; } } } // end if (auto_increment) } if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($comment)) { $query .= " COMMENT '" . PMA_sqlAddslashes($comment) . "'"; } return $query; }
/** * generates column/field specification for ALTER or CREATE TABLE syntax * * @todo move into class PMA_Column * @static * @param string $name name * @param string $type type ('INT', 'VARCHAR', 'BIT', ...) * @param string $length length ('2', '5,2', '', ...) * @param string $attribute * @param string $collation * @param string $null with 'NULL' or 'NOT NULL' * @param string $default default value * @param boolean $default_current_timestamp whether default value is * CURRENT_TIMESTAMP or not * this overrides $default value * @param string $extra 'AUTO_INCREMENT' * @param string $comment field comment * @param array &$field_primary list of fields for PRIMARY KEY * @param string $index * @param string $default_orig * @return string field specification */ function generateFieldSpec($name, $type, $length = '', $attribute = '', $collation = '', $null = false, $default = '', $default_current_timestamp = false, $extra = '', $comment = '', &$field_primary, $index, $default_orig = false) { // $default_current_timestamp has priority over $default // TODO: on the interface, some js to clear the default value // when the default current_timestamp is checked // TODO: include db-name $query = PMA_backquote($name) . ' ' . $type; if ($length != '' && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $type)) { $query .= '(' . $length . ')'; } if ($attribute != '') { $query .= ' ' . $attribute; } if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($collation) && $collation != 'NULL' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR|ENUM|SET)$@i', $type)) { $query .= PMA_generateCharsetQueryPart($collation); } if ($null !== false) { if (!empty($null)) { $query .= ' NOT NULL'; } else { $query .= ' NULL'; } } if ($default_current_timestamp && strpos(' ' . strtoupper($type), 'TIMESTAMP') == 1) { $query .= ' DEFAULT CURRENT_TIMESTAMP'; // auto_increment field cannot have a default value } elseif ($extra !== 'AUTO_INCREMENT' && (strlen($default) || $default != $default_orig)) { if (strtoupper($default) == 'NULL') { $query .= ' DEFAULT NULL'; } else { if (strlen($default)) { $query .= ' DEFAULT \'' . PMA_sqlAddslashes($default) . '\''; } } } if (!empty($extra)) { $query .= ' ' . $extra; // An auto_increment field must be use as a primary key if ($extra == 'AUTO_INCREMENT' && isset($field_primary)) { $primary_cnt = count($field_primary); for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $index; $j++) { // void } // end for if (isset($field_primary[$j]) && $field_primary[$j] == $index) { $query .= ' PRIMARY KEY'; unset($field_primary[$j]); } // end if } // end if (auto_increment) } if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($comment)) { $query .= " COMMENT '" . PMA_sqlAddslashes($comment) . "'"; } return $query; }
$reread_info = true; $reload = true; } else { $errors[] = $pma_table->getLastError(); $message .= $pma_table->getLastError(); } } if (isset($_REQUEST['comment']) && urldecode($_REQUEST['prev_comment']) !== $_REQUEST['comment']) { $table_alters[] = 'COMMENT = \'' . PMA_sqlAddslashes($_REQUEST['comment']) . '\''; } if (!empty($_REQUEST['new_tbl_type']) && strtolower($_REQUEST['new_tbl_type']) !== strtolower($tbl_type)) { $table_alters[] = PMA_ENGINE_KEYWORD . ' = ' . $_REQUEST['new_tbl_type']; $tbl_type = $_REQUEST['new_tbl_type']; } if (!empty($_REQUEST['tbl_collation']) && $_REQUEST['tbl_collation'] !== $tbl_collation) { $table_alters[] = 'DEFAULT ' . PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']); } $l_tbl_type = strtolower($tbl_type); if (($l_tbl_type === 'myisam' || $l_tbl_type === 'isam') && isset($_REQUEST['new_pack_keys']) && $_REQUEST['new_pack_keys'] != (string) $pack_keys) { $table_alters[] = 'pack_keys = ' . $_REQUEST['new_pack_keys']; } $checksum = empty($checksum) ? '0' : '1'; $_REQUEST['new_checksum'] = empty($_REQUEST['new_checksum']) ? '0' : '1'; if ($l_tbl_type === 'myisam' && $_REQUEST['new_checksum'] !== $checksum) { $table_alters[] = 'checksum = ' . $_REQUEST['new_checksum']; } $delay_key_write = empty($delay_key_write) ? '0' : '1'; $_REQUEST['new_delay_key_write'] = empty($_REQUEST['new_delay_key_write']) ? '0' : '1'; if ($l_tbl_type === 'myisam' && $_REQUEST['new_delay_key_write'] !== $delay_key_write) { $table_alters[] = 'delay_key_write = ' . $_REQUEST['new_delay_key_write']; }
} } // end for $fulltext = preg_replace('@, $@', '', $fulltext); if (strlen($fulltext)) { $sql_query .= ', FULLTEXT (' . $fulltext . ')'; } unset($fulltext); // Builds the 'create table' statement $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' (' . $sql_query . ')'; // Adds table type, character set, comments and partition definition if (!empty($_REQUEST['tbl_type']) && $_REQUEST['tbl_type'] != 'Default') { $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type']; } if (!empty($_REQUEST['tbl_collation'])) { $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']); } if (!empty($_REQUEST['comment'])) { $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($_REQUEST['comment']) . '\''; } if (!empty($_REQUEST['partition_definition'])) { $sql_query .= ' ' . PMA_sqlAddslashes($_REQUEST['partition_definition']); } $sql_query .= ';'; // Executes the query $result = PMA_DBI_try_query($sql_query); if ($result) { // If comments were sent, enable relation stuff require_once './libraries/transformations.lib.php'; // Update comment table for mime types [MIME] if (isset($_REQUEST['field_mimetype']) && is_array($_REQUEST['field_mimetype']) && $cfg['BrowseMIME']) {
/** * Get table alters array * * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not * @param boolean $is_isam whether ISAM or not * @param string $pack_keys pack keys * @param string $checksum value of checksum * @param boolean $is_aria whether ARIA or not * @param string $page_checksum value of page checksum * @param string $delay_key_write delay key write * @param boolean $is_innodb whether INNODB or not * @param boolean $is_pbxt whether PBXT or not * @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($is_myisam_or_aria, $is_isam, $pack_keys, $checksum, $is_aria, $page_checksum, $delay_key_write, $is_innodb, $is_pbxt, $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 = \'' . PMA\libraries\Util::sqlAddSlashes($_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 ' . PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']); } if (($is_myisam_or_aria || $is_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 ($is_myisam_or_aria && $_REQUEST['new_checksum'] !== $checksum) { $table_alters[] = 'checksum = ' . $_REQUEST['new_checksum']; } $_REQUEST['new_transactional'] = empty($_REQUEST['new_transactional']) ? '0' : '1'; if ($is_aria && $_REQUEST['new_transactional'] !== $transactional) { $table_alters[] = 'TRANSACTIONAL = ' . $_REQUEST['new_transactional']; } $_REQUEST['new_page_checksum'] = empty($_REQUEST['new_page_checksum']) ? '0' : '1'; if ($is_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 ($is_myisam_or_aria && $_REQUEST['new_delay_key_write'] !== $delay_key_write) { $table_alters[] = 'delay_key_write = ' . $_REQUEST['new_delay_key_write']; } if (($is_myisam_or_aria || $is_innodb || $is_pbxt) && !empty($_REQUEST['new_auto_increment']) && (!isset($auto_increment) || $_REQUEST['new_auto_increment'] !== $auto_increment)) { $table_alters[] = 'auto_increment = ' . PMA\libraries\Util::sqlAddSlashes($_REQUEST['new_auto_increment']); } if (!empty($_REQUEST['new_row_format'])) { $newRowFormat = $_REQUEST['new_row_format']; $newRowFormatLower = mb_strtolower($newRowFormat); if (($is_myisam_or_aria || $is_innodb || $is_pbxt) && (!mb_strlen($row_format) || $newRowFormatLower !== mb_strtolower($row_format))) { $table_alters[] = 'ROW_FORMAT = ' . PMA\libraries\Util::sqlAddSlashes($newRowFormat); } } return $table_alters; }
/** * Get table alters array * * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not * @param boolean $is_isam whether ISAM or not * @param string $pack_keys pack keys * @param string $checksum value of checksum * @param boolean $is_aria whether ARIA or not * @param string $page_checksum value of page checksum * @param string $delay_key_write delay key write * @param boolean $is_innodb whether INNODB or not * @param boolean $is_pbxt whether PBXT or not * @param string $row_format row format * @param string $tbl_storage_engine table storage engine * @param string $transactional value of transactional * * @return array $table_alters */ function PMA_getTableAltersArray($is_myisam_or_aria, $is_isam, $pack_keys, $checksum, $is_aria, $page_checksum, $delay_key_write, $is_innodb, $is_pbxt, $row_format, $tbl_storage_engine, $transactional, $tbl_collation) { $common_functions = PMA_CommonFunctions::getInstance(); $table_alters = array(); if (isset($_REQUEST['comment']) && urldecode($_REQUEST['prev_comment']) !== $_REQUEST['comment']) { $table_alters[] = 'COMMENT = \'' . $common_functions->sqlAddSlashes($_REQUEST['comment']) . '\''; } if (!empty($_REQUEST['new_tbl_storage_engine']) && strtolower($_REQUEST['new_tbl_storage_engine']) !== strtolower($tbl_storage_engine)) { $table_alters[] = 'ENGINE = ' . $_REQUEST['new_tbl_storage_engine']; } if (!empty($_REQUEST['tbl_collation']) && $_REQUEST['tbl_collation'] !== $tbl_collation) { $table_alters[] = 'DEFAULT ' . PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']); } if (($is_myisam_or_aria || $is_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 ($is_myisam_or_aria && $_REQUEST['new_checksum'] !== $checksum) { $table_alters[] = 'checksum = ' . $_REQUEST['new_checksum']; } $_REQUEST['new_transactional'] = empty($_REQUEST['new_transactional']) ? '0' : '1'; if ($is_aria && $_REQUEST['new_transactional'] !== $transactional) { $table_alters[] = 'TRANSACTIONAL = ' . $_REQUEST['new_transactional']; } $_REQUEST['new_page_checksum'] = empty($_REQUEST['new_page_checksum']) ? '0' : '1'; if ($is_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 ($is_myisam_or_aria && $_REQUEST['new_delay_key_write'] !== $delay_key_write) { $table_alters[] = 'delay_key_write = ' . $_REQUEST['new_delay_key_write']; } if (($is_myisam_or_aria || $is_innodb || $is_pbxt) && !empty($_REQUEST['new_auto_increment']) && (!isset($auto_increment) || $_REQUEST['new_auto_increment'] !== $auto_increment)) { $table_alters[] = 'auto_increment = ' . $common_functions->sqlAddSlashes($_REQUEST['new_auto_increment']); } if (($is_myisam_or_aria || $is_innodb || $is_pbxt) && !empty($_REQUEST['new_row_format']) && (!strlen($row_format) || strtolower($_REQUEST['new_row_format']) !== strtolower($row_format))) { $table_alters[] = 'ROW_FORMAT = ' . $common_functions->sqlAddSlashes($_REQUEST['new_row_format']); } return $table_alters; }