1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir package complex.connectivity.dbase; 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 30*cdf0e10cSrcweir import com.sun.star.sdbc.*; 31*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 34*cdf0e10cSrcweir import complex.connectivity.TestCase; 35*cdf0e10cSrcweir import complex.connectivity.SubTestCase; 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir public class DBaseDateFunctions extends SubTestCase 38*cdf0e10cSrcweir { 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir private final String where = "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'"; 41*cdf0e10cSrcweir private final XMultiServiceFactory m_xORB; 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir public DBaseDateFunctions(final XMultiServiceFactory _xORB, final TestCase i_testCase) 44*cdf0e10cSrcweir { 45*cdf0e10cSrcweir super( i_testCase ); 46*cdf0e10cSrcweir m_xORB = _xORB; 47*cdf0e10cSrcweir } 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir public void testFunctions() throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir final XRowSet xRowRes = (XRowSet) UnoRuntime.queryInterface(XRowSet.class, 52*cdf0e10cSrcweir m_xORB.createInstance("com.sun.star.sdb.RowSet")); 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir getLog().println("starting DateTime function test!"); 55*cdf0e10cSrcweir // set the properties needed to connect to a database 56*cdf0e10cSrcweir final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes); 57*cdf0e10cSrcweir xProp.setPropertyValue("DataSourceName", "Bibliography"); 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir xProp.setPropertyValue("CommandType", Integer.valueOf(com.sun.star.sdb.CommandType.COMMAND)); 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir try 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir curdate(xRowRes); 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir catch (SQLException ex) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir assure("upper " + ex.getMessage(), false); 68*cdf0e10cSrcweir throw ex; 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir try 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir curtime(xRowRes); 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir catch (SQLException ex) 75*cdf0e10cSrcweir { 76*cdf0e10cSrcweir assure("lower " + ex.getMessage(), false); 77*cdf0e10cSrcweir throw ex; 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir try 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir dayname(xRowRes); 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir catch (SQLException ex) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir assure("ascii " + ex.getMessage(), false); 86*cdf0e10cSrcweir throw ex; 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir try 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir dayofmonth(xRowRes); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir catch (SQLException ex) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir assure("char_len " + ex.getMessage(), false); 95*cdf0e10cSrcweir throw ex; 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir try 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir dayofweek(xRowRes); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir catch (SQLException ex) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir assure("concat " + ex.getMessage(), false); 104*cdf0e10cSrcweir throw ex; 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir try 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir dayofyear(xRowRes); 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir catch (SQLException ex) 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir assure("locate " + ex.getMessage(), false); 113*cdf0e10cSrcweir throw ex; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir try 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir hour(xRowRes); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir catch (SQLException ex) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir assure("substr " + ex.getMessage(), false); 122*cdf0e10cSrcweir throw ex; 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir try 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir minute(xRowRes); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir catch (SQLException ex) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir assure("ltrim " + ex.getMessage(), false); 131*cdf0e10cSrcweir throw ex; 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir try 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir month(xRowRes); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir catch (SQLException ex) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir assure("rtrim " + ex.getMessage(), false); 140*cdf0e10cSrcweir throw ex; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir try 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir monthname(xRowRes); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir catch (SQLException ex) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir assure("space " + ex.getMessage(), false); 149*cdf0e10cSrcweir throw ex; 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir try 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir now(xRowRes); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir catch (SQLException ex) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir assure("replace " + ex.getMessage(), false); 158*cdf0e10cSrcweir throw ex; 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir try 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir quarter(xRowRes); 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir catch (SQLException ex) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir assure("repeat " + ex.getMessage(), false); 167*cdf0e10cSrcweir throw ex; 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir try 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir second(xRowRes); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir catch (SQLException ex) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir assure("insert " + ex.getMessage(), false); 176*cdf0e10cSrcweir throw ex; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir try 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir week(xRowRes); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir catch (SQLException ex) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir assure("left " + ex.getMessage(), false); 185*cdf0e10cSrcweir throw ex; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir try 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir year(xRowRes); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir catch (SQLException ex) 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir assure("right " + ex.getMessage(), false); 194*cdf0e10cSrcweir throw ex; 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir private XRow execute(final XRowSet xRowRes, final String sql) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes); 201*cdf0e10cSrcweir xProp.setPropertyValue("Command", "SELECT " + sql + where); 202*cdf0e10cSrcweir xRowRes.execute(); 203*cdf0e10cSrcweir final XResultSet xRes = (XResultSet) UnoRuntime.queryInterface(XResultSet.class, xRowRes); 204*cdf0e10cSrcweir assure("No valid row! ", xRes.next()); 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir return (XRow) UnoRuntime.queryInterface(XRow.class, xRes); 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir private void dayofweek(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir final XRow row = execute(xRowRes, "DAYOFWEEK('1998-02-03') "); 212*cdf0e10cSrcweir assure("DAYOFWEEK('1998-02-03') failed!", row.getInt(1) == 3); 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir private void dayofmonth(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir final XRow row = execute(xRowRes, "DAYOFMONTH('1998-02-03') "); 218*cdf0e10cSrcweir assure("DAYOFMONTH('1998-02-03') failed!", row.getInt(1) == 3); 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir private void dayofyear(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 222*cdf0e10cSrcweir { 223*cdf0e10cSrcweir final XRow row = execute(xRowRes, "DAYOFYEAR('1998-02-03') "); 224*cdf0e10cSrcweir assure("DAYOFYEAR('1998-02-03') failed!", row.getInt(1) == 34); 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir private void month(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir final XRow row = execute(xRowRes, "month('1998-02-03') "); 230*cdf0e10cSrcweir assure("month('1998-02-03') failed!", row.getInt(1) == 2); 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir private void dayname(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir final XRow row = execute(xRowRes, "DAYNAME('1998-02-05') "); 236*cdf0e10cSrcweir assure("DAYNAME('1998-02-05') failed!", row.getString(1).equals("Thursday")); 237*cdf0e10cSrcweir } 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir private void monthname(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir final XRow row = execute(xRowRes, "MONTHNAME('1998-02-05') "); 242*cdf0e10cSrcweir assure("MONTHNAME('1998-02-05') failed!", row.getString(1).equals("February")); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir private void quarter(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 246*cdf0e10cSrcweir { 247*cdf0e10cSrcweir final XRow row = execute(xRowRes, "QUARTER('98-01-01'),QUARTER('98-04-01'),QUARTER('98-07-01'),QUARTER('98-10-01') "); 248*cdf0e10cSrcweir assure("QUARTER('98-01-01') failed!", row.getInt(1) == 1); 249*cdf0e10cSrcweir assure("QUARTER('98-04-01') failed!", row.getInt(2) == 2); 250*cdf0e10cSrcweir assure("QUARTER('98-07-01') failed!", row.getInt(3) == 3); 251*cdf0e10cSrcweir assure("QUARTER('98-10-01') failed!", row.getInt(4) == 4); 252*cdf0e10cSrcweir } 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir private void week(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 255*cdf0e10cSrcweir { 256*cdf0e10cSrcweir final XRow row = execute(xRowRes, "WEEK('1998-02-20') "); 257*cdf0e10cSrcweir assure("WEEK('1998-02-20') failed!", row.getInt(1) == 7); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir private void year(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir final XRow row = execute(xRowRes, "YEAR('98-02-03') "); 263*cdf0e10cSrcweir assure("YEAR('98-02-03') failed!", row.getInt(1) == 98); 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir private void hour(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 267*cdf0e10cSrcweir { 268*cdf0e10cSrcweir final XRow row = execute(xRowRes, "HOUR('10:05:03') "); 269*cdf0e10cSrcweir assure("HOUR('10:05:03') failed!", row.getInt(1) == 10); 270*cdf0e10cSrcweir } 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir private void minute(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 273*cdf0e10cSrcweir { 274*cdf0e10cSrcweir final XRow row = execute(xRowRes, "MINUTE('98-02-03 10:05:03') "); 275*cdf0e10cSrcweir assure("MINUTE('98-02-03 10:05:03') failed!", row.getInt(1) == 5); 276*cdf0e10cSrcweir } 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir private void second(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir final XRow row = execute(xRowRes, "SECOND('10:05:03') "); 281*cdf0e10cSrcweir assure("SECOND('10:05:03') failed!", row.getInt(1) == 3); 282*cdf0e10cSrcweir } 283*cdf0e10cSrcweir 284*cdf0e10cSrcweir private void curdate(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir final XRow row = execute(xRowRes, "CURDATE() "); 287*cdf0e10cSrcweir final com.sun.star.util.Date aDate = row.getDate(1); 288*cdf0e10cSrcweir getLog().println("CURDATE() is '" + aDate.Year + "-" + aDate.Month + "-" + aDate.Day + "'"); 289*cdf0e10cSrcweir } 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir private void curtime(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir final XRow row = execute(xRowRes, "CURTIME() "); 294*cdf0e10cSrcweir final com.sun.star.util.Time aTime = row.getTime(1); 295*cdf0e10cSrcweir getLog().println("CURTIME() is '" + aTime.Hours + ":" + aTime.Minutes + ":" + aTime.Seconds + "'"); 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir private void now(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 299*cdf0e10cSrcweir { 300*cdf0e10cSrcweir final XRow row = execute(xRowRes, "NOW() "); 301*cdf0e10cSrcweir final com.sun.star.util.DateTime aTime = row.getTimestamp(1); 302*cdf0e10cSrcweir getLog().println("NOW() is '" + aTime.Year + "-" + aTime.Month + "-" + aTime.Day + "'"); 303*cdf0e10cSrcweir getLog().println("'" + aTime.Hours + ":" + aTime.Minutes + ":" + aTime.Seconds + "'"); 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir } 306