LazyRecord\TableParser\TypeInfoParser::parseTypeInfo PHP Method

parseTypeInfo() public static method

public static parseTypeInfo ( $typeName, BaseDriver $driver = null )
$driver SQLBuilder\Driver\BaseDriver
    public static function parseTypeInfo($typeName, BaseDriver $driver = null)
    {
        $type = strtolower($typeName);
        $typeInfo = new TypeInfo();
        // Type name with precision
        if (preg_match('/^(
             double
            |float
            |decimal
            |numeric
            |int
            |integer
            |tinyint
            |smallint
            |mediumint
            |bigint
            |char
            |varchar
            |character\\ varying
            |character
            |binary
            |varbinary
            ) (?: \\(  (?:(\\d+),(\\d+)|(\\d+))   \\) )?\\s*(unsigned)?/ix', $typeName, $matches)) {
            if (isset($matches[1]) && $matches[1] && isset($matches[2]) && isset($matches[3]) && $matches[2] && $matches[3]) {
                $typeInfo->type = strtolower($matches[1]);
                $typeInfo->length = intval($matches[2]);
                $typeInfo->precision = intval($matches[3]);
            } elseif (isset($matches[1]) && $matches[1] && isset($matches[4]) && $matches[4]) {
                $typeInfo->type = strtolower($matches[1]);
                // override the original type
                $typeInfo->length = intval($matches[4]);
            } elseif (isset($matches[1]) && $matches[1]) {
                $typeInfo->type = strtolower($matches[1]);
            }
            if (isset($matches[5]) && $matches[5]) {
                $typeInfo->unsigned = true;
            } else {
                $typeInfo->unsigned = false;
            }
        } elseif ($driver instanceof MySQLDriver && preg_match('/(enum|set)\\((.*)\\)/', $typeName, $matches)) {
            $typeInfo->type = strtolower($matches[1]);
            $values = array();
            $strvalues = explode(',', $matches[2]);
            foreach ($strvalues as $strvalue) {
                // looks like a string
                if (preg_match('/^([\'"])(.*)\\1$/', $strvalue, $matches)) {
                    $values[] = $matches[2];
                } elseif (is_numeric($strvalue)) {
                    $values[] = intval($strvalue);
                }
            }
            switch ($typeInfo->type) {
                case 'enum':
                    $typeInfo->enum = $values;
                    break;
                case 'set':
                    $typeInfo->set = $values;
                    break;
            }
        } else {
            // for type like: 'text' or 'blob'.. type name without length or decimals
            $typeInfo->type = strtolower($typeName);
        }
        // Canonicalization for PgSQL
        if ($driver instanceof PgSQLDriver) {
            if ($typeInfo->type === 'character varying') {
                $typeInfo->type = 'varchar';
            }
        } elseif ($driver instanceof MySQLDriver) {
            switch ($typeInfo->type) {
                case 'tinyint':
                    if ($typeInfo->length == 1) {
                        $typeInfo->type = 'boolean';
                        $typeInfo->isa = 'bool';
                        $typeInfo->length = null;
                    } elseif ($typeInfo->unsigned && $typeInfo->length == 3 || !$typeInfo->unsigned && $typeInfo->length == 4) {
                        $typeInfo->length = null;
                    }
                    break;
                case 'integer':
                case 'int':
                    $typeInfo->type = 'int';
                    if ($typeInfo->unsigned && $typeInfo->length == 10 || !$typeInfo->unsigned && $typeInfo->length == 11) {
                        $typeInfo->length = null;
                    }
                    break;
                case 'smallint':
                    if ($typeInfo->unsigned && $typeInfo->length == 5 || !$typeInfo->unsigned && $typeInfo->length == 6) {
                        $typeInfo->length = null;
                    }
                    break;
                case 'mediumint':
                    if ($typeInfo->unsigned && $typeInfo->length == 8 || !$typeInfo->unsigned && $typeInfo->length == 9) {
                        $typeInfo->length = null;
                    }
                    break;
                case 'bigint':
                    if ($typeInfo->unsigned && $typeInfo->length == 20 || !$typeInfo->unsigned && $typeInfo->length == 21) {
                        $typeInfo->length = null;
                    }
                    break;
            }
        }
        // Update isa property
        if (!$typeInfo->isa) {
            if (in_array($typeInfo->type, ['char', 'varchar', 'text'])) {
                $typeInfo->isa = 'str';
            } elseif (preg_match('/int/', $typeInfo->type)) {
                $typeInfo->isa = 'int';
            } elseif (in_array($typeInfo->type, ['boolean', 'bool'])) {
                $typeInfo->isa = 'bool';
            } elseif (in_array($typeInfo->type, ['blob', 'binary'])) {
                $typeInfo->isa = 'str';
            } elseif ($typeInfo->type == 'double') {
                $typeInfo->isa = 'double';
            } elseif (in_array($typeInfo->type, ['float', 'decimal', 'numeric'])) {
                $typeInfo->isa = 'float';
            } elseif ($typeInfo->type == 'enum') {
                $typeInfo->isa = 'enum';
            } elseif ($typeInfo->type == 'set') {
                $typeInfo->isa = 'set';
            } elseif ($typeInfo->type == 'point') {
                $typeInfo->isa = 'point';
            } elseif ('datetime' === $typeInfo->type || 'date' === $typeInfo->type) {
                $typeInfo->isa = 'DateTime';
            } elseif (preg_match('/timestamp/', $typeInfo->type)) {
                // For postgresql, the 'timestamp' can be 'timestamp with timezone'
                $typeInfo->isa = 'DateTime';
            } elseif ('time' == $typeInfo->type) {
                // DateTime::createFromFormat('H:s','10:00')
                $typeInfo->isa = 'DateTime';
            } else {
                throw new Exception("Unknown type {$type}");
            }
        }
        return $typeInfo;
    }

Usage Example

 public function reverseTableSchema($table)
 {
     $tableDef = $this->parseTableSql($table);
     $schema = new DeclareSchema();
     $schema->columnNames = $schema->columns = array();
     $schema->table($table);
     foreach ($tableDef->columns as $columnDef) {
         $name = $columnDef->name;
         $column = $schema->column($name);
         if (!isset($columnDef->type)) {
             throw new LogicException("Missing column type definition on column {$table}.{$name}.");
         }
         $type = $columnDef->type;
         $typeInfo = TypeInfoParser::parseTypeInfo($type, $this->driver);
         $column->type($type);
         if (isset($columnDef->length)) {
             $column->length($columnDef->length);
         }
         if (isset($columnDef->decimals)) {
             $column->decimals($columnDef->decimals);
         }
         $isa = $this->typenameToIsa($type);
         $column->isa($isa);
         if (isset($columnDef->notNull) && $columnDef->notNull !== null) {
             if ($columnDef->notNull) {
                 $column->notNull();
             } else {
                 $column->null();
             }
         }
         if (isset($columnDef->primary)) {
             $column->primary(true);
             $schema->primaryKey = $name;
             if (isset($columnDef->autoIncrement)) {
                 $column->autoIncrement(true);
             }
         } else {
             if (isset($columnDef->unique)) {
                 $column->unique(true);
             }
         }
         if (isset($columnDef->default)) {
             $default = $columnDef->default;
             if (is_scalar($default)) {
                 $column->default($default);
             } else {
                 if ($default instanceof Token && $default->type == 'literal') {
                     $column->default(new Raw($default->val));
                 } else {
                     throw new Exception('Incorrect literal token');
                 }
             }
         }
     }
     return $schema;
 }
All Usage Examples Of LazyRecord\TableParser\TypeInfoParser::parseTypeInfo
TypeInfoParser