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);
|
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
|
|
|
} |