[libpostgresql-jdbc-java] 11/12: Support a parameter (?) for the amount part of the timestampadd function. Previously we only supported literals.
Emmanuel Bourg
ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:20:44 UTC 2017
This is an automated email from the git hooks/post-receive script.
ebourg-guest pushed a commit to tag REL8_3_604
in repository libpostgresql-jdbc-java.
commit 7a84f8a06ae0d892d699a1455bca6bf5103a00ee
Author: Kris Jurka <books at ejurka.com>
Date: Sun Nov 16 12:14:17 2008 +0000
Support a parameter (?) for the amount part of the timestampadd
function. Previously we only supported literals.
Christophe Janton
---
org/postgresql/jdbc2/EscapedFunctions.java | 95 ++++++++++++++--------------
org/postgresql/test/jdbc2/StatementTest.java | 20 +++++-
2 files changed, 66 insertions(+), 49 deletions(-)
diff --git a/org/postgresql/jdbc2/EscapedFunctions.java b/org/postgresql/jdbc2/EscapedFunctions.java
index 6cc91fd..2f65630 100644
--- a/org/postgresql/jdbc2/EscapedFunctions.java
+++ b/org/postgresql/jdbc2/EscapedFunctions.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/jdbc2/EscapedFunctions.java,v 1.10 2008/01/08 06:56:28 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/jdbc2/EscapedFunctions.java,v 1.10.2.1 2008/02/19 06:12:33 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -491,36 +491,36 @@ public class EscapedFunctions {
}
String interval = EscapedFunctions.constantToInterval(parsedArgs.get(0).toString(),parsedArgs.get(1).toString());
StringBuffer buf = new StringBuffer();
- buf.append("(interval ").append(interval)
- .append("+").append(parsedArgs.get(2)).append(")");
+ buf.append("(").append(interval).append("+");
+ buf.append(parsedArgs.get(2)).append(")");
return buf.toString();
}
private final static String constantToInterval(String type,String value)throws SQLException{
- if (!type.startsWith(SQL_TSI_ROOT))
- throw new PSQLException(GT.tr("Interval {0} not yet implemented",type),
+ if (!type.startsWith(SQL_TSI_ROOT))
+ throw new PSQLException(GT.tr("Interval {0} not yet implemented",type),
PSQLState.SYNTAX_ERROR);
- String shortType = type.substring(SQL_TSI_ROOT.length());
- if (SQL_TSI_DAY.equalsIgnoreCase(shortType))
- return "'"+value+" day'";
- else if (SQL_TSI_SECOND.equalsIgnoreCase(shortType))
- return "'"+value+" second'";
- else if (SQL_TSI_HOUR.equalsIgnoreCase(shortType))
- return "'"+value+" hour'";
- else if (SQL_TSI_MINUTE.equalsIgnoreCase(shortType))
- return "'"+value+" minute'";
- else if (SQL_TSI_MONTH.equalsIgnoreCase(shortType))
- return "'"+value+" month'";
- else if (SQL_TSI_QUARTER.equalsIgnoreCase(shortType))
- return "'"+ Integer.valueOf(value).intValue()*3+" month'";
- else if (SQL_TSI_WEEK.equalsIgnoreCase(shortType))
- return "'"+value+" week'";
- else if (SQL_TSI_YEAR.equalsIgnoreCase(shortType))
- return "'"+value+" year'";
- else if (SQL_TSI_FRAC_SECOND.equalsIgnoreCase(shortType))
- throw new PSQLException(GT.tr("Interval {0} not yet implemented","SQL_TSI_FRAC_SECOND"),
+ String shortType = type.substring(SQL_TSI_ROOT.length());
+ if (SQL_TSI_DAY.equalsIgnoreCase(shortType))
+ return "CAST(" + value + " || ' day' as interval)";
+ else if (SQL_TSI_SECOND.equalsIgnoreCase(shortType))
+ return "CAST(" + value + " || ' second' as interval)";
+ else if (SQL_TSI_HOUR.equalsIgnoreCase(shortType))
+ return "CAST(" + value + " || ' hour' as interval)";
+ else if (SQL_TSI_MINUTE.equalsIgnoreCase(shortType))
+ return "CAST(" + value + " || ' minute' as interval)";
+ else if (SQL_TSI_MONTH.equalsIgnoreCase(shortType))
+ return "CAST(" + value + " || ' month' as interval)";
+ else if (SQL_TSI_QUARTER.equalsIgnoreCase(shortType))
+ return "CAST((" + value + "::int * 3) || ' month' as interval)";
+ else if (SQL_TSI_WEEK.equalsIgnoreCase(shortType))
+ return "CAST(" + value + " || ' week' as interval)";
+ else if (SQL_TSI_YEAR.equalsIgnoreCase(shortType))
+ return "CAST(" + value + " || ' year' as interval)";
+ else if (SQL_TSI_FRAC_SECOND.equalsIgnoreCase(shortType))
+ throw new PSQLException(GT.tr("Interval {0} not yet implemented","SQL_TSI_FRAC_SECOND"),
PSQLState.SYNTAX_ERROR);
- else throw new PSQLException(GT.tr("Interval {0} not yet implemented",type),
+ else throw new PSQLException(GT.tr("Interval {0} not yet implemented",type),
PSQLState.SYNTAX_ERROR);
}
@@ -539,30 +539,31 @@ public class EscapedFunctions {
}
private final static String constantToDatePart(String type)throws SQLException{
- if (!type.startsWith(SQL_TSI_ROOT))
- throw new PSQLException(GT.tr("Interval {0} not yet implemented",type),
+ if (!type.startsWith(SQL_TSI_ROOT))
+ throw new PSQLException(GT.tr("Interval {0} not yet implemented",type),
PSQLState.SYNTAX_ERROR);
- String shortType = type.substring(SQL_TSI_ROOT.length());
- if (SQL_TSI_DAY.equalsIgnoreCase(shortType))
- return "day";
- else if (SQL_TSI_SECOND.equalsIgnoreCase(shortType))
- return "second";
- else if (SQL_TSI_HOUR.equalsIgnoreCase(shortType))
- return "hour";
- else if (SQL_TSI_MINUTE.equalsIgnoreCase(shortType))
- return "minute";
- /*else if (SQL_TSI_MONTH.equalsIgnoreCase(shortType))
- return "month";
- else if (SQL_TSI_QUARTER.equalsIgnoreCase(shortType))
- return "quarter";
- else if (SQL_TSI_WEEK.equalsIgnoreCase(shortType))
- return "week";
- else if (SQL_TSI_YEAR.equalsIgnoreCase(shortType))
- return "year";*/
- else if (SQL_TSI_FRAC_SECOND.equalsIgnoreCase(shortType))
- throw new PSQLException(GT.tr("Interval {0} not yet implemented","SQL_TSI_FRAC_SECOND"),
+ String shortType = type.substring(SQL_TSI_ROOT.length());
+ if (SQL_TSI_DAY.equalsIgnoreCase(shortType))
+ return "day";
+ else if (SQL_TSI_SECOND.equalsIgnoreCase(shortType))
+ return "second";
+ else if (SQL_TSI_HOUR.equalsIgnoreCase(shortType))
+ return "hour";
+ else if (SQL_TSI_MINUTE.equalsIgnoreCase(shortType))
+ return "minute";
+ // See http://archives.postgresql.org/pgsql-jdbc/2006-03/msg00096.php
+ /*else if (SQL_TSI_MONTH.equalsIgnoreCase(shortType))
+ return "month";
+ else if (SQL_TSI_QUARTER.equalsIgnoreCase(shortType))
+ return "quarter";
+ else if (SQL_TSI_WEEK.equalsIgnoreCase(shortType))
+ return "week";
+ else if (SQL_TSI_YEAR.equalsIgnoreCase(shortType))
+ return "year";*/
+ else if (SQL_TSI_FRAC_SECOND.equalsIgnoreCase(shortType))
+ throw new PSQLException(GT.tr("Interval {0} not yet implemented","SQL_TSI_FRAC_SECOND"),
PSQLState.SYNTAX_ERROR);
- else throw new PSQLException(GT.tr("Interval {0} not yet implemented",type),
+ else throw new PSQLException(GT.tr("Interval {0} not yet implemented",type),
PSQLState.SYNTAX_ERROR);
}
diff --git a/org/postgresql/test/jdbc2/StatementTest.java b/org/postgresql/test/jdbc2/StatementTest.java
index 9fe6647..bb12ef7 100644
--- a/org/postgresql/test/jdbc2/StatementTest.java
+++ b/org/postgresql/test/jdbc2/StatementTest.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/StatementTest.java,v 1.26 2007/07/27 10:15:36 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/StatementTest.java,v 1.27 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -286,6 +286,22 @@ public class StatementTest extends TestCase
assertEquals("bc",rs.getString(6));
assertEquals("ABCD",rs.getString(7));
}
+
+ public void testDateFuncWithParam() throws SQLException
+ {
+ // Prior to 8.0 there is not an interval + timestamp operator,
+ // so timestampadd does not work.
+ //
+ if (!TestUtil.haveMinimumServerVersion(con, "8.0"))
+ return;
+
+ PreparedStatement ps = con.prepareStatement("SELECT {fn timestampadd(SQL_TSI_QUARTER, ? ,{fn now()})}, {fn timestampadd(SQL_TSI_MONTH, ?, {fn now()})} ");
+ ps.setInt(1, 4);
+ ps.setInt(2, 12);
+ ResultSet rs = ps.executeQuery();
+ assertTrue(rs.next());
+ assertEquals(rs.getTimestamp(1), rs.getTimestamp(2));
+ }
public void testDateFunctions() throws SQLException
{
@@ -302,7 +318,7 @@ public class StatementTest extends TestCase
// ensure sunday =>1 and monday =>2
assertEquals(2,rs.getInt(5));
- // Prior to 8.0 there is not an interval + timestamp operator,
+ // Prior to 8.0 there is not an interval + timestamp operator,
// so timestampadd does not work.
//
if (!TestUtil.haveMinimumServerVersion(con, "8.0"))
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git
More information about the pkg-java-commits
mailing list