abstractjdbc1resultset.java
来自「PostgreSQL7.4.6 for Linux」· Java 代码 · 共 1,260 行 · 第 1/3 页
JAVA
1,260 行
if (Double.valueOf(s).doubleValue()==1) return true; } catch (NumberFormatException e) { } } return false; // SQL NULL } public static int toInt(String s) throws SQLException { if (s != null) { try { s = s.trim(); return Integer.parseInt(s); } catch (NumberFormatException e) { throw new PSQLException ("postgresql.res.badint", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL } public static long toLong(String s) throws SQLException { if (s != null) { try { s = s.trim(); return Long.parseLong(s); } catch (NumberFormatException e) { throw new PSQLException ("postgresql.res.badlong", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL } public static BigDecimal toBigDecimal(String s, int scale) throws SQLException { BigDecimal val; if (s != null) { try { s = s.trim(); val = new BigDecimal(s); } catch (NumberFormatException e) { throw new PSQLException ("postgresql.res.badbigdec", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } if (scale == -1) return val; try { return val.setScale(scale); } catch (ArithmeticException e) { throw new PSQLException ("postgresql.res.badbigdec", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return null; // SQL NULL } public static float toFloat(String s) throws SQLException { if (s != null) { try { s = s.trim(); return Float.valueOf(s).floatValue(); } catch (NumberFormatException e) { throw new PSQLException ("postgresql.res.badfloat", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL } public static double toDouble(String s) throws SQLException { if (s != null) { try { s = s.trim(); return Double.valueOf(s).doubleValue(); } catch (NumberFormatException e) { throw new PSQLException ("postgresql.res.baddouble", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL } public static java.sql.Date toDate(String s) throws SQLException { if (s == null) return null; // length == 10: SQL Date // length > 10: SQL Timestamp, assumes PGDATESTYLE=ISO try { s = s.trim(); return java.sql.Date.valueOf((s.length() == 10) ? s : s.substring(0, 10)); } catch (NumberFormatException e) { throw new PSQLException("postgresql.res.baddate",PSQLState.BAD_DATETIME_FORMAT, s); } } public static Time toTime(String s, BaseResultSet resultSet, String pgDataType) throws SQLException { if (s == null) return null; // SQL NULL try { s = s.trim(); if (s.length() == 8) { //value is a time value return java.sql.Time.valueOf(s); } else if (s.indexOf(".") == 8) { //value is a time value with fractional seconds java.sql.Time l_time = java.sql.Time.valueOf(s.substring(0, 8)); String l_strMillis = s.substring(9); if (l_strMillis.length() > 3) l_strMillis = l_strMillis.substring(0, 3); int l_millis = Integer.parseInt(l_strMillis); if (l_millis < 10) { l_millis = l_millis * 100; } else if (l_millis < 100) { l_millis = l_millis * 10; } return new java.sql.Time(l_time.getTime() + l_millis); } else { //value is a timestamp return new java.sql.Time(toTimestamp(s, resultSet, pgDataType).getTime()); } } catch (NumberFormatException e) { throw new PSQLException("postgresql.res.badtime", PSQLState.BAD_DATETIME_FORMAT, s); } } /** * Parse a string and return a timestamp representing its value. * * The driver is set to return ISO date formated strings. We modify this * string from the ISO format to a format that Java can understand. Java * expects timezone info as 'GMT+09:00' where as ISO gives '+09'. * Java also expects fractional seconds to 3 places where postgres * will give, none, 2 or 6 depending on the time and postgres version. * From version 7.2 postgres returns fractional seconds to 6 places. * * According to the Timestamp documentation, fractional digits are kept * in the nanos field of Timestamp and not in the milliseconds of Date. * Thus, parsing for fractional digits is entirely separated from the * rest. * * The method assumes that there are no more than 9 fractional * digits given. Undefined behavior if this is not the case. * * @param s The ISO formated date string to parse. * @param resultSet The ResultSet this date is part of. * * @return null if s is null or a timestamp of the parsed string s. * * @throws SQLException if there is a problem parsing s. **/ public static Timestamp toTimestamp(String s, BaseResultSet resultSet, String pgDataType) throws SQLException { BaseResultSet rs = resultSet; if (s == null) return null; s = s.trim(); // We must be synchronized here incase more theads access the ResultSet // bad practice but possible. Anyhow this is to protect sbuf and // SimpleDateFormat objects synchronized (rs) { StringBuffer l_sbuf = rs.getStringBuffer(); SimpleDateFormat df = null; if ( Driver.logDebug ) Driver.debug("the data from the DB is " + s); // If first time, create the buffer, otherwise clear it. if (l_sbuf == null) l_sbuf = new StringBuffer(32); else { l_sbuf.setLength(0); } // Copy s into sbuf for parsing. l_sbuf.append(s); int slen = s.length(); // For a Timestamp, the fractional seconds are stored in the // nanos field. As a DateFormat is used for parsing which can // only parse to millisecond precision and which returns a // Date object, the fractional second parsing is completely // separate. int nanos = 0; if (slen > 19) { // The len of the ISO string to the second value is 19 chars. If // greater then 19, there may be tz info and perhaps fractional // second info which we need to change to java to read it. // cut the copy to second value "2001-12-07 16:29:22" int i = 19; l_sbuf.setLength(i); char c = s.charAt(i++); if (c == '.') { // Found a fractional value. final int start = i; while (true) { c = s.charAt(i++); if (!Character.isDigit(c)) break; if (i == slen) { i++; break; } } // The range [start, i - 1) contains all fractional digits. final int end = i - 1; try { nanos = Integer.parseInt(s.substring(start, end)); } catch (NumberFormatException e) { throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR, e); } // The nanos field stores nanoseconds. Adjust the parsed // value to the correct magnitude. for (int digitsToNano = 9 - (end - start); digitsToNano > 0; --digitsToNano) nanos *= 10; } if (i < slen) { // prepend the GMT part and then add the remaining bit of // the string. l_sbuf.append(" GMT"); l_sbuf.append(c); l_sbuf.append(s.substring(i, slen)); // Lastly, if the tz part doesn't specify the :MM part then // we add ":00" for java. if (slen - i < 5) l_sbuf.append(":00"); // we'll use this dateformat string to parse the result. df = rs.getTimestampTZFormat(); } else { // Just found fractional seconds but no timezone. //If timestamptz then we use GMT, else local timezone if (pgDataType.equals("timestamptz")) { l_sbuf.append(" GMT"); df = rs.getTimestampTZFormat(); } else { df = rs.getTimestampFormat(); } } } else if (slen == 19) { // No tz or fractional second info. //If timestamptz then we use GMT, else local timezone if (pgDataType.equals("timestamptz")) { l_sbuf.append(" GMT"); df = rs.getTimestampTZFormat(); } else { df = rs.getTimestampFormat(); } } else { if (slen == 8 && s.equals("infinity")) //java doesn't have a concept of postgres's infinity //so set to an arbitrary future date s = "9999-01-01"; if (slen == 9 && s.equals("-infinity")) //java doesn't have a concept of postgres's infinity //so set to an arbitrary old date s = "0001-01-01"; // We must just have a date. This case is // needed if this method is called on a date // column df = rs.getDateFormat(); } try { // All that's left is to parse the string and return the ts. if ( Driver.logDebug ) Driver.debug("the data after parsing is " + l_sbuf.toString() + " with " + nanos + " nanos"); Timestamp result = new Timestamp(df.parse(l_sbuf.toString()).getTime()); result.setNanos(nanos); return result; } catch (ParseException e) { throw new PSQLException("postgresql.res.badtimestamp", PSQLState.BAD_DATETIME_FORMAT, new Integer(e.getErrorOffset()), s); } } } private boolean isColumnTrimmable(int columnIndex) throws SQLException { switch (fields[columnIndex-1].getSQLType()) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: return true; } return false; } private byte[] trimBytes(int p_columnIndex, byte[] p_bytes) throws SQLException { int l_maxSize = statement.getMaxFieldSize(); //we need to trim if maxsize is set and the length is greater than maxsize and the //type of this column is a candidate for trimming if (l_maxSize > 0 && p_bytes.length > l_maxSize && isColumnTrimmable(p_columnIndex)) { byte[] l_bytes = new byte[l_maxSize]; System.arraycopy (p_bytes, 0, l_bytes, 0, l_maxSize); return l_bytes; } else { return p_bytes; } } private String trimString(int p_columnIndex, String p_string) throws SQLException { int l_maxSize = statement.getMaxFieldSize(); //we need to trim if maxsize is set and the length is greater than maxsize and the //type of this column is a candidate for trimming if (l_maxSize > 0 && p_string.length() > l_maxSize && isColumnTrimmable(p_columnIndex)) { return p_string.substring(0,l_maxSize); } else { return p_string; } } public SimpleDateFormat getTimestampTZFormat() { if (m_tstzFormat == null) { m_tstzFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); } return m_tstzFormat; } public SimpleDateFormat getTimestampFormat() { if (m_tsFormat == null) { m_tsFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); } return m_tsFormat; } public SimpleDateFormat getDateFormat() { if (m_dateFormat == null) { m_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); } return m_dateFormat; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?