Вход Регистрация
Файл: system/vendor/illuminate/database/Schema/Grammars/ChangeColumn.php
Строк: 310
<?php

namespace IlluminateDatabaseSchemaGrammars;

use 
DoctrineDBALSchemaAbstractSchemaManager as SchemaManager;
use 
DoctrineDBALSchemaComparator;
use 
DoctrineDBALSchemaTable;
use 
DoctrineDBALTypesType;
use 
IlluminateDatabaseConnection;
use 
IlluminateDatabaseSchemaBlueprint;
use 
IlluminateSupportFluent;
use 
RuntimeException;

class 
ChangeColumn
{
    
/**
     * Compile a change column command into a series of SQL statements.
     *
     * @param  IlluminateDatabaseSchemaGrammarsGrammar  $grammar
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @param  IlluminateDatabaseConnection  $connection
     * @return array
     *
     * @throws RuntimeException
     */
    
public static function compile($grammarBlueprint $blueprintFluent $commandConnection $connection)
    {
        if (! 
$connection->isDoctrineAvailable()) {
            throw new 
RuntimeException(sprintf(
                
'Changing columns for table "%s" requires Doctrine DBAL; install "doctrine/dbal".',
                
$blueprint->getTable()
            ));
        }

        
$tableDiff = static::getChangedDiff(
            
$grammar$blueprint$schema $connection->getDoctrineSchemaManager()
        );

        if (
$tableDiff !== false) {
            return (array) 
$schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
        }

        return [];
    }

    
/**
     * Get the Doctrine table difference for the given changes.
     *
     * @param  IlluminateDatabaseSchemaGrammarsGrammar  $grammar
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  DoctrineDBALSchemaAbstractSchemaManager  $schema
     * @return DoctrineDBALSchemaTableDiff|bool
     */
    
protected static function getChangedDiff($grammarBlueprint $blueprintSchemaManager $schema)
    {
        
$current $schema->listTableDetails($grammar->getTablePrefix().$blueprint->getTable());

        return (new 
Comparator)->diffTable(
            
$current, static::getTableWithColumnChanges($blueprint$current)
        );
    }

    
/**
     * Get a copy of the given Doctrine table after making the column changes.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  DoctrineDBALSchemaTable  $table
     * @return DoctrineDBALSchemaTable
     */
    
protected static function getTableWithColumnChanges(Blueprint $blueprintTable $table)
    {
        
$table = clone $table;

        foreach (
$blueprint->getChangedColumns() as $fluent) {
            
$column = static::getDoctrineColumn($table$fluent);

            
// Here we will spin through each fluent column definition and map it to the proper
            // Doctrine column definitions - which is necessary because Laravel and Doctrine
            // use some different terminology for various column attributes on the tables.
            
foreach ($fluent->getAttributes() as $key => $value) {
                if (! 
is_null($option = static::mapFluentOptionToDoctrine($key))) {
                    if (
method_exists($column$method 'set'.ucfirst($option))) {
                        
$column->{$method}(static::mapFluentValueToDoctrine($option$value));
                        continue;
                    }

                    
$column->setCustomSchemaOption($option, static::mapFluentValueToDoctrine($option$value));
                }
            }
        }

        return 
$table;
    }

    
/**
     * Get the Doctrine column instance for a column change.
     *
     * @param  DoctrineDBALSchemaTable  $table
     * @param  IlluminateSupportFluent  $fluent
     * @return DoctrineDBALSchemaColumn
     */
    
protected static function getDoctrineColumn(Table $tableFluent $fluent)
    {
        return 
$table->changeColumn(
            
$fluent['name'], static::getDoctrineColumnChangeOptions($fluent)
        )->
getColumn($fluent['name']);
    }

    
/**
     * Get the Doctrine column change options.
     *
     * @param  IlluminateSupportFluent  $fluent
     * @return array
     */
    
protected static function getDoctrineColumnChangeOptions(Fluent $fluent)
    {
        
$options = ['type' => static::getDoctrineColumnType($fluent['type'])];

        if (
in_array($fluent['type'], ['text''mediumText''longText'])) {
            
$options['length'] = static::calculateDoctrineTextLength($fluent['type']);
        }

        if (
in_array($fluent['type'], ['json''binary'])) {
            
$options['customSchemaOptions'] = [
                
'collation' => '',
                
'charset' => '',
            ];
        }

        return 
$options;
    }

    
/**
     * Get the doctrine column type.
     *
     * @param  string  $type
     * @return DoctrineDBALTypesType
     */
    
protected static function getDoctrineColumnType($type)
    {
        
$type strtolower($type);

        switch (
$type) {
            case 
'biginteger':
                
$type 'bigint';
                break;
            case 
'smallinteger':
                
$type 'smallint';
                break;
            case 
'mediumtext':
            case 
'longtext':
                
$type 'text';
                break;
            case 
'binary':
                
$type 'blob';
                break;
        }

        return 
Type::getType($type);
    }

    
/**
     * Calculate the proper column length to force the Doctrine text type.
     *
     * @param  string  $type
     * @return int
     */
    
protected static function calculateDoctrineTextLength($type)
    {
        switch (
$type) {
            case 
'mediumText':
                return 
65535 1;
            case 
'longText':
                return 
16777215 1;
            default:
                return 
255 1;
        }
    }

    
/**
     * Get the matching Doctrine option for a given Fluent attribute name.
     *
     * @param  string  $attribute
     * @return string|null
     */
    
protected static function mapFluentOptionToDoctrine($attribute)
    {
        switch (
$attribute) {
            case 
'type':
            case 
'name':
                return;
            case 
'nullable':
                return 
'notnull';
            case 
'total':
                return 
'precision';
            case 
'places':
                return 
'scale';
            default:
                return 
$attribute;
        }
    }

    
/**
     * Get the matching Doctrine value for a given Fluent attribute.
     *
     * @param  string  $option
     * @param  mixed  $value
     * @return mixed
     */
    
protected static function mapFluentValueToDoctrine($option$value)
    {
        return 
$option === 'notnull' ? ! $value $value;
    }
}
Онлайн: 3
Реклама