1<?php
2
3/**
4 * `RENAME` statement.
5 */
6
7namespace PhpMyAdmin\SqlParser\Statements;
8
9use PhpMyAdmin\SqlParser\Components\RenameOperation;
10use PhpMyAdmin\SqlParser\Parser;
11use PhpMyAdmin\SqlParser\Statement;
12use PhpMyAdmin\SqlParser\Token;
13use PhpMyAdmin\SqlParser\TokensList;
14
15/**
16 * `RENAME` statement.
17 *
18 * RENAME TABLE tbl_name TO new_tbl_name
19 *  [, tbl_name2 TO new_tbl_name2] ...
20 *
21 * @category   Statements
22 *
23 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
24 */
25class RenameStatement extends Statement
26{
27    /**
28     * The old and new names of the tables.
29     *
30     * @var RenameOperation[]
31     */
32    public $renames;
33
34    /**
35     * Function called before the token is processed.
36     *
37     * Skips the `TABLE` keyword after `RENAME`.
38     *
39     * @param Parser     $parser the instance that requests parsing
40     * @param TokensList $list   the list of tokens to be parsed
41     * @param Token      $token  the token that is being parsed
42     */
43    public function before(Parser $parser, TokensList $list, Token $token)
44    {
45        if (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'RENAME')) {
46            // Checking if it is the beginning of the query.
47            $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'TABLE');
48        }
49    }
50
51    /**
52     * @return string
53     */
54    public function build()
55    {
56        return 'RENAME TABLE ' . RenameOperation::build($this->renames);
57    }
58}
59