generates column specification for ALTER or CREATE TABLE syntax
static public generateFieldSpec ( string $name, string $type, string $length = '', string $attribute = '', string $collation = '', boolean | string $null = false, string $default_type = 'USER_DEFINED', string $default_value = '', string $extra = '', string $comment = '', string $virtuality = '', string $expression = '', string $move_to = '' ) : string | ||
$name | string | name |
$type | string | type ('INT', 'VARCHAR', 'BIT', ...) |
$length | string | length ('2', '5,2', '', ...) |
$attribute | string | attribute |
$collation | string | collation |
$null | boolean | string | with 'NULL' or 'NOT NULL' |
$default_type | string | whether default is CURRENT_TIMESTAMP, NULL, NONE, USER_DEFINED |
$default_value | string | default value for USER_DEFINED default type |
$extra | string | 'AUTO_INCREMENT' |
$comment | string | field comment |
$virtuality | string | virtuality of the column |
$expression | string | expression for the virtual column |
$move_to | string | new position for column |
Résultat | string | field specification |
/** * 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; }
/** * 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 = 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 = 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 = 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 = 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 = 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 = 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); }
/** * 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 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 Util::backquote($oldcol) . ' ' . Table::generateFieldSpec($newcol, $type, $length, $attribute, $collation, $null, $default_type, $default_value, $extra, $comment, $virtuality, $expression, $move_to); }