package com.tofly.codegen.util;

/**
 * @author tuonbed
 * @date 2020/3/13  9:18
 */
public class OracleDataTypeSqlInfo implements DataTypeSqlInfo{

    String TABLEGEN_TABLENAME="TF_SMPN_TABLEGEN_X";
    String TABLEDETAILGEN_TABLENAME="TF_SMPN_TABLEDETAILGEN_X";

    private final String TABLE_COMMENT_ORACLE_SQL="select a.table_name tablename, c.comments, to_char(CREATED,'yyyy-MM-dd hh:mm:ss') created, LAST_DDL_TIME lasttime\n" +
            "\t\tfrom user_tables a, user_objects b, user_tab_comments c\n" +
            "\t\twhere a.TABLE_NAME = b.OBJECT_NAME\n" +
            "\t\tand a.TABLE_NAME = c.table_name\n" +
            "\t\t\tand a.table_name = ? \n" +
            "\t\torder by LAST_DDL_TIME desc";

    private final String TABLE_COMMENT_ALL_ORACLE_SQL="select a.table_name tablename, c.comments, to_char(CREATED,'yyyy-MM-dd hh:mm:ss') created, LAST_DDL_TIME lasttime\n" +
            "\t\tfrom user_tables a, user_objects b, user_tab_comments c\n" +
            "\t\twhere a.TABLE_NAME = b.OBJECT_NAME\n" +
            "\t\tand a.TABLE_NAME = c.table_name\n" +
            "\t\torder by LAST_DDL_TIME desc";

    private final String TABLECOLUM_COMMENT_ORACLE_SQL="    select a.COLUMN_NAME columnname,\n" +
            "       b.comments,\n" +
            "    (case when DATA_TYPE='NUMBER' and data_scale=0 and DATA_PRECISION<=4 then 'Short'   \n" +
            "    when DATA_TYPE='NUMBER' and data_scale=0 and DATA_PRECISION<=9 and DATA_PRECISION>=5   then 'Integer' \n" +
            "    when DATA_TYPE='NUMBER' and data_scale=0 and DATA_PRECISION<=18 and  DATA_PRECISION>=10 then 'Long' \n" +
            "    when DATA_TYPE='NUMBER'  and (DATA_PRECISION is null or DATA_PRECISION>=19) then 'BigDecimal' \n" +
            "    when DATA_TYPE='NUMBER' and data_scale>0 and DATA_PRECISION<=9  then 'Float' \n" +
            "    when DATA_TYPE='NUMBER' and data_scale>0 and DATA_PRECISION>=9  then 'Double'\n" +
            "    else data_type  end\n" +
            "    ) datatype,\n" +
            "       (select 'PRI'\n" +
            "        from user_cons_columns cu, user_constraints au\n" +
            "\t\t\t where cu.constraint_name = au.constraint_name\n" +
            "\t\t\t   and au.constraint_type = 'P'\n" +
            "\t\t\t   and cu.table_name = a.TABLE_NAME\n" +
            "\t\t\t   and cu.column_name = a.COLUMN_NAME) iskey\n" +
            "\t  from user_tab_cols a, user_col_comments b\n" +
            "\t where a.TABLE_NAME = b.table_name\n" +
            "\t   and a.COLUMN_NAME = b.column_name\n" +
            "\t   and a.table_name = ?";


    private final String QUERYMAXSQL="select nvl(max(id),0)+1 from  ";


    private final String  INSERT_TABLEGEN_SQL="INSERT INTO "+TABLEGEN_TABLENAME+"(id,table_name,create_time,create_user,numbers)"+
            " values (?,?,sysdate,?,?)";


    private String  BATCHINSERT_TABLEDETAILGEN_SQL="INSERT INTO "+TABLEDETAILGEN_TABLENAME+"(" +
            "id,table_name, create_time, create_user, numbers, type, tablegen_id, column_name, default_value, query_like, code_key,customsql)"+
            " values (?,?,sysdate,?,?,?,?,?,?,?,?,?)";

    @Override
    public String dataType() {
        return "oracle";
    }

    @Override
    public String getTABLE_COMMENT_SQL() {
        return TABLE_COMMENT_ORACLE_SQL;
    }

    @Override
    public String getTABLE_COMMENT_ALL_SQL() {
        return TABLE_COMMENT_ALL_ORACLE_SQL;
    }

    @Override
    public String getTABLECOLUM_COMMENT_SQL() {
        return TABLECOLUM_COMMENT_ORACLE_SQL;
    }


    @Override
    public String getQUERYMAXSQL() {
        return QUERYMAXSQL;
    }

    @Override
    public String getINSERT_TABLEGEN_SQL() {
        return INSERT_TABLEGEN_SQL;
    }

    @Override
    public String getBATCHINSERT_TABLEDETAILGEN_SQL() {
        return BATCHINSERT_TABLEDETAILGEN_SQL;
    }

    @Override
    public String getTABLEGEN_TABLENAME() {
        return TABLEGEN_TABLENAME;
    }

    @Override
    public String getTABLEDETAILGEN_TABLENAME() {
        return TABLEDETAILGEN_TABLENAME;
    }
    @Override
    public String getDELETE_TABLEGEN_SQL(){
        return "DELETE "+getTABLEGEN_TABLENAME()+"  where TABLE_NAME=? ";
    }
    @Override
    public String getDELETE_TABLEDETAILGEN_SQL(){
        return "DELETE "+getTABLEDETAILGEN_TABLENAME()+"  where TABLE_NAME=?";
    }


}
