/*
 * 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.oracle;

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.sql.ddl.AlterTableStatement;
import io.shardingsphere.core.parsing.antlr.sql.ddl.ColumnDefinition;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import org.antlr.v4.runtime.ParserRuleContext;

/**
 * Modify column extract handler for Oracle.
 * 
 * @author duhongjun
 */
public final class OracleModifyColumnExtractHandler implements ASTExtractHandler {
    
    private final ColumnDefinitionPhraseExtractor columnDefinitionPhraseExtractor = new ColumnDefinitionPhraseExtractor();
    
    @Override
    public void extract(final ParserRuleContext ancestorNode, final SQLStatement statement) {
        AlterTableStatement alterTableStatement = (AlterTableStatement) statement;
        for (ParserRuleContext modifyColumnNode : ASTUtils.getAllDescendantNodes(ancestorNode, RuleName.MODIFY_COLUMN)) {
            for (ParserRuleContext each : ASTUtils.getAllDescendantNodes(modifyColumnNode, RuleName.MODIFY_COL_PROPERTIES)) {
                // it`s not column definition, but can call this method
                Optional<ColumnDefinition> columnDefinition = columnDefinitionPhraseExtractor.extract(each);
                if (columnDefinition.isPresent()) {
                    alterTableStatement.getUpdateColumns().put(columnDefinition.get().getName(), columnDefinition.get());
                }
            }
        }
    }
}

@@ -703,16 +703,64 @@
 racle;%0A%0A
+import org.antlr.v4.runtime.ParserRuleContext;%0A%0A
 import c
@@ -791,16 +791,17 @@
 tional;%0A
+%0A
 import i
@@ -955,24 +955,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
@@ -1653,55 +1653,8 @@
 ent;
-%0Aimport org.antlr.v4.runtime.ParserRuleContext;
 %0A%0A/*
@@ -1809,16 +1809,35 @@
 tHandler
+,ASTExtractHandler1
  %7B%0A    %0A
@@ -2805,11 +2805,828 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public ExtractResult extract(ParserRuleContext ancestorNode) %7B%0A        ColumnDefinitionExtractResult extractResult = new ColumnDefinitionExtractResult();%0A        for (ParserRuleContext modifyColumnNode : ASTUtils.getAllDescendantNodes(ancestorNode, RuleName.MODIFY_COLUMN)) %7B%0A            for (ParserRuleContext each : ASTUtils.getAllDescendantNodes(modifyColumnNode, RuleName.MODIFY_COL_PROPERTIES)) %7B%0A                // it%60s not column definition, but can call this method%0A                Optional%3CColumnDefinition%3E columnDefinition = columnDefinitionPhraseExtractor.extract(each);%0A                if (columnDefinition.isPresent()) %7B%0A                    extractResult.getColumnDefintions().add(columnDefinition.get());%0A                %7D%0A            %7D%0A        %7D%0A        return extractResult;%0A    %7D
 %0A%7D%0A

implement ASTExtractHandler1