1
|
/**
|
2
|
* Copyright (C) 2019 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.database.update;
|
10
|
|
11
|
import java.sql.Types;
|
12
|
|
13
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
14
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
15
|
|
16
|
/**
|
17
|
* @author a.mueller
|
18
|
* @since 24.07.2019
|
19
|
*/
|
20
|
public enum Datatype {
|
21
|
INTEGER("int"),
|
22
|
CLOB("clob"),
|
23
|
VARCHAR("varchar"),
|
24
|
DATETIME("datetime"),
|
25
|
DOUBLE("double"),
|
26
|
FLOAT("float"),
|
27
|
TINYINTEGER("tinyint"),
|
28
|
BIT("bit"),
|
29
|
BIGDECIMAL("decimal"),
|
30
|
;
|
31
|
|
32
|
private String defaultStr;
|
33
|
|
34
|
private Datatype(String strType){
|
35
|
this.defaultStr = strType;
|
36
|
}
|
37
|
|
38
|
public String format(ICdmDataSource datasource, Integer size) {
|
39
|
return format(datasource, size, null);
|
40
|
}
|
41
|
|
42
|
public String format(ICdmDataSource datasource, Integer size, Integer scale) {
|
43
|
String result = defaultStr;
|
44
|
DatabaseTypeEnum dbType = datasource.getDatabaseType();
|
45
|
//nvarchar
|
46
|
if (dbType.equals(DatabaseTypeEnum.PostgreSQL)){ //TODO use PostgeSQL82 Dialect infos
|
47
|
result = result.replace("nvarchar", "varchar");
|
48
|
result = result.replace("float", dbType.getHibernateDialect().getTypeName(Types.FLOAT));
|
49
|
result = result.replace("double", dbType.getHibernateDialect().getTypeName(Types.DOUBLE));
|
50
|
result = result.replace("bit", dbType.getHibernateDialect().getTypeName(Types.BIT));
|
51
|
result = result.replace("datetime", dbType.getHibernateDialect().getTypeName(Types.TIMESTAMP));
|
52
|
result = result.replace("tinyint", DatabaseTypeEnum.PostgreSQL.getHibernateDialect().getTypeName(Types.TINYINT));
|
53
|
}
|
54
|
//CLOB
|
55
|
if (this == CLOB){
|
56
|
//TODO use hibernate dialects
|
57
|
if (dbType.equals(DatabaseTypeEnum.MySQL)){
|
58
|
result = "longtext";
|
59
|
}else if (dbType.equals(DatabaseTypeEnum.H2)){
|
60
|
result = "CLOB"; //or NVARCHAR
|
61
|
}else if (dbType.equals(DatabaseTypeEnum.PostgreSQL)){
|
62
|
result = "text";
|
63
|
}else if (dbType.equals(DatabaseTypeEnum.SqlServer2005)){
|
64
|
result = "NVARCHAR(MAX)";
|
65
|
}
|
66
|
}else if (this == VARCHAR){
|
67
|
result = result + "(" + size + ")";
|
68
|
}else if (this == BIGDECIMAL){
|
69
|
result = result + "(" + size + "," + scale + ")";
|
70
|
}
|
71
|
return result;
|
72
|
}
|
73
|
}
|