提交 | 用户 | 时间
|
e7c126
|
1 |
package liquibase.datatype.core; |
H |
2 |
|
|
3 |
import liquibase.change.core.LoadDataChange; |
|
4 |
import liquibase.database.Database; |
|
5 |
import liquibase.database.core.*; |
|
6 |
import liquibase.datatype.DataTypeInfo; |
|
7 |
import liquibase.datatype.DatabaseDataType; |
|
8 |
import liquibase.datatype.LiquibaseDataType; |
|
9 |
import liquibase.exception.UnexpectedLiquibaseException; |
|
10 |
import liquibase.statement.DatabaseFunction; |
|
11 |
import liquibase.util.StringUtil; |
|
12 |
|
|
13 |
import java.util.Locale; |
|
14 |
import java.util.regex.Pattern; |
|
15 |
|
|
16 |
@DataTypeInfo(name = "boolean", aliases = {"java.sql.Types.BOOLEAN", "java.lang.Boolean", "bit", "bool"}, minParameters = 0, maxParameters = 0, priority = LiquibaseDataType.PRIORITY_DEFAULT) |
|
17 |
public class BooleanType extends LiquibaseDataType { |
|
18 |
|
|
19 |
@Override |
|
20 |
public DatabaseDataType toDatabaseDataType(Database database) { |
|
21 |
String originalDefinition = StringUtil.trimToEmpty(getRawDefinition()); |
|
22 |
if ((database instanceof Firebird3Database)) { |
|
23 |
return new DatabaseDataType("BOOLEAN"); |
|
24 |
} |
|
25 |
|
|
26 |
if ((database instanceof Db2zDatabase) || (database instanceof FirebirdDatabase)) { |
|
27 |
return new DatabaseDataType("SMALLINT"); |
|
28 |
} else if (database instanceof MSSQLDatabase) { |
|
29 |
return new DatabaseDataType(database.escapeDataTypeName("bit")); |
|
30 |
} else if (database instanceof MySQLDatabase) { |
|
31 |
if (originalDefinition.toLowerCase(Locale.US).startsWith("bit")) { |
|
32 |
return new DatabaseDataType("BIT", getParameters()); |
|
33 |
} |
|
34 |
return new DatabaseDataType("BIT", 1); |
|
35 |
} else if (database instanceof OracleDatabase) { |
|
36 |
return new DatabaseDataType("NUMBER", 1); |
|
37 |
} else if ((database instanceof SybaseASADatabase) || (database instanceof SybaseDatabase)) { |
|
38 |
return new DatabaseDataType("BIT"); |
|
39 |
} else if (database instanceof DerbyDatabase) { |
|
40 |
if (((DerbyDatabase) database).supportsBooleanDataType()) { |
|
41 |
return new DatabaseDataType("BOOLEAN"); |
|
42 |
} else { |
|
43 |
return new DatabaseDataType("SMALLINT"); |
|
44 |
} |
|
45 |
} else if (database instanceof DB2Database) { |
|
46 |
if (((DB2Database) database).supportsBooleanDataType()) |
|
47 |
return new DatabaseDataType("BOOLEAN"); |
|
48 |
else |
|
49 |
return new DatabaseDataType("SMALLINT"); |
|
50 |
} else if (database instanceof HsqlDatabase) { |
|
51 |
return new DatabaseDataType("BOOLEAN"); |
|
52 |
} else if (database instanceof PostgresDatabase) { |
|
53 |
if (originalDefinition.toLowerCase(Locale.US).startsWith("bit")) { |
|
54 |
return new DatabaseDataType("BIT", getParameters()); |
|
55 |
} |
|
56 |
} else if (database instanceof DmDatabase) { // dhb52: DM Support |
|
57 |
return new DatabaseDataType("bit"); |
|
58 |
} |
|
59 |
|
|
60 |
return super.toDatabaseDataType(database); |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public String objectToSql(Object value, Database database) { |
|
65 |
if ((value == null) || "null".equals(value.toString().toLowerCase(Locale.US))) { |
|
66 |
return null; |
|
67 |
} |
|
68 |
|
|
69 |
String returnValue; |
|
70 |
if (value instanceof String) { |
|
71 |
value = ((String) value).replaceAll("'", ""); |
|
72 |
if ("true".equals(((String) value).toLowerCase(Locale.US)) || "1".equals(value) || "b'1'".equals(((String) value).toLowerCase(Locale.US)) || "t".equals(((String) value).toLowerCase(Locale.US)) || ((String) value).toLowerCase(Locale.US).equals(this.getTrueBooleanValue(database).toLowerCase(Locale.US))) { |
|
73 |
returnValue = this.getTrueBooleanValue(database); |
|
74 |
} else if ("false".equals(((String) value).toLowerCase(Locale.US)) || "0".equals(value) || "b'0'".equals( |
|
75 |
((String) value).toLowerCase(Locale.US)) || "f".equals(((String) value).toLowerCase(Locale.US)) || ((String) value).toLowerCase(Locale.US).equals(this.getFalseBooleanValue(database).toLowerCase(Locale.US))) { |
|
76 |
returnValue = this.getFalseBooleanValue(database); |
|
77 |
} else if (database instanceof PostgresDatabase && Pattern.matches("b?([01])\\1*(::bit|::\"bit\")?", (String) value)) { |
|
78 |
returnValue = "b'" |
|
79 |
+ value.toString() |
|
80 |
.replace("b", "") |
|
81 |
.replace("\"", "") |
|
82 |
.replace("::it", "") |
|
83 |
+ "'::\"bit\""; |
|
84 |
} else { |
|
85 |
throw new UnexpectedLiquibaseException("Unknown boolean value: " + value); |
|
86 |
} |
|
87 |
} else if (value instanceof Long) { |
|
88 |
if (Long.valueOf(1).equals(value)) { |
|
89 |
returnValue = this.getTrueBooleanValue(database); |
|
90 |
} else { |
|
91 |
returnValue = this.getFalseBooleanValue(database); |
|
92 |
} |
|
93 |
} else if (value instanceof Number) { |
|
94 |
if (value.equals(1) || "1".equals(value.toString()) || "1.0".equals(value.toString())) { |
|
95 |
returnValue = this.getTrueBooleanValue(database); |
|
96 |
} else { |
|
97 |
returnValue = this.getFalseBooleanValue(database); |
|
98 |
} |
|
99 |
} else if (value instanceof DatabaseFunction) { |
|
100 |
return value.toString(); |
|
101 |
} else if (value instanceof Boolean) { |
|
102 |
if (((Boolean) value)) { |
|
103 |
returnValue = this.getTrueBooleanValue(database); |
|
104 |
} else { |
|
105 |
returnValue = this.getFalseBooleanValue(database); |
|
106 |
} |
|
107 |
} else { |
|
108 |
throw new UnexpectedLiquibaseException("Cannot convert type " + value.getClass() + " to a boolean value"); |
|
109 |
} |
|
110 |
|
|
111 |
return returnValue; |
|
112 |
} |
|
113 |
|
|
114 |
protected boolean isNumericBoolean(Database database) { |
|
115 |
if (database instanceof Firebird3Database) { |
|
116 |
return false; |
|
117 |
} |
|
118 |
if (database instanceof DerbyDatabase) { |
|
119 |
return !((DerbyDatabase) database).supportsBooleanDataType(); |
|
120 |
} else if (database instanceof DB2Database) { |
|
121 |
return !((DB2Database) database).supportsBooleanDataType(); |
|
122 |
} |
|
123 |
return (database instanceof Db2zDatabase) |
|
124 |
|| (database instanceof FirebirdDatabase) |
|
125 |
|| (database instanceof MSSQLDatabase) |
|
126 |
|| (database instanceof MySQLDatabase) |
|
127 |
|| (database instanceof OracleDatabase) |
|
128 |
|| (database instanceof SQLiteDatabase) |
|
129 |
|| (database instanceof SybaseASADatabase) |
|
130 |
|| (database instanceof SybaseDatabase) |
|
131 |
|| (database instanceof DmDatabase); // dhb52: DM Support |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* The database-specific value to use for "false" "boolean" columns. |
|
136 |
*/ |
|
137 |
public String getFalseBooleanValue(Database database) { |
|
138 |
if (isNumericBoolean(database)) { |
|
139 |
return "0"; |
|
140 |
} |
|
141 |
if (database instanceof InformixDatabase) { |
|
142 |
return "'f'"; |
|
143 |
} |
|
144 |
return "FALSE"; |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* The database-specific value to use for "true" "boolean" columns. |
|
149 |
*/ |
|
150 |
public String getTrueBooleanValue(Database database) { |
|
151 |
if (isNumericBoolean(database)) { |
|
152 |
return "1"; |
|
153 |
} |
|
154 |
if (database instanceof InformixDatabase) { |
|
155 |
return "'t'"; |
|
156 |
} |
|
157 |
return "TRUE"; |
|
158 |
} |
|
159 |
|
|
160 |
@Override |
|
161 |
public LoadDataChange.LOAD_DATA_TYPE getLoadTypeName() { |
|
162 |
return LoadDataChange.LOAD_DATA_TYPE.BOOLEAN; |
|
163 |
} |
|
164 |
|
|
165 |
} |