Completed
Push — master ( 65f66e...428edc )
by Michal
04:14
created

AlterStatement   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 127
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 0 71 12
A build() 0 11 2
1
<?php
2
3
/**
4
 * `ALTER` statement.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Statements;
8
9
use PhpMyAdmin\SqlParser\Parser;
10
use PhpMyAdmin\SqlParser\Statement;
11
use PhpMyAdmin\SqlParser\Token;
12
use PhpMyAdmin\SqlParser\TokensList;
13
use PhpMyAdmin\SqlParser\Components\AlterOperation;
14
use PhpMyAdmin\SqlParser\Components\Expression;
15
use PhpMyAdmin\SqlParser\Components\OptionsArray;
16
17
/**
18
 * `ALTER` statement.
19
 *
20
 * @category   Statements
21
 *
22
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
23
 */
24
class AlterStatement extends Statement
25
{
26
    /**
27
     * Table affected.
28
     *
29
     * @var Expression
30
     */
31
    public $table;
32
33
    /**
34
     * Column affected by this statement.
35
     *
36
     * @var AlterOperation[]
37
     */
38
    public $altered = array();
39
40
    /**
41
     * Options of this statement.
42
     *
43
     * @var array
44
     */
45
    public static $OPTIONS = array(
46
        'ONLINE' => 1,
47
        'OFFLINE' => 1,
48
        'IGNORE' => 2,
49
50
        'DATABASE' => 3,
51
        'EVENT' => 3,
52
        'FUNCTION' => 3,
53
        'PROCEDURE' => 3,
54
        'SERVER' => 3,
55
        'TABLE' => 3,
56
        'TABLESPACE' => 3,
57
        'VIEW' => 3,
58
    );
59
60
    /**
61
     * @param Parser     $parser the instance that requests parsing
62
     * @param TokensList $list   the list of tokens to be parsed
63
     */
64 9
    public function parse(Parser $parser, TokensList $list)
65
    {
66 9
        ++$list->idx; // Skipping `ALTER`.
67 9
        $this->options = OptionsArray::parse(
68 9
            $parser,
69 9
            $list,
70
            static::$OPTIONS
71 9
        );
72 9
        ++$list->idx;
73
74
        // Parsing affected table.
75 9
        $this->table = Expression::parse(
76 9
            $parser,
77 9
            $list,
78
            array(
79 9
                'parseField' => 'table',
80 9
                'breakOnAlias' => true,
81
            )
82 9
        );
83 9
        ++$list->idx; // Skipping field.
84
85
        /**
86
         * The state of the parser.
87
         *
88
         * Below are the states of the parser.
89
         *
90
         *      0 -----------------[ alter operation ]-----------------> 1
91
         *
92
         *      1 -------------------------[ , ]-----------------------> 0
93
         *
94
         * @var int
95
         */
96 9
        $state = 0;
97
98 9
        for (; $list->idx < $list->count; ++$list->idx) {
99
            /**
100
             * Token parsed at this moment.
101
             *
102
             * @var Token
103
             */
104 9
            $token = $list->tokens[$list->idx];
105
106
            // End of statement.
107 9
            if ($token->type === Token::TYPE_DELIMITER) {
108 9
                break;
109
            }
110
111
            // Skipping whitespaces and comments.
112 9
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
113 6
                continue;
114
            }
115
116 9
            if ($state === 0) {
117 9
                $options = array();
118 9
                if ($this->options->has('DATABASE')) {
119 1
                    $options = AlterOperation::$DB_OPTIONS;
120 9
                } elseif ($this->options->has('TABLE')) {
121 7
                    $options = AlterOperation::$TABLE_OPTIONS;
122 8
                } elseif ($this->options->has('VIEW')) {
123 1
                    $options = AlterOperation::$VIEW_OPTIONS;
124 1
                }
125
126 9
                $this->altered[] = AlterOperation::parse($parser, $list, $options);
127 9
                $state = 1;
128 9
            } elseif ($state === 1) {
129 5
                if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
130 3
                    $state = 0;
131 3
                }
132 5
            }
133 9
        }
134 9
    }
135
136
    /**
137
     * @return string
138
     */
139 1
    public function build()
140
    {
141 1
        $tmp = array();
142 1
        foreach ($this->altered as $altered) {
143 1
            $tmp[] = $altered::build($altered);
144 1
        }
145
146 1
        return 'ALTER ' . OptionsArray::build($this->options)
147 1
            . ' ' . Expression::build($this->table)
148 1
            . ' ' . implode(', ', $tmp);
149
    }
150
}
151