1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataDb\Dialect; |
4
|
|
|
|
5
|
|
|
use Graze\DataDb\TableNodeInterface; |
6
|
|
|
|
7
|
|
|
class MysqlDialect extends AbstractDialect |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
|
|
public function getIdentifierQuote() |
13
|
|
|
{ |
14
|
|
|
return '`'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param TableNodeInterface $old |
19
|
|
|
* @param TableNodeInterface $new |
20
|
|
|
* |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function getCreateTableLike(TableNodeInterface $old, TableNodeInterface $new) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
|
|
$this->format( |
27
|
|
|
'CREATE TABLE {new:schema|q}.{new:table|q} LIKE {old:schema|q}.{old:table|q}', |
28
|
|
|
['old' => $old, 'new' => $new,] |
29
|
|
|
), |
30
|
|
|
[], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param TableNodeInterface $source |
36
|
|
|
* @param TableNodeInterface $join |
37
|
|
|
* @param string $on |
38
|
|
|
* @param string|null $where |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getDeleteTableJoinSoftDelete( |
43
|
|
|
TableNodeInterface $source, |
44
|
|
|
TableNodeInterface $join, |
45
|
|
|
$on, |
46
|
|
|
$where = null |
47
|
|
|
) { |
48
|
|
|
return [ |
49
|
|
|
$this->format( |
50
|
|
|
'UPDATE {source:schema|q}.{source:table|q} |
51
|
|
|
JOIN {join:schema|q}.{join:table|q} |
52
|
|
|
ON {on} |
53
|
|
|
SET{softUpdated} |
54
|
|
|
{source:softDeleted|q} = CURRENT_TIMESTAMP |
55
|
|
|
{where}', |
56
|
|
|
[ |
57
|
|
|
'source' => $source, |
58
|
|
|
'join' => $join, |
59
|
|
|
'on' => $on, |
60
|
|
|
'softUpdated' => ($source->getSoftUpdated() ? |
61
|
|
|
$this->format( |
62
|
|
|
' {source:softUpdated|q} = CURRENT_TIMESTAMP,', |
63
|
|
|
['source' => $source] |
64
|
|
|
) : '' |
65
|
|
|
), |
66
|
|
|
'where' => ($where ? 'WHERE ' . $where : ''), |
67
|
|
|
] |
68
|
|
|
), |
69
|
|
|
[], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param TableNodeInterface $source |
75
|
|
|
* @param TableNodeInterface $join |
76
|
|
|
* @param string $on |
77
|
|
|
* @param string|null $where |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function getDeleteTableJoin( |
|
|
|
|
82
|
|
|
TableNodeInterface $source, |
83
|
|
|
TableNodeInterface $join, |
84
|
|
|
$on, |
85
|
|
|
$where = null |
86
|
|
|
) { |
87
|
|
|
return [ |
88
|
|
|
$this->format( |
89
|
|
|
'DELETE {source:schema|q}.{source:table|q} |
90
|
|
|
FROM {source:schema|q}.{source:table|q} |
91
|
|
|
JOIN {join:schema|q}.{join:table|q} |
92
|
|
|
ON {on} |
93
|
|
|
{where}', |
94
|
|
|
[ |
95
|
|
|
'source' => $source, |
96
|
|
|
'join' => $join, |
97
|
|
|
'on' => $on, |
98
|
|
|
'where' => ($where ? 'WHERE ' . $where : ''), |
99
|
|
|
] |
100
|
|
|
), |
101
|
|
|
[], |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param TableNodeInterface $table |
107
|
|
|
* @param string|null $where |
108
|
|
|
* |
109
|
|
|
* @return array [sql, bind] |
110
|
|
|
*/ |
111
|
|
|
public function getDeleteFromTableSoftDelete(TableNodeInterface $table, $where = null) |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
|
|
$this->format( |
115
|
|
|
'UPDATE {table:schema|q}.{table:table|q} |
116
|
|
|
SET{softUpdated} |
117
|
|
|
{table:softDeleted|q} = CURRENT_TIMESTAMP |
118
|
|
|
{where}', |
119
|
|
|
[ |
120
|
|
|
'table' => $table, |
121
|
|
|
'softUpdated' => ($table->getSoftUpdated() ? |
122
|
|
|
$this->format(' {table:softUpdated|q} = CURRENT_TIMESTAMP,', ['table' => $table]) : |
123
|
|
|
''), |
124
|
|
|
'where' => ($where ? 'WHERE ' . $where : ''), |
125
|
|
|
] |
126
|
|
|
), |
127
|
|
|
[], |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param TableNodeInterface $table |
133
|
|
|
* @param array $columns |
134
|
|
|
* @param array $primary |
135
|
|
|
* @param array $index |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function getCreateTable(TableNodeInterface $table, array $columns, array $primary, array $index) |
140
|
|
|
{ |
141
|
|
|
$allColumns = array_merge($columns, $primary, $index); |
142
|
|
|
|
143
|
|
|
return [ |
144
|
|
|
$this->format( |
145
|
|
|
"CREATE TABLE {table:schema|q}.{table:table|q} (\n {allColumns}\n)", |
146
|
|
|
[ |
147
|
|
|
'table' => $table, |
148
|
|
|
'allColumns' => implode(",\n ", $allColumns), |
149
|
|
|
] |
150
|
|
|
), |
151
|
|
|
[], |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $column |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function getColumnDefinition(array $column) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return $this->format( |
163
|
|
|
'{column|q} {type} {notnull}', |
164
|
|
|
array_merge($column, ['notnull' => $column['nullable'] ? 'NULL' : 'NOT NULL']) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param array $key |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function getPrimaryKeyDefinition(array $key) |
174
|
|
|
{ |
175
|
|
|
return $this->format('PRIMARY KEY ({column|q})', $key); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $index |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function getIndexDefinition(array $index) |
184
|
|
|
{ |
185
|
|
|
return $this->format('KEY {column|q} ({column|q})', $index); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param TableNodeInterface $table |
190
|
|
|
* |
191
|
|
|
* @return array [sql, bind] |
192
|
|
|
*/ |
193
|
|
|
public function getDescribeTable(TableNodeInterface $table) |
194
|
|
|
{ |
195
|
|
|
return [$this->format('DESCRIBE {table:schema|q}.{table:table|q}', ['table' => $table]), []]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param TableNodeInterface $table |
200
|
|
|
* |
201
|
|
|
* @return array [sql, bind] |
202
|
|
|
*/ |
203
|
|
|
public function getCreateSyntax(TableNodeInterface $table) |
204
|
|
|
{ |
205
|
|
|
return [ |
206
|
|
|
$this->format('SHOW CREATE TABLE {table:schema|q}.{table:table|q}', ['table' => $table]), |
207
|
|
|
[], |
208
|
|
|
]; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.