|
1
|
|
|
<?php |
|
2
|
|
|
namespace vakata\database\schema; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* A column definition |
|
6
|
|
|
*/ |
|
7
|
|
|
class TableColumn |
|
8
|
|
|
{ |
|
9
|
|
|
protected $name; |
|
10
|
|
|
protected $type; |
|
11
|
|
|
protected $btype = 'text'; |
|
12
|
|
|
protected $values = []; |
|
13
|
|
|
protected $default = null; |
|
14
|
|
|
protected $comment = null; |
|
15
|
|
|
protected $nullable = false; |
|
16
|
|
|
|
|
17
|
1 |
|
public function __construct(string $name) |
|
18
|
|
|
{ |
|
19
|
1 |
|
$this->setName($name); |
|
20
|
1 |
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
public static function fromArray(string $name, array $data = []) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$instance = new static($name); |
|
25
|
1 |
|
if (isset($data['Type'])) { |
|
26
|
1 |
|
$instance->setType($data['Type']); |
|
27
|
|
|
} |
|
28
|
1 |
|
if (isset($data['type'])) { |
|
29
|
|
|
$instance->setType($data['type']); |
|
30
|
|
|
} |
|
31
|
1 |
|
if (isset($data['Comment'])) { |
|
32
|
1 |
|
$instance->setComment($data['Comment']); |
|
33
|
|
|
} |
|
34
|
1 |
|
if (isset($data['comment'])) { |
|
35
|
|
|
$instance->setComment($data['comment']); |
|
36
|
|
|
} |
|
37
|
1 |
|
if (isset($data['Null']) && $data['Null'] === 'YES') { |
|
38
|
|
|
$instance->setNullable(true); |
|
39
|
|
|
} |
|
40
|
1 |
|
if (isset($data['nullable']) && is_bool($data['nullable'])) { |
|
41
|
|
|
$instance->setNullable($data['nullable']); |
|
42
|
|
|
} |
|
43
|
1 |
|
if (isset($data['Default'])) { |
|
44
|
|
|
$instance->setDefault($data['Default']); |
|
45
|
|
|
} |
|
46
|
1 |
|
if (isset($data['default'])) { |
|
47
|
|
|
$instance->setDefault($data['default']); |
|
48
|
|
|
} |
|
49
|
1 |
|
if ($instance->getBasicType() === 'enum' && strpos($instance->getType(), 'enum(') === 0) { |
|
50
|
|
|
$temp = array_map(function ($v) { |
|
51
|
|
|
return str_replace("''", "'", $v); |
|
52
|
|
|
}, explode("','", substr($instance->getType(), 6, -2))); |
|
53
|
|
|
$instance->setValues($temp); |
|
54
|
|
|
} |
|
55
|
1 |
|
if (isset($data['values']) && is_array($data['values'])) { |
|
56
|
|
|
$instance->setValues($data['values']); |
|
57
|
|
|
} |
|
58
|
1 |
|
if (isset($data['DATA_TYPE'])) { |
|
59
|
|
|
$instance->setType($data['DATA_TYPE']); |
|
60
|
|
|
} |
|
61
|
1 |
|
if (isset($data['NULLABLE']) && $data['NULLABLE'] !== 'N') { |
|
62
|
|
|
$instance->setNullable(true); |
|
63
|
|
|
} |
|
64
|
1 |
|
if (isset($data['DATA_DEFAULT'])) { |
|
65
|
|
|
$instance->setDefault($data['DATA_DEFAULT']); |
|
66
|
|
|
} |
|
67
|
1 |
|
return $instance; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getName() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->name; |
|
73
|
|
|
} |
|
74
|
|
|
public function getType() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->type; |
|
77
|
|
|
} |
|
78
|
|
|
public function getValues() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->values; |
|
81
|
|
|
} |
|
82
|
|
|
public function getDefault() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->default; |
|
85
|
|
|
} |
|
86
|
|
|
public function isNullable() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->nullable; |
|
89
|
|
|
} |
|
90
|
|
|
public function getComment() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->comment; |
|
93
|
|
|
} |
|
94
|
5 |
|
public function getBasicType() |
|
95
|
|
|
{ |
|
96
|
5 |
|
return $this->btype; |
|
97
|
|
|
} |
|
98
|
1 |
|
public function setName(string $name) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->name = $name; |
|
101
|
1 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
1 |
|
public function setType(string $type) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$this->type = $type; |
|
106
|
1 |
|
$type = strtolower($type); |
|
107
|
1 |
|
if (strpos($type, 'text') !== false || strpos($type, 'char') !== false) { |
|
108
|
1 |
|
$this->btype = 'text'; |
|
109
|
1 |
|
} elseif (strpos($type, 'int') !== false || strpos($type, 'bit') !== false) { |
|
110
|
1 |
|
$this->btype = 'int'; |
|
111
|
|
|
} elseif (strpos($type, 'float') !== false || strpos($type, 'double') !== false || strpos($type, 'decimal') !== false) { |
|
112
|
|
|
$this->btype = 'float'; |
|
113
|
|
|
} elseif (strpos($type, 'enum') !== false || strpos($type, 'set') !== false) { |
|
114
|
|
|
$this->btype = 'enum'; |
|
115
|
|
|
} elseif (strpos($type, 'datetime') !== false || strpos($type, 'timestamp') !== false) { |
|
116
|
|
|
$this->btype = 'datetime'; |
|
117
|
|
|
} elseif (strpos($type, 'date') !== false) { |
|
118
|
|
|
$this->btype = 'date'; |
|
119
|
|
|
} elseif (strpos($type, 'lob') !== false || strpos($type, 'binary') !== false) { |
|
120
|
|
|
$this->btype = 'blob'; |
|
121
|
|
|
} |
|
122
|
1 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
public function setValues(array $values) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->values = $values; |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
public function setDefault($default = null) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->default = $default; |
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
public function setNullable(bool $nullable) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->nullable = $nullable; |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
1 |
|
public function setComment(string $comment) |
|
140
|
|
|
{ |
|
141
|
1 |
|
$this->comment = $comment; |
|
142
|
1 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
} |