📄 tdsdata.java
字号:
return new UniqueIdentifier(bytes);
}
break;
case SYBNUMERIC:
case SYBDECIMAL:
len = in.read();
if (len > 0) {
int sign = in.read();
len--;
byte[] bytes = new byte[len];
BigInteger bi;
if (in.getServerType() == Driver.SYBASE) {
// Sybase order is MSB first!
for (int i = 0; i < len; i++) {
bytes[i] = (byte) in.read();
}
bi = new BigInteger((sign == 0) ? 1 : -1, bytes);
} else {
while (len-- > 0) {
bytes[len] = (byte)in.read();
}
bi = new BigInteger((sign == 0) ? -1 : 1, bytes);
}
return new BigDecimal(bi, ci.scale);
}
break;
case SYBVARIANT:
return getVariant(connection, in);
default:
throw new ProtocolException("Unsupported TDS data type 0x"
+ Integer.toHexString(ci.tdsType & 0xFF));
}
return null;
}
/**
* Retrieve the signed status of the column.
*
* @param ci the column meta data
* @return <code>true</code> if the column is a signed numeric.
*/
static boolean isSigned(ColInfo ci) {
int type = ci.tdsType;
if (type < 0 || type > 255 || types[type] == null) {
throw new IllegalArgumentException("TDS data type " + type
+ " invalid");
}
if (type == TdsData.SYBINTN && ci.bufferSize == 1) {
type = TdsData.SYBINT1; // Tiny int is not signed!
}
return types[type].isSigned;
}
/**
* Retrieve the collation status of the column.
* <p/>
* TDS 8.0 character columns include collation information.
*
* @param ci the column meta data
* @return <code>true</code> if the column requires collation data.
*/
static boolean isCollation(ColInfo ci) {
int type = ci.tdsType;
if (type < 0 || type > 255 || types[type] == null) {
throw new IllegalArgumentException("TDS data type " + type
+ " invalid");
}
return types[type].isCollation;
}
/**
* Retrieve the currency status of the column.
*
* @param ci The column meta data.
* @return <code>boolean</code> true if the column is a currency type.
*/
static boolean isCurrency(ColInfo ci) {
int type = ci.tdsType;
if (type < 0 || type > 255 || types[type] == null) {
throw new IllegalArgumentException("TDS data type " + type
+ " invalid");
}
return type == SYBMONEY || type == SYBMONEY4 || type == SYBMONEYN;
}
/**
* Retrieve the searchable status of the column.
*
* @param ci the column meta data
* @return <code>true</code> if the column is not a text or image type.
*/
static boolean isSearchable(ColInfo ci) {
int type = ci.tdsType;
if (type < 0 || type > 255 || types[type] == null) {
throw new IllegalArgumentException("TDS data type " + type
+ " invalid");
}
return types[type].size != -4;
}
/**
* Determines whether the column is Unicode encoded.
*
* @param ci the column meta data
* @return <code>true</code> if the column is Unicode encoded
*/
static boolean isUnicode(ColInfo ci) {
int type = ci.tdsType;
if (type < 0 || type > 255 || types[type] == null) {
throw new IllegalArgumentException("TDS data type " + type
+ " invalid");
}
switch (type) {
case SYBNVARCHAR:
case SYBNTEXT:
case XSYBNCHAR:
case XSYBNVARCHAR:
case XSYBCHAR: // Not always
case SYBVARIANT: // Not always
return true;
default:
return false;
}
}
/**
* Fill in the TDS native type code and all other fields for a
* <code>ColInfo</code> instance with the JDBC type set.
*
* @param ci the <code>ColInfo</code> instance
*/
static void fillInType(ColInfo ci)
throws SQLException {
switch (ci.jdbcType) {
case java.sql.Types.VARCHAR:
ci.tdsType = SYBVARCHAR;
ci.bufferSize = MS_LONGVAR_MAX;
ci.displaySize = MS_LONGVAR_MAX;
ci.precision = MS_LONGVAR_MAX;
break;
case java.sql.Types.INTEGER:
ci.tdsType = SYBINT4;
ci.bufferSize = 4;
ci.displaySize = 11;
ci.precision = 10;
break;
case java.sql.Types.SMALLINT:
ci.tdsType = SYBINT2;
ci.bufferSize = 2;
ci.displaySize = 6;
ci.precision = 5;
break;
case java.sql.Types.BIT:
ci.tdsType = SYBBIT;
ci.bufferSize = 1;
ci.displaySize = 1;
ci.precision = 1;
break;
default:
throw new SQLException(Messages.get(
"error.baddatatype",
Integer.toString(ci.jdbcType)), "HY000");
}
ci.sqlType = types[ci.tdsType].sqlType;
ci.scale = 0;
}
/**
* Retrieve the TDS native type code for the parameter.
*
* @param connection the connectionJDBC object
* @param pi the parameter descriptor
*/
static void getNativeType(ConnectionJDBC2 connection, ParamInfo pi)
throws SQLException {
int len;
int jdbcType = pi.jdbcType;
if (jdbcType == java.sql.Types.OTHER) {
jdbcType = Support.getJdbcType(pi.value);
}
switch (jdbcType) {
case java.sql.Types.CHAR:
case java.sql.Types.VARCHAR:
case java.sql.Types.LONGVARCHAR:
case java.sql.Types.CLOB:
if (pi.value == null) {
len = 0;
} else {
len = pi.length;
}
if (connection.getTdsVersion() < Driver.TDS70) {
if (len > 0
&& len <= SYB_LONGVAR_MAX / 2
&& connection.getSybaseInfo(TdsCore.SYB_UNICODE)
&& connection.isUseUnicode()) {
// Sybase can send values as unicode if conversion to the
// server charset fails.
// One option to determine if conversion will fail is to use
// the CharSetEncoder class but this is only available from
// JDK 1.4.
// For now we will call a local method to see if the string
// should be sent as unicode.
// This behaviour can be disabled by setting the connection
// property sendParametersAsUnicode=false.
// TODO: Find a better way of testing for convertable charset.
try {
String charset = connection.getCharset();
String tmp = pi.getString(charset);
if (!canEncode(tmp, charset)) {
// Conversion fails need to send as unicode.
pi.sqlType = "univarchar("+tmp.length()+')';
pi.tdsType = SYBLONGBINARY;
pi.length = tmp.length();
break;
}
} catch (IOException e) {
throw new SQLException(
Messages.get("error.generic.ioerror", e.getMessage()), "HY000");
}
}
//
// If the client character set is wide then we need to ensure that the size
// is within bounds even after conversion from Unicode
//
if (connection.isWideChar() && len <= SYB_LONGVAR_MAX) {
try {
String tmp = pi.getString(connection.getCharset());
len = (tmp == null) ? 0 : tmp.length();
} catch (IOException e) {
throw new SQLException(
Messages.get("error.generic.ioerror", e.getMessage()), "HY000");
}
}
if (len <= VAR_MAX) {
pi.tdsType = SYBVARCHAR;
pi.sqlType = "varchar(255)";
} else {
if (connection.getSybaseInfo(TdsCore.SYB_LONGDATA)) {
if (len > SYB_LONGVAR_MAX) {
// Use special Sybase long data type which
// allows text data to be sent as a statement parameter
// (although not as a SP parameter).
pi.tdsType = SYBLONGDATA;
pi.sqlType = "text";
} else {
// Use Sybase 12.5+ long varchar type which
// is limited to 16384 bytes.
pi.tdsType = XSYBCHAR;
pi.sqlType = "varchar(" + len + ')';
}
} else {
pi.tdsType = SYBTEXT;
pi.sqlType = "text";
}
}
} else {
if (pi.isUnicode && len <= MS_LONGVAR_MAX / 2) {
pi.tdsType = XSYBNVARCHAR;
pi.sqlType = "nvarchar(4000)";
} else if (!pi.isUnicode && len <= MS_LONGVAR_MAX) {
pi.tdsType = XSYBVARCHAR;
pi.sqlType = "varchar(8000)";
} else {
if (pi.isOutput) {
throw new SQLException(
Messages.get("error.textoutparam"), "HY000");
}
if (pi.isUnicode) {
pi.tdsType = SYBNTEXT;
pi.sqlType = "ntext";
} else {
pi.tdsType = SYBTEXT;
pi.sqlType = "text";
}
}
}
break;
case java.sql.Types.TINYINT:
case java.sql.Types.SMALLINT:
case java.sql.Types.INTEGER:
pi.tdsType = SYBINTN;
pi.sqlType = "int";
break;
case JtdsStatement.BOOLEAN:
case java.sql.Types.BIT:
if (connection.getTdsVersion() >= Driver.TDS70 ||
connection.getSybaseInfo(TdsCore.SYB_BITNULL)) {
pi.tdsType = SYBBITN;
} else {
pi.tdsType = SYBBIT;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -