package io.ebeaninternal.server.type;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarType;
import io.ebeaninternal.server.core.BasicTypeConverter;
import java.math.BigInteger;
import java.sql.Types;
import java.util.Calendar;
import java.util.Date;
public class DefaultTypeFactory {
private final DatabaseConfig config;
public DefaultTypeFactory(DatabaseConfig config) {
this.config = config;
}
protected ScalarTypeBool createBoolean(String trueValue, String falseValue) {
try {
Integer intTrue = BasicTypeConverter.toInteger(trueValue);
Integer intFalse = BasicTypeConverter.toInteger(falseValue);
return new ScalarTypeBoolean.IntBoolean(intTrue, intFalse);
} catch (NumberFormatException e) {
return new ScalarTypeBoolean.StringBoolean(trueValue, falseValue);
}
}
public ScalarTypeBool createBoolean() {
if (config == null) {
return new ScalarTypeBoolean.Native();
}
String trueValue = config.getDatabaseBooleanTrue();
String falseValue = config.getDatabaseBooleanFalse();
if (falseValue != null && trueValue != null) {
return createBoolean(trueValue, falseValue);
}
int booleanDbType = config.getDatabasePlatform().getBooleanDbType();
if (booleanDbType == Types.BIT) {
return new ScalarTypeBoolean.BitBoolean();
}
if (booleanDbType == Types.INTEGER) {
return new ScalarTypeBoolean.IntBoolean(1, 0);
}
if (booleanDbType == Types.VARCHAR) {
return new ScalarTypeBoolean.StringBoolean("T", "F");
}
return new ScalarTypeBoolean.Native();
}
public ScalarType<Date> createUtilDate(JsonConfig.DateTime mode, JsonConfig.Date jsonDate) {
return createUtilDate(mode, jsonDate, java.sql.Types.TIMESTAMP);
}
public ScalarType<java.util.Date> createUtilDate(JsonConfig.DateTime jsonDateTime, JsonConfig.Date jsonDate, int utilDateType) {
switch (utilDateType) {
case Types.DATE:
return new ScalarTypeUtilDate.DateType(jsonDate);
case Types.TIMESTAMP:
return new ScalarTypeUtilDate.TimestampType(jsonDateTime);
default:
throw new RuntimeException("Invalid type " + utilDateType);
}
}
public ScalarType<Calendar> createCalendar(JsonConfig.DateTime mode) {
return createCalendar(mode, java.sql.Types.TIMESTAMP);
}
public ScalarType<Calendar> createCalendar(JsonConfig.DateTime mode, int jdbcType) {
return new ScalarTypeCalendar(mode, jdbcType);
}
public ScalarType<BigInteger> createMathBigInteger() {
return new ScalarTypeMathBigInteger();
}
}