public static PostgresType fromDerby(DataTypeDescriptor sqlType, final TInstance type) {
TypeOid oid;
short length = -1;
int modifier = -1;
TypeId typeId = sqlType.getTypeId();
switch (typeId.getTypeFormatId()) {
case TypeId.FormatIds.INTERVAL_DAY_SECOND_ID:
oid = TypeOid.INTERVAL_TYPE_OID;
break;
case TypeId.FormatIds.INTERVAL_YEAR_MONTH_ID:
oid = TypeOid.INTERVAL_TYPE_OID;
break;
case TypeId.FormatIds.BIT_TYPE_ID:
oid = TypeOid.BYTEA_TYPE_OID;
break;
case TypeId.FormatIds.BOOLEAN_TYPE_ID:
oid = TypeOid.BOOL_TYPE_OID;
break;
case TypeId.FormatIds.CHAR_TYPE_ID:
oid = TypeOid.BPCHAR_TYPE_OID;
break;
case TypeId.FormatIds.DATE_TYPE_ID:
oid = TypeOid.DATE_TYPE_OID;
break;
case TypeId.FormatIds.DECIMAL_TYPE_ID:
case TypeId.FormatIds.NUMERIC_TYPE_ID:
oid = TypeOid.NUMERIC_TYPE_OID;
break;
case TypeId.FormatIds.DOUBLE_TYPE_ID:
oid = TypeOid.FLOAT8_TYPE_OID;
break;
case TypeId.FormatIds.INT_TYPE_ID:
if (typeId.isUnsigned())
oid = TypeOid.INT8_TYPE_OID;
else
oid = TypeOid.INT4_TYPE_OID;
break;
case TypeId.FormatIds.LONGINT_TYPE_ID:
if (typeId.isUnsigned()) {
return new PostgresType(TypeOid.NUMERIC_TYPE_OID, (short)8, (20 << 16) + 4,
type);
}
oid = TypeOid.INT8_TYPE_OID;
break;
case TypeId.FormatIds.LONGVARBIT_TYPE_ID:
oid = TypeOid.TEXT_TYPE_OID;
break;
case TypeId.FormatIds.LONGVARCHAR_TYPE_ID:
oid = TypeOid.TEXT_TYPE_OID;
break;
case TypeId.FormatIds.REAL_TYPE_ID:
oid = TypeOid.FLOAT4_TYPE_OID;
break;
case TypeId.FormatIds.SMALLINT_TYPE_ID:
if (typeId.isUnsigned())
oid = TypeOid.INT4_TYPE_OID;
else
oid = TypeOid.INT2_TYPE_OID;
break;
case TypeId.FormatIds.TIME_TYPE_ID:
oid = TypeOid.TIME_TYPE_OID;
break;
case TypeId.FormatIds.TIMESTAMP_TYPE_ID:
// TODO: MDatetimes.TIMESTAMP is MYSQL_TIMESTAMP, another
// way of representing seconds precision, not ISO
// timestamp with fractional seconds.
oid = TypeOid.TIMESTAMP_TYPE_OID;
break;
case TypeId.FormatIds.TINYINT_TYPE_ID:
oid = TypeOid.INT2_TYPE_OID; // No INT1, room for unsigned
break;
case TypeId.FormatIds.VARBIT_TYPE_ID:
oid = TypeOid.BYTEA_TYPE_OID;
break;
case TypeId.FormatIds.BLOB_TYPE_ID:
oid = TypeOid.TEXT_TYPE_OID;
break;
case TypeId.FormatIds.VARCHAR_TYPE_ID:
oid = TypeOid.VARCHAR_TYPE_OID;
break;
case TypeId.FormatIds.CLOB_TYPE_ID:
oid = TypeOid.TEXT_TYPE_OID;
break;
case TypeId.FormatIds.XML_TYPE_ID:
oid = TypeOid.XML_TYPE_OID;
break;
case TypeId.FormatIds.GUID_TYPE_ID:
oid = TypeOid.UUID_TYPE_OID;
break;
case TypeId.FormatIds.USERDEFINED_TYPE_ID:
default:
throw new UnknownDataTypeException(sqlType.toString());
}
if (typeId.isDecimalTypeId() || typeId.isNumericTypeId()) {
modifier = (sqlType.getPrecision() << 16) + sqlType.getScale() + 4;
}
else if (typeId.variableLength()) {
modifier = sqlType.getMaximumWidth() + 4;
}
else {
length = (short)typeId.getMaximumMaximumWidth();
}
return new PostgresType(oid, length, modifier, type);
}