SQLLib.php
5.5 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
<?php
namespace addons\workorder\library;
class SQLLib
{
protected static $instance = null;
protected $config = [];
protected $data = [
'table' => '',
'oldname' => '',
'name' => '',
'type' => 'VARCHAR',
'length' => '255',
'values' => '',
'comment' => '',
'after' => '',
];
public function __construct($options = [])
{
$this->options = array_merge($this->config, $options);
}
public static function instance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
public function setTable($table)
{
$this->data['table'] = db()->name($table)->getTable();
return $this;
}
public function setType($type)
{
switch ($type) {
case 'checkbox':
case 'selects':
$this->data['type'] = 'SET';
break;
case 'radio':
case 'select':
$this->data['type'] = 'ENUM';
break;
case 'number':
$this->data['type'] = 'INT';
break;
case 'date':
case 'datetime':
case 'time':
$this->data['type'] = strtoupper($type);
break;
case 'editor':
$this->data['type'] = 'TEXT';
break;
case 'switch':
$this->data['type'] = 'switch';
break;
default:
$this->data['type'] = 'VARCHAR';
break;
}
return $this;
}
public function setOldname($oldname)
{
$this->data['oldname'] = $oldname;
return $this;
}
public function setName($name)
{
$this->data['name'] = $name;
return $this;
}
public function setFieldLength($length)
{
$this->data['length'] = $length;
return $this;
}
public function setValues($values)
{
$this->data['values'] = $values;
return $this;
}
public function setComment($comment)
{
$this->data['comment'] = $comment;
return $this;
}
public function setDefaultvalue($defaultvalue)
{
$this->data['defaultvalue'] = $defaultvalue;
return $this;
}
public function setDecimals($decimals)
{
$this->data['decimals'] = $decimals;
return $this;
}
protected function prebuilt()
{
if ($this->data['type'] == 'INT') {
if ($this->data['decimals'] > 0) {
$this->data['type'] = 'DECIMAL';
$this->data['length'] = "({$this->data['length']},{$this->data['decimals']})";
} else {
$this->data['length'] = "({$this->data['length']})";
}
$this->data['defaultvalue'] = $this->data['defaultvalue'] == '' ? 'NULL' : $this->data['defaultvalue'];
} elseif ($this->data['type'] == 'switch') {
$this->data['type'] = 'TINYINT';
$this->data['length'] = "(1)";
$this->data['comment'] .= $this->data['comment'] . ':0=关,1=开';
} elseif (in_array($this->data['type'], ['SET', 'ENUM'])) {
$fieldValues = \app\common\model\Config::decode($this->data['values']);
$this->data['comment'] .= ':';
foreach ($fieldValues as $key => $value) {
$this->data['comment'] .= $key . '=' . $value . ',';
}
$this->data['comment'] = trim($this->data['comment'], ',');
$this->data['length'] = "('" . implode("','", array_keys($fieldValues)) . "')";
$this->data['defaultvalue'] = in_array($this->data['defaultvalue'], array_keys($fieldValues)) ? $this->data['defaultvalue'] : ($this->data['type'] == 'ENUM' ? key($fieldValues) : '');
} elseif (in_array($this->data['type'], ['DATE', 'TIME', 'DATETIME'])) {
$this->data['length'] = '';
$this->data['defaultvalue'] = "NULL";
} elseif (in_array($this->data['type'], ['TEXT'])) {
$this->data['length'] = "(0)";
$this->data['defaultvalue'] = 'NULL';
} else {
$this->data['length'] = "({$this->data['length']})";
}
$this->data['defaultvalue'] = strtoupper($this->data['defaultvalue']) === 'NULL' ? "NULL" : "'{$this->data['defaultvalue']}'";
}
public function getAddSql()
{
$this->prebuilt();
$sql = "ALTER TABLE `{$this->data['table']}` " . "ADD `{$this->data['name']}` {$this->data['type']} {$this->data['length']} " . "DEFAULT {$this->data['defaultvalue']} " . "COMMENT '{$this->data['comment']}' " . ($this->data['after'] ? "AFTER `{$this->data['after']}`" : '');
return $sql;
}
public function getModifySql()
{
$this->prebuilt();
$sql = "ALTER TABLE `{$this->data['table']}` " . ($this->data['oldname'] ? 'CHANGE' : 'MODIFY') . " COLUMN " . ($this->data['oldname'] ? "`{$this->data['oldname']}`" : '') . " `{$this->data['name']}` {$this->data['type']} {$this->data['length']} " . "DEFAULT {$this->data['defaultvalue']} " . "COMMENT '{$this->data['comment']}' " . ($this->data['after'] ? "AFTER `{$this->data['after']}`" : '');
return $sql;
}
public function getDropSql()
{
$sql = "ALTER TABLE `{$this->data['table']}` " . "DROP `{$this->data['name']}`";
return $sql;
}
}