/*
 * 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.merger.dal.show;

import com.google.common.base.Optional;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.merger.QueryResult;
import io.shardingsphere.core.merger.dql.common.MemoryMergedResult;
import io.shardingsphere.core.merger.dql.common.MemoryQueryResultRow;
import io.shardingsphere.core.parsing.SQLParsingEngine;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.core.rule.TableRule;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Merged result for show create table.
 *
 * @author zhangliang
 */
public final class ShowCreateTableMergedResult extends MemoryMergedResult {
    
    private static final Map<String, Integer> LABEL_AND_INDEX_MAP = new HashMap<>(2, 1);
    
    private final ShardingRule shardingRule;
    
    private final Iterator<MemoryQueryResultRow> memoryResultSetRows;
    
    static {
        LABEL_AND_INDEX_MAP.put("Table", 1);
        LABEL_AND_INDEX_MAP.put("Create Table", 2);
    }
    
    public ShowCreateTableMergedResult(final ShardingRule shardingRule, final List<QueryResult> queryResults) throws SQLException {
        super(LABEL_AND_INDEX_MAP);
        this.shardingRule = shardingRule;
        memoryResultSetRows = init(queryResults);
    }
    
    private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults) throws SQLException {
        List<MemoryQueryResultRow> result = new LinkedList<>();
        for (QueryResult each : queryResults) {
            while (each.next()) {
                MemoryQueryResultRow memoryResultSetRow = new MemoryQueryResultRow(each);
                String tableName = memoryResultSetRow.getCell(1).toString();
                Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(tableName);
                // TODO :panjuan add default datasource
                if (tableRule.isPresent()) {
                    String logicTableName = tableRule.get().getLogicTable();
                    memoryResultSetRow.setCell(1, logicTableName);
                    String createTableDDL = memoryResultSetRow.getCell(2).toString();
                    SQLParsingEngine sqlParsingEngine = new SQLParsingEngine(DatabaseType.MySQL, createTableDDL, shardingRule, null);
                    String actualTableName = sqlParsingEngine.parse(true).getTables().getSingleTableName();
                    if (actualTableName.startsWith("`")) {
                        logicTableName = "`" + logicTableName + "`";
                    }
                    memoryResultSetRow.setCell(2, createTableDDL.replaceFirst(actualTableName, logicTableName));
                    result.add(memoryResultSetRow);
                }
            }
        }
        if (!result.isEmpty()) {
            setCurrentResultSetRow(result.get(0));
        }
        return result.iterator();
    }
    
    @Override
    public boolean next() {
        if (memoryResultSetRows.hasNext()) {
            setCurrentResultSetRow(memoryResultSetRows.next());
            return true;
        }
        return false;
    }
}

@@ -666,48 +666,8 @@
 w;%0A%0A
-import com.google.common.base.Optional;%0A
 impo
@@ -2419,17 +2419,8 @@
     
-Optional%3C
 Tabl
@@ -2424,17 +2424,16 @@
 ableRule
-%3E
  tableRu
@@ -2454,15 +2454,11 @@
 ule.
-tryFind
+get
 Tabl
@@ -2475,16 +2475,20 @@
 ualTable
+Name
 (tableNa
@@ -2496,113 +2496,8 @@
 e);%0A
-                // TODO :panjuan add default datasource%0A                if (tableRule.isPresent()) %7B%0A    
     
@@ -2546,14 +2546,8 @@
 ule.
-get().
 getL
@@ -2555,28 +2555,24 @@
 gicTable();%0A
-    
             
@@ -2614,36 +2614,32 @@
 ogicTableName);%0A
-    
                 
@@ -2720,20 +2720,16 @@
         
-    
 SQLParsi
@@ -2846,28 +2846,24 @@
             
-    
 String actua
@@ -2934,28 +2934,24 @@
 ableName();%0A
-    
             
@@ -3013,20 +3013,16 @@
         
-    
 logicTab
@@ -3070,33 +3070,26 @@
             
-    %7D%0A   
+%7D%0A
             
@@ -3084,33 +3084,32 @@
                 
- 
 memoryResultSetR
@@ -3201,20 +3201,16 @@
         
-    
 result.a
@@ -3233,34 +3233,16 @@
 etRow);%0A
-                %7D%0A
         

add default datasource sharding rule