X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id DB6659DC813 for ; Fri, 24 Mar 2006 08:03:15 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 90542-07 for ; Fri, 24 Mar 2006 08:03:17 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.207]) by postgresql.org (Postfix) with ESMTP id E39749DC804 for ; Fri, 24 Mar 2006 08:03:12 -0400 (AST) Received: by xproxy.gmail.com with SMTP id h31so533155wxd for ; Fri, 24 Mar 2006 04:03:16 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:user-agent:x-accept-language:mime-version:to:cc:subject:references:in-reply-to:x-enigmail-version:content-type:sender; b=VbjHKqHvrtliLQf+ZWF8tS/7srFFAemZZZtIbZxDjRj4dHDGihvzXrIJrfjH+i0wyrTTYjES66qrCLcyhKvnQUi4XC18x9yHAqW8xfMRXQJDdrYmR9URYS2fprOJ/3BLRPMGx6dYDD1CCpqtNbRuMqmPDr64FzAucBJUXuMoieQ= Received: by 10.70.11.12 with SMTP id 12mr969323wxk; Fri, 24 Mar 2006 04:03:16 -0800 (PST) Received: from ?192.168.0.128? ( [80.65.224.34]) by mx.gmail.com with ESMTP id h15sm1961092wxd.2006.03.24.04.03.14; Fri, 24 Mar 2006 04:03:15 -0800 (PST) Message-ID: <4423DFFF.1010204@free.fr> Date: Fri, 24 Mar 2006 13:03:11 +0100 From: "Xavier Poinsard" User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051013) X-Accept-Language: fr, en MIME-Version: 1.0 To: Dave Cramer CC: "pgsql-jdbc@postgresql.org" Subject: Re: Patch implementing escaped functions timestampadd and References: <44210CFF.6030802@free.fr> <3CA53C9F-D12A-4D66-824A-7F4015EAFED2@fastcrypt.com> <44216544.7080309@free.fr> <099223BB-A5F9-4A5F-AAEB-B9C1F1561101@fastcrypt.com> <44216B34.3000607@free.fr> <44217697.1060303@free.fr> <9797E783-BD6C-4691-A726-BD58D3A285A3@fastcrypt.com> <44217A75.3040808@free.fr> <4422B938.4050303@free.fr> <3BEF8B79-8A3A-471A-AFF1-FE09B98EE6EC@fastcrypt.com> In-Reply-To: <3BEF8B79-8A3A-471A-AFF1-FE09B98EE6EC@fastcrypt.com> X-Enigmail-Version: 0.92.1.0 Content-Type: multipart/mixed; boundary="------------050509030801040201000408" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.103 required=5 tests=[AWL=0.103] X-Spam-Score: 0.103 X-Spam-Level: X-Archive-Number: 200603/100 X-Sequence-Number: 14881 This is a multi-part message in MIME format. --------------050509030801040201000408 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Here are the updated patchs. Dave Cramer a écrit : > Xavier, > > So at this point do you want to remove some of the less correct escapes ? > --------------050509030801040201000408 Content-Type: text/plain; name="timestampfunc4.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="timestampfunc4.diff" Index: jdbc2/AbstractJdbc2DatabaseMetaData.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java,v retrieving revision 1.29 diff -c -r1.29 AbstractJdbc2DatabaseMetaData.java *** jdbc2/AbstractJdbc2DatabaseMetaData.java 3 Feb 2006 21:10:15 -0000 1.29 --- jdbc2/AbstractJdbc2DatabaseMetaData.java 24 Mar 2006 12:00:11 -0000 *************** *** 477,483 **** ','+EscapedFunctions.MONTH+ ','+EscapedFunctions.MONTHNAME+','+EscapedFunctions.NOW+ ','+EscapedFunctions.QUARTER+','+EscapedFunctions.SECOND+ ! ','+EscapedFunctions.WEEK+','+EscapedFunctions.YEAR; } /* --- 477,484 ---- ','+EscapedFunctions.MONTH+ ','+EscapedFunctions.MONTHNAME+','+EscapedFunctions.NOW+ ','+EscapedFunctions.QUARTER+','+EscapedFunctions.SECOND+ ! ','+EscapedFunctions.WEEK+','+EscapedFunctions.YEAR+ ! ','+EscapedFunctions.TIMESTAMPADD; //+','+EscapedFunctions.TIMESTAMPDIFF; } /* Index: jdbc2/EscapedFunctions.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/EscapedFunctions.java,v retrieving revision 1.6 diff -c -r1.6 EscapedFunctions.java *** jdbc2/EscapedFunctions.java 4 Jun 2005 18:24:08 -0000 1.6 --- jdbc2/EscapedFunctions.java 24 Mar 2006 12:00:11 -0000 *************** *** 90,97 **** public final static String SECOND="second"; public final static String WEEK="week"; public final static String YEAR="year"; ! // TODO : timestampadd and timestampdiff // system functions public final static String DATABASE="database"; public final static String IFNULL="ifnull"; --- 90,114 ---- public final static String SECOND="second"; public final static String WEEK="week"; public final static String YEAR="year"; ! // for timestampadd and timestampdiff the fractional part of second is not supported ! // by the backend ! // timestampdiff is very partially supported ! public final static String TIMESTAMPADD="timestampadd"; ! public final static String TIMESTAMPDIFF="timestampdiff"; ! ! // constants for timestampadd and timestampdiff ! public final static String SQL_TSI_ROOT="SQL_TSI_"; ! public final static String SQL_TSI_DAY="DAY"; ! public final static String SQL_TSI_FRAC_SECOND="FRAC_SECOND"; ! public final static String SQL_TSI_HOUR="HOUR"; ! public final static String SQL_TSI_MINUTE="MINUTE"; ! public final static String SQL_TSI_MONTH="MONTH"; ! public final static String SQL_TSI_QUARTER="QUARTER"; ! public final static String SQL_TSI_SECOND="SECOND"; ! public final static String SQL_TSI_WEEK="WEEK"; ! public final static String SQL_TSI_YEAR="YEAR"; + // system functions public final static String DATABASE="database"; public final static String IFNULL="ifnull"; *************** *** 478,483 **** --- 495,583 ---- return "extract(year from "+parsedArgs.get(0)+")"; } + /** time stamp add */ + public static String sqltimestampadd(List parsedArgs) throws SQLException{ + if (parsedArgs.size()!=3){ + throw new PSQLException(GT.tr("{0} function takes three and only three arguments.","timestampadd"), + PSQLState.SYNTAX_ERROR); + } + 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(")"); + 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), + 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"), + PSQLState.SYNTAX_ERROR); + else throw new PSQLException(GT.tr("Interval {0} not yet implemented",type), + PSQLState.SYNTAX_ERROR); + } + + + /** time stamp diff */ + public static String sqltimestampdiff(List parsedArgs) throws SQLException{ + if (parsedArgs.size()!=3){ + throw new PSQLException(GT.tr("{0} function takes three and only three arguments.","timestampdiff"), + PSQLState.SYNTAX_ERROR); + } + String datePart = EscapedFunctions.constantToDatePart(parsedArgs.get(0).toString()); + StringBuffer buf = new StringBuffer(); + buf.append("extract( ").append(datePart) + .append(" from (").append(parsedArgs.get(2)).append("-").append(parsedArgs.get(1)).append("))"); + return buf.toString(); + } + + 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), + 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"), + PSQLState.SYNTAX_ERROR); + else throw new PSQLException(GT.tr("Interval {0} not yet implemented",type), + PSQLState.SYNTAX_ERROR); + } + /** database translation */ public static String sqldatabase(List parsedArgs) throws SQLException{ if (parsedArgs.size()!=0){ Index: test/jdbc2/StatementTest.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/StatementTest.java,v retrieving revision 1.19 diff -c -r1.19 StatementTest.java *** test/jdbc2/StatementTest.java 1 Feb 2006 18:52:13 -0000 1.19 --- test/jdbc2/StatementTest.java 24 Mar 2006 12:00:11 -0000 *************** *** 297,302 **** --- 297,334 ---- assertTrue(rs.next()); // ensure sunday =>1 and monday =>2 assertEquals(2,rs.getInt(5)); + // second + rs = stmt.executeQuery("select {fn timestampdiff(SQL_TSI_SECOND,{fn now()},{fn timestampadd(SQL_TSI_SECOND,3,{fn now()})})} "); + assertTrue(rs.next()); + assertEquals(3,rs.getInt(1)); + // MINUTE + rs = stmt.executeQuery("select {fn timestampdiff(SQL_TSI_MINUTE,{fn now()},{fn timestampadd(SQL_TSI_MINUTE,3,{fn now()})})} "); + assertTrue(rs.next()); + assertEquals(3,rs.getInt(1)); + // HOUR + rs = stmt.executeQuery("select {fn timestampdiff(SQL_TSI_HOUR,{fn now()},{fn timestampadd(SQL_TSI_HOUR,3,{fn now()})})} "); + assertTrue(rs.next()); + assertEquals(3,rs.getInt(1)); + // day + rs = stmt.executeQuery("select {fn timestampdiff(SQL_TSI_DAY,{fn now()},{fn timestampadd(SQL_TSI_DAY,-3,{fn now()})})} "); + assertTrue(rs.next()); + assertEquals(-3,rs.getInt(1)); + // WEEK => extract week from interval is not supported by backend + //rs = stmt.executeQuery("select {fn timestampdiff(SQL_TSI_WEEK,{fn now()},{fn timestampadd(SQL_TSI_WEEK,3,{fn now()})})} "); + //assertTrue(rs.next()); + //assertEquals(3,rs.getInt(1)); + // MONTH => backend assume there are 0 month in an interval of 92 days... + //rs = stmt.executeQuery("select {fn timestampdiff(SQL_TSI_MONTH,{fn now()},{fn timestampadd(SQL_TSI_MONTH,3,{fn now()})})} "); + //assertTrue(rs.next()); + //assertEquals(3,rs.getInt(1)); + // QUARTER => backend assume there are 1 quater even in 270 days... + //rs = stmt.executeQuery("select {fn timestampdiff(SQL_TSI_QUARTER,{fn now()},{fn timestampadd(SQL_TSI_QUARTER,3,{fn now()})})} "); + //assertTrue(rs.next()); + //assertEquals(3,rs.getInt(1)); + // YEAR + //rs = stmt.executeQuery("select {fn timestampdiff(SQL_TSI_YEAR,{fn now()},{fn timestampadd(SQL_TSI_YEAR,3,{fn now()})})} "); + //assertTrue(rs.next()); + //assertEquals(3,rs.getInt(1)); } public void testSystemFunctions() throws SQLException --------------050509030801040201000408 Content-Type: text/plain; name="timestampfuncdoc4.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="timestampfuncdoc4.diff" Index: pgjdbc.xml =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/doc/pgjdbc.xml,v retrieving revision 1.27 diff -c -r1.27 pgjdbc.xml *** pgjdbc.xml 24 Nov 2005 06:36:03 -0000 1.27 --- pgjdbc.xml 24 Mar 2006 12:00:39 -0000 *************** *** 2040,2045 **** --- 2040,2057 ---- extract(year from arg1) + + timestampadd(argIntervalType,argCount,argTimeStamp) + yes + ('(interval according to argIntervalType and argCount)'+argTimeStamp) + an argIntervalType value of SQL_TSI_FRAC_SECOND is not implemented since backend does not support it + + + timestampdiff(argIntervalType,argTimeStamp1,argTimeStamp2) + not + extract((interval according to argIntervalType) from argTimeStamp2-argTimeStamp1 ) + only an argIntervalType value of SQL_TSI_FRAC_SECOND,SQL_TSI_FRAC_MINUTE,SQL_TSI_FRAC_HOUR or SQL_TSI_FRAC_DAY is supported + --------------050509030801040201000408--