/*
 * Copyright 1999-2017 Alibaba Group Holding Ltd.
 *
 * 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.
 */
package com.alibaba.druid.bvt.sql.mysql.createTable;

import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import org.junit.Assert;
import org.junit.Test;

public class MySqlCreateTableTest57 extends MysqlTest {
    @Test
    public void test_one() throws Exception {
        String sql = "CREATE TABLE `t_cpi_driskconfig_bak` (" + "`Sequence` bigint(20) NOT NULL AUTO_INCREMENT,"
                + "`comcode` varchar(20) binary NOT NULL," + "`riskcode` varchar(10) binary NOT NULL,"
                + "`configcodehead` varchar(30) binary NOT NULL," + "`configcodebody` varchar(100) binary,"
                + "`configvalue` varchar(200) binary ," + "`inputdate` datetime NOT NULL,"
                + "`validstatus` char(1) NOT NULL," + "`remark` varchar(3000)," + "`flag` varchar(10) ,"
                + "PRIMARY KEY (`Sequence`)" + ") ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8;";

        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement stmt = parser.parseCreateTable();

        MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
        stmt.accept(visitor);

//        System.out.println("Tables : " + visitor.getTables());
//        System.out.println("fields : " + visitor.getColumns());
//        System.out.println("coditions : " + visitor.getConditions());

        Assert.assertEquals(1, visitor.getTables().size());
        Assert.assertEquals(10, visitor.getColumns().size());
        Assert.assertEquals(0, visitor.getConditions().size());

        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals("CREATE TABLE `t_cpi_driskconfig_bak` (" + "\n\t`Sequence` bigint(20) NOT NULL AUTO_INCREMENT,"
                + "\n\t`comcode` varchar(20) BINARY  NOT NULL," + "\n\t`riskcode` varchar(10) BINARY  NOT NULL,"
                + "\n\t`configcodehead` varchar(30) BINARY  NOT NULL," + "\n\t`configcodebody` varchar(100) BINARY ,"
                + "\n\t`configvalue` varchar(200) BINARY ," + "\n\t`inputdate` datetime NOT NULL,"
                + "\n\t`validstatus` char(1) NOT NULL," + "\n\t`remark` varchar(3000)," + "\n\t`flag` varchar(10),"
                + "\n\tPRIMARY KEY (`Sequence`)" + "\n) ENGINE = InnoDB AUTO_INCREMENT = 49 CHARSET = utf8", output);

    }

    @Test
    public void test_two() throws Exception {
        String sql = "CREATE TABLE `t_cpi_driskconfig_bak` (" + "`Sequence` bigint(20) NOT NULL AUTO_INCREMENT,"
                + "`comcode` binary(20) NOT NULL," + "`riskcode` varchar(10) binary NOT NULL,"
                + "`configcodehead` varchar(30) binary NOT NULL," + "`configcodebody` varchar(100) binary,"
                + "`configvalue` varchar(200) binary ," + "`inputdate` datetime NOT NULL,"
                + "`validstatus` char(1) NOT NULL," + "`remark` varchar(3000)," + "`flag` varchar(10) ,"
                + "PRIMARY KEY (`Sequence`)" + ") ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8;";

        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement stmt = parser.parseCreateTable();

        MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
        stmt.accept(visitor);

//        System.out.println("Tables : " + visitor.getTables());
//        System.out.println("fields : " + visitor.getColumns());
//        System.out.println("coditions : " + visitor.getConditions());

        Assert.assertEquals(1, visitor.getTables().size());
        Assert.assertEquals(10, visitor.getColumns().size());
        Assert.assertEquals(0, visitor.getConditions().size());

        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals("CREATE TABLE `t_cpi_driskconfig_bak` (" + "\n\t`Sequence` bigint(20) NOT NULL AUTO_INCREMENT,"
                + "\n\t`comcode` binary(20) NOT NULL," + "\n\t`riskcode` varchar(10) BINARY  NOT NULL,"
                + "\n\t`configcodehead` varchar(30) BINARY  NOT NULL," + "\n\t`configcodebody` varchar(100) BINARY ,"
                + "\n\t`configvalue` varchar(200) BINARY ," + "\n\t`inputdate` datetime NOT NULL,"
                + "\n\t`validstatus` char(1) NOT NULL," + "\n\t`remark` varchar(3000)," + "\n\t`flag` varchar(10),"
                + "\n\tPRIMARY KEY (`Sequence`)" + "\n) ENGINE = InnoDB AUTO_INCREMENT = 49 CHARSET = utf8", output);

    }
}
