Passed
Push — master ( b16987...8b6d77 )
by Maurício
03:49 queued 13s
created

PartitionDefinition::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
10
use function trim;
11
12
/**
13
 * Parses the create definition of a partition.
14
 *
15
 * Used for parsing `CREATE TABLE` statement.
16
 */
17
final class PartitionDefinition implements Component
18
{
19
    /**
20
     * All field options.
21
     *
22
     * @var array<string, int|array<int, int|string>>
23
     * @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
24
     */
25
    public static $partitionOptions = [
26
        'STORAGE ENGINE' => [
27
            1,
28
            'var',
29
        ],
30
        'ENGINE' => [
31
            1,
32
            'var',
33
        ],
34
        'COMMENT' => [
35
            2,
36
            'var',
37
        ],
38
        'DATA DIRECTORY' => [
39
            3,
40
            'var',
41
        ],
42
        'INDEX DIRECTORY' => [
43
            4,
44
            'var',
45
        ],
46
        'MAX_ROWS' => [
47
            5,
48
            'var',
49
        ],
50
        'MIN_ROWS' => [
51
            6,
52
            'var',
53
        ],
54
        'TABLESPACE' => [
55
            7,
56
            'var',
57
        ],
58
        'NODEGROUP' => [
59
            8,
60
            'var',
61
        ],
62
    ];
63
64
    /**
65
     * Whether this entry is a subpartition or a partition.
66
     *
67
     * @var bool
68
     */
69
    public $isSubpartition;
70
71
    /**
72
     * The name of this partition.
73
     *
74
     * @var string
75
     */
76
    public $name;
77
78
    /**
79
     * The type of this partition (what follows the `VALUES` keyword).
80
     *
81
     * @var string
82
     */
83
    public $type;
84
85
    /**
86
     * The expression used to defined this partition.
87
     *
88
     * @var Expression|string
89
     */
90
    public $expr;
91
92
    /**
93
     * The subpartitions of this partition.
94
     *
95
     * @var PartitionDefinition[]
96
     */
97
    public $subpartitions;
98
99
    /**
100
     * The options of this field.
101
     *
102
     * @var OptionsArray
103
     */
104
    public $options;
105
106 8
    public function build(): string
107
    {
108 8
        if ($this->isSubpartition) {
109 4
            return trim('SUBPARTITION ' . $this->name . ' ' . $this->options);
110
        }
111
112 8
        $subpartitions = empty($this->subpartitions) ? '' : ' ' . PartitionDefinitions::buildAll($this->subpartitions);
113
114 8
        return trim(
115 8
            'PARTITION ' . $this->name
116 8
            . (empty($this->type) ? '' : ' VALUES ' . $this->type . ' ' . $this->expr . ' ')
117 8
            . (! empty($this->options) && ! empty($this->type) ? '' : ' ')
118 8
            . $this->options . $subpartitions,
119 8
        );
120
    }
121
122 8
    public function __toString(): string
123
    {
124 8
        return $this->build();
125
    }
126
}
127