
package com.lideeyunji.tool.framework.sql.core.ddl.create;


import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.create.table.ColDataType;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.create.table.Index;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JX
 * @create 2024-03-27 10:40
 * @dedescription:
 */
public class lideeYunJiCreateTable {

    //表名
    private String tableName;

    //别名
    private List<String> tableOptions;

    //字段
    private List<ColumnDefinition> columnDefinitions=new ArrayList<>();

    //索引
    private List<Index> indices = new ArrayList<>();

    public void setTableName(String tableName){
        this.tableName = tableName;
    }

    public void setTableOptions(List<String> tableOptions) {
        this.tableOptions = tableOptions;
    }



    public void addCoulumn(String columnName, String dataType, String... specs){
        ColumnDefinition columnDefinition = new ColumnDefinition(columnName,new ColDataType(dataType));
        if (!FuncBase.isEmpty(specs)){
            columnDefinition.addColumnSpecs(specs);
        }
        this.columnDefinitions.add(columnDefinition);
    }


    public String getDDL(){
        CreateTable createTable = new CreateTable();
        createTable.setTable(new Table(tableName));
        createTable.setTableOptionsStrings(tableOptions);
        createTable.setColumnDefinitions(columnDefinitions);
        createTable.setIndexes(indices);
        return createTable.toString();
    }



    @Test
    public void testCreate(){
        lideeYunJiCreateTable createTable=new lideeYunJiCreateTable();
        createTable.setTableName("tbl_lin2");
        createTable.addCoulumn("id","varchar(12)","PRIMARY KEY","COMMENT 'id'","not null");
        createTable.addCoulumn("name","varchar(20)","not null","COMMENT '姓名'");
        System.out.println(createTable.getDDL());
    }
}
