-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysqlAnalyse.php
More file actions
309 lines (291 loc) · 7.53 KB
/
mysqlAnalyse.php
File metadata and controls
309 lines (291 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* Created by PhpStorm.
* User: bruce
* Date: 16/7/14
* Time: 11:37
*/
class mysqlAnalyse
{
private $tablesqls = array();
private $tables = array();
private $keys = array(
"DEFAULT",
"NOT",
"COMMENT",
"UNSIGNED",
"AUTO_INCREMENT",
"BINARY",
);
private $twoWordKeys = array(
"NOT"=>"NULL",
"UNSIGNED"=>"ZEROFILL",
);
private $multiWordKeys = array(
"DEFAULT"=>array("CURRENT_TIMESTAMP","ON","UPDATE","CURRENT_TIMESTAMP"),
);
public function getTableStart($html){
$needle = "CREATE";
$lastPos = 0;
$positions = array();
while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
return $positions;
}
public function getTableEnd($html,$start){
$needle = "ENGINE";
$endPositions = array();
if(is_array($start)) {
foreach ($start as $item) {
if(($linePos = strpos($html,$needle, $item))!==false){
$pos = strpos($html,"\n",$linePos) - 1;
$endPositions[] = $pos;
}
}
}elseif($start >= 0){
if(($pos = strpos($html,$needle, $start))!==false){
$endPositions = $pos;
}
}
return $endPositions;
}
public function getTableSql($html){
$startPos = $this->getTableStart($html);
$endPos = $this->getTableEnd($html,$startPos);
if(count(array_diff_key($startPos,$endPos))>0){
return $this->tablesqls;
}
foreach ($startPos as $key=>$startPo) {
if($startPo >= $endPos[$key]){
break;
}else{
$this->tablesqls[] = substr($html,$startPo,$endPos[$key]-$startPo+1);
}
}
return $this->tablesqls;
}
public function initTables($html){
$tablesql = $this->getTableSql($html);
foreach ($tablesql as $item) {
$tableName = $this->getTableName($item);
if($tableName != '' && is_string($tableName)) {
$this->tables[$tableName] =
array(
'createSql' => $item,
'tableFields' => $this->getTableFields($item),
'tableIndexes' => $this->getTableIndexes($item),
'tableProperties' => $this->getTableProperties($item),
);
}
}
}
public function getTables(){
return $this->tables;
}
public function getTableName($subject){
$pattern = '/(?<=\`)[^`]\w*(?=\`)/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
if(count($matches)>0) {
return $matches[0][0];
}else{
return '';
}
}
public function getTableFields($item){
$result = array();
$lines = explode("\n",$item);
foreach ($lines as $line) {
if(strpos($line,'CREATE TABLE')!==false){
continue;
}
if(strpos($line,'KEY')!==false){
continue;
}
if($line==end($lines)){
continue;
}
$name = $this->getFieldNameFromLine($line);
$properties = $this->getFieldPropertiesFromLine($line);
if($name!=''){
$result[$name]=$properties;
}
}
return $result;
}
public function getTableIndexes($item){
$result = array();
$lines = explode("\n",$item);
foreach ($lines as $line) {
if(strpos($line,'KEY')!==false){
$name = $this->getIndexNameFromLine($line);
$field = $this->getIndexFieldNameFromLine($line);
$properties = $this->getIndexPropertiesFromLine($line);
if($name!=''){
$result[$name]=array("field"=>$field,"properties"=>$properties);
}
}
}
return $result;
}
public function getTableProperties($item){
$result = array();
$lines = explode("\n",$item);
$line = array_pop($lines);
$line = trim($line,") ;");
$properties = explode(" ",$line);
$properties = $this->mergeComments($properties);
foreach ($properties as $property) {
$res = explode("=",$property);
if(count($res)>1){
$result[$res[0]] = $res[1];
}
}
return $result;
}
public function getFieldNameFromLine($line){
$poss = strpos($line,'`');
$pose = strpos($line,'`',$poss+1);
if($poss!==false&&$pose!==false) {
return substr($line, $poss + 1, $pose - $poss - 1);
}else{
return '';
}
}
//analyse each fields properties of a table
public function getFieldPropertiesFromLine($line){
$result = array();
$poss = strpos($line,'`');
$pose = strpos($line,'`',$poss+1);
if($poss!==false&&$pose!==false) {
$strBehind = trim(trim(substr($line, $pose + 1)), ",");
$res = explode(" ", $strBehind);
$res = $this->mergeComments($res);
$preKeyword = '';
$multiKeyIterator = 0;
$multiKeyPartiallyMatches = array();
foreach($res as $key=>$value){
$value = strtoupper($value);
//第一个是字段类型和长度
if($key==0){
if(($bracketS=strpos($res[0],"("))!==false){
$result['type'] = substr($res[0],0,$bracketS);
$result['length'] = substr(trim($res[0],")"),$bracketS+1);
}else{
$result['type'] = $res[0];
$result['length'] = '';
}
continue;
}
if(in_array($value,$this->keys)){
if($preKeyword!=''){
if(count($multiKeyPartiallyMatches)>0){
$result[$preKeyword] = implode(" ",$multiKeyPartiallyMatches);
$multiKeyPartiallyMatches = array();
}else {
$result[$preKeyword] = true;
}
}
$preKeyword = $value;
continue;
}
if(array_key_exists($preKeyword,$this->twoWordKeys)&&$this->twoWordKeys[$preKeyword]==$value){
$result["$preKeyword $value"]=true;
$preKeyword = '';
continue;
}
if(array_key_exists($preKeyword,$this->multiWordKeys)&&in_array($value,$this->multiWordKeys[$preKeyword])){
if($value == $this->multiWordKeys[$preKeyword][$multiKeyIterator]){
$multiKeyIterator += 1;
$multiKeyPartiallyMatches[]=$value;
}
if($multiKeyIterator==count($this->multiWordKeys[$preKeyword])){
$result[$preKeyword] = implode(" ", $this->multiWordKeys[$preKeyword]);
$multiKeyIterator = 0;
$preKeyword = '';
$multiKeyPartiallyMatches = array();
}else {
continue;
}
}
if($preKeyword != ''){
$result[$preKeyword] = $value;
$preKeyword = '';
}
}
if($preKeyword!=''){
if(count($multiKeyPartiallyMatches)>0){
$result[$preKeyword] = implode(" ",$multiKeyPartiallyMatches);
}else {
$result[$preKeyword] = true;
}
}
}
return $result;
}
public function getIndexNameFromLine($line){
return $this->getFieldNameFromLine($line);
}
public function getIndexFieldNameFromLine($line){
//deal with case of "PRIMARY KEY (`id`),"
if(strpos($line,"PRIMARY")!==false){
return $this->getFieldNameFromLine($line);
}
$poss = strpos($line,'(`');
$pose = strpos($line,'`)',$poss+2);
if($poss!==false&&$pose !== false) {
$fieldString = substr($line, $poss + 2, $pose - $poss - 2);
//deal with case of "KEY `userid` (`userid`,`token`)"
return explode("`,`",$fieldString);
}else{
return array();
}
}
public function getIndexPropertiesFromLine($line){
$properties = explode(" ",trim($line));
foreach($properties as $key=>$property){
if($property == "KEY"){
if($key==0) {
return "INDEX";
}
}else{
return $property;
}
}
return '';
}
public function mergeComments($wordsArr){
if(is_array($wordsArr)){
$needToMerge = array();
$commentStart = false;
$mergedIndex = 0;
$needToDelete = array();
foreach ($wordsArr as $key=>$word) {
if($commentStart){
if(!in_array($word,$this->keys)){
$needToMerge[] = $word;
if($mergedIndex < $key) {
$needToDelete[] = $key;
}
}else{
break;
}
}
if($word=="COMMENT"){
$commentStart = true;
$mergedIndex = $key + 1;
}
}
if(count($needToDelete) > 0){
$wordsArr[$mergedIndex] = " ".implode(" ",$needToMerge);
foreach ($needToDelete as $item) {
unset($wordsArr[$item]);
}
}
}else{
return array();
}
return $wordsArr;
}
}