PMA\libraries\Table::generateFieldSpec PHP Method

generateFieldSpec() static public method

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
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) && $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 ($length !== 0 && $is_timestamp) {
                        $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;
    }

Usage Example

Exemplo n.º 1
0
/**
 * 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;
}
All Usage Examples Of PMA\libraries\Table::generateFieldSpec