Skip to content

Commit 6896bfa

Browse files
author
Barry Lind
committed
Applied patch from Kim Ho @ redhat.com to make support for setObject() more
spec complient with regards to various data/time/timestamp objects Modified Files: jdbc/org/postgresql/errors.properties jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
1 parent f207718 commit 6896bfa

File tree

2 files changed

+132
-5
lines changed

2 files changed

+132
-5
lines changed

src/interfaces/jdbc/org/postgresql/errors.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,6 @@ postgresql.call.wrongrtntype:A CallableStatement Function was executed and the r
100100
postgresql.input.fetch.gt0:Fetch size must be a value greater than or equal to 0.
101101
postgresql.input.query.gt0:Query Timeout must be a value greater than or equal to 0.
102102
postgresql.input.rows.gt0:Maximum number of rows must be a value greater than or equal to 0.
103+
postgresql.format.baddate:The date given: {0} does not match the format required: {1}.
104+
postgresql.format.badtime:The time given: {0} does not match the format required: {1}.
105+
postgresql.format.badtimestamp:The timestamp given {0} does not match the format required: {1}.

src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java

Lines changed: 129 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.sql.Types;
2626
import java.util.Vector;
2727

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 $
2929
* This class defines methods of the jdbc1 specification. This class is
3030
* extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
3131
* 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
14881488
setString(parameterIndex, x.toString());
14891489
break;
14901490
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+
}
14921498
break;
14931499
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+
}
14951507
break;
14961508
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+
}
14981516
break;
14991517
case Types.BIT:
15001518
if (x instanceof Boolean)
@@ -2032,7 +2050,113 @@ public boolean isUseServerPrepare()
20322050
return m_useServerPrepare;
20332051
}
20342052

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+
20362160
private static final String PG_TEXT = "text";
20372161
private static final String PG_INTEGER = "integer";
20382162
private static final String PG_INT2 = "int2";

0 commit comments

Comments
 (0)