-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathCreateDefinition.php
More file actions
108 lines (88 loc) · 2.53 KB
/
CreateDefinition.php
File metadata and controls
108 lines (88 loc) · 2.53 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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Context;
use function trim;
/**
* Parses the create definition of a column or a key.
*
* Used for parsing `CREATE TABLE` statement.
*/
final class CreateDefinition implements Component
{
/**
* The name of the new column.
*/
public string|null $name = null;
/**
* Whether this field is a constraint or not.
*/
public bool|null $isConstraint = null;
/**
* The data type of thew new column.
*/
public DataType|null $type = null;
/**
* The key.
*/
public Key|null $key = null;
/**
* The table that is referenced.
*/
public Reference|null $references = null;
/**
* The options of this field.
*/
public OptionsArray|null $options = null;
/**
* @param string|null $name the name of the field
* @param OptionsArray|null $options the options of this field
* @param DataType|Key|null $type the data type of this field or the key
* @param bool $isConstraint whether this field is a constraint or not
* @param Reference|null $references references
*/
public function __construct(
string|null $name = null,
OptionsArray|null $options = null,
DataType|Key|null $type = null,
bool $isConstraint = false,
Reference|null $references = null,
) {
$this->name = $name;
$this->options = $options;
if ($type instanceof DataType) {
$this->type = $type;
} elseif ($type instanceof Key) {
$this->key = $type;
$this->isConstraint = $isConstraint;
$this->references = $references;
}
}
public function build(): string
{
$tmp = '';
if ($this->isConstraint) {
$tmp .= 'CONSTRAINT ';
}
if (isset($this->name) && ($this->name !== '')) {
$tmp .= Context::escape($this->name) . ' ';
}
if (! empty($this->type)) {
$this->type->lowercase = true;
$tmp .= $this->type->build() . ' ';
}
if (! empty($this->key)) {
$tmp .= $this->key . ' ';
}
if (! empty($this->references)) {
$tmp .= 'REFERENCES ' . $this->references . ' ';
}
$tmp .= $this->options;
return trim($tmp);
}
public function __toString(): string
{
return $this->build();
}
}