forked from a07061625/swooleyaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_mysql.php
207 lines (191 loc) · 8.01 KB
/
helper_mysql.php
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
<?php
require_once __DIR__ . '/helper_load.php';
class MysqlTool {
use \Traits\SimpleTrait;
private static function handlePath(){
$needStr1 = \Tool\Tool::getClientOption('-path', false, '');
$needStr2 = preg_replace([
'/\s+/',
'/\/+/'
], [
'',
'/',
], $needStr1);
$needStr3 = trim($needStr2);
$length = strlen($needStr3);
if($length == 0){
$path = '';
} else {
$path = substr($needStr3, ($length - 1), 1) == '/' ? $needStr3 : $needStr3 . '/';
}
return $path;
}
/**
* 转换表名或数据库名
* @param string $name 表名或数据库名
* @return string
*/
private static function transferName(string $name) : string {
$str1 = strtolower($name);
$str2 = preg_replace([
'/[^0-9a-z]+/',
'/\s+/'
], [
' ',
' ',
], $str1);
$str3 = ucwords(trim($str2));
return str_replace(' ', '', $str3);
}
public static function generator(){
$option = \Tool\Tool::getClientOption(1, true);
switch ($option) {
case 'entities' :
self::createDbEntities([
'db' => trim(\Tool\Tool::getClientOption('-db', false, '')),
'path' => self::handlePath(),
'prefix' => trim(\Tool\Tool::getClientOption('-prefix', false, '')),
'suffix' => trim(\Tool\Tool::getClientOption('-suffix', false, '')),
]);
break;
case 'entity' :
self::createDbEntity([
'db' => trim(\Tool\Tool::getClientOption('-db', false, '')),
'table' => trim(\Tool\Tool::getClientOption('-table', false, '')),
'path' => self::handlePath(),
'prefix' => trim(\Tool\Tool::getClientOption('-prefix', false, '')),
'suffix' => trim(\Tool\Tool::getClientOption('-suffix', false, '')),
]);
break;
default :
self::help();
break;
}
}
private static function help(){
echo '显示帮助: php helper_mysql.php -h' . PHP_EOL;
echo '生成数据库下所有的实体类: php helper_mysql.php entities -db xxx -path /xxx -prefix xxx -suffix xxx' . PHP_EOL;
echo ' -db:数据库名' . PHP_EOL;
echo ' -path:存放实体类文件的目录' . PHP_EOL;
echo ' -prefix:实体类文件前缀' . PHP_EOL;
echo ' -suffix:实体类文件后缀' . PHP_EOL;
echo '生成数据库下指定的实体类: php helper_mysql.php entity -db xxx -table xxx -path /xxx -prefix xxx -suffix xxx' . PHP_EOL;
echo ' -db:数据库名' . PHP_EOL;
echo ' -table:表名' . PHP_EOL;
echo ' -path:存放实体类文件的目录' . PHP_EOL;
echo ' -prefix:实体类文件前缀' . PHP_EOL;
echo ' -suffix:实体类文件后缀' . PHP_EOL;
}
/**
* 创建数据库下所有的实体类
*/
private static function createDbEntities(array $configs){
if(!$configs['db']){
exit('数据库名不能为空' . PHP_EOL);
}
$tables = \DesignPatterns\Singletons\MysqlSingleton::getInstance()->getDbTables($configs['db']);
foreach ($tables as $eTable) {
self::createDbEntity([
'db' => $configs['db'],
'table' => $eTable,
'path' => $configs['path'],
'prefix' => $configs['prefix'],
'suffix' => $configs['suffix'],
]);
echo 'create entity ' . $eTable . ' success' . PHP_EOL;
}
echo 'create all entities success' . PHP_EOL;
}
/**
* 创建数据库下指定的实体类
*/
private static function createDbEntity(array $configs){
if(!$configs['db']){
exit('数据库名不能为空' . PHP_EOL);
} else if(!$configs['table']){
exit('数据库表名不能为空' . PHP_EOL);
}
$fields = \DesignPatterns\Singletons\MysqlSingleton::getInstance()->getTableFields($configs['table'], $configs['db']);
$fileName = $configs['prefix'] . self::transferName($configs['table']) . $configs['suffix'];
$primaryKey = 'id';
$filedStr = '';
foreach ($fields as $eField) {
if (strpos($eField['Type'], 'int') !== false) {
$varType = 'int';
if(is_null($eField['Default'])){
if($eField['Key'] == 'PRI'){
$primaryKey = $eField['Field'];
$default = 'null';
} else {
$default = $eField['Null'] == 'NO' ? '0' : 'null';
}
} else {
$default = (int)$eField['Default'];
}
} else if (strpos($eField['Type'], 'float') !== false) {
$varType = 'double';
if(is_null($eField['Default'])){
if($eField['Key'] == 'PRI'){
$primaryKey = $eField['Field'];
$default = 'null';
} else {
$default = $eField['Null'] == 'NO' ? '0.00' : 'null';
}
} else {
$default = (double)$eField['Default'];
}
} else if (strpos($eField['Type'], 'decimal') !== false) {
$varType = 'double';
if(is_null($eField['Default'])){
if($eField['Key'] == 'PRI'){
$primaryKey = $eField['Field'];
$default = 'null';
} else {
$default = $eField['Null'] == 'NO' ? '0.00' : 'null';
}
} else {
$default = (double)$eField['Default'];
}
} else if (strpos($eField['Type'], 'double') !== false) {
$varType = 'double';
if(is_null($eField['Default'])){
if($eField['Key'] == 'PRI'){
$primaryKey = $eField['Field'];
$default = 'null';
} else {
$default = $eField['Null'] == 'NO' ? '0.00' : 'null';
}
} else {
$default = (double)$eField['Default'];
}
} else {
$varType = 'string';
if(is_null($eField['Default'])){
if($eField['Key'] == 'PRI'){
$primaryKey = $eField['Field'];
$default = 'null';
} else {
$default = $eField['Null'] == 'NO' ? "''" : 'null';
}
} else {
$default = "'" . $eField['Default'] . "'";
}
}
$filedStr .= PHP_EOL . ' /**' . PHP_EOL;
$filedStr .= ' * ' . $eField['Comment'] . PHP_EOL;
$filedStr .= ' * @var ' . $varType . PHP_EOL;
$filedStr .= ' */' . PHP_EOL;
$filedStr .= ' public $' . $eField['Field'] . ' = ' . $default . ';' . PHP_EOL;
}
$content = '<?php' . PHP_EOL . 'namespace Entities\\' . self::transferName($configs['db']) . ';' . PHP_EOL . PHP_EOL;
$content .= 'use DB\\Entities\\MysqlEntity;' . PHP_EOL . PHP_EOL;
$content .= 'class ' . $fileName . ' extends MysqlEntity {' . PHP_EOL;
$content .= ' public function __construct() {' . PHP_EOL;
$content .= ' parent::__construct(\'' . $configs['db'] . '\', \'' . $configs['table'] . '\', \'' . $primaryKey . '\');' . PHP_EOL;
$content .= ' }' . PHP_EOL;
$content .= $filedStr;
$content .= '}' . PHP_EOL;
file_put_contents($configs['path'] . $fileName . '.php', $content);
}
}
MysqlTool::generator();