1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\database\struct; |
4
|
|
|
|
5
|
|
|
use function array_key_exists; |
6
|
|
|
use function array_search; |
7
|
|
|
use ArrayIterator; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use Traversable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TableStruct |
13
|
|
|
* @package suda\database\struct |
14
|
|
|
*/ |
15
|
|
|
class TableStruct implements IteratorAggregate |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* 数据表名 |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* 字段集合 |
26
|
|
|
* |
27
|
|
|
* @var Field[] |
28
|
|
|
*/ |
29
|
|
|
protected $fields; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 键值对映射 |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $alias; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 创建字段集合 |
40
|
|
|
* |
41
|
|
|
* @param string $name |
42
|
|
|
*/ |
43
|
|
|
public function __construct(string $name) |
44
|
|
|
{ |
45
|
|
|
$this->name = $name; |
46
|
|
|
$this->fields = []; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 新建表列 |
51
|
|
|
* |
52
|
|
|
* @param string $name |
53
|
|
|
* @param string $type |
54
|
|
|
* @param int|array $length |
55
|
|
|
* @return Field |
56
|
|
|
*/ |
57
|
|
|
public function field(string $name, string $type, $length = null) |
58
|
|
|
{ |
59
|
|
|
if ($length === null) { |
60
|
|
|
$this->fields[$name] = new Field($this->name, $name, $type); |
61
|
|
|
} else { |
62
|
|
|
$this->fields[$name] = new Field($this->name, $name, $type, $length); |
63
|
|
|
} |
64
|
|
|
return $this->fields[$name]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @param string $type |
70
|
|
|
* @param mixed $length |
71
|
|
|
* @return Field |
72
|
|
|
*/ |
73
|
|
|
public function newField(string $name, string $type, $length = null) |
74
|
|
|
{ |
75
|
|
|
return $this->field($name, $type, $length); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @return Field|null |
81
|
|
|
*/ |
82
|
|
|
public function getField(string $name) |
83
|
|
|
{ |
84
|
|
|
return $this->fields[$name] ?? null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* 添加表结构字段 |
89
|
|
|
* |
90
|
|
|
* @param array|Field $fields |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function fields($fields) |
94
|
|
|
{ |
95
|
|
|
if (!is_array($fields) && $fields instanceof Field) { |
96
|
|
|
$fields = func_get_args(); |
97
|
|
|
} |
98
|
|
|
foreach ($fields as $field) { |
99
|
|
|
$this->addField($field); |
100
|
|
|
} |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $name |
106
|
|
|
* @param string|null $type |
107
|
|
|
* @param int|string|array|null $length |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function hasField(string $name, string $type = null, $length = null) |
111
|
|
|
{ |
112
|
|
|
// 检查字段 |
113
|
|
|
if (array_key_exists($name, $this->fields) === false) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
// 检查类型 |
117
|
|
|
if ($type !== null |
118
|
|
|
&& $this->fields[$name]->equals(new Field($this->getName(), $name, $type, $length)) === false) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Field $field |
126
|
|
|
*/ |
127
|
|
|
public function addField(Field $field) |
128
|
|
|
{ |
129
|
|
|
if ($field->getTableName() != $this->name) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
$name = $field->getName(); |
133
|
|
|
$this->fields[$name] = $field; |
134
|
|
|
$this->alias[$name] = $field->getAlias(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $name |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function outputName(string $name): string |
142
|
|
|
{ |
143
|
|
|
if (array_key_exists($name, $this->alias)) { |
144
|
|
|
return $this->alias[$name]; |
145
|
|
|
} |
146
|
|
|
return $name; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $name |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function inputName(string $name): string |
154
|
|
|
{ |
155
|
|
|
if ($key = array_search($name, $this->alias)) { |
156
|
|
|
return $key; |
157
|
|
|
} |
158
|
|
|
return $name; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getFieldsName() |
165
|
|
|
{ |
166
|
|
|
return array_keys($this->fields); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the value of name |
171
|
|
|
*/ |
172
|
|
|
public function getName() |
173
|
|
|
{ |
174
|
|
|
return $this->name; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $name |
179
|
|
|
*/ |
180
|
|
|
public function setName(string $name): void |
181
|
|
|
{ |
182
|
|
|
$this->name = $name; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the value of fields |
187
|
|
|
*/ |
188
|
|
|
public function all() |
189
|
|
|
{ |
190
|
|
|
return $this->fields; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return ArrayIterator|Traversable |
195
|
|
|
*/ |
196
|
|
|
public function getIterator() |
197
|
|
|
{ |
198
|
|
|
return new ArrayIterator($this->fields); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param TableStruct $some |
203
|
|
|
* @param TableStruct $struct |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public static function isSubOf(TableStruct $some, TableStruct $struct) |
207
|
|
|
{ |
208
|
|
|
foreach ($struct->fields as $field) { |
209
|
|
|
$name = $field->getName(); |
210
|
|
|
if (array_key_exists($name, $some->fields) === false |
211
|
|
|
|| $some->fields[$name]->equals($field) === false) { |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|