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 28*cdf0e10cSrcweir package util; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.uno.Exception; 31*cdf0e10cSrcweir import java.io.PrintWriter ; 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir // access the implementations via names 34*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 35*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 36*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 39*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 40*cdf0e10cSrcweir import com.sun.star.sdbc.XConnection ; 41*cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet ; 42*cdf0e10cSrcweir import com.sun.star.sdbc.XResultSetUpdate ; 43*cdf0e10cSrcweir import com.sun.star.sdbc.XStatement ; 44*cdf0e10cSrcweir import com.sun.star.sdbc.XRowUpdate ; 45*cdf0e10cSrcweir import com.sun.star.util.Date ; 46*cdf0e10cSrcweir import com.sun.star.uno.XNamingService ; 47*cdf0e10cSrcweir import com.sun.star.task.XInteractionHandler ; 48*cdf0e10cSrcweir import com.sun.star.sdb.XCompletedConnection ; 49*cdf0e10cSrcweir import com.sun.star.container.XEnumeration ; 50*cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess ; 51*cdf0e10cSrcweir import com.sun.star.io.XInputStream ; 52*cdf0e10cSrcweir import com.sun.star.io.XTextInputStream ; 53*cdf0e10cSrcweir import com.sun.star.io.XDataInputStream ; 54*cdf0e10cSrcweir import com.sun.star.container.XNameAccess ; 55*cdf0e10cSrcweir import com.sun.star.frame.XStorable; 56*cdf0e10cSrcweir import com.sun.star.sdb.XDocumentDataSource; 57*cdf0e10cSrcweir import com.sun.star.sdbc.XCloseable ; 58*cdf0e10cSrcweir import java.sql.Statement; 59*cdf0e10cSrcweir import java.sql.Connection; 60*cdf0e10cSrcweir import java.sql.DriverManager; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir /** 63*cdf0e10cSrcweir * Provides useful methods for working with SOffice databases. 64*cdf0e10cSrcweir * Database creation, data transfering, outputting infromation. 65*cdf0e10cSrcweir */ 66*cdf0e10cSrcweir public class DBTools { 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir private XMultiServiceFactory xMSF = null ; 69*cdf0e10cSrcweir private XNamingService dbContext = null ; 70*cdf0e10cSrcweir private PrintWriter m_log = null; 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir //JDBC driver 73*cdf0e10cSrcweir public final static String TST_JDBC_DRIVER = "org.gjt.mm.mysql.Driver"; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir // constants for TestDB table column indexes 76*cdf0e10cSrcweir public final static int TST_STRING = 1 ; 77*cdf0e10cSrcweir public final static int TST_INT = 2 ; 78*cdf0e10cSrcweir public final static int TST_DOUBLE = 5 ; 79*cdf0e10cSrcweir public final static int TST_DATE = 6 ; 80*cdf0e10cSrcweir public final static int TST_BOOLEAN = 10 ; 81*cdf0e10cSrcweir public final static int TST_CHARACTER_STREAM = 11 ; 82*cdf0e10cSrcweir public final static int TST_BINARY_STREAM = 12 ; 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir // constants for TestDB columns names 85*cdf0e10cSrcweir public final static String TST_STRING_F = "_TEXT" ; 86*cdf0e10cSrcweir public final static String TST_INT_F = "_INT" ; 87*cdf0e10cSrcweir public final static String TST_DOUBLE_F = "_DOUBLE" ; 88*cdf0e10cSrcweir public final static String TST_DATE_F = "_DATE" ; 89*cdf0e10cSrcweir public final static String TST_BOOLEAN_F = "_BOOL" ; 90*cdf0e10cSrcweir public final static String TST_CHARACTER_STREAM_F = "_MEMO1" ; 91*cdf0e10cSrcweir public final static String TST_BINARY_STREAM_F = "_MEMO2" ; 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir /** 94*cdf0e10cSrcweir * Values for filling test table. 95*cdf0e10cSrcweir */ 96*cdf0e10cSrcweir public final static Object[][] TST_TABLE_VALUES = new Object[][] { 97*cdf0e10cSrcweir {"String1", new Integer(1), null, null, new Double(1.1), 98*cdf0e10cSrcweir new Date((short) 1,(short) 1, (short) 2001), null, null, null, 99*cdf0e10cSrcweir Boolean.TRUE, null, null}, 100*cdf0e10cSrcweir {"String2", new Integer(2), null, null, new Double(1.2), 101*cdf0e10cSrcweir new Date((short) 2, (short) 1,(short) 2001), null, null, null, 102*cdf0e10cSrcweir Boolean.FALSE, null, null}, 103*cdf0e10cSrcweir {null, null, null, null, null, 104*cdf0e10cSrcweir null, null, null, null, 105*cdf0e10cSrcweir null, null, null} 106*cdf0e10cSrcweir } ; 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir /** 109*cdf0e10cSrcweir * Array of lengths of streams for each row in of the 110*cdf0e10cSrcweir * <code>TST_TABLE_VALUES</code> constants. 111*cdf0e10cSrcweir */ 112*cdf0e10cSrcweir public final static int[] TST_STREAM_LENGTHS = {0, 0, 0} ; 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir /** 115*cdf0e10cSrcweir * It's just a structure with some useful methods for representing 116*cdf0e10cSrcweir * <code>com.sun.star.sdb.DataSource</code> service. All this 117*cdf0e10cSrcweir * service's properties are stored in appropriate class fields. 118*cdf0e10cSrcweir * Class also allows to construct its instances using service 119*cdf0e10cSrcweir * information, and create new service instance upon class 120*cdf0e10cSrcweir * fields. 121*cdf0e10cSrcweir * @see com.sun.star.sdb.DataSource 122*cdf0e10cSrcweir */ 123*cdf0e10cSrcweir public class DataSourceInfo { 124*cdf0e10cSrcweir /** 125*cdf0e10cSrcweir * Representation of <code>'Name'</code> property. 126*cdf0e10cSrcweir */ 127*cdf0e10cSrcweir public String Name = null ; 128*cdf0e10cSrcweir /** 129*cdf0e10cSrcweir * Representation of <code>'URL'</code> property. 130*cdf0e10cSrcweir */ 131*cdf0e10cSrcweir public String URL = null ; 132*cdf0e10cSrcweir /** 133*cdf0e10cSrcweir * Representation of <code>'Info'</code> property. 134*cdf0e10cSrcweir */ 135*cdf0e10cSrcweir public PropertyValue[] Info = null ; 136*cdf0e10cSrcweir /** 137*cdf0e10cSrcweir * Representation of <code>'User'</code> property. 138*cdf0e10cSrcweir */ 139*cdf0e10cSrcweir public String User = null ; 140*cdf0e10cSrcweir /** 141*cdf0e10cSrcweir * Representation of <code>'Password'</code> property. 142*cdf0e10cSrcweir */ 143*cdf0e10cSrcweir public String Password = null ; 144*cdf0e10cSrcweir /** 145*cdf0e10cSrcweir * Representation of <code>'IsPasswordRequired'</code> property. 146*cdf0e10cSrcweir */ 147*cdf0e10cSrcweir public Boolean IsPasswordRequired = null ; 148*cdf0e10cSrcweir /** 149*cdf0e10cSrcweir * Representation of <code>'SuppressVersionColumns'</code> property. 150*cdf0e10cSrcweir */ 151*cdf0e10cSrcweir public Boolean SuppressVersionColumns = null ; 152*cdf0e10cSrcweir /** 153*cdf0e10cSrcweir * Representation of <code>'IsReadOnly'</code> property. 154*cdf0e10cSrcweir */ 155*cdf0e10cSrcweir public Boolean IsReadOnly = null ; 156*cdf0e10cSrcweir /** 157*cdf0e10cSrcweir * Representation of <code>'TableFilter'</code> property. 158*cdf0e10cSrcweir */ 159*cdf0e10cSrcweir public String[] TableFilter = null ; 160*cdf0e10cSrcweir /** 161*cdf0e10cSrcweir * Representation of <code>'TableTypeFilter'</code> property. 162*cdf0e10cSrcweir */ 163*cdf0e10cSrcweir public String[] TableTypeFilter = null ; 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir /** 166*cdf0e10cSrcweir * Creates an empty instance. 167*cdf0e10cSrcweir */ 168*cdf0e10cSrcweir public DataSourceInfo() 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir /** 173*cdf0e10cSrcweir * Creates an instance laying upon specified DataSource. 174*cdf0e10cSrcweir * @param dataSource All source properties are copied into 175*cdf0e10cSrcweir * class fields. 176*cdf0e10cSrcweir */ 177*cdf0e10cSrcweir public DataSourceInfo(Object dataSource) { 178*cdf0e10cSrcweir XPropertySet xProps = (XPropertySet) 179*cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, dataSource) ; 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir try { 182*cdf0e10cSrcweir Name = (String)xProps.getPropertyValue("Name") ; 183*cdf0e10cSrcweir URL = (String)xProps.getPropertyValue("URL") ; 184*cdf0e10cSrcweir Info = (PropertyValue[])xProps.getPropertyValue("Info") ; 185*cdf0e10cSrcweir User = (String)xProps.getPropertyValue("User") ; 186*cdf0e10cSrcweir Password = (String)xProps.getPropertyValue("Password") ; 187*cdf0e10cSrcweir IsPasswordRequired = (Boolean)xProps.getPropertyValue("IsPasswordRequired") ; 188*cdf0e10cSrcweir SuppressVersionColumns = (Boolean) 189*cdf0e10cSrcweir xProps.getPropertyValue("SuppressVersionColumns") ; 190*cdf0e10cSrcweir IsReadOnly = (Boolean)xProps.getPropertyValue("IsReadOnly") ; 191*cdf0e10cSrcweir TableFilter = (String[])xProps.getPropertyValue("TableFilter") ; 192*cdf0e10cSrcweir TableTypeFilter = (String[])xProps.getPropertyValue("TableTypeFilter") ; 193*cdf0e10cSrcweir } catch (com.sun.star.beans.UnknownPropertyException e) { 194*cdf0e10cSrcweir System.err.println("util.DBTools.DataSourceInfo: Error retrieving property") ; 195*cdf0e10cSrcweir e.printStackTrace(System.err) ; 196*cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 197*cdf0e10cSrcweir System.err.println("util.DBTools.DataSourceInfo: Error retrieving property") ; 198*cdf0e10cSrcweir e.printStackTrace(System.err) ; 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir /** 203*cdf0e10cSrcweir * Prints datasource info. 204*cdf0e10cSrcweir * @param out Stream to which information is printed. 205*cdf0e10cSrcweir */ 206*cdf0e10cSrcweir public void printInfo(PrintWriter out) { 207*cdf0e10cSrcweir out.println("Name = '" + Name + "'") ; 208*cdf0e10cSrcweir out.println(" URL = '" + URL + "'") ; 209*cdf0e10cSrcweir out.print(" Info = ") ; 210*cdf0e10cSrcweir if (Info == null) out.println("null") ; 211*cdf0e10cSrcweir else { 212*cdf0e10cSrcweir out.print("{") ; 213*cdf0e10cSrcweir for (int i = 0; i < Info.length; i++) { 214*cdf0e10cSrcweir out.print(Info[i].Name + " = '" + Info[i].Value + "'") ; 215*cdf0e10cSrcweir if (i + 1 < Info.length) out.print("; ") ; 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir out.println("}") ; 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir out.println(" User = '" + User + "'") ; 220*cdf0e10cSrcweir out.println(" Password = '" + Password + "'") ; 221*cdf0e10cSrcweir out.println(" IsPasswordRequired = '" + IsPasswordRequired + "'") ; 222*cdf0e10cSrcweir out.println(" SuppressVersionColumns = '" + SuppressVersionColumns + "'") ; 223*cdf0e10cSrcweir out.println(" IsReadOnly = '" + IsReadOnly + "'") ; 224*cdf0e10cSrcweir out.print(" TableFilter = ") ; 225*cdf0e10cSrcweir if (TableFilter == null) out.println("null") ; 226*cdf0e10cSrcweir else { 227*cdf0e10cSrcweir out.print("{") ; 228*cdf0e10cSrcweir for (int i = 0; i < TableFilter.length; i++) { 229*cdf0e10cSrcweir out.print("'" + TableFilter[i] + "'") ; 230*cdf0e10cSrcweir if (i+1 < TableFilter.length) out.print("; "); 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir out.println("}") ; 233*cdf0e10cSrcweir } 234*cdf0e10cSrcweir out.print(" TableTypeFilter = ") ; 235*cdf0e10cSrcweir if (TableTypeFilter == null) out.println("null") ; 236*cdf0e10cSrcweir else { 237*cdf0e10cSrcweir out.print("{") ; 238*cdf0e10cSrcweir for (int i = 0; i < TableTypeFilter.length; i++) { 239*cdf0e10cSrcweir out.print("'" + TableTypeFilter[i] + "'") ; 240*cdf0e10cSrcweir if (i+1 < TableTypeFilter.length) out.print("; "); 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir out.println("}") ; 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir /** 247*cdf0e10cSrcweir * Creates new <code>com.sun.star.sdb.DataSource</code> service 248*cdf0e10cSrcweir * instance and copies all fields (which are not null) to 249*cdf0e10cSrcweir * appropriate service properties. 250*cdf0e10cSrcweir * @return <code>com.sun.star.sdb.DataSource</code> service. 251*cdf0e10cSrcweir */ 252*cdf0e10cSrcweir public Object getDataSourceService() throws Exception 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir Object src = src = xMSF.createInstance("com.sun.star.sdb.DataSource") ; 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir XPropertySet props = (XPropertySet) UnoRuntime.queryInterface 257*cdf0e10cSrcweir (XPropertySet.class, src) ; 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir if (Name != null) props.setPropertyValue("Name", Name) ; 260*cdf0e10cSrcweir if (URL != null) props.setPropertyValue("URL", URL) ; 261*cdf0e10cSrcweir if (Info != null) props.setPropertyValue("Info", Info) ; 262*cdf0e10cSrcweir if (User != null) props.setPropertyValue("User", User) ; 263*cdf0e10cSrcweir if (Password != null) props.setPropertyValue("Password", Password) ; 264*cdf0e10cSrcweir if (IsPasswordRequired != null) props.setPropertyValue("IsPasswordRequired", IsPasswordRequired) ; 265*cdf0e10cSrcweir if (SuppressVersionColumns != null) props.setPropertyValue("SuppressVersionColumns", SuppressVersionColumns) ; 266*cdf0e10cSrcweir if (IsReadOnly != null) props.setPropertyValue("IsReadOnly", IsReadOnly) ; 267*cdf0e10cSrcweir if (TableFilter != null) props.setPropertyValue("TableFilter", TableFilter) ; 268*cdf0e10cSrcweir if (TableTypeFilter != null) props.setPropertyValue("TableTypeFilter", TableTypeFilter) ; 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir return src ; 271*cdf0e10cSrcweir } 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir /** 275*cdf0e10cSrcweir * Creates class instance. 276*cdf0e10cSrcweir * @param xMSF <code>XMultiServiceFactory</code>. 277*cdf0e10cSrcweir */ 278*cdf0e10cSrcweir public DBTools(XMultiServiceFactory xMSF, PrintWriter _logger ) 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir this.xMSF = xMSF ; 281*cdf0e10cSrcweir this.m_log = _logger; 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir try { 284*cdf0e10cSrcweir Object cont = xMSF.createInstance("com.sun.star.sdb.DatabaseContext") ; 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir dbContext = (XNamingService) UnoRuntime.queryInterface 287*cdf0e10cSrcweir (XNamingService.class, cont) ; 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) {} 290*cdf0e10cSrcweir } 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir /** 293*cdf0e10cSrcweir * Returns new instance of <code>DataSourceInfo</code> class. 294*cdf0e10cSrcweir */ 295*cdf0e10cSrcweir public DataSourceInfo newDataSourceInfo() { return new DataSourceInfo() ;} 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir /** 298*cdf0e10cSrcweir * Returns new instance of <code>DataSourceInfo</code> class. 299*cdf0e10cSrcweir */ 300*cdf0e10cSrcweir public DataSourceInfo newDataSourceInfo(Object dataSource) { 301*cdf0e10cSrcweir return new DataSourceInfo(dataSource); 302*cdf0e10cSrcweir } 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir /** 305*cdf0e10cSrcweir * Registers the datasource on the specified name in 306*cdf0e10cSrcweir * <code>DatabaseContext</code> service. 307*cdf0e10cSrcweir * @param name Name which dataSource will have in global context. 308*cdf0e10cSrcweir * @param dataSource <code>DataSource</code> object which is to 309*cdf0e10cSrcweir * be registered. 310*cdf0e10cSrcweir */ 311*cdf0e10cSrcweir public void registerDB(String name, Object dataSource) 312*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 313*cdf0e10cSrcweir 314*cdf0e10cSrcweir dbContext.registerObject(name, dataSource) ; 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir 317*cdf0e10cSrcweir 318*cdf0e10cSrcweir /** 319*cdf0e10cSrcweir * First tries to revoke the datasource with the specified 320*cdf0e10cSrcweir * name and then registers a new one. 321*cdf0e10cSrcweir * @param name Name which dataSource will have in global context. 322*cdf0e10cSrcweir * @param dataSource <code>DataSource</code> object which is to 323*cdf0e10cSrcweir * be registered. 324*cdf0e10cSrcweir */ 325*cdf0e10cSrcweir public void reRegisterDB(String name, Object dataSource) 326*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 327*cdf0e10cSrcweir 328*cdf0e10cSrcweir try { 329*cdf0e10cSrcweir revokeDB(name) ; 330*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) {} 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir XDocumentDataSource xDDS = (XDocumentDataSource) 333*cdf0e10cSrcweir UnoRuntime.queryInterface(XDocumentDataSource.class, dataSource); 334*cdf0e10cSrcweir XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, 335*cdf0e10cSrcweir xDDS.getDatabaseDocument()); 336*cdf0e10cSrcweir String aFile = utils.getOfficeTemp(xMSF) + name + ".odb"; 337*cdf0e10cSrcweir store.storeAsURL(aFile, new PropertyValue[] { }); 338*cdf0e10cSrcweir 339*cdf0e10cSrcweir registerDB(name, dataSource) ; 340*cdf0e10cSrcweir } 341*cdf0e10cSrcweir 342*cdf0e10cSrcweir /** 343*cdf0e10cSrcweir * RESERVED. Not used. 344*cdf0e10cSrcweir */ 345*cdf0e10cSrcweir public XConnection connectToTextDB(String contextName, 346*cdf0e10cSrcweir String dbDir, String fileExtension) 347*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir try { 350*cdf0e10cSrcweir XInterface newSource = (XInterface) xMSF.createInstance 351*cdf0e10cSrcweir ("com.sun.star.sdb.DataSource") ; 352*cdf0e10cSrcweir 353*cdf0e10cSrcweir XPropertySet xSrcProp = (XPropertySet) 354*cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, newSource); 355*cdf0e10cSrcweir 356*cdf0e10cSrcweir xSrcProp.setPropertyValue("URL", "sdbc:text:" + dirToUrl(dbDir)); 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir PropertyValue extParam = new PropertyValue() ; 359*cdf0e10cSrcweir extParam.Name = "EXT" ; 360*cdf0e10cSrcweir extParam.Value = fileExtension ; 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir xSrcProp.setPropertyValue("Info", new PropertyValue[] {extParam}) ; 363*cdf0e10cSrcweir 364*cdf0e10cSrcweir dbContext.registerObject(contextName, newSource) ; 365*cdf0e10cSrcweir 366*cdf0e10cSrcweir Object handler = xMSF.createInstance("com.sun.star.sdb.InteractionHandler"); 367*cdf0e10cSrcweir XInteractionHandler xHandler = (XInteractionHandler) 368*cdf0e10cSrcweir UnoRuntime.queryInterface(XInteractionHandler.class, handler) ; 369*cdf0e10cSrcweir 370*cdf0e10cSrcweir XCompletedConnection xSrcCon = (XCompletedConnection) 371*cdf0e10cSrcweir UnoRuntime.queryInterface(XCompletedConnection.class, newSource) ; 372*cdf0e10cSrcweir 373*cdf0e10cSrcweir XConnection con = xSrcCon.connectWithCompletion(xHandler) ; 374*cdf0e10cSrcweir 375*cdf0e10cSrcweir return con ; 376*cdf0e10cSrcweir } finally { 377*cdf0e10cSrcweir try { 378*cdf0e10cSrcweir dbContext.revokeObject(contextName) ; 379*cdf0e10cSrcweir } catch (Exception e) {} 380*cdf0e10cSrcweir } 381*cdf0e10cSrcweir } 382*cdf0e10cSrcweir 383*cdf0e10cSrcweir /** 384*cdf0e10cSrcweir * Registers DBase database (directory with DBF files) in the 385*cdf0e10cSrcweir * global DB context, then connects to it. 386*cdf0e10cSrcweir * @param contextName Name under which DB will be registered. 387*cdf0e10cSrcweir * @param dbDir The directory with DBF tables. 388*cdf0e10cSrcweir * @return Connection to the DB. 389*cdf0e10cSrcweir */ 390*cdf0e10cSrcweir public XConnection connectToDBase(String contextName, 391*cdf0e10cSrcweir String dbDir) 392*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 393*cdf0e10cSrcweir 394*cdf0e10cSrcweir try { 395*cdf0e10cSrcweir XInterface newSource = (XInterface) xMSF.createInstance 396*cdf0e10cSrcweir ("com.sun.star.sdb.DataSource") ; 397*cdf0e10cSrcweir 398*cdf0e10cSrcweir XPropertySet xSrcProp = (XPropertySet) 399*cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, newSource); 400*cdf0e10cSrcweir xSrcProp.setPropertyValue("URL", "sdbc:dbase:" + dirToUrl(dbDir)); 401*cdf0e10cSrcweir 402*cdf0e10cSrcweir dbContext.registerObject(contextName, newSource) ; 403*cdf0e10cSrcweir 404*cdf0e10cSrcweir XConnection con = connectToSource(newSource) ; 405*cdf0e10cSrcweir 406*cdf0e10cSrcweir return con ; 407*cdf0e10cSrcweir } catch(com.sun.star.uno.Exception e) { 408*cdf0e10cSrcweir try { 409*cdf0e10cSrcweir dbContext.revokeObject(contextName) ; 410*cdf0e10cSrcweir } catch (Exception ex) {} 411*cdf0e10cSrcweir 412*cdf0e10cSrcweir throw e ; 413*cdf0e10cSrcweir } 414*cdf0e10cSrcweir } 415*cdf0e10cSrcweir 416*cdf0e10cSrcweir /** 417*cdf0e10cSrcweir * Performs connection to DataSource specified. 418*cdf0e10cSrcweir * @param dbSource <code>com.sun.star.sdb.DataSource</code> service 419*cdf0e10cSrcweir * specified data source which must be already registered in the 420*cdf0e10cSrcweir * <code>DatabaseContext</code> service. 421*cdf0e10cSrcweir * @param dbSource Data source to be connected to. 422*cdf0e10cSrcweir * @return Connection to the data source. 423*cdf0e10cSrcweir */ 424*cdf0e10cSrcweir public XConnection connectToSource(Object dbSource) 425*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 426*cdf0e10cSrcweir 427*cdf0e10cSrcweir Object handler = xMSF.createInstance("com.sun.star.sdb.InteractionHandler"); 428*cdf0e10cSrcweir XInteractionHandler xHandler = (XInteractionHandler) 429*cdf0e10cSrcweir UnoRuntime.queryInterface(XInteractionHandler.class, handler) ; 430*cdf0e10cSrcweir 431*cdf0e10cSrcweir XCompletedConnection xSrcCon = (XCompletedConnection) 432*cdf0e10cSrcweir UnoRuntime.queryInterface(XCompletedConnection.class, dbSource) ; 433*cdf0e10cSrcweir 434*cdf0e10cSrcweir return xSrcCon.connectWithCompletion(xHandler) ; 435*cdf0e10cSrcweir } 436*cdf0e10cSrcweir 437*cdf0e10cSrcweir /** 438*cdf0e10cSrcweir * Registers Test data source in the <code>DatabaseContext</code> service. 439*cdf0e10cSrcweir * This source always has name <code>'APITestDatabase'</code> and it 440*cdf0e10cSrcweir * is registered in subdirectory <code>TestDB</code> of directory 441*cdf0e10cSrcweir * <code>docPath</code> which is supposed to be a directory with test 442*cdf0e10cSrcweir * documents, but can be any other (it must have subdirectory with DBF 443*cdf0e10cSrcweir * tables). If such data source doesn't exists or exists with 444*cdf0e10cSrcweir * different URL it is recreated and reregistered. 445*cdf0e10cSrcweir * @param docPath Path to database <code>TestDB</code> directory. 446*cdf0e10cSrcweir * @return <code>com.sun.star.sdb.DataSource</code> service 447*cdf0e10cSrcweir * implementation which represents TestDB. 448*cdf0e10cSrcweir */ 449*cdf0e10cSrcweir public Object registerTestDB(String docPath) 450*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 451*cdf0e10cSrcweir 452*cdf0e10cSrcweir String testURL = null ; 453*cdf0e10cSrcweir if (docPath.endsWith("/") || docPath.endsWith("\\")) 454*cdf0e10cSrcweir testURL = dirToUrl(docPath + "TestDB") ; 455*cdf0e10cSrcweir else 456*cdf0e10cSrcweir testURL = dirToUrl(docPath + "/" + "TestDB") ; 457*cdf0e10cSrcweir testURL = "sdbc:dbase:" + testURL ; 458*cdf0e10cSrcweir 459*cdf0e10cSrcweir String existURL = null ; 460*cdf0e10cSrcweir 461*cdf0e10cSrcweir XNameAccess na = (XNameAccess) UnoRuntime.queryInterface 462*cdf0e10cSrcweir (XNameAccess.class, dbContext) ; 463*cdf0e10cSrcweir 464*cdf0e10cSrcweir Object src = null ; 465*cdf0e10cSrcweir if (na.hasByName("APITestDatabase")) { 466*cdf0e10cSrcweir src = dbContext.getRegisteredObject("APITestDatabase") ; 467*cdf0e10cSrcweir 468*cdf0e10cSrcweir XPropertySet srcPs = (XPropertySet) UnoRuntime.queryInterface 469*cdf0e10cSrcweir (XPropertySet.class, src) ; 470*cdf0e10cSrcweir 471*cdf0e10cSrcweir existURL = (String) srcPs.getPropertyValue("URL") ; 472*cdf0e10cSrcweir } 473*cdf0e10cSrcweir 474*cdf0e10cSrcweir if (src == null || !testURL.equals(existURL)) { 475*cdf0e10cSrcweir // test data source must be reregistered. 476*cdf0e10cSrcweir DataSourceInfo info = new DataSourceInfo() ; 477*cdf0e10cSrcweir info.URL = testURL ; 478*cdf0e10cSrcweir src = info.getDataSourceService() ; 479*cdf0e10cSrcweir reRegisterDB("APITestDatabase", src) ; 480*cdf0e10cSrcweir src = dbContext.getRegisteredObject("APITestDatabase") ; 481*cdf0e10cSrcweir } 482*cdf0e10cSrcweir 483*cdf0e10cSrcweir return src ; 484*cdf0e10cSrcweir } 485*cdf0e10cSrcweir 486*cdf0e10cSrcweir /** 487*cdf0e10cSrcweir * Connects to <code>DataSource</code> specially created for testing. 488*cdf0e10cSrcweir * This source always has name <code>'APITestDatabase'</code> and it 489*cdf0e10cSrcweir * is registered in subdirectory <code>TestDB</code> of directory 490*cdf0e10cSrcweir * <code>docPath</code> which is supposed to be a directory with test 491*cdf0e10cSrcweir * documents, but can be any other (it must have subdirectory with DBF 492*cdf0e10cSrcweir * tables). If such data source doesn't exists or exists with 493*cdf0e10cSrcweir * different URL it is recreated and reregistered. Finally connection 494*cdf0e10cSrcweir * performed. 495*cdf0e10cSrcweir * @param docPath Path to database <code>TestDB</code> directory. 496*cdf0e10cSrcweir * @return Connection to test database. 497*cdf0e10cSrcweir */ 498*cdf0e10cSrcweir public XConnection connectToTestDB(String docPath) 499*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 500*cdf0e10cSrcweir 501*cdf0e10cSrcweir return connectToSource(registerTestDB(docPath)) ; 502*cdf0e10cSrcweir } 503*cdf0e10cSrcweir 504*cdf0e10cSrcweir /** 505*cdf0e10cSrcweir * Empties the table in the specified source. 506*cdf0e10cSrcweir * @param con Connection to the DataSource where appropriate 507*cdf0e10cSrcweir * table exists. 508*cdf0e10cSrcweir * @param table The name of the table where all rows will be deleted. 509*cdf0e10cSrcweir * @return Number of rows deleted. 510*cdf0e10cSrcweir */ 511*cdf0e10cSrcweir 512*cdf0e10cSrcweir // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 513*cdf0e10cSrcweir // Currently doesn't work because of bugs 85509, 85510 514*cdf0e10cSrcweir 515*cdf0e10cSrcweir public int deleteAllRows(XConnection con, String table) 516*cdf0e10cSrcweir throws com.sun.star.sdbc.SQLException { 517*cdf0e10cSrcweir 518*cdf0e10cSrcweir XStatement stat = con.createStatement() ; 519*cdf0e10cSrcweir 520*cdf0e10cSrcweir XResultSet set = stat.executeQuery("SELECT * FROM " + table) ; 521*cdf0e10cSrcweir 522*cdf0e10cSrcweir XResultSetUpdate updt = (XResultSetUpdate) UnoRuntime.queryInterface 523*cdf0e10cSrcweir (XResultSetUpdate.class, set) ; 524*cdf0e10cSrcweir 525*cdf0e10cSrcweir int count = 0 ; 526*cdf0e10cSrcweir set.last() ; 527*cdf0e10cSrcweir int rowNum = set.getRow() ; 528*cdf0e10cSrcweir set.first() ; 529*cdf0e10cSrcweir 530*cdf0e10cSrcweir for (int i = 0; i < rowNum; i++) { 531*cdf0e10cSrcweir updt.deleteRow() ; 532*cdf0e10cSrcweir set.next() ; 533*cdf0e10cSrcweir count ++ ; 534*cdf0e10cSrcweir } 535*cdf0e10cSrcweir 536*cdf0e10cSrcweir XCloseable xClose = (XCloseable) UnoRuntime.queryInterface 537*cdf0e10cSrcweir (XCloseable.class, set) ; 538*cdf0e10cSrcweir xClose.close() ; 539*cdf0e10cSrcweir 540*cdf0e10cSrcweir return count ; 541*cdf0e10cSrcweir } 542*cdf0e10cSrcweir 543*cdf0e10cSrcweir /** 544*cdf0e10cSrcweir * Inserts row into test table of the specified connection. 545*cdf0e10cSrcweir * Test table has some predefined format which includes as much 546*cdf0e10cSrcweir * field types as possible. For every column type constants 547*cdf0e10cSrcweir * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc. 548*cdf0e10cSrcweir * are declared for column index fast find. 549*cdf0e10cSrcweir * @param con Connection to data source where test table exists. 550*cdf0e10cSrcweir * @param table Test table name. 551*cdf0e10cSrcweir * @param values Values to be inserted into test table. Values of 552*cdf0e10cSrcweir * this array inserted into appropriate fields depending on their 553*cdf0e10cSrcweir * types. So <code>String</code> value of the array is inserted 554*cdf0e10cSrcweir * into the field of <code>CHARACTER</code> type, etc. 555*cdf0e10cSrcweir * @param streamLength Is optional. It is used only if in values 556*cdf0e10cSrcweir * list <code>XCharacterInputStream</code> or <code>XBinaryInputStream 557*cdf0e10cSrcweir * </code> types specified. In this case the parameter specifies 558*cdf0e10cSrcweir * the length of the stream for inserting. 559*cdf0e10cSrcweir */ 560*cdf0e10cSrcweir public void addRowToTestTable(XConnection con, String table, Object[] values, 561*cdf0e10cSrcweir int streamLength) 562*cdf0e10cSrcweir throws com.sun.star.sdbc.SQLException { 563*cdf0e10cSrcweir 564*cdf0e10cSrcweir XStatement stat = con.createStatement() ; 565*cdf0e10cSrcweir 566*cdf0e10cSrcweir XResultSet set = stat.executeQuery("SELECT * FROM " + table) ; 567*cdf0e10cSrcweir 568*cdf0e10cSrcweir XResultSetUpdate updt = (XResultSetUpdate) UnoRuntime.queryInterface 569*cdf0e10cSrcweir (XResultSetUpdate.class, set) ; 570*cdf0e10cSrcweir 571*cdf0e10cSrcweir XRowUpdate rowUpdt = (XRowUpdate) UnoRuntime.queryInterface 572*cdf0e10cSrcweir (XRowUpdate.class, set) ; 573*cdf0e10cSrcweir 574*cdf0e10cSrcweir updt.moveToInsertRow() ; 575*cdf0e10cSrcweir 576*cdf0e10cSrcweir for (int i = 0; i < values.length; i++) { 577*cdf0e10cSrcweir if (values[i] instanceof String) { 578*cdf0e10cSrcweir rowUpdt.updateString(TST_STRING, (String) values[i]) ; 579*cdf0e10cSrcweir } else 580*cdf0e10cSrcweir if (values[i] instanceof Integer) { 581*cdf0e10cSrcweir rowUpdt.updateInt(TST_INT, ((Integer) values[i]).intValue()) ; 582*cdf0e10cSrcweir } else 583*cdf0e10cSrcweir if (values[i] instanceof Double) { 584*cdf0e10cSrcweir rowUpdt.updateDouble(TST_DOUBLE, ((Double) values[i]).doubleValue()) ; 585*cdf0e10cSrcweir } else 586*cdf0e10cSrcweir if (values[i] instanceof Date) { 587*cdf0e10cSrcweir rowUpdt.updateDate(TST_DATE, (Date) values[i]) ; 588*cdf0e10cSrcweir } else 589*cdf0e10cSrcweir if (values[i] instanceof Boolean) { 590*cdf0e10cSrcweir rowUpdt.updateBoolean(TST_BOOLEAN, ((Boolean) values[i]).booleanValue()) ; 591*cdf0e10cSrcweir } else 592*cdf0e10cSrcweir if (values[i] instanceof XTextInputStream) { 593*cdf0e10cSrcweir rowUpdt.updateCharacterStream(TST_CHARACTER_STREAM, (XInputStream) values[i], 594*cdf0e10cSrcweir streamLength) ; 595*cdf0e10cSrcweir } else 596*cdf0e10cSrcweir if (values[i] instanceof XDataInputStream) { 597*cdf0e10cSrcweir rowUpdt.updateBinaryStream(TST_BINARY_STREAM, (XInputStream) values[i], 598*cdf0e10cSrcweir streamLength) ; 599*cdf0e10cSrcweir } 600*cdf0e10cSrcweir } 601*cdf0e10cSrcweir 602*cdf0e10cSrcweir updt.insertRow() ; 603*cdf0e10cSrcweir 604*cdf0e10cSrcweir XCloseable xClose = (XCloseable) UnoRuntime.queryInterface 605*cdf0e10cSrcweir (XCloseable.class, set) ; 606*cdf0e10cSrcweir xClose.close() ; 607*cdf0e10cSrcweir } 608*cdf0e10cSrcweir 609*cdf0e10cSrcweir /** 610*cdf0e10cSrcweir * Initializes test table specified of the connection specified. 611*cdf0e10cSrcweir * Deletes all record from table, and then inserts data from 612*cdf0e10cSrcweir * <code>TST_TABLE_VALUES</code> constant array. <p> 613*cdf0e10cSrcweir * Test table has some predefined format which includes as much 614*cdf0e10cSrcweir * field types as possible. For every column type constants 615*cdf0e10cSrcweir * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc. 616*cdf0e10cSrcweir * are declared for column index fast find. 617*cdf0e10cSrcweir * @param con Connection to data source where test table exists. 618*cdf0e10cSrcweir * @param table Test table name. 619*cdf0e10cSrcweir */ 620*cdf0e10cSrcweir public void initializeTestTable(XConnection con, String table) 621*cdf0e10cSrcweir throws com.sun.star.sdbc.SQLException { 622*cdf0e10cSrcweir 623*cdf0e10cSrcweir deleteAllRows(con, table) ; 624*cdf0e10cSrcweir 625*cdf0e10cSrcweir for (int i = 0; i < TST_TABLE_VALUES.length; i++) { 626*cdf0e10cSrcweir addRowToTestTable(con, table, TST_TABLE_VALUES[i], TST_STREAM_LENGTHS[i]) ; 627*cdf0e10cSrcweir } 628*cdf0e10cSrcweir } 629*cdf0e10cSrcweir 630*cdf0e10cSrcweir /** 631*cdf0e10cSrcweir * Prints full info about currently registered DataSource's. 632*cdf0e10cSrcweir */ 633*cdf0e10cSrcweir public void printRegisteredDatabasesInfo(PrintWriter out) { 634*cdf0e10cSrcweir XEnumerationAccess dbContEA = (XEnumerationAccess) 635*cdf0e10cSrcweir UnoRuntime.queryInterface(XEnumerationAccess.class, dbContext) ; 636*cdf0e10cSrcweir 637*cdf0e10cSrcweir XEnumeration xEnum = dbContEA.createEnumeration() ; 638*cdf0e10cSrcweir 639*cdf0e10cSrcweir out.println("DatabaseContext registered DataSource's :") ; 640*cdf0e10cSrcweir while (xEnum.hasMoreElements()) { 641*cdf0e10cSrcweir try { 642*cdf0e10cSrcweir DataSourceInfo inf = new DataSourceInfo(xEnum.nextElement()) ; 643*cdf0e10cSrcweir inf.printInfo(out) ; 644*cdf0e10cSrcweir } catch (com.sun.star.container.NoSuchElementException e) {} 645*cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) {} 646*cdf0e10cSrcweir } 647*cdf0e10cSrcweir } 648*cdf0e10cSrcweir 649*cdf0e10cSrcweir /** 650*cdf0e10cSrcweir * Convert system pathname to SOffice URL string 651*cdf0e10cSrcweir * (for example 'C:\Temp\DBDir\' -> 'file:///C|/Temp/DBDir/'). 652*cdf0e10cSrcweir * (for example '\\server\Temp\DBDir\' -> 'file://server/Temp/DBDir/'). 653*cdf0e10cSrcweir * Already converted string retured unchanged. 654*cdf0e10cSrcweir */ 655*cdf0e10cSrcweir public static String dirToUrl(String dir) { 656*cdf0e10cSrcweir String retVal = null; 657*cdf0e10cSrcweir if (dir.startsWith("file:/")) retVal = dir; 658*cdf0e10cSrcweir else { 659*cdf0e10cSrcweir retVal = dir.replace(':', '|').replace('\\', '/'); 660*cdf0e10cSrcweir 661*cdf0e10cSrcweir if (dir.startsWith("\\\\")) { 662*cdf0e10cSrcweir retVal = "file:" + retVal; 663*cdf0e10cSrcweir } 664*cdf0e10cSrcweir 665*cdf0e10cSrcweir else retVal = "file:///" + retVal ; 666*cdf0e10cSrcweir } 667*cdf0e10cSrcweir return retVal; 668*cdf0e10cSrcweir } 669*cdf0e10cSrcweir 670*cdf0e10cSrcweir /** 671*cdf0e10cSrcweir * Revokes datasource from global DB context. 672*cdf0e10cSrcweir * @param name DataSource name to be revoked. 673*cdf0e10cSrcweir */ 674*cdf0e10cSrcweir public void revokeDB(String name) throws com.sun.star.uno.Exception 675*cdf0e10cSrcweir { 676*cdf0e10cSrcweir dbContext.revokeObject(name) ; 677*cdf0e10cSrcweir } 678*cdf0e10cSrcweir 679*cdf0e10cSrcweir /** 680*cdf0e10cSrcweir * Initializes test table specified of the connection specified 681*cdf0e10cSrcweir * using JDBC driver. Drops table with the name <code>tbl_name</code>, 682*cdf0e10cSrcweir * creates new table with this name and then inserts data from 683*cdf0e10cSrcweir * <code>TST_TABLE_VALUES</code> constant array. <p> 684*cdf0e10cSrcweir * Test table has some predefined format which includes as much 685*cdf0e10cSrcweir * field types as possible. For every column type constants 686*cdf0e10cSrcweir * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc. 687*cdf0e10cSrcweir * are declared for column index fast find. 688*cdf0e10cSrcweir * @param tbl_name Test table name. 689*cdf0e10cSrcweir */ 690*cdf0e10cSrcweir public void initTestTableUsingJDBC(String tbl_name, DataSourceInfo dsi) 691*cdf0e10cSrcweir throws java.sql.SQLException, 692*cdf0e10cSrcweir ClassNotFoundException { 693*cdf0e10cSrcweir //register jdbc driver 694*cdf0e10cSrcweir if ( dsi.Info[0].Name.equals("JavaDriverClass") ) { 695*cdf0e10cSrcweir Class.forName((String)dsi.Info[0].Value); 696*cdf0e10cSrcweir } else { 697*cdf0e10cSrcweir Class.forName(TST_JDBC_DRIVER); 698*cdf0e10cSrcweir } 699*cdf0e10cSrcweir 700*cdf0e10cSrcweir //getting connection 701*cdf0e10cSrcweir Connection connection = null; 702*cdf0e10cSrcweir 703*cdf0e10cSrcweir connection = DriverManager.getConnection( 704*cdf0e10cSrcweir dsi.URL, dsi.User, dsi.Password); 705*cdf0e10cSrcweir Statement statement = connection.createStatement(); 706*cdf0e10cSrcweir 707*cdf0e10cSrcweir //drop table 708*cdf0e10cSrcweir dropMySQLTable(statement, tbl_name); 709*cdf0e10cSrcweir 710*cdf0e10cSrcweir //create table 711*cdf0e10cSrcweir createMySQLTable(statement, tbl_name); 712*cdf0e10cSrcweir 713*cdf0e10cSrcweir //insert some content 714*cdf0e10cSrcweir insertContentMySQLTable(statement, tbl_name); 715*cdf0e10cSrcweir } 716*cdf0e10cSrcweir 717*cdf0e10cSrcweir /** 718*cdf0e10cSrcweir * Inserts data from <code>TST_TABLE_VALUES</code> constant array 719*cdf0e10cSrcweir * to test table <code>tbl_name</code>. 720*cdf0e10cSrcweir * @param statement object used for executing a static SQL 721*cdf0e10cSrcweir * statement and obtaining the results produced by it. 722*cdf0e10cSrcweir * @param tbl_name Test table name. 723*cdf0e10cSrcweir */ 724*cdf0e10cSrcweir protected void insertContentMySQLTable(Statement statement, String tbl_name) 725*cdf0e10cSrcweir throws java.sql.SQLException { 726*cdf0e10cSrcweir 727*cdf0e10cSrcweir 728*cdf0e10cSrcweir for(int i = 0; i < DBTools.TST_TABLE_VALUES.length; i++) { 729*cdf0e10cSrcweir String query = "insert into " + tbl_name + " values ("; 730*cdf0e10cSrcweir int j = 0; 731*cdf0e10cSrcweir while(j < DBTools.TST_TABLE_VALUES[i].length) { 732*cdf0e10cSrcweir if (j > 0) { 733*cdf0e10cSrcweir query += ", "; 734*cdf0e10cSrcweir } 735*cdf0e10cSrcweir Object value = DBTools.TST_TABLE_VALUES[i][j]; 736*cdf0e10cSrcweir if (value instanceof String || 737*cdf0e10cSrcweir value instanceof Date) { 738*cdf0e10cSrcweir query += "'"; 739*cdf0e10cSrcweir } 740*cdf0e10cSrcweir if (value instanceof Date) { 741*cdf0e10cSrcweir Date date = (Date)value; 742*cdf0e10cSrcweir query += date.Year + "-" + date.Month + 743*cdf0e10cSrcweir "-" + date.Day; 744*cdf0e10cSrcweir } else if (value instanceof Boolean) { 745*cdf0e10cSrcweir query += (((Boolean)value).booleanValue()) 746*cdf0e10cSrcweir ? "1" : "0"; 747*cdf0e10cSrcweir } else { 748*cdf0e10cSrcweir query += value; 749*cdf0e10cSrcweir } 750*cdf0e10cSrcweir 751*cdf0e10cSrcweir if (value instanceof String || 752*cdf0e10cSrcweir value instanceof Date) { 753*cdf0e10cSrcweir query += "'"; 754*cdf0e10cSrcweir } 755*cdf0e10cSrcweir j++; 756*cdf0e10cSrcweir } 757*cdf0e10cSrcweir query += ")"; 758*cdf0e10cSrcweir statement.executeUpdate(query); 759*cdf0e10cSrcweir } 760*cdf0e10cSrcweir } 761*cdf0e10cSrcweir 762*cdf0e10cSrcweir /** 763*cdf0e10cSrcweir * Creates test table specified. 764*cdf0e10cSrcweir * Test table has some predefined format which includes as much 765*cdf0e10cSrcweir * field types as possible. For every column type constants 766*cdf0e10cSrcweir * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc. 767*cdf0e10cSrcweir * are declared for column index fast find. 768*cdf0e10cSrcweir * @param statement object used for executing a static SQL 769*cdf0e10cSrcweir * statement and obtaining the results produced by it. 770*cdf0e10cSrcweir * @param table Test table name. 771*cdf0e10cSrcweir */ 772*cdf0e10cSrcweir protected void createMySQLTable(Statement statement, String tbl_name) 773*cdf0e10cSrcweir throws java.sql.SQLException { 774*cdf0e10cSrcweir 775*cdf0e10cSrcweir final String empty_col_name = "Column"; 776*cdf0e10cSrcweir int c = 0; 777*cdf0e10cSrcweir String query = "create table " + tbl_name + " ("; 778*cdf0e10cSrcweir for (int i = 0; i < TST_TABLE_VALUES[0].length; i++) { 779*cdf0e10cSrcweir if (i > 0) query += ","; 780*cdf0e10cSrcweir 781*cdf0e10cSrcweir switch(i + 1) { 782*cdf0e10cSrcweir case TST_BINARY_STREAM: 783*cdf0e10cSrcweir query += TST_BINARY_STREAM_F + " BLOB"; 784*cdf0e10cSrcweir break; 785*cdf0e10cSrcweir case TST_BOOLEAN: 786*cdf0e10cSrcweir query += TST_BOOLEAN_F + " TINYINT"; 787*cdf0e10cSrcweir break; 788*cdf0e10cSrcweir case TST_CHARACTER_STREAM: 789*cdf0e10cSrcweir query += TST_CHARACTER_STREAM_F + " TEXT"; 790*cdf0e10cSrcweir break; 791*cdf0e10cSrcweir case TST_DATE: 792*cdf0e10cSrcweir query += TST_DATE_F + " DATE"; 793*cdf0e10cSrcweir break; 794*cdf0e10cSrcweir case TST_DOUBLE: 795*cdf0e10cSrcweir query += TST_DOUBLE_F + " DOUBLE"; 796*cdf0e10cSrcweir break; 797*cdf0e10cSrcweir case TST_INT: 798*cdf0e10cSrcweir query += TST_INT_F + " INT"; 799*cdf0e10cSrcweir break; 800*cdf0e10cSrcweir case TST_STRING: 801*cdf0e10cSrcweir query += TST_STRING_F + " TEXT"; 802*cdf0e10cSrcweir break; 803*cdf0e10cSrcweir default: query += empty_col_name + (c++) + " INT"; 804*cdf0e10cSrcweir if (c == 1) { 805*cdf0e10cSrcweir query += " NOT NULL AUTO_INCREMENT"; 806*cdf0e10cSrcweir } 807*cdf0e10cSrcweir } 808*cdf0e10cSrcweir } 809*cdf0e10cSrcweir query += ", PRIMARY KEY (" + empty_col_name + "0)"; 810*cdf0e10cSrcweir query += ")"; 811*cdf0e10cSrcweir statement.execute(query); 812*cdf0e10cSrcweir } 813*cdf0e10cSrcweir 814*cdf0e10cSrcweir /** 815*cdf0e10cSrcweir * Drops table. 816*cdf0e10cSrcweir * @param statement object used for executing a static SQL 817*cdf0e10cSrcweir * statement and obtaining the results produced by it. 818*cdf0e10cSrcweir * @param table Test table name. 819*cdf0e10cSrcweir */ 820*cdf0e10cSrcweir protected void dropMySQLTable(Statement statement, String tbl_name) 821*cdf0e10cSrcweir throws java.sql.SQLException { 822*cdf0e10cSrcweir statement.executeUpdate("drop table if exists " + tbl_name); 823*cdf0e10cSrcweir } 824*cdf0e10cSrcweir } 825