in src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java [861:1123]
private void writeTypeInfo(TDSWriter tdsWriter, int srcJdbcType, int srcScale, int srcPrecision, SSType destSSType,
SQLCollation collation, boolean isStreaming, boolean srcNullable,
boolean isBaseType) throws SQLServerException {
switch (srcJdbcType) {
case java.sql.Types.INTEGER: // 0x38
if (!srcNullable) {
tdsWriter.writeByte(TDSType.INT4.byteValue());
} else {
tdsWriter.writeByte(TDSType.INTN.byteValue());
tdsWriter.writeByte((byte) 0x04);
}
break;
case java.sql.Types.BIGINT: // 0x7f
if (!srcNullable) {
tdsWriter.writeByte(TDSType.INT8.byteValue());
} else {
tdsWriter.writeByte(TDSType.INTN.byteValue());
tdsWriter.writeByte((byte) 0x08);
}
break;
case java.sql.Types.BIT: // 0x32
if (!srcNullable) {
tdsWriter.writeByte(TDSType.BIT1.byteValue());
} else {
tdsWriter.writeByte(TDSType.BITN.byteValue());
tdsWriter.writeByte((byte) 0x01);
}
break;
case java.sql.Types.SMALLINT: // 0x34
if (!srcNullable) {
tdsWriter.writeByte(TDSType.INT2.byteValue());
} else {
tdsWriter.writeByte(TDSType.INTN.byteValue());
tdsWriter.writeByte((byte) 0x02);
}
break;
case java.sql.Types.TINYINT: // 0x30
if (!srcNullable) {
tdsWriter.writeByte(TDSType.INT1.byteValue());
} else {
tdsWriter.writeByte(TDSType.INTN.byteValue());
tdsWriter.writeByte((byte) 0x01);
}
break;
case java.sql.Types.FLOAT:
case java.sql.Types.DOUBLE: // (FLT8TYPE) 0x3E
if (!srcNullable) {
tdsWriter.writeByte(TDSType.FLOAT8.byteValue());
} else {
tdsWriter.writeByte(TDSType.FLOATN.byteValue());
tdsWriter.writeByte((byte) 0x08);
}
break;
case java.sql.Types.REAL: // (FLT4TYPE) 0x3B
if (!srcNullable) {
tdsWriter.writeByte(TDSType.FLOAT4.byteValue());
} else {
tdsWriter.writeByte(TDSType.FLOATN.byteValue());
tdsWriter.writeByte((byte) 0x04);
}
break;
case microsoft.sql.Types.MONEY:
case microsoft.sql.Types.SMALLMONEY:
tdsWriter.writeByte(TDSType.MONEYN.byteValue()); // 0x6E
if (SSType.MONEY == destSSType)
tdsWriter.writeByte((byte) 8);
else
tdsWriter.writeByte((byte) 4);
break;
case java.sql.Types.NUMERIC:
case java.sql.Types.DECIMAL:
/*
* SQL Server allows the insertion of decimal and numeric into a money (and smallmoney) column, but
* Azure DW only accepts money types for money column. To make the code compatible against both SQL
* Server and Azure DW, always send decimal and numeric as money/smallmoney if the destination column is
* money/smallmoney and the source is decimal/numeric.
*/
if (destSSType == SSType.MONEY) {
tdsWriter.writeByte(TDSType.MONEYN.byteValue());
tdsWriter.writeByte((byte) 8);
break;
} else if (destSSType == SSType.SMALLMONEY) {
tdsWriter.writeByte(TDSType.MONEYN.byteValue());
tdsWriter.writeByte((byte) 4);
break;
}
byte byteType = (java.sql.Types.DECIMAL == srcJdbcType) ? TDSType.DECIMALN.byteValue()
: TDSType.NUMERICN.byteValue();
tdsWriter.writeByte(byteType);
tdsWriter.writeByte((byte) TDSWriter.BIGDECIMAL_MAX_LENGTH); // maximum length
tdsWriter.writeByte((byte) srcPrecision); // unsigned byte
tdsWriter.writeByte((byte) srcScale); // unsigned byte
break;
case microsoft.sql.Types.GUID:
case java.sql.Types.CHAR: // 0xAF
if (isBaseType && (SSType.GUID == destSSType)) {
tdsWriter.writeByte(TDSType.GUID.byteValue());
tdsWriter.writeByte((byte) 0x10);
} else {
if (unicodeConversionRequired(srcJdbcType, destSSType)) {
tdsWriter.writeByte(TDSType.NCHAR.byteValue());
tdsWriter.writeShort(isBaseType ? (short) (srcPrecision) : (short) (2 * srcPrecision));
} else {
tdsWriter.writeByte(TDSType.BIGCHAR.byteValue());
tdsWriter.writeShort((short) (srcPrecision));
}
collation.writeCollation(tdsWriter);
}
break;
case java.sql.Types.NCHAR: // 0xEF
tdsWriter.writeByte(TDSType.NCHAR.byteValue());
tdsWriter.writeShort(isBaseType ? (short) (srcPrecision) : (short) (2 * srcPrecision));
collation.writeCollation(tdsWriter);
break;
case java.sql.Types.LONGVARCHAR:
case java.sql.Types.VARCHAR: // 0xA7
if (unicodeConversionRequired(srcJdbcType, destSSType)) {
tdsWriter.writeByte(TDSType.NVARCHAR.byteValue());
if (isStreaming) {
tdsWriter.writeShort((short) 0xFFFF);
} else {
tdsWriter.writeShort(isBaseType ? (short) (srcPrecision) : (short) (2 * srcPrecision));
}
} else {
tdsWriter.writeByte(TDSType.BIGVARCHAR.byteValue());
if (isStreaming) {
tdsWriter.writeShort((short) 0xFFFF);
} else {
tdsWriter.writeShort((short) (srcPrecision));
}
}
collation.writeCollation(tdsWriter);
break;
case java.sql.Types.LONGNVARCHAR:
case java.sql.Types.NVARCHAR: // 0xE7
tdsWriter.writeByte(TDSType.NVARCHAR.byteValue());
if (isStreaming) {
tdsWriter.writeShort((short) 0xFFFF);
} else {
tdsWriter.writeShort(isBaseType ? (short) (srcPrecision) : (short) (2 * srcPrecision));
}
collation.writeCollation(tdsWriter);
break;
case java.sql.Types.BINARY: // 0xAD
tdsWriter.writeByte(TDSType.BIGBINARY.byteValue());
tdsWriter.writeShort((short) (srcPrecision));
break;
case java.sql.Types.LONGVARBINARY:
case java.sql.Types.VARBINARY: // 0xA5
// BIGVARBINARY
tdsWriter.writeByte(TDSType.BIGVARBINARY.byteValue());
if (isStreaming) {
tdsWriter.writeShort((short) 0xFFFF);
} else {
tdsWriter.writeShort((short) (srcPrecision));
}
break;
case microsoft.sql.Types.DATETIME:
case microsoft.sql.Types.SMALLDATETIME:
case java.sql.Types.TIMESTAMP:
if (((!isBaseType) && (null != serverBulkData))
&& connection.getSendTemporalDataTypesAsStringForBulkCopy()) {
tdsWriter.writeByte(TDSType.BIGVARCHAR.byteValue());
tdsWriter.writeShort((short) (srcPrecision));
collation.writeCollation(tdsWriter);
} else {
switch (destSSType) {
case SMALLDATETIME:
if (!srcNullable)
tdsWriter.writeByte(TDSType.DATETIME4.byteValue());
else {
tdsWriter.writeByte(TDSType.DATETIMEN.byteValue());
tdsWriter.writeByte((byte) 4);
}
break;
case DATETIME:
if (!srcNullable)
tdsWriter.writeByte(TDSType.DATETIME8.byteValue());
else {
tdsWriter.writeByte(TDSType.DATETIMEN.byteValue());
tdsWriter.writeByte((byte) 8);
}
break;
default:
// DATETIME2 0x2A
tdsWriter.writeByte(TDSType.DATETIME2N.byteValue());
tdsWriter.writeByte((byte) srcScale);
break;
}
}
break;
case java.sql.Types.DATE: // 0x28
/*
* SQL Server supports numerous string literal formats for temporal types, hence sending them as varchar
* with approximate precision(length) needed to send supported string literals if destination is
* unencrypted. string literal formats supported by temporal types are available in MSDN page on data
* types.
*/
if (((!isBaseType) && (null != serverBulkData))
&& connection.getSendTemporalDataTypesAsStringForBulkCopy()) {
tdsWriter.writeByte(TDSType.BIGVARCHAR.byteValue());
tdsWriter.writeShort((short) (srcPrecision));
collation.writeCollation(tdsWriter);
} else {
tdsWriter.writeByte(TDSType.DATEN.byteValue());
}
break;
case java.sql.Types.TIME: // 0x29
if (((!isBaseType) && (null != serverBulkData))
&& connection.getSendTemporalDataTypesAsStringForBulkCopy()) {
tdsWriter.writeByte(TDSType.BIGVARCHAR.byteValue());
tdsWriter.writeShort((short) (srcPrecision));
collation.writeCollation(tdsWriter);
} else {
tdsWriter.writeByte(TDSType.TIMEN.byteValue());
tdsWriter.writeByte((byte) srcScale);
}
break;
// Send as DATETIMEOFFSET for TIME_WITH_TIMEZONE and TIMESTAMP_WITH_TIMEZONE
case 2013: // java.sql.Types.TIME_WITH_TIMEZONE
case 2014: // java.sql.Types.TIMESTAMP_WITH_TIMEZONE
tdsWriter.writeByte(TDSType.DATETIMEOFFSETN.byteValue());
tdsWriter.writeByte((byte) srcScale);
break;
case microsoft.sql.Types.DATETIMEOFFSET: // 0x2B
if (((!isBaseType) && (null != serverBulkData))
&& connection.getSendTemporalDataTypesAsStringForBulkCopy()) {
tdsWriter.writeByte(TDSType.BIGVARCHAR.byteValue());
tdsWriter.writeShort((short) (srcPrecision));
collation.writeCollation(tdsWriter);
} else {
tdsWriter.writeByte(TDSType.DATETIMEOFFSETN.byteValue());
tdsWriter.writeByte((byte) srcScale);
}
break;
case microsoft.sql.Types.SQL_VARIANT: // 0x62
tdsWriter.writeByte(TDSType.SQL_VARIANT.byteValue());
tdsWriter.writeInt(TDS.SQL_VARIANT_LENGTH);
break;
default:
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_BulkTypeNotSupported"));
String unsupportedDataType = JDBCType.of(srcJdbcType).toString().toLowerCase(Locale.ENGLISH);
throw new SQLServerException(form.format(new Object[] {unsupportedDataType}), null, 0, null);
}
}