-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatransform.php
259 lines (221 loc) · 6.37 KB
/
datatransform.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
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
<?php
/**
* 转换数据
*
* PHP version 7.2
*
* @category API
* @package YII
* @author saruri <saruri@163.com>
* @copyright 2006-2019 saruri
* @license https://saruri.cn/licence.txt BSD Licence
* @link http://saruri.cn
* @date 2020/04/12 11:28:29
*/
namespace saruri;
//use Yii;
class DataTransform
{
private $_config = [];
//初始化
public function __construct($_config)
{
//各种配置
$this->_config=$_config;
$this->init();
}
//继承上级的初始化
public function init()
{
//可选
$this->_config['requestString']=str_replace("#","",$this->_config['requestString']);
$this->_config['requestString']=str_replace(">","",$this->_config['requestString']);
$this->_config['requestString']=str_replace("*","",$this->_config['requestString']);
$this->arrResult=explode("\n",$this->_config['requestString']);
}
/*
* @desc 保存字段数据
* @author saruri <saruri@163.com>
* @date 2020/04/12 11:10:21
*/
public function saveTableFiled()
{
$arr=$this->arrResult;
unset($arr[0]);
unset($arr[1]);
unset($arr[2]);
//var_dump($arr);
//echo '保存字段'."\r\n";
foreach ($arr as $key => $value) {
# code...
$filed=$this->getField($value);
$this->tableConfig['fields'][$filed['name']]=$filed;
}
}
/*
* @desc 获取具体字段数据
* @author saruri <saruri@163.com>
* @date 2020/04/12 17:12:01
*/
public function getField($string)
{
$string=trim($string);
$arr=explode("|",$string);
$result['name']=trim($arr[0]);//字段名称
$result['comment']=$arr[2];//字段中文说明
$result['type']=$this->getType($string);//类型
$result['length']=$this->getLength($string);//长度
$result['key']=$this->getKey($string);//主键
$result['null']=$this->getNull($string);//是否允许空
$result['auto']=$this->getAuto($string);//是否自增
$result['default']=$this->getDefault($string);//是否自增
return $result;
}
/*
* @desc 获取类型 就设置了几个简单类型 多的您可以自行扩展 目前就这些了
* @author saruri <saruri@163.com>
* @date 2020/04/12 19:57:25
*/
public function getType($string)
{
$arr=[
'tinyint'=>'tinyint',
'mediumint'=>'mediumint',
'int'=>'int',
'float'=>'float',
'double'=>'double',
'varchar'=>'varchar',
'char'=>'char',
'text'=>'text',
'整数'=>'int',
'文本'=>'text',
'datetime'=>'datetime',
'时间戳'=>'timestamp',
'时间'=>'time',
'日期'=>'datetime',
];
//数字类
//return $string;
foreach ($arr as $key => $value) {
if($this->check_str($string,$key)){ return strtoupper($value); }
}
return 'VARCHAR';
}
public function check_str($str, $substr)
{
$nums=substr_count($str,$substr);
if ($nums>=1)
{
return true;
}
else
{
return false;
}
}
/*
* @desc 获取长度
* @author saruri <saruri@163.com>
* @date 2020/04/12 19:57:25
*/
public function getLength($string)
{
$arr=explode("|",trim($string));
if($arr){
foreach ($arr as $key => $value) {
if(intval($value)>0){
return '('.$value.')';
}
}
}
//默认值
$type=$this->getType($string);
//return $type;
if($this->_config[$type]){
return '('.$this->_config[$type].')';
}
return '';//'长度';
}
/*
* @desc 获取主键
* @author saruri <saruri@163.com>
* @date 2020/04/12 19:57:25
*/
public function getKey($string)
{
if($this->check_str($string,'key')){ return 'PRIMARY KEY'; }
if($this->check_str($string,'主键')){ return 'PRIMARY KEY'; }
return '';
}
/*
* @desc 获取是否自增
* @author saruri <saruri@163.com>
* @date 2020/04/12 19:57:25
*/
public function getAuto($string)
{
if($this->check_str($string,'auto')){ return 'AUTO_INCREMENT'; }
if($this->check_str($string,'自增')){ return 'AUTO_INCREMENT'; }
return '';
}
/*
* @desc 获取是否允许空
* @author saruri <saruri@163.com>
* @date 2020/04/12 19:57:25
*/
public function getNull($string)
{
if($this->check_str($string,'NULL')){ return 'NULL'; }
if($this->check_str($string,'允许空')){ return 'NULL'; }
return 'NOT NULL';
}
/*
* @desc 获取默认值
* @author saruri <saruri@163.com>
* @date 2020/04/12 21:39:16
*/
public function getDefault($string)
{
$key=$this->getKey($string);
if($key=='PRIMARY KEY'){
return '';
}
$type=$this->getType($string);
$arrText=['VARCHAR','CHAR','TEXT'];
$arrNum=['MEDIUMINT','INT','TINYINT','FLOAT','DOUBLE'];
$arrTime=['TIMESTAMP'];
//文本类
if(in_array($type, $arrText)){
return " DEFAULT '' ";
}
//数字类
if(in_array($type, $arrNum)){
return " DEFAULT 0 ";
}
//时间类
if(in_array($type, $arrTime)){
return " DEFAULT CURRENT_TIMESTAMP ";
}
return '';
}
/*
* @desc 保存表头
* @author saruri <saruri@163.com>
* @date 2020/04/12 10:58:20
*/
public function saveTableHead()
{
$string=trim($this->arrResult[0]);
$arr=explode(" ",$string);
$this->tableConfig['tableComment']=$arr[0];
$this->tableConfig['tableName']=end($arr);
}
public function run()
{
//保存表头
self::saveTableHead();
//保存字段数据
self::saveTableFiled();
return $this->tableConfig;
}
}