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