-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTableHelpers.php
More file actions
278 lines (253 loc) · 9.45 KB
/
Copy pathTableHelpers.php
File metadata and controls
278 lines (253 loc) · 9.45 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
<?php
namespace Andruby\DeepAdmin;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Str;
class TableHelpers
{
/**
* 创建表
*
* @param $table_name
* @return mixed
* @throws \Exception
*/
public static function create_table($table_name)
{
try {
if (Schema::hasTable($table_name)) {
return \Admin::responseError("数据库表已存在");
}
Schema::create($table_name, function (Blueprint $table) {
$table->increments('id');
$table->softDeletes();
$table->timestamps();
$table->engine = 'InnoDB';
});
} catch (\Exception $e) {
throw new \Exception("创建数据库表异常");
}
}
/**
* 重命名表名
* @param $table_from
* @param $table_to
* @throws \Exception
*/
public static function rename_table($table_from, $table_to)
{
try {
if (Schema::hasTable($table_to)) {
throw new \Exception("数据库表已存在");
}
Schema::rename($table_from, $table_to);
} catch (\Exception $e) {
throw new \Exception("更新数据库表异常");
}
}
/**
* 检测表是否存在
*
* @param $table_name
* @return mixed
* @throws \Exception
*/
public static function check_exist_table($table_name)
{
try {
return Schema::hasTable($table_name);
} catch (\Exception $e) {
throw new \Exception("检测数据库表异常");
}
}
/**
* 删除表
*
* @param $table_name
* @throws \Exception
*/
public static function drop_table($table_name)
{
try {
Schema::dropIfExists($table_name);
} catch (\Exception $e) {
throw new \Exception("删除数据库表异常");
}
}
/**
* 添加表字段
*
* @param $tableName
* @param $fieldInfo
* @return mixed
* @throws \Exception
*/
public static function create_field($tableName, $fieldInfo)
{
try {
if (!Schema::hasTable($tableName)) {
return \Admin::responseError("数据库表不存在");
}
if (Schema::hasColumn($tableName, $fieldInfo['name'])) {
return \Admin::responseError("数据库表字段已存在");
}
if (!isset(config('deep_admin.db_table_field_type')[$fieldInfo['type']])) {
return \Admin::responseError("无效字段类型");
}
Schema::table($tableName, function (Blueprint $table) use ($fieldInfo) {
$type = $fieldInfo['type'];
$length = isset($fieldInfo['field_length']) ? intval($fieldInfo['field_length']) : 0;
$total = isset($fieldInfo['field_total']) ? intval($fieldInfo['field_total']) : 0;
$scale = isset($fieldInfo['field_scale']) ? intval($fieldInfo['field_scale']) : 0;
if (in_array($type, ['char', 'string'])) {
$table = $table->$type($fieldInfo['name'], $length > 0 ? $length : 255)
->comment($fieldInfo['comment'])
->default(strval($fieldInfo['default_value']));
} elseif (Str::contains(Str::lower($type), 'integer')) {
$table = $table->$type($fieldInfo['name'])
->comment($fieldInfo['comment'])
->default(intval($fieldInfo['default_value']));
} elseif (in_array($type, ['float', 'double', 'decimal', 'unsignedDecimal'])) {
if ($total > 0 && $scale > 0 && $total > $scale) {
$table = $table->$type($fieldInfo['name'], $total, $scale)
->comment($fieldInfo['comment'])
->default(doubleval($fieldInfo['default_value']));
} else {
$table = $table->$type($fieldInfo['name'])
->comment($fieldInfo['comment'])
->default(doubleval($fieldInfo['default_value']));
}
} else {
$table = $table->$type($fieldInfo['name'])->comment($fieldInfo['comment']);
}
if (!$fieldInfo['is_required']) {
$table = $table->nullable();
}
if ($fieldInfo['is_unique']) {
$table = $table->unique();
}
});
} catch (\Exception $e) {
throw new \Exception("创建数据库表字段异常");
}
}
/**
* 删除表字段
*
* @param $tableName
* @param $fieldName
* @throws \Exception
*/
public static function drop_field($tableName, $fieldName)
{
try {
Schema::table($tableName, function (Blueprint $table) use ($fieldName) {
$table->dropColumn($fieldName);
});
} catch (\Exception $e) {
throw new \Exception("删除数据库表字段异常");
}
}
/**
* 修改表字段
*
* @param $tableName
* @param $fieldInfo
* @return mixed
* @throws \Exception
*/
public static function update_field($tableName, $fieldInfo)
{
try {
if (!Schema::hasTable($tableName)) {
return \Admin::responseError("数据库表不存在");
}
if (!Schema::hasColumn($tableName, $fieldInfo['name'])) {
return \Admin::responseError("数据库表字段不存在");
}
if (!isset(config('deep_admin.db_table_field_type')[$fieldInfo['type']])) {
return \Admin::responseError("无效字段类型");
}
Schema::table($tableName, function (Blueprint $table) use ($fieldInfo) {
$type = $fieldInfo['type'];
$length = isset($fieldInfo['field_length']) ? intval($fieldInfo['field_length']) : 255;
$total = isset($fieldInfo['field_total']) ? intval($fieldInfo['field_total']) : 0;
$scale = isset($fieldInfo['field_scale']) ? intval($fieldInfo['field_scale']) : 0;
if (in_array($type, ['char', 'string'])) {
$table = $table->$type($fieldInfo['name'], $length)
->comment($fieldInfo['comment'])
->default(strval($fieldInfo['default_value']));
} elseif (Str::contains(Str::lower($type), 'integer')) {
$table = $table->$type($fieldInfo['name'])
->comment($fieldInfo['comment'])
->default(intval($fieldInfo['default_value']));
} elseif (in_array($type, ['float', 'double', 'decimal', 'unsignedDecimal'])) {
if ($total > 0 && $scale > 0 && $total > $scale) {
$table = $table->$type($fieldInfo['name'], $total, $scale)
->comment($fieldInfo['comment'])
->default(doubleval($fieldInfo['default_value']));
} else {
$table = $table->$type($fieldInfo['name'])
->comment($fieldInfo['comment'])
->default(doubleval($fieldInfo['default_value']));
}
} elseif (in_array($type, ['date', 'dateTime'])) {
$table = $table->$type($fieldInfo['name'])
->comment($fieldInfo['comment']);
} else {
$table = $table->$type($fieldInfo['name'])
->comment($fieldInfo['comment'])
->default(strval($fieldInfo['default_value']));
}
if (!$fieldInfo['is_required']) {
$table = $table->nullable();
}
$table = $table->change();
});
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* 修改表字段
*
* @param $tableName
* @param $oldFieldName
* @param $newFieldName
* @return mixed
* @throws \Exception
*/
public static function rename_field($tableName, $oldFieldName, $newFieldName)
{
try {
if (!Schema::hasTable($tableName)) {
return \Admin::responseError("数据库表不存在");
}
if (!Schema::hasColumn($tableName, $oldFieldName)) {
return \Admin::responseError("旧数据库表字段不存在");
}
if (Schema::hasColumn($tableName, $newFieldName)) {
return \Admin::responseError("新数据库表字段已存在");
}
Schema::table($tableName, function (Blueprint $table) use ($oldFieldName, $newFieldName) {
$table->renameColumn($oldFieldName, $newFieldName);
});
} catch (\Exception $e) {
throw new \Exception("更新数据库表字段异常");
}
}
/**
* 检测表字段是否存在
*
* @param $tableName
* @param $field
* @return true
*/
public static function check_exist_table_field($tableName, $field)
{
if (Schema::hasColumn($tableName, $field)) {
return true;
}
return false;
}
}