📄 support.java
字号:
}
Object value = list[i].value;
if (value instanceof java.io.InputStream
|| value instanceof java.io.Reader) {
try {
if (list[i].jdbcType == java.sql.Types.LONGVARCHAR ||
list[i].jdbcType == java.sql.Types.CLOB ||
list[i].jdbcType == java.sql.Types.VARCHAR) {
// TODO: Should improve the character set handling here
value = list[i].getString("US-ASCII");
} else {
value = list[i].getBytes("US-ASCII");
}
// Replace the stream/reader with the String/byte[]
list[i].value = value;
} catch (java.io.IOException e) {
throw new SQLException(Messages.get("error.generic.ioerror",
e.getMessage()),
"HY000");
}
}
if (value instanceof String) {
len += ((String) value).length() + 5;
} else if (value instanceof byte[]) {
len += ((byte[]) value).length * 2 + 4;
} else {
len += 32; // Default size
}
}
StringBuffer buf = new StringBuffer(len + 16);
int start = 0;
for (int i = 0; i < list.length; i++) {
int pos = list[i].markerPos;
if (pos > 0) {
buf.append(sql.substring(start, list[i].markerPos));
start = pos + 1;
final boolean isUnicode = connection.getTdsVersion() >= Driver.TDS70 && list[i].isUnicode;
Support.embedData(buf, list[i].value, isUnicode, connection);
}
}
if (start < sql.length()) {
buf.append(sql.substring(start));
}
return buf.toString();
}
/**
* Encode a string into a byte array using the specified character set.
*
* @param cs The Charset name.
* @param value The value to encode.
* @return The value of the String as a <code>byte[]</code>.
*/
static byte[] encodeString(String cs, String value) {
try {
return value.getBytes(cs);
} catch (UnsupportedEncodingException e) {
return value.getBytes();
}
}
/**
* Link the original cause to an <code>SQLWarning</code>.
* <p>
* This convenience method calls {@link #linkException(Exception, Throwable)}
* and casts the result for cleaner code elsewhere.
*
* @param sqle The <code>SQLWarning</code> to enhance.
* @param cause The <code>Throwable</code> to link.
* @return The original <code>SQLWarning</code> object.
*/
public static SQLWarning linkException(SQLWarning sqle, Throwable cause) {
return (SQLWarning) linkException((Exception) sqle, cause);
}
/**
* Link the original cause to an <code>SQLException</code>.
* <p>
* This convenience method calls {@link #linkException(Exception, Throwable)}
* and casts the result for cleaner code elsewhere.
*
* @param sqle The <code>SQLException</code> to enhance.
* @param cause The <code>Throwable</code> to link.
* @return The original <code>SQLException</code> object.
*/
public static SQLException linkException(SQLException sqle, Throwable cause) {
return (SQLException) linkException((Exception) sqle, cause);
}
/**
* Link the original cause to an <code>Exception</code>.
* <p>
* If running under JVM 1.4+ the <code>Throwable.initCause(Throwable)</code>
* method will be invoked to chain the exception, else the exception is
* logged via the {@link Logger} class.
* Modeled after the code written by Brian Heineman.
*
* @param exception The <code>Exception</code> to enhance.
* @param cause The <code>Throwable</code> to link.
* @return The original <code>Exception</code> object.
*/
public static Throwable linkException(Exception exception, Throwable cause) {
Class exceptionClass = exception.getClass();
Class[] parameterTypes = new Class[] {Throwable.class};
Object[] arguments = new Object[] {cause};
try {
Method initCauseMethod = exceptionClass.getMethod("initCause", parameterTypes);
initCauseMethod.invoke(exception, arguments);
} catch (NoSuchMethodException e) {
// Best we can do; this method does not exist in older JVMs.
if (Logger.isActive()) {
Logger.logException((Exception) cause);
}
} catch (Exception e) {
// Ignore all other exceptions. Do not prevent the main exception
// from being returned if reflection fails for any reason.
}
return exception;
}
/**
* Convert a timestamp to a different Timezone.
*
* @param value the timestamp value
* @param target the <code>Calendar</code> containing the TimeZone
* @return the new timestamp value as a <code>long</code>
*/
public static long timeToZone(java.util.Date value, Calendar target) {
synchronized (cal) {
java.util.Date tmp = target.getTime();
try {
cal.setTime(value);
if (!Driver.JDBC3 && value instanceof Timestamp) {
// Not Running under 1.4 so need to add milliseconds
cal.set(Calendar.MILLISECOND,
((Timestamp)value).getNanos() / 1000000);
}
target.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
target.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
target.set(Calendar.SECOND, cal.get(Calendar.SECOND));
target.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
target.set(Calendar.YEAR, cal.get(Calendar.YEAR));
target.set(Calendar.MONTH, cal.get(Calendar.MONTH));
target.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
return target.getTime().getTime();
}
finally {
target.setTime(tmp);
}
}
}
/**
* Convert a timestamp from a different Timezone.
* @param value the timestamp value.
* @param target the Calendar containing the TimeZone.
* @return The new timestamp value as a <code>long</code>.
*/
public static long timeFromZone(java.util.Date value , Calendar target) {
synchronized (cal) {
java.util.Date tmp = target.getTime();
try {
target.setTime(value);
if (!Driver.JDBC3 && value instanceof Timestamp) {
// Not Running under 1.4 so need to add milliseconds
target.set(Calendar.MILLISECOND,
((Timestamp)value).getNanos() / 1000000);
}
cal.set(Calendar.HOUR_OF_DAY, target.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, target.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, target.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, target.get(Calendar.MILLISECOND));
cal.set(Calendar.YEAR, target.get(Calendar.YEAR));
cal.set(Calendar.MONTH, target.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, target.get(Calendar.DAY_OF_MONTH));
return cal.getTime().getTime();
}
finally {
target.setTime(tmp);
}
}
}
/**
* Converts a LOB to the equivalent Java type, i.e. <code>Clob</code> to
* <code>String</code> and <code>Blob</code> to <code>byte[]</code>. If the
* value passed is not a LOB object, it is left unchanged and no exception
* is thrown; the idea is to transparently convert only LOBs.
*
* @param value an object that may be a LOB
* @return if the value was a LOB, the equivalent Java object, otherwise
* the original value
* @throws SQLException if an error occurs while reading the LOB contents
*/
public static Object convertLOB(Object value) throws SQLException {
if (value instanceof Clob) {
Clob c = (Clob) value;
return c.getSubString(1, (int) c.length());
}
if (value instanceof Blob) {
Blob b = (Blob) value;
return b.getBytes(1, (int) b.length());
}
return value;
}
/**
* Converts a LOB type constant to the equivalent Java type constant, i.e.
* <code>Types.CLOB</code> to <code>Types.LONGVARCHAR</code> and
* <code>Types.BLOB</code> to <code>Types.LONGVARBINARY</code>. If the
* type passed is not that of a LOB, it is left unchanged and no exception
* is thrown; the idea is to transparently convert only LOB types.
*
* @param type a {@link Types} constant defining a JDBC type, possibly a
* LOB
* @return if the type was that of a LOB, the equivalent Java object type,
* otherwise the original type
*/
public static int convertLOBType(int type) {
switch (type) {
case Types.BLOB:
return Types.LONGVARBINARY;
case Types.CLOB:
return Types.LONGVARCHAR;
default:
return type;
}
}
/**
* Checks the <code>os.name</code> system property to see if it starts
* with "windows".
*
* @return <code>true</code> if <code>os.name</code> starts with "windows",
* else <code>false</code>.
*/
public static boolean isWindowsOS() {
return System.getProperty("os.name").toLowerCase().startsWith("windows");
}
// ------------- Private methods ---------
/**
* Returns the connection for a given <code>ResultSet</code>,
* <code>Statement</code> or <code>Connection</code> object.
*
* @param callerReference an object reference to the caller of this method;
* must be a <code>Connection</code>, <code>Statement</code> or
* <code>ResultSet</code>
* @return a connection
*/
private static ConnectionJDBC2 getConnection(Object callerReference) {
if (callerReference == null) {
throw new IllegalArgumentException("callerReference cannot be null.");
}
Connection connection;
try {
if (callerReference instanceof Connection) {
connection = (Connection) callerReference;
} else if (callerReference instanceof Statement) {
connection = ((Statement) callerReference).getConnection();
} else if (callerReference instanceof ResultSet) {
connection = ((ResultSet) callerReference).getStatement().getConnection();
} else {
throw new IllegalArgumentException("callerReference is invalid.");
}
} catch (SQLException e) {
throw new IllegalStateException(e.getMessage());
}
return (ConnectionJDBC2) connection;
}
private Support() {
// Prevent an instance of this class being created.
}
/**
* Calculate the buffer size to use when buffering the <code>InputStream</code>
* for named pipes.
* <p/>
* The buffer size is tied directly to the packet size because each request
* to the <code>SmbNamedPipe</code> will send a request for a particular
* size of packet. In other words, if you only request 1 byte, the
* <code>SmbNamedPipe</code> will send a request out and only ask for 1 byte
* back. Buffering the expected packet size ensures that all of the data
* will be returned in the buffer without wasting any space.
*
* @param tdsVersion the TDS version for the connection
* @param packetSize requested packet size for the connection
* @return minimum default packet size if <code>packetSize == 0</code>,
* else <code>packetSize</code>
*/
static int calculateNamedPipeBufferSize(final int tdsVersion, final int packetSize) {
if (packetSize == 0) {
if (tdsVersion >= Driver.TDS70) {
return TdsCore.DEFAULT_MIN_PKT_SIZE_TDS70;
}
return TdsCore.MIN_PKT_SIZE;
}
return packetSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -