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

import com.google.common.base.Optional;
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.ColumnDefinition;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.ddl.create.table.CreateTableStatement;
import io.shardingsphere.core.util.SQLUtil;
import org.antlr.v4.runtime.ParserRuleContext;

/**
 * Column definition extract handler.
 * 
 * @author duhongjun
 */
public final class ColumnDefinitionExtractHandler implements ASTExtractHandler {
    
    private final ColumnDefinitionPhraseExtractor columnDefinitionPhraseExtractor = new ColumnDefinitionPhraseExtractor();
    
    @Override
    public void extract(final ParserRuleContext ancestorNode, final SQLStatement statement) {
        CreateTableStatement createTableStatement = (CreateTableStatement) statement;
        for (ParserRuleContext each : ASTUtils.getAllDescendantNodes(ancestorNode, RuleName.COLUMN_DEFINITION)) {
            Optional<ColumnDefinition> columnDefinition = columnDefinitionPhraseExtractor.extract(each);
            if (columnDefinition.isPresent()) {
                setColumnDefinition(createTableStatement, columnDefinition.get());
            }
        }
    }
    
    private void setColumnDefinition(final CreateTableStatement createTableStatement, final ColumnDefinition columnDefinition) {
        createTableStatement.getColumnNames().add(SQLUtil.getExactlyValue(columnDefinition.getName()));
        createTableStatement.getColumnTypes().add(columnDefinition.getType());
        if (columnDefinition.isPrimaryKey()) {
            createTableStatement.getPrimaryKeyColumns().add(columnDefinition.getName());
        }
    }
}

@@ -727,16 +727,221 @@
 ptional;
+%0A%0Aimport io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ColumnDefinitionExtractResult;%0Aimport io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ExtractResult;
 %0Aimport 
@@ -1578,16 +1578,36 @@
 tHandler
+, ASTExtractHandler1
  %7B%0A    %0A
@@ -2757,24 +2757,24 @@
 getName());%0A
-
         %7D%0A  
@@ -2776,11 +2776,592 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public ExtractResult extract(ParserRuleContext ancestorNode) %7B%0A        ColumnDefinitionExtractResult extractResult = new ColumnDefinitionExtractResult();%0A        for (ParserRuleContext each : ASTUtils.getAllDescendantNodes(ancestorNode, RuleName.COLUMN_DEFINITION)) %7B%0A            Optional%3CColumnDefinition%3E columnDefinition = columnDefinitionPhraseExtractor.extract(each);%0A            if (columnDefinition.isPresent()) %7B%0A                extractResult.getColumnDefintions().add(columnDefinition.get());%0A            %7D%0A        %7D%0A        return extractResult;%0A    %7D
 %0A%7D%0A

implement ASTExtractHandler1