update.func.php
5.25 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
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: update.func.php 35024 2014-10-14 07:43:43Z nemohou $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
include_once libfile('function/plugin');
if(!function_exists('updatetable')) {
function updatetable($sql) {
global $_G;
$config = array(
'dbcharset' => $_G['config']['db']['1']['dbcharset'],
'charset' => $_G['config']['output']['charset'],
'tablepre' => $_G['config']['db']['1']['tablepre']
);
preg_match_all("/CREATE\s+TABLE.+?pre\_(.+?)\s*\((.+?)\)\s*(ENGINE|TYPE)\s*=\s*(\w+)/is", $sql, $matches);
$newtables = empty($matches[1])?array():$matches[1];
$newsqls = empty($matches[0])?array():$matches[0];
if(empty($newtables) || empty($newsqls)) {
return array(1);
}
foreach($newtables as $i => $newtable) {
$newcols = updatetable_getcolumn($newsqls[$i]);
if(!$query = DB::query("SHOW CREATE TABLE ".DB::table($newtable), 'SILENT')) {
preg_match("/(CREATE TABLE .+?)\s*(ENGINE|TYPE)\s*=\s*(\w+)/is", $newsqls[$i], $maths);
$maths[3] = strtoupper($maths[3]);
if($maths[3] == 'MEMORY' || $maths[3] == 'HEAP') {
$type = helper_dbtool::dbversion() > '4.1' ? " ENGINE=MEMORY".(empty($config['dbcharset'])?'':" DEFAULT CHARSET=$config[dbcharset]" ): " TYPE=HEAP";
} else {
$type = helper_dbtool::dbversion() > '4.1' ? " ENGINE=MYISAM".(empty($config['dbcharset'])?'':" DEFAULT CHARSET=$config[dbcharset]" ): " TYPE=MYISAM";
}
$usql = $maths[1].$type;
$usql = str_replace("CREATE TABLE IF NOT EXISTS pre_", 'CREATE TABLE IF NOT EXISTS '.$config['tablepre'], $usql);
$usql = str_replace("CREATE TABLE pre_", 'CREATE TABLE '.$config['tablepre'], $usql);
if(!DB::query($usql, 'SILENT')) {
return array(-1, $newtable);
}
} else {
$value = DB::fetch($query);
$oldcols = updatetable_getcolumn($value['Create Table']);
$updates = array();
$allfileds =array_keys($newcols);
foreach ($newcols as $key => $value) {
if($key == 'PRIMARY') {
if($value != $oldcols[$key]) {
if(!empty($oldcols[$key])) {
$usql = "RENAME TABLE ".DB::table($newtable)." TO ".DB::table($newtable.'_bak');
if(!DB::query($usql, 'SILENT')) {
return array(-1, $newtable);
}
}
$updates[] = "ADD PRIMARY KEY $value";
}
} elseif ($key == 'KEY') {
foreach ($value as $subkey => $subvalue) {
if(!empty($oldcols['KEY'][$subkey])) {
if($subvalue != $oldcols['KEY'][$subkey]) {
$updates[] = "DROP INDEX `$subkey`";
$updates[] = "ADD INDEX `$subkey` $subvalue";
}
} else {
$updates[] = "ADD INDEX `$subkey` $subvalue";
}
}
} elseif ($key == 'UNIQUE') {
foreach ($value as $subkey => $subvalue) {
if(!empty($oldcols['UNIQUE'][$subkey])) {
if($subvalue != $oldcols['UNIQUE'][$subkey]) {
$updates[] = "DROP INDEX `$subkey`";
$updates[] = "ADD UNIQUE INDEX `$subkey` $subvalue";
}
} else {
$usql = "ALTER TABLE ".DB::table($newtable)." DROP INDEX `$subkey`";
DB::query($usql, 'SILENT');
$updates[] = "ADD UNIQUE INDEX `$subkey` $subvalue";
}
}
} else {
if(!empty($oldcols[$key])) {
if(strtolower($value) != strtolower($oldcols[$key])) {
$updates[] = "CHANGE `$key` `$key` $value";
}
} else {
$i = array_search($key, $allfileds);
$fieldposition = $i > 0 ? 'AFTER `'.$allfileds[$i-1].'`' : 'FIRST';
$updates[] = "ADD `$key` $value $fieldposition";
}
}
}
if(!empty($updates)) {
$usql = "ALTER TABLE ".DB::table($newtable)." ".implode(', ', $updates);
if(!DB::query($usql, 'SILENT')) {
return array(-1, $newtable);
}
}
}
}
return array(1);
}
function updatetable_getcolumn($creatsql) {
$creatsql = preg_replace("/ COMMENT '.*?'/i", '', $creatsql);
preg_match("/\((.+)\)\s*(ENGINE|TYPE)\s*\=/is", $creatsql, $matchs);
$cols = explode("\n", $matchs[1]);
$newcols = array();
foreach ($cols as $value) {
$value = trim($value);
if(empty($value)) continue;
$value = updatetable_remakesql($value);
if(substr($value, -1) == ',') $value = substr($value, 0, -1);
$vs = explode(' ', $value);
$cname = $vs[0];
if($cname == 'KEY' || $cname == 'INDEX' || $cname == 'UNIQUE') {
$name_length = strlen($cname);
if($cname == 'UNIQUE') $name_length = $name_length + 4;
$subvalue = trim(substr($value, $name_length));
$subvs = explode(' ', $subvalue);
$subcname = $subvs[0];
$newcols[$cname][$subcname] = trim(substr($value, ($name_length+2+strlen($subcname))));
} elseif($cname == 'PRIMARY') {
$newcols[$cname] = trim(substr($value, 11));
} else {
$newcols[$cname] = trim(substr($value, strlen($cname)));
}
}
return $newcols;
}
function updatetable_remakesql($value) {
$value = trim(preg_replace("/\s+/", ' ', $value));
$value = str_replace(array('`',', ', ' ,', '( ' ,' )', 'mediumtext'), array('', ',', ',','(',')','text'), $value);
return $value;
}
}
?>