PMA\libraries\Table::getColumnGenerationExpression PHP Method

getColumnGenerationExpression() public method

Returns the generation expression for virtual columns
public getColumnGenerationExpression ( string $column = null ) : array | boolean
$column string name of the column
return array | boolean associative array of column name and their expressions or false on failure
    public function getColumnGenerationExpression($column = null)
    {
        $serverType = Util::getServerType();
        if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION > 50705 && !$GLOBALS['cfg']['Server']['DisableIS']) {
            $sql = "SELECT\n                `COLUMN_NAME` AS `Field`,\n                `GENERATION_EXPRESSION` AS `Expression`\n                FROM\n                `information_schema`.`COLUMNS`\n                WHERE\n                `TABLE_SCHEMA` = '" . $GLOBALS['dbi']->escapeString($this->_db_name) . "'\n                AND `TABLE_NAME` = '" . $GLOBALS['dbi']->escapeString($this->_name) . "'";
            if ($column != null) {
                $sql .= " AND  `COLUMN_NAME` = '" . $GLOBALS['dbi']->escapeString($column) . "'";
            }
            $columns = $this->_dbi->fetchResult($sql, 'Field', 'Expression');
            return $columns;
        }
        $createTable = $this->showCreate();
        if (!$createTable) {
            return false;
        }
        $parser = new Parser($createTable);
        /**
         * @var \SqlParser\Statements\CreateStatement $stmt
         */
        $stmt = $parser->statements[0];
        $fields = Table::getFields($stmt);
        if ($column != null) {
            $expression = isset($fields[$column]['expr']) ? substr($fields[$column]['expr'], 1, -1) : '';
            return array($column => $expression);
        }
        $ret = array();
        foreach ($fields as $field => $options) {
            if (isset($options['expr'])) {
                $ret[$field] = substr($options['expr'], 1, -1);
            }
        }
        return $ret;
    }

Usage Example

             break;
         case 'NULL':
         case 'CURRENT_TIMESTAMP':
             $columnMeta['Default'] = $columnMeta['DefaultType'];
             break;
     }
     $length = Util\get($_REQUEST, "field_length.{$columnNumber}", $length);
     $submit_attribute = Util\get($_REQUEST, "field_attribute.{$columnNumber}", false);
     $comments_map[$columnMeta['Field']] = Util\get($_REQUEST, "field_comments.{$columnNumber}");
     $mime_map[$columnMeta['Field']] = array_merge($mime_map[$columnMeta['Field']], array('mimetype' => Util\get($_REQUEST, "field_mimetype.{${$columnNumber}}"), 'transformation' => Util\get($_REQUEST, "field_transformation.{${$columnNumber}}"), 'transformation_options' => Util\get($_REQUEST, "field_transformation_options.{${$columnNumber}}")));
 } elseif (isset($fields_meta[$columnNumber])) {
     $columnMeta = $fields_meta[$columnNumber];
     $virtual = array('VIRTUAL', 'PERSISTENT', 'VIRTUAL GENERATED', 'STORED GENERATED');
     if (in_array($columnMeta['Extra'], $virtual)) {
         $tableObj = new Table($GLOBALS['table'], $GLOBALS['db']);
         $expressions = $tableObj->getColumnGenerationExpression($columnMeta['Field']);
         $columnMeta['Expression'] = $expressions[$columnMeta['Field']];
     }
     switch ($columnMeta['Default']) {
         case null:
             if (is_null($columnMeta['Default'])) {
                 // null
                 if ($columnMeta['Null'] == 'YES') {
                     $columnMeta['DefaultType'] = 'NULL';
                     $columnMeta['DefaultValue'] = '';
                 } else {
                     $columnMeta['DefaultType'] = 'NONE';
                     $columnMeta['DefaultValue'] = '';
                 }
             } else {
                 // empty