📄 support.java
字号:
// 7 extra characters needed for each parameter. char[] buf = new char[sql.length() + list.length * 7]; int bufferPtr = 0; // Output buffer pointer int start = 0; // Input string pointer StringBuffer number = new StringBuffer(4); for (int i = 0; i < list.length; i++) { int pos = list[i].markerPos; if (pos > 0) { sql.getChars(start, pos, buf, bufferPtr); bufferPtr += (pos - start); start = pos + 1; // Append " @P" buf[bufferPtr++] = ' '; buf[bufferPtr++] = '@'; buf[bufferPtr++] = 'P'; // Append parameter number // Rather complicated, but it's the only way in which no // unnecessary objects are created number.setLength(0); number.append(i); number.getChars(0, number.length(), buf, bufferPtr); bufferPtr += number.length(); // Append " " buf[bufferPtr++] = ' '; } } if (start < sql.length()) { sql.getChars(start, sql.length(), buf, bufferPtr); bufferPtr += (sql.length() - start); } return new String(buf, 0, bufferPtr); } /** * Substitute actual data for the parameter markers to simulate * parameter substitution in a PreparedStatement. * * @param sql The SQL containing parameter markers to substitute. * @param list The parameter descriptors. * @return The modified SQL statement. */ static String substituteParameters(String sql, ParamInfo[] list, int tdsVersion) throws SQLException { int len = sql.length(); for (int i = 0; i < list.length; i++) { if (!list[i].isRetVal && !list[i].isSet && !list[i].isOutput) { throw new SQLException(Messages.get("error.prepare.paramnotset", Integer.toString(i+1)), "07000"); } 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; Support.embedData(buf, list[i].value, tdsVersion >= Driver.TDS70 && list[i].isUnicode, tdsVersion); } } 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 an the original cause exception to a SQL Exception. * <p>If running under VM 1.4+ the Exception.initCause() method * will be used to chain the exception. * Modeled after the code written by Brian Heineman. * * @param sqle The SQLException to enhance. * @param cause The child exception to link. * @return The enhanced <code>SQLException</code>. */ public static SQLException linkException(SQLException sqle, Throwable cause) { Class sqlExceptionClass = sqle.getClass(); Class[] parameterTypes = new Class[] {Throwable.class}; Object[] arguments = new Object[] {cause}; try { Method initCauseMethod = sqlExceptionClass.getMethod("initCause", parameterTypes); initCauseMethod.invoke(sqle, arguments); } catch (NoSuchMethodException e) { // Ignore; this method does not exist in older JVM's. if (Logger.isActive()) { Logger.logException((Exception) cause); // Best we can do } } catch (Exception e) { // Ignore all other exceptions, do not prevent the main exception // from being returned if reflection fails for any reason... } return sqle; } /** * 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; } } // ------------- 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. }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -