All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.schoolwow.quickdao.domain.provider.MySQLDatabaseProvider Maven / Gradle / Ivy

There is a newer version: 5.3.1
Show newest version
package cn.schoolwow.quickdao.domain.provider;

import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;

public class MySQLDatabaseProvider implements DatabaseProvider {
    @Override
    public String name() {
        return "mysql";
    }

    @Override
    public DatabaseType getDatabaseType() {
        return DatabaseType.Mysql;
    }

    @Override
    public String comment(String comment) {
        return "comment \"" + comment + "\"";
    }

    @Override
    public String escape(String value) {
        return "`" + value + "`";
    }

    @Override
    public boolean returnGeneratedKeys() {
        return true;
    }

    @Override
    public String convertJavaTypeToColumnType(String javaType) {
        switch (javaType){
            case "byte":
            case "java.lang.Byte":{return "TINYINT";}
            case "[B":{return "LONGBLOB";}
            case "boolean":
            case "java.lang.Boolean":
            case "char":
            case "java.lang.Character":{return "TINYINT";}
            case "short":
            case "java.lang.Short":{return "SMALLINT";}
            case "int":{return "INT";}
            case "java.lang.Integer":{return "INTEGER(11)";}
            case "float":
            case "java.lang.Float":{return "FLOAT(4,2)";}
            case "long":
            case "java.lang.Long":{return "BIGINT";}
            case "double":
            case "java.lang.Double":{return "DOUBLE(5,2)";}
            case "java.lang.String":{return "VARCHAR(255)";}
            case "java.util.Date":{return "DATETIME";}
            case "java.sql.Date":{return "DATE";}
            case "java.sql.Time":{return "TIME";}
            case "java.sql.Timestamp":{return "TIMESTAMP";}
            case "java.time.LocalDate":{return "DATE";}
            case "java.time.LocalDateTime":{return "DATETIME";}
            case "java.math.BigDecimal":{return "DECIMAL(20,4)";}
            case "java.sql.Blob":{return "BLOB";}
            case "java.sql.Clob":{return "CLOB";}
            case "java.sql.NClob":{return "NCLOB";}
            case "java.io.InputStream":
            case "java.io.Reader":{return "LONGTEXT";}
            case "com.alibaba.fastjson.JSONObject":
            case "com.alibaba.fastjson.JSONArray":{return "JSON";}
        }
        throw new IllegalArgumentException("目前不支持转换java数据类型["+javaType+"]为数据库类型");
    }

    @Override
    public String convertColumnTypeToJavaType(String columnType) {
        String rawColumnType = columnType;
        if(columnType.contains("(")){
            columnType = columnType.substring(0, columnType.indexOf("("));
        }
        columnType = columnType.trim().toUpperCase();
        //INT类型
        switch (columnType){
            case "TINYINT":{return "Boolean";}
            case "SMALLINT":{return "Short";}
            case "INT":
            case "INTEGER":{return "Integer";}
            case "BIGINT":{return "Long";}
            case "FLOAT":{return "Float";}
            case "DOUBLE":{return "Double";}
            case "LONGBLOB":{return "[B";}
            case "VARCHAR":{return "String";}
            case "DATETIME":{return "java.time.LocalDateTime";}
            case "DATE":{return "java.time.LocalDate";}
            case "TIME":{return "java.sql.Time";}
            case "TIMESTAMP":{return "java.sql.Timestamp";}
            case "DECIMAL":{return "java.math.BigDecimal";}
            case "BLOB":{return "java.sql.Blob";}
            case "TEXT":{return "String";}
            case "LONGTEXT":{return "String";}
            case "JSON":{return "com.alibaba.fastjson.JSONObject";}
        }
        throw new IllegalArgumentException("目前不支持转换数据库类型["+rawColumnType+"]为java数据类型");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy