/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.core.parsing.antlr.extractor.statement.handler.dialect.mysql;

import com.google.common.base.Optional;
import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.ASTExtractHandler;
import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.RuleName;
import io.shardingsphere.core.parsing.antlr.extractor.statement.phrase.ColumnDefinitionPhraseExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.util.ASTUtils;
import io.shardingsphere.core.parsing.antlr.extractor.statement.util.ExtractorUtils;
import io.shardingsphere.core.parsing.antlr.sql.ddl.ColumnDefinition;
import io.shardingsphere.core.parsing.antlr.sql.ddl.ColumnPosition;
import io.shardingsphere.core.parsing.antlr.sql.ddl.mysql.MySQLAlterTableStatement;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import org.antlr.v4.runtime.ParserRuleContext;

/**
 * Change column extract handler for MySQL.
 * 
 * @author duhongjun
 */
public final class MySQLChangeColumnExtractHandler implements ASTExtractHandler {
    
    private final ColumnDefinitionPhraseExtractor columnDefinitionPhraseExtractor = new ColumnDefinitionPhraseExtractor();
    
    @Override
    public void extract(final ParserRuleContext ancestorNode, final SQLStatement statement) {
        Optional<ParserRuleContext> changeColumnNode = ASTUtils.findFirstChildNode(ancestorNode, RuleName.CHANGE_COLUMN);
        if (!changeColumnNode.isPresent()) {
            return;
        }
        Optional<ParserRuleContext> oldColumnNode = ASTUtils.findFirstChildNode(changeColumnNode.get(), RuleName.COLUMN_NAME);
        if (!oldColumnNode.isPresent()) {
            return;
        }
        Optional<ParserRuleContext> columnDefinitionNode = ASTUtils.findFirstChildNode(changeColumnNode.get(), RuleName.COLUMN_DEFINITION);
        if (!columnDefinitionNode.isPresent()) {
            return;
        }
        MySQLAlterTableStatement alterStatement = (MySQLAlterTableStatement) statement;
        Optional<ColumnDefinition> columnDefinition = columnDefinitionPhraseExtractor.extract(columnDefinitionNode.get());
        if (!columnDefinition.isPresent()) {
            return;
        }
        alterStatement.getUpdateColumns().put(oldColumnNode.get().getText(), columnDefinition.get());
        Optional<ColumnPosition> columnPosition = ExtractorUtils.extractFirstOrAfterColumn(changeColumnNode.get(), columnDefinition.get().getName());
        if (columnPosition.isPresent()) {
            alterStatement.getPositionChangedColumns().add(columnPosition.get());
        }
    }
}

@@ -702,16 +702,64 @@
 mysql;%0A%0A
+import org.antlr.v4.runtime.ParserRuleContext;%0A%0A
 import c
@@ -790,16 +790,17 @@
 tional;%0A
+%0A
 import i
@@ -954,24 +954,320 @@
 handler.
-RuleName
+ASTExtractHandler1;%0Aimport io.shardingsphere.core.parsing.antlr.extractor.statement.handler.RuleName;%0Aimport io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ColumnDefinitionExtractResult;%0Aimport io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ExtractResult
 ;%0Aimport
@@ -1816,55 +1816,8 @@
 ent;
-%0Aimport org.antlr.v4.runtime.ParserRuleContext;
 %0A%0A/*
@@ -1970,16 +1970,35 @@
 tHandler
+,ASTExtractHandler1
  %7B%0A    %0A
@@ -3525,11 +3525,1434 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public ExtractResult extract(ParserRuleContext ancestorNode) %7B%0A        ColumnDefinitionExtractResult extractResult = new ColumnDefinitionExtractResult();%0A        Optional%3CParserRuleContext%3E changeColumnNode = ASTUtils.findFirstChildNode(ancestorNode, RuleName.CHANGE_COLUMN);%0A        if (!changeColumnNode.isPresent()) %7B%0A            return extractResult;%0A        %7D%0A        Optional%3CParserRuleContext%3E oldColumnNode = ASTUtils.findFirstChildNode(changeColumnNode.get(), RuleName.COLUMN_NAME);%0A        if (!oldColumnNode.isPresent()) %7B%0A            return extractResult;%0A        %7D%0A        Optional%3CParserRuleContext%3E columnDefinitionNode = ASTUtils.findFirstChildNode(changeColumnNode.get(), RuleName.COLUMN_DEFINITION);%0A        if (!columnDefinitionNode.isPresent()) %7B%0A            return extractResult;%0A        %7D%0A        Optional%3CColumnDefinition%3E columnDefinition = columnDefinitionPhraseExtractor.extract(columnDefinitionNode.get());%0A        if (!columnDefinition.isPresent()) %7B%0A            return extractResult;%0A        %7D%0A        extractResult.getColumnDefintions().add(columnDefinition.get());%0A        Optional%3CColumnPosition%3E columnPosition = ExtractorUtils.extractFirstOrAfterColumn(changeColumnNode.get(), columnDefinition.get().getName());%0A        if (columnPosition.isPresent()) %7B%0A            columnDefinition.get().setPosition(columnPosition.get());%0A        %7D%0A        return extractResult;%0A    %7D
 %0A%7D%0A

implement ASTExtractHandler1