PMA\libraries\Util::extractColumnSpec PHP Method

extractColumnSpec() public static method

Extracts the various parts from a column spec
public static extractColumnSpec ( string $columnspec ) : array
$columnspec string Column specification
return array associative array containing type, spec_in_brackets and possibly enum_set_values (another array)
    public static function extractColumnSpec($columnspec)
    {
        $first_bracket_pos = mb_strpos($columnspec, '(');
        if ($first_bracket_pos) {
            $spec_in_brackets = chop(mb_substr($columnspec, $first_bracket_pos + 1, mb_strrpos($columnspec, ')') - $first_bracket_pos - 1));
            // convert to lowercase just to be sure
            $type = mb_strtolower(chop(mb_substr($columnspec, 0, $first_bracket_pos)));
        } else {
            // Split trailing attributes such as unsigned,
            // binary, zerofill and get data type name
            $type_parts = explode(' ', $columnspec);
            $type = mb_strtolower($type_parts[0]);
            $spec_in_brackets = '';
        }
        if ('enum' == $type || 'set' == $type) {
            // Define our working vars
            $enum_set_values = self::parseEnumSetValues($columnspec, false);
            $printtype = $type . '(' . str_replace("','", "', '", $spec_in_brackets) . ')';
            $binary = false;
            $unsigned = false;
            $zerofill = false;
        } else {
            $enum_set_values = array();
            /* Create printable type name */
            $printtype = mb_strtolower($columnspec);
            // Strip the "BINARY" attribute, except if we find "BINARY(" because
            // this would be a BINARY or VARBINARY column type;
            // by the way, a BLOB should not show the BINARY attribute
            // because this is not accepted in MySQL syntax.
            if (preg_match('@binary@', $printtype) && !preg_match('@binary[\\(]@', $printtype)) {
                $printtype = preg_replace('@binary@', '', $printtype);
                $binary = true;
            } else {
                $binary = false;
            }
            $printtype = preg_replace('@zerofill@', '', $printtype, -1, $zerofill_cnt);
            $zerofill = $zerofill_cnt > 0;
            $printtype = preg_replace('@unsigned@', '', $printtype, -1, $unsigned_cnt);
            $unsigned = $unsigned_cnt > 0;
            $printtype = trim($printtype);
        }
        $attribute = ' ';
        if ($binary) {
            $attribute = 'BINARY';
        }
        if ($unsigned) {
            $attribute = 'UNSIGNED';
        }
        if ($zerofill) {
            $attribute = 'UNSIGNED ZEROFILL';
        }
        $can_contain_collation = false;
        if (!$binary && preg_match("@^(char|varchar|text|tinytext|mediumtext|longtext|set|enum)@", $type)) {
            $can_contain_collation = true;
        }
        // for the case ENUM('–','“')
        $displayed_type = htmlspecialchars($printtype);
        if (mb_strlen($printtype) > $GLOBALS['cfg']['LimitChars']) {
            $displayed_type = '<abbr title="' . htmlspecialchars($printtype) . '">';
            $displayed_type .= htmlspecialchars(mb_substr($printtype, 0, $GLOBALS['cfg']['LimitChars']) . '...');
            $displayed_type .= '</abbr>';
        }
        return array('type' => $type, 'spec_in_brackets' => $spec_in_brackets, 'enum_set_values' => $enum_set_values, 'print_type' => $printtype, 'binary' => $binary, 'unsigned' => $unsigned, 'zerofill' => $zerofill, 'attribute' => $attribute, 'can_contain_collation' => $can_contain_collation, 'displayed_type' => $displayed_type);
    }

Usage Example

コード例 #1
0
/**
 * build the html for columns of $colTypeCategory category
 * in form of given $listType in a table
 *
 * @param string $db              current database
 * @param string $table           current table
 * @param string $colTypeCategory supported all|Numeric|String|Spatial
 *                                |Date and time using the _pgettext() format
 * @param string $listType        type of list to build, supported dropdown|checkbox
 *
 * @return string HTML for list of columns in form of given list types
 */
function PMA_getHtmlForColumnsList($db, $table, $colTypeCategory = 'all', $listType = 'dropdown')
{
    $columnTypeList = array();
    if ($colTypeCategory != 'all') {
        $types = $GLOBALS['PMA_Types']->getColumns();
        $columnTypeList = $types[$colTypeCategory];
    }
    $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
    $columns = $GLOBALS['dbi']->getColumns($db, $table, null, true, $GLOBALS['userlink']);
    $type = "";
    $selectColHtml = "";
    foreach ($columns as $column => $def) {
        if (isset($def['Type'])) {
            $extracted_columnspec = Util::extractColumnSpec($def['Type']);
            $type = $extracted_columnspec['type'];
        }
        if (empty($columnTypeList) || in_array(mb_strtoupper($type), $columnTypeList)) {
            if ($listType == 'checkbox') {
                $selectColHtml .= '<input type="checkbox" value="' . htmlspecialchars($column) . '"/>' . htmlspecialchars($column) . ' [ ' . htmlspecialchars($def['Type']) . ' ]</br>';
            } else {
                $selectColHtml .= '<option value="' . htmlspecialchars($column) . '' . '">' . htmlspecialchars($column) . ' [ ' . htmlspecialchars($def['Type']) . ' ]' . '</option>';
            }
        }
    }
    return $selectColHtml;
}
All Usage Examples Of PMA\libraries\Util::extractColumnSpec
Util