|
25 | 25 | import java.sql.Types;
|
26 | 26 | import java.util.Vector;
|
27 | 27 |
|
28 |
| -/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.26 2003/06/30 21:10:55 davec Exp $ |
| 28 | +/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.27 2003/07/09 05:12:04 barry Exp $ |
29 | 29 | * This class defines methods of the jdbc1 specification. This class is
|
30 | 30 | * extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
|
31 | 31 | * methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
|
@@ -1488,13 +1488,31 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
|
1488 | 1488 | setString(parameterIndex, x.toString());
|
1489 | 1489 | break;
|
1490 | 1490 | case Types.DATE:
|
1491 |
| - setDate(parameterIndex, (java.sql.Date)x); |
| 1491 | + if (x instanceof java.sql.Date) |
| 1492 | + setDate(parameterIndex, (java.sql.Date)x); |
| 1493 | + else |
| 1494 | + { |
| 1495 | + java.sql.Date tmpd = (x instanceof java.util.Date) ? new java.sql.Date(((java.util.Date)x).getTime()) : dateFromString(x.toString()); |
| 1496 | + setDate(parameterIndex, tmpd); |
| 1497 | + } |
1492 | 1498 | break;
|
1493 | 1499 | case Types.TIME:
|
1494 |
| - setTime(parameterIndex, (Time)x); |
| 1500 | + if (x instanceof java.sql.Time) |
| 1501 | + setTime(parameterIndex, (java.sql.Time)x); |
| 1502 | + else |
| 1503 | + { |
| 1504 | + java.sql.Time tmpt = (x instanceof java.util.Date) ? new java.sql.Time(((java.util.Date)x).getTime()) : timeFromString(x.toString()); |
| 1505 | + setTime(parameterIndex, tmpt); |
| 1506 | + } |
1495 | 1507 | break;
|
1496 | 1508 | case Types.TIMESTAMP:
|
1497 |
| - setTimestamp(parameterIndex, (Timestamp)x); |
| 1509 | + if (x instanceof java.sql.Timestamp) |
| 1510 | + setTimestamp(parameterIndex ,(java.sql.Timestamp)x); |
| 1511 | + else |
| 1512 | + { |
| 1513 | + java.sql.Timestamp tmpts = (x instanceof java.util.Date) ? new java.sql.Timestamp(((java.util.Date)x).getTime()) : timestampFromString(x.toString()); |
| 1514 | + setTimestamp(parameterIndex, tmpts); |
| 1515 | + } |
1498 | 1516 | break;
|
1499 | 1517 | case Types.BIT:
|
1500 | 1518 | if (x instanceof Boolean)
|
@@ -2032,7 +2050,113 @@ public boolean isUseServerPrepare()
|
2032 | 2050 | return m_useServerPrepare;
|
2033 | 2051 | }
|
2034 | 2052 |
|
2035 |
| - |
| 2053 | + private java.sql.Date dateFromString (String s) throws SQLException |
| 2054 | + { |
| 2055 | + int timezone = 0; |
| 2056 | + long millis = 0; |
| 2057 | + long localoffset = 0; |
| 2058 | + int timezoneLocation = (s.indexOf('+') == -1) ? s.lastIndexOf("-") : s.indexOf('+'); |
| 2059 | + //if the last index of '-' or '+' is past 8. we are guaranteed that it is a timezone marker |
| 2060 | + //shortest = yyyy-m-d |
| 2061 | + //longest = yyyy-mm-dd |
| 2062 | + try |
| 2063 | + { |
| 2064 | + timezone = (timezoneLocation>7) ? timezoneLocation : s.length(); |
| 2065 | + millis = java.sql.Date.valueOf(s.substring(0,timezone)).getTime(); |
| 2066 | + } |
| 2067 | + catch (Exception e) |
| 2068 | + { |
| 2069 | + throw new PSQLException("postgresql.format.baddate",s , "yyyy-MM-dd[-tz]"); |
| 2070 | + } |
| 2071 | + timezone = 0; |
| 2072 | + if (timezoneLocation>7 && timezoneLocation+3 == s.length()) |
| 2073 | + { |
| 2074 | + timezone = Integer.parseInt(s.substring(timezoneLocation+1,s.length())); |
| 2075 | + localoffset = java.util.Calendar.getInstance().getTimeZone().getOffset(millis); |
| 2076 | + if (s.charAt(timezoneLocation)=='+') |
| 2077 | + timezone*=-1; |
| 2078 | + } |
| 2079 | + millis = millis + timezone*60*60*1000 + localoffset; |
| 2080 | + return new java.sql.Date(millis); |
| 2081 | + } |
| 2082 | + |
| 2083 | + private java.sql.Time timeFromString (String s) throws SQLException |
| 2084 | + { |
| 2085 | + int timezone = 0; |
| 2086 | + long millis = 0; |
| 2087 | + long localoffset = 0; |
| 2088 | + int timezoneLocation = (s.indexOf('+') == -1) ? s.lastIndexOf("-") : s.indexOf('+'); |
| 2089 | + //if the index of the last '-' or '+' is greater than 0 that means this time has a timezone. |
| 2090 | + //everything earlier than that position, we treat as the time and parse it as such. |
| 2091 | + try |
| 2092 | + { |
| 2093 | + timezone = (timezoneLocation==-1) ? s.length() : timezoneLocation; |
| 2094 | + millis = java.sql.Time.valueOf(s.substring(0,timezone)).getTime(); |
| 2095 | + } |
| 2096 | + catch (Exception e) |
| 2097 | + { |
| 2098 | + throw new PSQLException("postgresql.format.badtime",s, "HH:mm:ss[-tz]"); |
| 2099 | + } |
| 2100 | + timezone = 0; |
| 2101 | + if (timezoneLocation != -1 && timezoneLocation+3 == s.length()) |
| 2102 | + { |
| 2103 | + timezone = Integer.parseInt(s.substring(timezoneLocation+1,s.length())); |
| 2104 | + localoffset = java.util.Calendar.getInstance().getTimeZone().getOffset(millis); |
| 2105 | + if (s.charAt(timezoneLocation)=='+') |
| 2106 | + timezone*=-1; |
| 2107 | + } |
| 2108 | + millis = millis + timezone*60*60*1000 + localoffset; |
| 2109 | + return new java.sql.Time(millis); |
| 2110 | + } |
| 2111 | + |
| 2112 | + private java.sql.Timestamp timestampFromString (String s) throws SQLException |
| 2113 | + { |
| 2114 | + int timezone = 0; |
| 2115 | + long millis = 0; |
| 2116 | + long localoffset = 0; |
| 2117 | + int nanosVal = 0; |
| 2118 | + int timezoneLocation = (s.indexOf('+') == -1) ? s.lastIndexOf("-") : s.indexOf('+'); |
| 2119 | + int nanospos = s.indexOf("."); |
| 2120 | + //if there is a '.', that means there are nanos info, and we take the timestamp up to that point |
| 2121 | + //if not, then we check to see if the last +/- (to indicate a timezone) is greater than 8 |
| 2122 | + //8 is because the shortest date, will have last '-' at position 7. e.g yyyy-x-x |
| 2123 | + try |
| 2124 | + { |
| 2125 | + if (nanospos != -1) |
| 2126 | + timezone = nanospos; |
| 2127 | + else if (timezoneLocation > 8) |
| 2128 | + timezone = timezoneLocation; |
| 2129 | + else |
| 2130 | + timezone = s.length(); |
| 2131 | + millis = java.sql.Timestamp.valueOf(s.substring(0,timezone)).getTime(); |
| 2132 | + } |
| 2133 | + catch (Exception e) |
| 2134 | + { |
| 2135 | + throw new PSQLException("postgresql.format.badtimestamp", s, "yyyy-MM-dd HH:mm:ss[.xxxxxx][-tz]"); |
| 2136 | + } |
| 2137 | + timezone = 0; |
| 2138 | + if (nanospos != -1) |
| 2139 | + { |
| 2140 | + int tmploc = (timezoneLocation > 8) ? timezoneLocation : s.length(); |
| 2141 | + nanosVal = Integer.parseInt(s.substring(nanospos+1,tmploc)); |
| 2142 | + int diff = 8-((tmploc-1)-(nanospos+1)); |
| 2143 | + for (int i=0;i<diff;i++) |
| 2144 | + nanosVal*=10; |
| 2145 | + } |
| 2146 | + if (timezoneLocation>8 && timezoneLocation+3 == s.length()) |
| 2147 | + { |
| 2148 | + timezone = Integer.parseInt(s.substring(timezoneLocation+1,s.length())); |
| 2149 | + localoffset = java.util.Calendar.getInstance().getTimeZone().getOffset(millis); |
| 2150 | + if (s.charAt(timezoneLocation)=='+') |
| 2151 | + timezone*=-1; |
| 2152 | + } |
| 2153 | + millis = millis + timezone*60*60*1000 + localoffset; |
| 2154 | + java.sql.Timestamp tmpts = new java.sql.Timestamp(millis); |
| 2155 | + tmpts.setNanos(nanosVal); |
| 2156 | + return tmpts; |
| 2157 | + } |
| 2158 | + |
| 2159 | + |
2036 | 2160 | private static final String PG_TEXT = "text";
|
2037 | 2161 | private static final String PG_INTEGER = "integer";
|
2038 | 2162 | private static final String PG_INT2 = "int2";
|
|
0 commit comments