phpmyadmin /
sql-parser
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace PhpMyAdmin\SqlParser\Components; |
||
| 6 | |||
| 7 | use PhpMyAdmin\SqlParser\Component; |
||
| 8 | use PhpMyAdmin\SqlParser\Context; |
||
| 9 | |||
| 10 | use function trim; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Parses the create definition of a column or a key. |
||
| 14 | * |
||
| 15 | * Used for parsing `CREATE TABLE` statement. |
||
| 16 | */ |
||
| 17 | final class CreateDefinition implements Component |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The name of the new column. |
||
| 21 | */ |
||
| 22 | public string|null $name = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Whether this field is a constraint or not. |
||
| 26 | */ |
||
| 27 | public bool|null $isConstraint = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The data type of thew new column. |
||
| 31 | */ |
||
| 32 | public DataType|null $type = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The key. |
||
| 36 | */ |
||
| 37 | public Key|null $key = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The table that is referenced. |
||
| 41 | */ |
||
| 42 | public Reference|null $references = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The options of this field. |
||
| 46 | */ |
||
| 47 | public OptionsArray|null $options = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string|null $name the name of the field |
||
| 51 | * @param OptionsArray|null $options the options of this field |
||
| 52 | * @param DataType|Key|null $type the data type of this field or the key |
||
| 53 | * @param bool $isConstraint whether this field is a constraint or not |
||
| 54 | * @param Reference|null $references references |
||
| 55 | */ |
||
| 56 | 118 | public function __construct( |
|
| 57 | string|null $name = null, |
||
| 58 | OptionsArray|null $options = null, |
||
| 59 | DataType|Key|null $type = null, |
||
| 60 | bool $isConstraint = false, |
||
| 61 | Reference|null $references = null, |
||
| 62 | ) { |
||
| 63 | 118 | $this->name = $name; |
|
| 64 | 118 | $this->options = $options; |
|
| 65 | 118 | if ($type instanceof DataType) { |
|
| 66 | 2 | $this->type = $type; |
|
| 67 | 118 | } elseif ($type instanceof Key) { |
|
| 68 | 2 | $this->key = $type; |
|
| 69 | 2 | $this->isConstraint = $isConstraint; |
|
| 70 | 2 | $this->references = $references; |
|
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | 36 | public function build(): string |
|
| 75 | { |
||
| 76 | 36 | $tmp = ''; |
|
| 77 | |||
| 78 | 36 | if ($this->isConstraint) { |
|
| 79 | 4 | $tmp .= 'CONSTRAINT '; |
|
| 80 | } |
||
| 81 | |||
| 82 | 36 | if (isset($this->name) && ($this->name !== '')) { |
|
| 83 | 34 | $tmp .= Context::escape($this->name) . ' '; |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 84 | } |
||
| 85 | |||
| 86 | 36 | if (! empty($this->type)) { |
|
| 87 | 30 | $this->type->lowercase = true; |
|
| 88 | 30 | $tmp .= $this->type->build() . ' '; |
|
| 89 | } |
||
| 90 | |||
| 91 | 36 | if (! empty($this->key)) { |
|
| 92 | 12 | $tmp .= $this->key . ' '; |
|
| 93 | } |
||
| 94 | |||
| 95 | 36 | if (! empty($this->references)) { |
|
| 96 | 4 | $tmp .= 'REFERENCES ' . $this->references . ' '; |
|
| 97 | } |
||
| 98 | |||
| 99 | 36 | $tmp .= $this->options; |
|
| 100 | |||
| 101 | 36 | return trim($tmp); |
|
| 102 | } |
||
| 103 | |||
| 104 | 28 | public function __toString(): string |
|
| 105 | { |
||
| 106 | 28 | return $this->build(); |
|
| 107 | } |
||
| 108 | } |
||
| 109 |