Вход Регистрация
Файл: gapps/vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Строк: 959
<?php

namespace IlluminateDatabaseSchemaGrammars;

use 
IlluminateSupportFluent;
use 
IlluminateDatabaseConnection;
use 
IlluminateDatabaseSchemaBlueprint;

class 
SQLiteGrammar extends Grammar
{
    
/**
     * The possible column modifiers.
     *
     * @var array
     */
    
protected $modifiers = ['Nullable''Default''Increment'];

    
/**
     * The columns available as serials.
     *
     * @var array
     */
    
protected $serials = ['bigInteger''integer''mediumInteger''smallInteger''tinyInteger'];

    
/**
     * Compile the query to determine if a table exists.
     *
     * @return string
     */
    
public function compileTableExists()
    {
        return 
"select * from sqlite_master where type = 'table' and name = ?";
    }

    
/**
     * Compile the query to determine the list of columns.
     *
     * @param  string  $table
     * @return string
     */
    
public function compileColumnExists($table)
    {
        return 
'pragma table_info('.str_replace('.''__'$table).')';
    }

    
/**
     * Compile a create table command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileCreate(Blueprint $blueprintFluent $command)
    {
        
$columns implode(', '$this->getColumns($blueprint));

        
$sql $blueprint->temporary 'create temporary' 'create';

        
$sql .= ' table '.$this->wrapTable($blueprint)." ($columns";

        
// SQLite forces primary keys to be added when the table is initially created
        // so we will need to check for a primary key commands and add the columns
        // to the table's declaration here so they can be created on the tables.
        
$sql .= (string) $this->addForeignKeys($blueprint);

        
$sql .= (string) $this->addPrimaryKeys($blueprint);

        return 
$sql.')';
    }

    
/**
     * Get the foreign key syntax for a table creation statement.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @return string|null
     */
    
protected function addForeignKeys(Blueprint $blueprint)
    {
        
$sql '';

        
$foreigns $this->getCommandsByName($blueprint'foreign');

        
// Once we have all the foreign key commands for the table creation statement
        // we'll loop through each of them and add them to the create table SQL we
        // are building, since SQLite needs foreign keys on the tables creation.
        
foreach ($foreigns as $foreign) {
            
$sql .= $this->getForeignKey($foreign);

            if (! 
is_null($foreign->onDelete)) {
                
$sql .= " on delete {$foreign->onDelete}";
            }

            if (! 
is_null($foreign->onUpdate)) {
                
$sql .= " on update {$foreign->onUpdate}";
            }
        }

        return 
$sql;
    }

    
/**
     * Get the SQL for the foreign key.
     *
     * @param  IlluminateSupportFluent  $foreign
     * @return string
     */
    
protected function getForeignKey($foreign)
    {
        
$on $this->wrapTable($foreign->on);

        
// We need to columnize the columns that the foreign key is being defined for
        // so that it is a properly formatted list. Once we have done this, we can
        // return the foreign key SQL declaration to the calling method for use.
        
$columns $this->columnize($foreign->columns);

        
$onColumns $this->columnize((array) $foreign->references);

        return 
", foreign key($columns) references $on($onColumns)";
    }

    
/**
     * Get the primary key syntax for a table creation statement.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @return string|null
     */
    
protected function addPrimaryKeys(Blueprint $blueprint)
    {
        
$primary $this->getCommandByName($blueprint'primary');

        if (! 
is_null($primary)) {
            
$columns $this->columnize($primary->columns);

            return 
", primary key ({$columns})";
        }
    }

    
/**
     * Compile alter table commands for adding columns.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return array
     */
    
public function compileAdd(Blueprint $blueprintFluent $command)
    {
        
$table $this->wrapTable($blueprint);

        
$columns $this->prefixArray('add column'$this->getColumns($blueprint));

        
$statements = [];

        foreach (
$columns as $column) {
            
$statements[] = 'alter table '.$table.' '.$column;
        }

        return 
$statements;
    }

    
/**
     * Compile a unique key command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileUnique(Blueprint $blueprintFluent $command)
    {
        
$columns $this->columnize($command->columns);

        
$table $this->wrapTable($blueprint);

        
$index $this->wrap($command->index);

        return 
"create unique index {$index} on {$table} ({$columns})";
    }

    
/**
     * Compile a plain index key command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileIndex(Blueprint $blueprintFluent $command)
    {
        
$columns $this->columnize($command->columns);

        
$table $this->wrapTable($blueprint);

        
$index $this->wrap($command->index);

        return 
"create index {$index} on {$table} ({$columns})";
    }

    
/**
     * Compile a foreign key command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileForeign(Blueprint $blueprintFluent $command)
    {
        
// Handled on table creation...
    
}

    
/**
     * Compile a drop table command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileDrop(Blueprint $blueprintFluent $command)
    {
        return 
'drop table '.$this->wrapTable($blueprint);
    }

    
/**
     * Compile a drop table (if exists) command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileDropIfExists(Blueprint $blueprintFluent $command)
    {
        return 
'drop table if exists '.$this->wrapTable($blueprint);
    }

    
/**
     * Compile a drop column command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @param  IlluminateDatabaseConnection  $connection
     * @return array
     */
    
public function compileDropColumn(Blueprint $blueprintFluent $commandConnection $connection)
    {
        
$schema $connection->getDoctrineSchemaManager();

        
$tableDiff $this->getDoctrineTableDiff($blueprint$schema);

        foreach (
$command->columns as $name) {
            
$column $connection->getDoctrineColumn($blueprint->getTable(), $name);

            
$tableDiff->removedColumns[$name] = $column;
        }

        return (array) 
$schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
    }

    
/**
     * Compile a drop unique key command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileDropUnique(Blueprint $blueprintFluent $command)
    {
        
$index $this->wrap($command->index);

        return 
"drop index {$index}";
    }

    
/**
     * Compile a drop index command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileDropIndex(Blueprint $blueprintFluent $command)
    {
        
$index $this->wrap($command->index);

        return 
"drop index {$index}";
    }

    
/**
     * Compile a rename table command.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $command
     * @return string
     */
    
public function compileRename(Blueprint $blueprintFluent $command)
    {
        
$from $this->wrapTable($blueprint);

        return 
"alter table {$from} rename to ".$this->wrapTable($command->to);
    }

    
/**
     * Compile the command to enable foreign key constraints.
     *
     * @return string
     */
    
public function compileEnableForeignKeyConstraints()
    {
        return 
'PRAGMA foreign_keys = ON;';
    }

    
/**
     * Compile the command to disable foreign key constraints.
     *
     * @return string
     */
    
public function compileDisableForeignKeyConstraints()
    {
        return 
'PRAGMA foreign_keys = OFF;';
    }

    
/**
     * Create the column definition for a char type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeChar(Fluent $column)
    {
        return 
'varchar';
    }

    
/**
     * Create the column definition for a string type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeString(Fluent $column)
    {
        return 
'varchar';
    }

    
/**
     * Create the column definition for a text type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeText(Fluent $column)
    {
        return 
'text';
    }

    
/**
     * Create the column definition for a medium text type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeMediumText(Fluent $column)
    {
        return 
'text';
    }

    
/**
     * Create the column definition for a long text type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeLongText(Fluent $column)
    {
        return 
'text';
    }

    
/**
     * Create the column definition for a integer type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeInteger(Fluent $column)
    {
        return 
'integer';
    }

    
/**
     * Create the column definition for a big integer type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeBigInteger(Fluent $column)
    {
        return 
'integer';
    }

    
/**
     * Create the column definition for a medium integer type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeMediumInteger(Fluent $column)
    {
        return 
'integer';
    }

    
/**
     * Create the column definition for a tiny integer type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeTinyInteger(Fluent $column)
    {
        return 
'integer';
    }

    
/**
     * Create the column definition for a small integer type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeSmallInteger(Fluent $column)
    {
        return 
'integer';
    }

    
/**
     * Create the column definition for a float type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeFloat(Fluent $column)
    {
        return 
'float';
    }

    
/**
     * Create the column definition for a double type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeDouble(Fluent $column)
    {
        return 
'float';
    }

    
/**
     * Create the column definition for a decimal type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeDecimal(Fluent $column)
    {
        return 
'numeric';
    }

    
/**
     * Create the column definition for a boolean type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeBoolean(Fluent $column)
    {
        return 
'tinyint(1)';
    }

    
/**
     * Create the column definition for an enum type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeEnum(Fluent $column)
    {
        return 
'varchar';
    }

    
/**
     * Create the column definition for a json type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeJson(Fluent $column)
    {
        return 
'text';
    }

    
/**
     * Create the column definition for a jsonb type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeJsonb(Fluent $column)
    {
        return 
'text';
    }

    
/**
     * Create the column definition for a date type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeDate(Fluent $column)
    {
        return 
'date';
    }

    
/**
     * Create the column definition for a date-time type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeDateTime(Fluent $column)
    {
        return 
'datetime';
    }

    
/**
     * Create the column definition for a date-time type.
     *
     * Note: "SQLite does not have a storage class set aside for storing dates and/or times."
     * @link https://www.sqlite.org/datatype3.html
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeDateTimeTz(Fluent $column)
    {
        return 
'datetime';
    }

    
/**
     * Create the column definition for a time type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeTime(Fluent $column)
    {
        return 
'time';
    }

    
/**
     * Create the column definition for a time type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeTimeTz(Fluent $column)
    {
        return 
'time';
    }

    
/**
     * Create the column definition for a timestamp type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeTimestamp(Fluent $column)
    {
        if (
$column->useCurrent) {
            return 
'datetime default CURRENT_TIMESTAMP';
        }

        return 
'datetime';
    }

    
/**
     * Create the column definition for a timestamp type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeTimestampTz(Fluent $column)
    {
        if (
$column->useCurrent) {
            return 
'datetime default CURRENT_TIMESTAMP';
        }

        return 
'datetime';
    }

    
/**
     * Create the column definition for a binary type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeBinary(Fluent $column)
    {
        return 
'blob';
    }

    
/**
     * Create the column definition for a uuid type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeUuid(Fluent $column)
    {
        return 
'varchar';
    }

    
/**
     * Create the column definition for an IP address type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeIpAddress(Fluent $column)
    {
        return 
'varchar';
    }

    
/**
     * Create the column definition for a MAC address type.
     *
     * @param  IlluminateSupportFluent  $column
     * @return string
     */
    
protected function typeMacAddress(Fluent $column)
    {
        return 
'varchar';
    }

    
/**
     * Get the SQL for a nullable column modifier.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $column
     * @return string|null
     */
    
protected function modifyNullable(Blueprint $blueprintFluent $column)
    {
        return 
$column->nullable ' null' ' not null';
    }

    
/**
     * Get the SQL for a default column modifier.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $column
     * @return string|null
     */
    
protected function modifyDefault(Blueprint $blueprintFluent $column)
    {
        if (! 
is_null($column->default)) {
            return 
' default '.$this->getDefaultValue($column->default);
        }
    }

    
/**
     * Get the SQL for an auto-increment column modifier.
     *
     * @param  IlluminateDatabaseSchemaBlueprint  $blueprint
     * @param  IlluminateSupportFluent  $column
     * @return string|null
     */
    
protected function modifyIncrement(Blueprint $blueprintFluent $column)
    {
        if (
in_array($column->type$this->serials) && $column->autoIncrement) {
            return 
' primary key autoincrement';
        }
    }
}
Онлайн: 1
Реклама