-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathAlterOperation.php
More file actions
83 lines (69 loc) · 2.26 KB
/
AlterOperation.php
File metadata and controls
83 lines (69 loc) · 2.26 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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parsers\PartitionDefinitions;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function trim;
/**
* Parses an alter operation.
*/
final class AlterOperation implements Component
{
/**
* Options of this operation.
*/
public OptionsArray|null $options = null;
/**
* The altered field.
*/
public Expression|string|null $field = null;
/**
* The partitions.
*
* @var PartitionDefinition[]|null
*/
public array|null $partitions = null;
/**
* @param OptionsArray|null $options options of alter operation
* @param Expression|string|null $field altered field
* @param PartitionDefinition[]|null $partitions partitions definition found in the operation
* @param Token[] $unknown unparsed tokens found at the end of operation
*/
public function __construct(
OptionsArray|null $options = null,
Expression|string|null $field = null,
array|null $partitions = null,
public array $unknown = [],
) {
$this->partitions = $partitions;
$this->options = $options;
$this->field = $field;
$this->unknown = $unknown;
}
public function build(): string
{
// Specific case of RENAME COLUMN that insert the field between 2 options.
$afterFieldsOptions = new OptionsArray();
if ($this->options->has('RENAME') && $this->options->has('COLUMN')) {
$afterFieldsOptions = clone $this->options;
$afterFieldsOptions->remove('RENAME');
$afterFieldsOptions->remove('COLUMN');
$this->options->remove('TO');
}
$ret = $this->options . ' ';
if (isset($this->field) && ($this->field !== '')) {
$ret .= $this->field . ' ';
}
$ret .= $afterFieldsOptions . TokensList::buildFromArray($this->unknown);
if (isset($this->partitions)) {
$ret .= PartitionDefinitions::buildAll($this->partitions);
}
return trim($ret);
}
public function __toString(): string
{
return $this->build();
}
}