package com.pictc.jdbc.model;

import java.sql.Types;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public enum JdbcType {

	CHAR(Types.CHAR,"CHAR",String.class,false,1,null),VARCHAR(Types.VARCHAR,"VARCHAR",String.class,false,255,null),
	
	INTEGER(Types.INTEGER,"INTEGER",Integer.class,true,7,null),BIGINT(Types.BIGINT,"BIGINT",Long.class,true,12,null),
	DECIMAL(Types.DECIMAL,"DECIMAL",java.math.BigDecimal.class,true,60,30),
	
	FLOAT(Types.FLOAT,"FLOAT",float.class,true,10,5),DOUBLE(Types.DOUBLE,"DOUBLE",double.class,true,30,10),
	
	DATE(Types.DATE,"DATE",java.sql.Date.class,false,null,null),TIMESTAMP(Types.TIMESTAMP,"DATETIME",java.sql.Timestamp.class,false,null,null),
	
	CLOB(Types.CLOB,"CLOB",java.sql.Clob.class,false,null,null),

	JSONOBJ(Types.LONGVARCHAR,"JSON",com.alibaba.fastjson.JSONObject.class,false,null,null),
	JSONLIST(Types.LONGVARCHAR,"JSON",com.alibaba.fastjson.JSONArray.class,false,null,null);
    
    private Integer jdbc;
    
    private Class<?> javaClass;
    
    private boolean isDecimal;
    
    private String nativeName;
    
    private Integer leng;
    
    private Integer digits;

    private JdbcType(int jdbcType,String nativeName,Class<?> javaClass, boolean isDecimal,Integer leng,Integer digits) {
		this.jdbc = jdbcType;
		this.javaClass = javaClass;
		this.isDecimal = isDecimal;
		this.nativeName = nativeName;
		this.leng = leng;
		this.digits = digits;
	}
    
    public boolean isDate() {
    	return this == DATE || this==TIMESTAMP;
    }
    
    public static JdbcType valueOf(int jdbc) {
    	JdbcType[] values = JdbcType.values();
    	for (int i = 0; i < values.length; i++) {
    		JdbcType item = values[i];
    		if(item.getJdbc()==jdbc) {
    			return item;
    		}
		}
		return JdbcType.VARCHAR;
    }
    
    public static JdbcType valueOf(Class<?> java) {
    	JdbcType[] values = JdbcType.values();
    	for (int i = 0; i < values.length; i++) {
    		JdbcType item = values[i];
    		if(item.getJavaClass().equals(java)) {
    			return item;
    		}
    	}
    	return JdbcType.VARCHAR;
    }

}
