|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Migratio\GrammarStructure\Mysql\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Resta\Support\Generator\Generator; |
|
6
|
|
|
use Resta\Support\Utils; |
|
7
|
|
|
|
|
8
|
|
|
trait QueryStack |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @return mixed |
|
12
|
|
|
*/ |
|
13
|
|
|
public function connection() |
|
14
|
|
|
{ |
|
15
|
|
|
return $this->schema->getConnection(); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return mixed |
|
20
|
|
|
*/ |
|
21
|
|
|
public function showTables() |
|
22
|
|
|
{ |
|
23
|
|
|
$tables = $this->connection()->query('SHOW TABLES')->fetchAll(); |
|
24
|
|
|
|
|
25
|
|
|
$list = []; |
|
26
|
|
|
|
|
27
|
|
|
foreach ($tables as $key=>$table) { |
|
28
|
|
|
$list[] = $table[0]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $list; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $table |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function showColumnsFrom($table) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->connection()->query('SHOW FULL COLUMNS FROM '.$table)->fetchAll(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* get charset for collation |
|
45
|
|
|
* |
|
46
|
|
|
* @param $collation |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getCharsetForCollation($collation) |
|
50
|
|
|
{ |
|
51
|
|
|
$collation = $this->connection()->query('SHOW COLLATION LIKE \''.$collation.'%\'')->fetchAll(); |
|
52
|
|
|
|
|
53
|
|
|
if(isset($collation[0])){ |
|
54
|
|
|
return $collation[0]['Charset']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return 'utf8'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* get table status |
|
62
|
|
|
* |
|
63
|
|
|
* @param $table |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getTableStatus($table) |
|
67
|
|
|
{ |
|
68
|
|
|
$status = $this->connection()->query('show table status like \''.$table.'\'')->fetchAll(); |
|
69
|
|
|
|
|
70
|
|
|
if(isset($status[0])){ |
|
71
|
|
|
return $status[0]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return []; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* get show indexes |
|
79
|
|
|
* |
|
80
|
|
|
* @param $table |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
|
|
public function showIndexes($table) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->connection()->query('SHOW INDEXES FROM '.$table)->fetchAll(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* get foreign keys from table |
|
90
|
|
|
* |
|
91
|
|
|
* @param $table |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getForeignKeys($table) |
|
95
|
|
|
{ |
|
96
|
|
|
$config = $this->getConfig(); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$database = (isset($config['database'])) ? $config['database'] : null; |
|
99
|
|
|
|
|
100
|
|
|
$query = $this->connection()->query('SELECT rc.MATCH_OPTION,rc.UPDATE_RULE,rc.DELETE_RULE,r.* |
|
101
|
|
|
FROM information_schema.REFERENTIAL_CONSTRAINTS as rc |
|
102
|
|
|
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE as r ON r.TABLE_NAME=rc.TABLE_NAME |
|
103
|
|
|
WHERE rc.CONSTRAINT_SCHEMA = \''.$database.'\' |
|
104
|
|
|
AND rc.TABLE_NAME = \''.$table.'\' AND r.REFERENCED_TABLE_NAME is not null')->fetchAll(); |
|
105
|
|
|
|
|
106
|
|
|
if(isset($query[0])){ |
|
107
|
|
|
return $query[0]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return []; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* set query basic |
|
115
|
|
|
* |
|
116
|
|
|
* @param $query |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setQueryBasic($query) |
|
120
|
|
|
{ |
|
121
|
|
|
try { |
|
122
|
|
|
|
|
123
|
|
|
$query =$this->connection()->query($query); |
|
124
|
|
|
|
|
125
|
|
|
return [ |
|
126
|
|
|
'result'=>true, |
|
127
|
|
|
'query'=>$query, |
|
128
|
|
|
'message'=>null, |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
catch (\PDOException $exception){ |
|
132
|
|
|
|
|
133
|
|
|
return [ |
|
134
|
|
|
'result'=>false, |
|
135
|
|
|
'query'=>$query, |
|
136
|
|
|
'message'=>$exception->getMessage(), |
|
137
|
|
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param $table |
|
144
|
|
|
*/ |
|
145
|
|
|
public function generateEntity($table) |
|
146
|
|
|
{ |
|
147
|
|
|
if(in_array($table,$this->showTables()) || in_array($table = strtolower($table),$this->showTables())){ |
|
148
|
|
|
|
|
149
|
|
|
$entityDirectory = path()->model().''.DIRECTORY_SEPARATOR.'Entity'; |
|
150
|
|
|
|
|
151
|
|
|
if(!file_exists($entityDirectory)){ |
|
152
|
|
|
files()->makeDirectory($entityDirectory); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$columns = $this->showColumnsFrom($table); |
|
156
|
|
|
|
|
157
|
|
|
$list = []; |
|
158
|
|
|
|
|
159
|
|
|
foreach ($columns as $column) { |
|
160
|
|
|
$list[] = $column['Field']; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if(!file_exists($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table))){ |
|
164
|
|
|
$generator = new Generator($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table),$table.''); |
|
165
|
|
|
|
|
166
|
|
|
$generator->createClass(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
$abstractClassPath = $entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table).''.DIRECTORY_SEPARATOR.'Entity'; |
|
171
|
|
|
$abstractNamespace = Utils::getNamespace($abstractClassPath.''.DIRECTORY_SEPARATOR.''.ucfirst($table).'Abstract'); |
|
172
|
|
|
|
|
173
|
|
|
$generator->createClassExtend($abstractNamespace,ucfirst($table).'Abstract'); |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
$generator = new Generator($abstractClassPath,$table.'Abstract'); |
|
176
|
|
|
|
|
177
|
|
|
$generator->createClass(); |
|
178
|
|
|
|
|
179
|
|
|
$method =array_merge([ |
|
180
|
|
|
'__construct' |
|
181
|
|
|
],array_merge($list,['__get'])); |
|
182
|
|
|
|
|
183
|
|
|
$generator->createMethod($method); |
|
184
|
|
|
|
|
185
|
|
|
$generator->createMethodParameters([ |
|
186
|
|
|
'__construct' => '$query', |
|
187
|
|
|
'__get' => '$name' |
|
188
|
|
|
]); |
|
189
|
|
|
|
|
190
|
|
|
$methodBodyList = []; |
|
191
|
|
|
$createMethodAccessibleProperty = []; |
|
192
|
|
|
$createMethodDocument = []; |
|
193
|
|
|
$createClassDocument = []; |
|
194
|
|
|
|
|
195
|
|
|
foreach ($list as $item) { |
|
196
|
|
|
$methodBodyList[$item] = 'return self::$query->'.$item.';'; |
|
197
|
|
|
$createClassDocument[] = '@property $this '.$item; |
|
198
|
|
|
$createMethodAccessibleProperty[$item] = 'protected static'; |
|
199
|
|
|
$createMethodDocument[$item] = [ |
|
200
|
|
|
'@return mixed' |
|
201
|
|
|
]; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$generator->createClassDocument($createClassDocument); |
|
205
|
|
|
|
|
206
|
|
|
$generator->createMethodDocument(array_merge($createMethodDocument,[ |
|
207
|
|
|
'__construct' => [ |
|
208
|
|
|
''.$table.' constructor.', |
|
209
|
|
|
'@param null|object $query' |
|
210
|
|
|
], |
|
211
|
|
|
'__get' =>[ |
|
212
|
|
|
'access entity object with magic method', |
|
213
|
|
|
'', |
|
214
|
|
|
'@param $name', |
|
215
|
|
|
'@return mixed' |
|
216
|
|
|
] |
|
217
|
|
|
])); |
|
218
|
|
|
|
|
219
|
|
|
$createMethodBody = array_merge([ |
|
220
|
|
|
'__construct' => 'self::$query = $query;', |
|
221
|
|
|
'__get' => 'return static::{$name}();' |
|
222
|
|
|
],$methodBodyList); |
|
223
|
|
|
|
|
224
|
|
|
$generator->createMethodBody($createMethodBody); |
|
225
|
|
|
|
|
226
|
|
|
$generator->createMethodAccessibleProperty($createMethodAccessibleProperty); |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
$generator->createClassProperty([ |
|
230
|
|
|
'protected static $query;' |
|
231
|
|
|
]); |
|
232
|
|
|
|
|
233
|
|
|
$generator->createClassPropertyDocument([ |
|
234
|
|
|
'protected static $query' => [ |
|
235
|
|
|
'@var object|null' |
|
236
|
|
|
] |
|
237
|
|
|
]); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
|