1
|
|
|
<?php namespace jlourenco\base\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
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @return void |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
if ( ! in_array('Collate', $this->modifiers) ) |
21
|
|
|
{ |
22
|
|
|
array_splice($this->modifiers, array_search('Unsigned', $this->modifiers) + 1, 0, 'Collate'); |
23
|
|
|
} |
24
|
|
|
// new versions of Laravel already have comment modifier |
25
|
|
|
if ( ! in_array('Comment', $this->modifiers) ) |
26
|
|
|
{ |
27
|
|
|
array_splice($this->modifiers, array_search('After', $this->modifiers) - 1, 0, 'Comment'); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the SQL for a "comment" column modifier. |
33
|
|
|
* |
34
|
|
|
* @param \Illuminate\Database\Schema\Blueprint $blueprint |
35
|
|
|
* @param \Illuminate\Support\Fluent $column |
36
|
|
|
* @return string|null |
37
|
|
|
*/ |
38
|
|
|
protected function modifyCollate(IlluminateBlueprint $blueprint, Fluent $column) |
39
|
|
|
{ |
40
|
|
|
if ( ! is_null($column->collate) ) |
41
|
|
|
{ |
42
|
|
|
$characterSet = strtok($column->collate, '_'); |
43
|
|
|
return " character set $characterSet collate {$column->collate}"; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the SQL for a "comment" column modifier. |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\Database\Schema\Blueprint $blueprint |
51
|
|
|
* @param \Illuminate\Support\Fluent $column |
52
|
|
|
* @return string|null |
53
|
|
|
*/ |
54
|
|
|
protected function modifyComment(IlluminateBlueprint $blueprint, Fluent $column) |
55
|
|
|
{ |
56
|
|
|
if ( ! is_null($column->comment) ) |
57
|
|
|
{ |
58
|
|
|
$comment = str_replace("'", "\'", $column->comment); |
59
|
|
|
return " comment '$comment'"; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Compile a create table command. |
65
|
|
|
* |
66
|
|
|
* @param \Illuminate\Database\Schema\Blueprint $blueprint |
67
|
|
|
* @param \Illuminate\Support\Fluent $command |
68
|
|
|
* @param \Illuminate\Database\Connection $connection |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function compileCreate(IlluminateBlueprint $blueprint, Fluent $command, Connection $connection) |
72
|
|
|
{ |
73
|
|
|
$sql = parent::compileCreate($blueprint, $command, $connection); |
74
|
|
|
// Table annotation support |
75
|
|
|
if ( isset($blueprint->comment) ) |
76
|
|
|
{ |
77
|
|
|
$comment = str_replace("'", "\'", $blueprint->comment); |
78
|
|
|
$sql .= " comment = '$comment'"; |
79
|
|
|
} |
80
|
|
|
return $sql; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create the column definition for a binary type. |
85
|
|
|
* |
86
|
|
|
* @param \Illuminate\Support\Fluent $column |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
protected function typeBinary(Fluent $column) |
90
|
|
|
{ |
91
|
|
|
return "binary({$column->length})"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create the column definition for an 'set' type. |
96
|
|
|
* |
97
|
|
|
* @param \Illuminate\Support\Fluent $column |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
protected function typeSet(Fluent $column) |
101
|
|
|
{ |
102
|
|
|
return "set('" . implode("', '", $column->allowed) . "')"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Compile an index creation command. |
107
|
|
|
* |
108
|
|
|
* @param \Illuminate\Database\Schema\Blueprint $blueprint |
109
|
|
|
* @param \Illuminate\Support\Fluent $command |
110
|
|
|
* @param string $type |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function compileKey(IlluminateBlueprint $blueprint, Fluent $command, $type) |
114
|
|
|
{ |
115
|
|
|
$columns = []; |
116
|
|
|
foreach($command->columns as $commandColumn) |
117
|
|
|
{ |
118
|
|
|
foreach($blueprint->getColumns() as $blueprintColumn) |
119
|
|
|
{ |
120
|
|
|
if ( $blueprintColumn->name != $commandColumn ) |
121
|
|
|
{ |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$column = $this->wrap($commandColumn); |
126
|
|
|
if ( isset($command->length) ) |
127
|
|
|
{ |
128
|
|
|
$column .= "({$command->length})"; |
129
|
|
|
} |
130
|
|
|
elseif ( 'string' == $blueprintColumn->type && $blueprintColumn->length > 255 ) |
131
|
|
|
{ |
132
|
|
|
$column .= '(255)'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$columns[] = $column; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$columns = implode(', ', $columns); |
140
|
|
|
$table = $this->wrapTable($blueprint); |
141
|
|
|
return "alter table {$table} add {$type} {$command->index}($columns)"; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Compile the query to determine if the foreign key exists |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function compileHasForeign() |
150
|
|
|
{ |
151
|
|
|
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 = ?'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.