Completed
Push — master ( 177022...fa1d20 )
by Joao
11:37
created

MySqlGrammar   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 19
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 139
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A modifyCollate() 0 8 2
A modifyComment() 0 8 2
A compileCreate() 0 11 2
A typeBinary() 0 4 1
A typeSet() 0 4 1
C compileKey() 0 30 7
A compileHasForeign() 0 4 1
1
<?php namespace jlourenco\support\Database;
2
3
use Illuminate\Database\Connection;
4
use Illuminate\Database\Schema\Blueprint as IlluminateBlueprint;
5
use Illuminate\Database\Schema\Grammars\MySqlGrammar as IlluminateMySqlGrammar;
6
use Illuminate\Support\Fluent;
7
8
/**
9
 * Extended version of MySqlGrammar with
10
 * support of 'set' data type
11
 */
12
class MySqlGrammar extends IlluminateMySqlGrammar {
13
14
    public function __construct()
15
    {
16
        if ( ! in_array('Collate', $this->modifiers) )
17
        {
18
            array_splice($this->modifiers, array_search('Unsigned', $this->modifiers) + 1, 0, 'Collate');
19
        }
20
        // new versions of Laravel already have comment modifier
21
        if ( ! in_array('Comment', $this->modifiers) )
22
        {
23
            array_splice($this->modifiers, array_search('After', $this->modifiers) - 1, 0, 'Comment');
24
        }
25
    }
26
27
    /**
28
     * Get the SQL for a "comment" column modifier.
29
     *
30
     * @param \Illuminate\Database\Schema\Blueprint  $blueprint
31
     * @param \Illuminate\Support\Fluent             $column
32
     * @return string|null
33
     */
34
    protected function modifyCollate(IlluminateBlueprint $blueprint, Fluent $column)
35
    {
36
        if ( ! is_null($column->collate) )
37
        {
38
            $characterSet = strtok($column->collate, '_');
39
            return " character set $characterSet collate {$column->collate}";
40
        }
41
    }
42
43
    /**
44
     * Get the SQL for a "comment" column modifier.
45
     *
46
     * @param \Illuminate\Database\Schema\Blueprint  $blueprint
47
     * @param \Illuminate\Support\Fluent             $column
48
     * @return string|null
49
     */
50
    protected function modifyComment(IlluminateBlueprint $blueprint, Fluent $column)
51
    {
52
        if ( ! is_null($column->comment) )
53
        {
54
            $comment = str_replace("'", "\'", $column->comment);
55
            return " comment '$comment'";
56
        }
57
    }
58
59
    /**
60
     * Compile a create table command.
61
     *
62
     * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
63
     * @param  \Illuminate\Support\Fluent  $command
64
     * @param  \Illuminate\Database\Connection  $connection
65
     * @return string
66
     */
67
    public function compileCreate(IlluminateBlueprint $blueprint, Fluent $command, Connection $connection)
68
    {
69
        $sql = parent::compileCreate($blueprint, $command, $connection);
70
        // Table annotation support
71
        if ( isset($blueprint->comment) )
72
        {
73
            $comment = str_replace("'", "\'", $blueprint->comment);
1 ignored issue
show
Bug introduced by
The property comment does not seem to exist in Illuminate\Database\Schema\Blueprint.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
74
            $sql .= " comment = '$comment'";
75
        }
76
        return $sql;
77
    }
78
79
    /**
80
     * Create the column definition for a binary type.
81
     *
82
     * @param  \Illuminate\Support\Fluent  $column
83
     * @return string
84
     */
85
    protected function typeBinary(Fluent $column)
86
    {
87
        return "binary({$column->length})";
88
    }
89
90
    /**
91
     * Create the column definition for an 'set' type.
92
     *
93
     * @param  \Illuminate\Support\Fluent  $column
94
     * @return string
95
     */
96
    protected function typeSet(Fluent $column)
97
    {
98
        return "set('" . implode("', '", $column->allowed) . "')";
99
    }
100
101
    /**
102
     * Compile an index creation command.
103
     *
104
     * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
105
     * @param  \Illuminate\Support\Fluent  $command
106
     * @param  string  $type
107
     * @return string
108
     */
109
    protected function compileKey(IlluminateBlueprint $blueprint, Fluent $command, $type)
110
    {
111
        $columns = [];
112
        foreach($command->columns as $commandColumn)
113
        {
114
            foreach($blueprint->getColumns() as $blueprintColumn)
115
            {
116
                if ( $blueprintColumn->name != $commandColumn )
117
                {
118
                    continue;
119
                }
120
121
                $column = $this->wrap($commandColumn);
122
                if ( isset($command->length) )
123
                {
124
                    $column .= "({$command->length})";
125
                }
126
                elseif ( 'string' == $blueprintColumn->type && $blueprintColumn->length > 255 )
127
                {
128
                    $column .= '(255)';
129
                }
130
131
                $columns[] = $column;
132
            }
133
        }
134
135
        $columns = implode(', ', $columns);
136
        $table = $this->wrapTable($blueprint);
137
        return "alter table {$table} add {$type} {$command->index}($columns)";
138
    }
139
140
    /**
141
     * Compile the query to determine if the foreign key exists
142
     *
143
     * @return string
144
     */
145
    public function compileHasForeign()
146
    {
147
        return 'select TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME = ? and CONSTRAINT_NAME = ?';
148
    }
149
150
}