/** * 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 int &$field_primary primary index field * @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, &$field_primary, $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) . PMA_Table::generateFieldSpec($_REQUEST['field_name'][$i], $_REQUEST['field_type'][$i], $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] : '', $field_primary); $definition .= PMA_setColumnCreationStatementSuffix($i, $is_create_tbl); $definitions[] = $definition; } // end for return $definitions; }
/** * @todo add documentation */ function generateAlter($oldcol, $newcol, $type, $length, $attribute, $collation, $null, $default, $default_current_timestamp, $extra, $comment = '', $default_orig) { $empty_a = array(); return PMA_backquote($oldcol) . ' ' . PMA_Table::generateFieldSpec($newcol, $type, $length, $attribute, $collation, $null, $default, $default_current_timestamp, $extra, $comment, $empty_a, -1, $default_orig); }
$field_index[] = $i; } if (${'field_key_' . $i} == 'unique_' . $i) { $field_unique[] = $i; } } // end if } // end for // Builds the fields creation statements for ($i = 0; $i < $field_cnt; $i++) { // '0' is also empty for php :-( if (empty($field_name[$i]) && $field_name[$i] != '0') { continue; } $query = PMA_Table::generateFieldSpec($field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], isset($field_collation[$i]) ? $field_collation[$i] : '', $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], isset($field_comments[$i]) ? $field_comments[$i] : '', $field_primary, $i); $query .= ', '; $sql_query .= $query; $query_cpy .= "\n" . ' ' . $query; } // end for unset($field_cnt); unset($query); $sql_query = preg_replace('@, $@', '', $sql_query); $query_cpy = preg_replace('@, $@', '', $query_cpy); // Builds the primary keys statements $primary = ''; $primary_cnt = isset($field_primary) ? count($field_primary) : 0; for ($i = 0; $i < $primary_cnt; $i++) { $j = $field_primary[$i]; if (isset($field_name[$j]) && strlen($field_name[$j])) {
/** * @see PMA_Table::generateFieldSpec() */ public static function generateAlter($oldcol, $newcol, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment = '', &$field_primary, $index, $default_orig) { return PMA_backquote($oldcol) . ' ' . PMA_Table::generateFieldSpec($newcol, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $field_primary, $index, $default_orig); }
/** * Test for generateFieldSpec * * @return void */ public function testGenerateFieldSpec() { //type is BIT $name = "PMA_name"; $type = "BIT"; $length = '12'; $attribute = 'PMA_attribute'; $collation = 'PMA_collation'; $null = 'NULL'; $default_type = 'USER_DEFINED'; $default_value = 12; $extra = 'AUTO_INCREMENT'; $comment = 'PMA_comment'; $virtuality = ''; $expression = ''; $move_to = '-first'; $query = PMA_Table::generateFieldSpec($name, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to); $this->assertEquals("`PMA_name` BIT(12) PMA_attribute NULL DEFAULT b'10' " . "AUTO_INCREMENT COMMENT 'PMA_comment' FIRST", $query); //type is DOUBLE $type = "DOUBLE"; $query = PMA_Table::generateFieldSpec($name, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to); $this->assertEquals("`PMA_name` DOUBLE(12) PMA_attribute NULL DEFAULT '12' " . "AUTO_INCREMENT COMMENT 'PMA_comment' FIRST", $query); //type is BOOLEAN $type = "BOOLEAN"; $query = PMA_Table::generateFieldSpec($name, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to); $this->assertEquals("`PMA_name` BOOLEAN PMA_attribute NULL DEFAULT TRUE " . "AUTO_INCREMENT COMMENT 'PMA_comment' FIRST", $query); //$default_type is NULL $default_type = 'NULL'; $query = PMA_Table::generateFieldSpec($name, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to); $this->assertEquals("`PMA_name` BOOLEAN PMA_attribute NULL DEFAULT NULL " . "AUTO_INCREMENT COMMENT 'PMA_comment' FIRST", $query); //$default_type is CURRENT_TIMESTAMP $default_type = 'CURRENT_TIMESTAMP'; $query = PMA_Table::generateFieldSpec($name, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to); $this->assertEquals("`PMA_name` BOOLEAN PMA_attribute NULL DEFAULT CURRENT_TIMESTAMP " . "AUTO_INCREMENT COMMENT 'PMA_comment' FIRST", $query); //$default_type is NONE $default_type = 'NONE'; $extra = 'INCREMENT'; $move_to = '-first'; $query = PMA_Table::generateFieldSpec($name, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to); $this->assertEquals("`PMA_name` BOOLEAN PMA_attribute NULL INCREMENT " . "COMMENT 'PMA_comment' FIRST", $query); }
$field_unique[] = $i; } if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) { $field_fulltext[] = $i; } } // end if } // end for // Builds the field creation statement and alters the table 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 = ' ADD ' . PMA_Table::generateFieldSpec($_REQUEST['field_name'][$i], $_REQUEST['field_type'][$i], $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] : '', $field_primary); if ($_REQUEST['field_where'] != 'last') { // Only the first field can be added somewhere other than at the end if ($i == 0) { if ($_REQUEST['field_where'] == 'first') { $definition .= ' FIRST'; } else { $definition .= ' AFTER ' . PMA_Util::backquote($_REQUEST['after_field']); } } else { $definition .= ' AFTER ' . PMA_Util::backquote($_REQUEST['field_name'][$i - 1]); } } $definitions[] = $definition; } // end for
/** * Generates column specification for ALTER syntax * * @param string $oldcol old column name * @param string $newcol new column 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 * * @see PMA_Table::generateFieldSpec() * * @return string field specification */ public static function generateAlter($oldcol, $newcol, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to) { return PMA_Util::backquote($oldcol) . ' ' . PMA_Table::generateFieldSpec($newcol, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to); }
/** * Generates column specification for ALTER syntax * * @param string $oldcol old column name * @param string $newcol new column 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 index * @param string $move_to new position for column * * @see PMA_Table::generateFieldSpec() * * @return string field specification */ public static function generateAlter($oldcol, $newcol, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, &$field_primary, $index, $move_to) { return PMA_CommonFunctions::getInstance()->backquote($oldcol) . ' ' . PMA_Table::generateFieldSpec($newcol, $type, $index, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $field_primary, $move_to); }