1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Component; |
8
|
|
|
use PhpMyAdmin\SqlParser\Parsers\PartitionDefinitions; |
9
|
|
|
use PhpMyAdmin\SqlParser\Token; |
10
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
11
|
|
|
|
12
|
|
|
use function trim; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Parses an alter operation. |
16
|
|
|
*/ |
17
|
|
|
final class AlterOperation implements Component |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Options of this operation. |
21
|
|
|
* |
22
|
|
|
* @var OptionsArray |
23
|
|
|
*/ |
24
|
|
|
public $options; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The altered field. |
28
|
|
|
* |
29
|
|
|
* @var Expression|string|null |
30
|
|
|
*/ |
31
|
|
|
public $field; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The partitions. |
35
|
|
|
* |
36
|
|
|
* @var PartitionDefinition[]|null |
37
|
|
|
*/ |
38
|
|
|
public $partitions; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param OptionsArray $options options of alter operation |
42
|
|
|
* @param Expression|string|null $field altered field |
43
|
|
|
* @param PartitionDefinition[]|null $partitions partitions definition found in the operation |
44
|
|
|
* @param Token[] $unknown unparsed tokens found at the end of operation |
45
|
|
|
*/ |
46
|
186 |
|
public function __construct( |
47
|
|
|
OptionsArray|null $options = null, |
48
|
|
|
Expression|string|null $field = null, |
49
|
|
|
array|null $partitions = null, |
50
|
|
|
public array $unknown = [], |
51
|
|
|
) { |
52
|
186 |
|
$this->partitions = $partitions; |
53
|
186 |
|
$this->options = $options; |
54
|
186 |
|
$this->field = $field; |
55
|
186 |
|
$this->unknown = $unknown; |
56
|
|
|
} |
57
|
|
|
|
58
|
24 |
|
public function build(): string |
59
|
|
|
{ |
60
|
|
|
// Specific case of RENAME COLUMN that insert the field between 2 options. |
61
|
24 |
|
$afterFieldsOptions = new OptionsArray(); |
62
|
24 |
|
if ($this->options->has('RENAME') && $this->options->has('COLUMN')) { |
63
|
8 |
|
$afterFieldsOptions = clone $this->options; |
64
|
8 |
|
$afterFieldsOptions->remove('RENAME'); |
65
|
8 |
|
$afterFieldsOptions->remove('COLUMN'); |
66
|
8 |
|
$this->options->remove('TO'); |
67
|
|
|
} |
68
|
|
|
|
69
|
24 |
|
$ret = $this->options . ' '; |
70
|
24 |
|
if (isset($this->field) && ($this->field !== '')) { |
71
|
18 |
|
$ret .= $this->field . ' '; |
72
|
|
|
} |
73
|
|
|
|
74
|
24 |
|
$ret .= $afterFieldsOptions . TokensList::buildFromArray($this->unknown); |
75
|
|
|
|
76
|
24 |
|
if (isset($this->partitions)) { |
77
|
2 |
|
$ret .= PartitionDefinitions::buildAll($this->partitions); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
24 |
|
return trim($ret); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function __toString(): string |
84
|
|
|
{ |
85
|
|
|
return $this->build(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|