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 package com.sun.star.wizards.db; 23 24 import com.sun.star.awt.XWindow; 25 import com.sun.star.lang.XInitialization; 26 import com.sun.star.ui.dialogs.XExecutableDialog; 27 28 import com.sun.star.lang.IllegalArgumentException; 29 import com.sun.star.lang.WrappedTargetException; 30 import com.sun.star.lang.XMultiServiceFactory; 31 import com.sun.star.awt.VclWindowPeerAttribute; 32 import com.sun.star.awt.XWindowPeer; 33 import com.sun.star.beans.PropertyValue; 34 import com.sun.star.beans.UnknownPropertyException; 35 import com.sun.star.beans.XPropertySet; 36 import com.sun.star.container.XHierarchicalNameAccess; 37 import com.sun.star.container.XHierarchicalNameContainer; 38 import com.sun.star.container.XNameAccess; 39 import com.sun.star.container.XNameContainer; 40 import com.sun.star.frame.XModel; 41 import com.sun.star.frame.XStorable; 42 import com.sun.star.lang.XComponent; 43 import com.sun.star.sdbc.DataType; 44 import com.sun.star.sdb.XOfficeDatabaseDocument; 45 import com.sun.star.sdb.XDocumentDataSource; 46 import com.sun.star.sdb.tools.XConnectionTools; 47 import com.sun.star.sdbcx.XColumnsSupplier; 48 49 import com.sun.star.ucb.XSimpleFileAccess; 50 import com.sun.star.uno.UnoRuntime; 51 import com.sun.star.uno.XInterface; 52 import com.sun.star.uno.AnyConverter; 53 import com.sun.star.util.XCloseable; 54 import com.sun.star.util.XNumberFormatsSupplier; 55 56 import com.sun.star.task.XInteractionHandler; 57 import com.sun.star.sdb.XFormDocumentsSupplier; 58 import com.sun.star.sdb.XQueryDefinitionsSupplier; 59 import com.sun.star.sdb.XReportDocumentsSupplier; 60 import com.sun.star.sdbc.SQLException; 61 import com.sun.star.sdbc.XDatabaseMetaData; 62 import com.sun.star.sdbc.XDataSource; 63 import com.sun.star.sdbc.XResultSet; 64 import com.sun.star.sdbc.XRow; 65 import com.sun.star.sdb.XCompletedConnection; 66 import com.sun.star.lang.Locale; 67 import com.sun.star.lang.XSingleServiceFactory; 68 import com.sun.star.sdb.XQueriesSupplier; 69 import com.sun.star.sdbc.XConnection; 70 import com.sun.star.sdbcx.XTablesSupplier; 71 import com.sun.star.wizards.common.Configuration; 72 import com.sun.star.wizards.common.Desktop; 73 import com.sun.star.wizards.common.FileAccess; 74 import com.sun.star.wizards.common.JavaTools; 75 import com.sun.star.wizards.common.NamedValueCollection; 76 import com.sun.star.wizards.common.NumberFormatter; 77 import com.sun.star.wizards.common.Properties; 78 import com.sun.star.wizards.common.Resource; 79 import com.sun.star.wizards.common.SystemDialog; 80 import com.sun.star.uno.Any; 81 import com.sun.star.wizards.common.PropertyNames; 82 import java.util.Vector; 83 import java.util.logging.Level; 84 import java.util.logging.Logger; 85 86 public class DBMetaData 87 { 88 private XNameAccess xQueryNames; 89 public XDatabaseMetaData xDBMetaData; 90 private XDataSource m_dataSource; 91 private XPropertySet m_dataSourceSettings; 92 private XOfficeDatabaseDocument xModel; 93 private XPropertySet xDataSourcePropertySet; 94 public String[] DataSourceNames; 95 public String[] CommandNames; 96 public java.util.Vector CommandObjects = new Vector(1); 97 public Locale aLocale; 98 public int[] CommandTypes; 99 public String DataSourceName; 100 public com.sun.star.sdbc.XConnection DBConnection; 101 private com.sun.star.sdb.tools.XConnectionTools m_connectionTools; 102 public com.sun.star.lang.XMultiServiceFactory xMSF; 103 public XComponent xConnectionComponent; 104 105 private XNameAccess xNameAccess; 106 private XInterface xDatabaseContext; 107 private XWindowPeer xWindowPeer; 108 private String[] TableNames = new String[] {}; 109 private String[] QueryNames = new String[] {}; 110 111 protected int[][] WidthList; 112 protected static final int[] NumericTypes = { 113 DataType.TINYINT, // == -6; 114 DataType.BIGINT, // == -5 115 DataType.NUMERIC, // == - 2 116 DataType.DECIMAL, // == 3; 117 DataType.INTEGER, // == 4; 118 DataType.SMALLINT, // == 5; 119 DataType.FLOAT, // == 6; 120 DataType.REAL, // == 7; 121 DataType.DOUBLE, // == 8; 122 }; 123 protected static final int[] BinaryTypes = { //new int[12]; 124 DataType.BINARY, 125 DataType.VARBINARY, 126 DataType.LONGVARBINARY, 127 DataType.BLOB, 128 DataType.SQLNULL, 129 DataType.OBJECT, 130 DataType.DISTINCT, 131 DataType.STRUCT, 132 DataType.ARRAY, 133 DataType.CLOB, 134 DataType.REF 135 /* DataType.OTHER, */ 136 }; 137 138 private int iMaxColumnsInSelect; 139 private int iMaxColumnsInGroupBy; 140 private int iMaxColumnNameLength = -1; 141 private int iMaxTableNameLength = -1; 142 private boolean bPasswordIsRequired; 143 private final static int NOLIMIT = 9999999; 144 protected final static int RID_DB_COMMON = 1000; 145 private final static int INVALID = 9999999; 146 public TypeInspector oTypeInspector; 147 private NumberFormatter oNumberFormatter = null; 148 private long lDateCorrection = INVALID; 149 private boolean bdisposeConnection = false; 150 getDataSourcePropertySet()151 public XPropertySet getDataSourcePropertySet() 152 { 153 return xDataSourcePropertySet; 154 } 155 DBMetaData(XMultiServiceFactory xMSF)156 public DBMetaData(XMultiServiceFactory xMSF) 157 { 158 getInterfaces(xMSF); 159 InitializeWidthList(); 160 } 161 DBMetaData(XMultiServiceFactory xMSF, Locale _aLocale, NumberFormatter _oNumberFormatter)162 public DBMetaData(XMultiServiceFactory xMSF, Locale _aLocale, NumberFormatter _oNumberFormatter) 163 { 164 oNumberFormatter = _oNumberFormatter; 165 aLocale = _aLocale; 166 getInterfaces(xMSF); 167 InitializeWidthList(); 168 } 169 getNumberFormatter()170 public NumberFormatter getNumberFormatter() 171 { 172 if (oNumberFormatter == null) 173 { 174 try 175 { 176 XNumberFormatsSupplier xNumberFormatsSupplier = (XNumberFormatsSupplier) AnyConverter.toObject(XNumberFormatsSupplier.class, xDataSourcePropertySet.getPropertyValue("NumberFormatsSupplier")); 177 //TODO get the locale from the datasource 178 aLocale = Configuration.getOfficeLocale(xMSF); 179 oNumberFormatter = new NumberFormatter(xMSF, xNumberFormatsSupplier, aLocale); 180 lDateCorrection = oNumberFormatter.getNullDateCorrection(); 181 } 182 catch (Exception e) 183 { 184 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 185 } 186 } 187 return oNumberFormatter; 188 } 189 getNullDateCorrection()190 public long getNullDateCorrection() 191 { 192 if (lDateCorrection == INVALID) 193 { 194 if (oNumberFormatter == null) 195 { 196 oNumberFormatter = getNumberFormatter(); 197 } 198 lDateCorrection = oNumberFormatter.getNullDateCorrection(); 199 } 200 return lDateCorrection; 201 } 202 getInterfaces(XMultiServiceFactory xMSF)203 private void getInterfaces(XMultiServiceFactory xMSF) 204 { 205 try 206 { 207 this.xMSF = xMSF; 208 xDatabaseContext = (XInterface) xMSF.createInstance("com.sun.star.sdb.DatabaseContext"); 209 xNameAccess = UnoRuntime.queryInterface( XNameAccess.class, xDatabaseContext ); 210 DataSourceNames = xNameAccess.getElementNames(); 211 } 212 catch (Exception e) 213 { 214 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 215 } 216 } 217 setCommandTypes()218 public void setCommandTypes() 219 { 220 int TableCount; 221 int QueryCount; 222 int CommandCount; 223 int i; 224 int a; 225 TableCount = JavaTools.getArraylength(TableNames); 226 QueryCount = JavaTools.getArraylength(QueryNames); 227 CommandCount = TableCount + QueryCount; 228 CommandTypes = new int[CommandCount]; 229 if (TableCount > 0) 230 { 231 for (i = 0; i < TableCount; i++) 232 { 233 CommandTypes[i] = com.sun.star.sdb.CommandType.TABLE; 234 } 235 a = i; 236 for (i = 0; i < QueryCount; i++) 237 { 238 CommandTypes[a] = com.sun.star.sdb.CommandType.QUERY; 239 a += 1; 240 } 241 } 242 } 243 hasTableByName(String _stablename)244 public boolean hasTableByName(String _stablename) 245 { 246 return getTableNamesAsNameAccess().hasByName(_stablename); 247 } 248 249 @SuppressWarnings("unchecked") setTableByName(String _tableName)250 public void setTableByName(String _tableName) 251 { 252 CommandObject oTableObject = new CommandObject(_tableName, com.sun.star.sdb.CommandType.TABLE); 253 this.CommandObjects.addElement(oTableObject); 254 } 255 getTableByName(String _tablename)256 public CommandObject getTableByName(String _tablename) 257 { 258 return getCommandByName(_tablename, com.sun.star.sdb.CommandType.TABLE); 259 } 260 getQueryByName(String _queryname)261 public CommandObject getQueryByName(String _queryname) 262 { 263 return getCommandByName(_queryname, com.sun.star.sdb.CommandType.QUERY); 264 } 265 getCommandByName(String _commandname, int _commandtype)266 public CommandObject getCommandByName(String _commandname, int _commandtype) 267 { 268 CommandObject oCommand = null; 269 for (int i = 0; i < CommandObjects.size(); i++) 270 { 271 oCommand = (CommandObject) CommandObjects.elementAt(i); 272 if ((oCommand.Name.equals(_commandname)) && (oCommand.CommandType == _commandtype)) 273 { 274 return oCommand; 275 } 276 } 277 if (oCommand == null) 278 { 279 oCommand = new CommandObject(_commandname, _commandtype); 280 CommandObjects.addElement(oCommand); 281 } 282 return oCommand; 283 } 284 setQueryByName(String _QueryName)285 public void setQueryByName(String _QueryName) 286 { 287 CommandObject oQueryObject = new CommandObject(_QueryName, com.sun.star.sdb.CommandType.QUERY); 288 this.CommandObjects.addElement(oQueryObject); 289 } 290 291 public class CommandObject 292 { 293 294 private XNameAccess xColumns; 295 private XPropertySet xPropertySet; 296 private String Name; 297 private int CommandType; 298 CommandObject(String _CommandName, int _CommandType)299 public CommandObject(String _CommandName, int _CommandType) 300 { 301 try 302 { 303 Object oCommand; 304 this.Name = _CommandName; 305 this.CommandType = _CommandType; 306 // if (getTableNamesAsNameAccess() == null) 307 // { 308 // initCommandNames(); 309 // } 310 if (CommandType == com.sun.star.sdb.CommandType.TABLE) 311 { 312 oCommand = getTableNamesAsNameAccess().getByName(Name); 313 } 314 else 315 { 316 oCommand = getQueryNamesAsNameAccess().getByName(Name); 317 } 318 XColumnsSupplier xCommandCols = UnoRuntime.queryInterface( XColumnsSupplier.class, oCommand ); 319 xPropertySet = UnoRuntime.queryInterface( XPropertySet.class, oCommand ); 320 // TODO: Performance leak getColumns() take very long. 321 xColumns = UnoRuntime.queryInterface( XNameAccess.class, xCommandCols.getColumns() ); 322 } 323 catch (Exception e) 324 { 325 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 326 } 327 } getColumns()328 public XNameAccess getColumns() 329 { 330 return xColumns; 331 } getName()332 public String getName() 333 { 334 return Name; 335 } getPropertySet()336 public XPropertySet getPropertySet() 337 { 338 return xPropertySet; 339 } 340 } 341 hasEscapeProcessing(XPropertySet _xQueryPropertySet)342 public boolean hasEscapeProcessing(XPropertySet _xQueryPropertySet) 343 { 344 boolean bHasEscapeProcessing = false; 345 try 346 { 347 if (_xQueryPropertySet.getPropertySetInfo().hasPropertyByName("EscapeProcessing")) 348 { 349 bHasEscapeProcessing = AnyConverter.toBoolean(_xQueryPropertySet.getPropertyValue("EscapeProcessing")); 350 } 351 } 352 catch (Exception e) 353 { 354 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 355 } 356 return bHasEscapeProcessing; 357 } 358 getQueryNamesAsNameAccess()359 public XNameAccess getQueryNamesAsNameAccess() 360 { 361 XQueriesSupplier xDBQueries = UnoRuntime.queryInterface( XQueriesSupplier.class, DBConnection ); 362 xQueryNames = xDBQueries.getQueries(); 363 return xQueryNames; 364 } 365 getTableNamesAsNameAccess()366 public XNameAccess getTableNamesAsNameAccess() 367 { 368 XTablesSupplier xDBTables = UnoRuntime.queryInterface( XTablesSupplier.class, DBConnection ); 369 return xDBTables.getTables(); 370 } 371 getQueryNames()372 public String[] getQueryNames() 373 { 374 if (QueryNames != null) 375 { 376 if (QueryNames.length > 0) 377 { 378 return QueryNames; 379 } 380 } 381 QueryNames = getQueryNamesAsNameAccess().getElementNames(); 382 return QueryNames; 383 } 384 getTableNames()385 public String[] getTableNames() 386 { 387 if (TableNames != null) 388 { 389 if (TableNames.length > 0) 390 { 391 return TableNames; 392 } 393 } 394 TableNames = getTableNamesAsNameAccess().getElementNames(); 395 return TableNames; 396 } 397 InitializeWidthList()398 private void InitializeWidthList() 399 { 400 WidthList = new int[17][2]; 401 WidthList[0][0] = DataType.BIT; // == -7; 402 WidthList[1][0] = DataType.BOOLEAN; // = 16 403 WidthList[2][0] = DataType.TINYINT; // == -6; 404 WidthList[3][0] = DataType.BIGINT; // == -5; 405 WidthList[4][0] = DataType.LONGVARCHAR; // == -1; 406 WidthList[5][0] = DataType.CHAR; // == 1; 407 WidthList[6][0] = DataType.NUMERIC; // == 2; 408 WidthList[7][0] = DataType.DECIMAL; // == 3; [mit Nachkommastellen] 409 WidthList[8][0] = DataType.INTEGER; // == 4; 410 WidthList[9][0] = DataType.SMALLINT; // == 5; 411 WidthList[10][0] = DataType.FLOAT; // == 6; 412 WidthList[11][0] = DataType.REAL; // == 7; 413 WidthList[12][0] = DataType.DOUBLE; // == 8; 414 WidthList[13][0] = DataType.VARCHAR; // == 12; 415 WidthList[14][0] = DataType.DATE; // == 91; 416 WidthList[15][0] = DataType.TIME; // == 92; 417 WidthList[16][0] = DataType.TIMESTAMP; // == 93; 418 // NumericTypes are all types where aggregate functions can be performed on. 419 // Similarly to a major competitor date/time/timestamp fields are not included 420 421 422 } 423 isBinaryDataType(int _itype)424 public boolean isBinaryDataType(int _itype) 425 { 426 if (NumericTypes == null) 427 { 428 InitializeWidthList(); 429 } 430 return (JavaTools.FieldInIntTable(BinaryTypes, _itype) > -1); 431 } 432 getMaxTablesInSelect()433 public int getMaxTablesInSelect() 434 { 435 try 436 { 437 int itablecount = xDBMetaData.getMaxTablesInSelect(); 438 if (itablecount == 0) 439 { 440 return DBMetaData.NOLIMIT; 441 } 442 else 443 { 444 return itablecount; 445 } 446 } 447 catch (SQLException e) 448 { 449 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 450 return - 1; 451 } 452 } 453 getMaxColumnsInSelect()454 public int getMaxColumnsInSelect() 455 { 456 return iMaxColumnsInSelect; 457 } 458 getMaxColumnsInGroupBy()459 public int getMaxColumnsInGroupBy() 460 { 461 return iMaxColumnsInGroupBy; 462 } 463 setMaxColumnsInSelect()464 private void setMaxColumnsInSelect() throws SQLException 465 { 466 iMaxColumnsInSelect = xDBMetaData.getMaxColumnsInSelect(); 467 if (iMaxColumnsInSelect == 0) 468 { 469 iMaxColumnsInSelect = DBMetaData.NOLIMIT; 470 } 471 } 472 setMaxColumnsInGroupBy()473 private void setMaxColumnsInGroupBy() throws SQLException 474 { 475 iMaxColumnsInGroupBy = xDBMetaData.getMaxColumnsInGroupBy(); 476 if (iMaxColumnsInGroupBy == 0) 477 { 478 iMaxColumnsInGroupBy = DBMetaData.NOLIMIT; 479 } 480 } 481 getMaxColumnsInTable()482 public int getMaxColumnsInTable() throws SQLException 483 { 484 int iMaxColumnsInTable = xDBMetaData.getMaxColumnsInTable(); 485 if (iMaxColumnsInTable == 0) 486 { 487 iMaxColumnsInTable = DBMetaData.NOLIMIT; 488 } 489 return iMaxColumnsInTable; 490 } 491 getDataSourceObjects()492 private void getDataSourceObjects() throws Exception 493 { 494 try 495 { 496 xDBMetaData = DBConnection.getMetaData(); 497 getDataSourceInterfaces(); 498 setMaxColumnsInGroupBy(); 499 setMaxColumnsInSelect(); 500 } 501 catch (SQLException e) 502 { 503 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 504 } 505 } 506 ensureDataSourceSettings()507 private void ensureDataSourceSettings() throws UnknownPropertyException, WrappedTargetException 508 { 509 if ( m_dataSourceSettings != null ) 510 return; 511 512 XPropertySet dataSourceProperties = UnoRuntime.queryInterface( XPropertySet.class, getDataSource() ); 513 m_dataSourceSettings = UnoRuntime.queryInterface( XPropertySet.class, dataSourceProperties.getPropertyValue( "Settings" ) ); 514 } 515 isSQL92CheckEnabled()516 public boolean isSQL92CheckEnabled() 517 { 518 boolean isSQL92CheckEnabled = false; 519 try 520 { 521 ensureDataSourceSettings(); 522 isSQL92CheckEnabled = AnyConverter.toBoolean( m_dataSourceSettings.getPropertyValue( "EnableSQL92Check" ) ); 523 } 524 catch (Exception e) 525 { 526 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 527 } 528 return isSQL92CheckEnabled; 529 } 530 verifyName(String _sname, int _maxlen)531 public String verifyName(String _sname, int _maxlen) 532 { 533 if (_sname.length() > _maxlen) 534 { 535 return _sname.substring(0, _maxlen); 536 } 537 if (this.isSQL92CheckEnabled()) 538 { 539 return Desktop.removeSpecialCharacters(xMSF, Configuration.getOfficeLocale(xMSF), _sname); 540 } 541 return _sname; 542 } 543 getDataSource()544 public XDataSource getDataSource() 545 { 546 if (m_dataSource == null) 547 { 548 try 549 { 550 Object oDataSource = xNameAccess.getByName(DataSourceName); 551 m_dataSource = UnoRuntime.queryInterface( XDataSource.class, oDataSource ); 552 } 553 catch (com.sun.star.container.NoSuchElementException e) 554 { 555 } 556 catch (com.sun.star.lang.WrappedTargetException e) 557 { 558 } 559 } 560 return m_dataSource; 561 } 562 setDataSourceByName(String _DataSourceName)563 private void setDataSourceByName(String _DataSourceName) 564 { 565 try 566 { 567 this.DataSourceName = _DataSourceName; 568 getDataSourceInterfaces(); 569 XDocumentDataSource xDocu = UnoRuntime.queryInterface( XDocumentDataSource.class, getDataSource() ); 570 if (xDocu != null) 571 { 572 xModel = xDocu.getDatabaseDocument(); 573 } 574 } 575 catch (Exception e) 576 { 577 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 578 } 579 } 580 getDataSourceInterfaces()581 public void getDataSourceInterfaces() throws Exception 582 { 583 xDataSourcePropertySet = UnoRuntime.queryInterface( XPropertySet.class, getDataSource() ); 584 bPasswordIsRequired = ((Boolean) xDataSourcePropertySet.getPropertyValue("IsPasswordRequired")).booleanValue(); 585 } 586 getConnection(PropertyValue[] curproperties)587 public boolean getConnection(PropertyValue[] curproperties) 588 { 589 try 590 { 591 XConnection xConnection = null; 592 if (Properties.hasPropertyValue(curproperties, PropertyNames.ACTIVE_CONNECTION)) 593 { 594 xConnection = UnoRuntime.queryInterface( XConnection.class, Properties.getPropertyValue( curproperties, PropertyNames.ACTIVE_CONNECTION ) ); 595 if (xConnection != null) 596 { 597 com.sun.star.container.XChild child = UnoRuntime.queryInterface( com.sun.star.container.XChild.class, xConnection ); 598 599 m_dataSource = UnoRuntime.queryInterface( XDataSource.class, child.getParent() ); 600 XDocumentDataSource xDocu = UnoRuntime.queryInterface( XDocumentDataSource.class, m_dataSource ); 601 if (xDocu != null) 602 { 603 xModel = xDocu.getDatabaseDocument(); 604 } 605 XPropertySet xPSet = UnoRuntime.queryInterface( XPropertySet.class, m_dataSource ); 606 if (xPSet != null) 607 { 608 DataSourceName = AnyConverter.toString(xPSet.getPropertyValue(PropertyNames.PROPERTY_NAME)); 609 } 610 return getConnection(xConnection); 611 } 612 else 613 { 614 bdisposeConnection = true; 615 } 616 } 617 else 618 { 619 bdisposeConnection = true; 620 } 621 if (Properties.hasPropertyValue(curproperties, "DataSourceName")) 622 { 623 String sDataSourceName = AnyConverter.toString(Properties.getPropertyValue(curproperties, "DataSourceName")); 624 return getConnection(sDataSourceName); 625 } 626 else if (Properties.hasPropertyValue(curproperties, "DataSource")) 627 { 628 m_dataSource = UnoRuntime.queryInterface( XDataSource.class, Properties.getPropertyValue( curproperties, "DataSource" ) ); 629 XDocumentDataSource xDocu = UnoRuntime.queryInterface( XDocumentDataSource.class, this.m_dataSource ); 630 if (xDocu != null) 631 { 632 xModel = xDocu.getDatabaseDocument(); 633 } 634 return getConnection(m_dataSource); 635 } 636 if (Properties.hasPropertyValue(curproperties, "DatabaseLocation")) 637 { 638 String sDataSourceName = AnyConverter.toString(Properties.getPropertyValue(curproperties, "DatabaseLocation")); 639 return getConnection(sDataSourceName); 640 } 641 } 642 catch (IllegalArgumentException e) 643 { 644 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 645 } 646 catch (UnknownPropertyException e) 647 { 648 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 649 } 650 catch (WrappedTargetException e) 651 { 652 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 653 } 654 655 return false; 656 } 657 getConnection(String _DataSourceName)658 private boolean getConnection(String _DataSourceName) 659 { 660 setDataSourceByName(_DataSourceName); 661 return getConnection( getDataSource() ); 662 } 663 getConnection(com.sun.star.sdbc.XConnection _DBConnection)664 private boolean getConnection(com.sun.star.sdbc.XConnection _DBConnection) 665 { 666 try 667 { 668 this.DBConnection = _DBConnection; 669 this.m_connectionTools = UnoRuntime.queryInterface( XConnectionTools.class, this.DBConnection ); 670 getDataSourceObjects(); 671 return true; 672 } 673 catch (Exception e) 674 { 675 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 676 return false; 677 } 678 } 679 getConnection(XDataSource _dataSource)680 private boolean getConnection(XDataSource _dataSource) 681 { 682 Resource oResource = new Resource(xMSF, "Database", "dbw"); 683 try 684 { 685 int iMsg = 0; 686 boolean bgetConnection = false; 687 if (DBConnection != null) 688 { 689 xConnectionComponent.dispose(); 690 } 691 getDataSourceInterfaces(); 692 if (!bPasswordIsRequired) 693 { 694 DBConnection = _dataSource.getConnection(PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING); 695 bgetConnection = true; 696 } 697 else 698 { 699 XInteractionHandler xInteractionHandler = UnoRuntime.queryInterface( XInteractionHandler.class, xMSF.createInstance("com.sun.star.task.InteractionHandler") ); 700 boolean bExitLoop = true; 701 do 702 { 703 XCompletedConnection xCompleted2 = UnoRuntime.queryInterface( XCompletedConnection.class, _dataSource ); 704 try 705 { 706 DBConnection = xCompleted2.connectWithCompletion( xInteractionHandler ); 707 bgetConnection = DBConnection != null; 708 if (!bgetConnection) 709 { 710 bExitLoop = true; 711 } 712 } 713 catch (Exception exception) 714 { 715 // Note: WindowAttributes from toolkit/source/awt/vclxtoolkit.cxx 716 String sMsgNoConnection = oResource.getResText(RID_DB_COMMON + 14); 717 iMsg = showMessageBox("QueryBox", VclWindowPeerAttribute.RETRY_CANCEL, sMsgNoConnection); 718 bExitLoop = iMsg == 0; 719 bgetConnection = false; 720 } 721 } 722 while (!bExitLoop); 723 } 724 if (!bgetConnection) 725 { 726 String sMsgConnectionImpossible = oResource.getResText(RID_DB_COMMON + 35); 727 showMessageBox("ErrorBox", VclWindowPeerAttribute.OK, sMsgConnectionImpossible); 728 } 729 else 730 { 731 xConnectionComponent = UnoRuntime.queryInterface( XComponent.class, DBConnection ); 732 m_connectionTools = UnoRuntime.queryInterface( XConnectionTools.class, DBConnection ); 733 getDataSourceObjects(); 734 } 735 return bgetConnection; 736 } 737 catch (Exception e) 738 { 739 String sMsgConnectionImpossible = oResource.getResText(RID_DB_COMMON + 35); 740 showMessageBox("ErrorBox", VclWindowPeerAttribute.OK, sMsgConnectionImpossible); 741 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 742 return false; 743 } 744 } 745 getMaxColumnNameLength()746 public int getMaxColumnNameLength() 747 { 748 try 749 { 750 if (iMaxColumnNameLength <= 0) 751 { 752 iMaxColumnNameLength = xDBMetaData.getMaxColumnNameLength(); 753 } 754 return iMaxColumnNameLength; 755 } 756 catch (SQLException e) 757 { 758 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 759 return 0; 760 } 761 } 762 getMaxTableNameLength()763 public int getMaxTableNameLength() 764 { 765 try 766 { 767 if (iMaxTableNameLength <= 0) 768 { 769 iMaxTableNameLength = xDBMetaData.getMaxTableNameLength(); 770 } 771 return iMaxTableNameLength; 772 } 773 catch (SQLException e) 774 { 775 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 776 return 0; 777 } 778 } 779 supportsPrimaryKeys()780 public boolean supportsPrimaryKeys() 781 { 782 boolean supportsPrimaryKeys = false; 783 try 784 { 785 ensureDataSourceSettings(); 786 Any primaryKeySupport = (Any)m_dataSourceSettings.getPropertyValue( "PrimaryKeySupport" ); 787 if ( AnyConverter.isVoid( primaryKeySupport ) ) 788 supportsPrimaryKeys = supportsCoreSQLGrammar(); 789 else 790 supportsPrimaryKeys = AnyConverter.toBoolean( primaryKeySupport ); 791 } 792 catch ( Exception ex ) 793 { 794 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, ex ); 795 } 796 return supportsPrimaryKeys; 797 } 798 supportsCoreSQLGrammar()799 public boolean supportsCoreSQLGrammar() 800 { 801 try 802 { 803 return xDBMetaData.supportsCoreSQLGrammar(); 804 } 805 catch (SQLException e) 806 { 807 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 808 return false; 809 } 810 } 811 supportsAutoIncrementation()812 public boolean supportsAutoIncrementation() 813 { 814 return false; 815 } 816 supportsQueriesInFrom()817 public boolean supportsQueriesInFrom() 818 { 819 return m_connectionTools.getDataSourceMetaData().supportsQueriesInFrom(); 820 } 821 suggestName( final int i_objectType, final String i_baseName )822 public String suggestName( final int i_objectType, final String i_baseName ) throws IllegalArgumentException 823 { 824 return m_connectionTools.getObjectNames().suggestName( i_objectType, i_baseName ); 825 } 826 827 /** 828 * inserts a Query to a datasource; There is no validation if the queryname is already existing in the datasource 829 * @param oQuery 830 * @param QueryName 831 */ createQuery(SQLQueryComposer _oSQLQueryComposer, String _QueryName)832 public boolean createQuery(SQLQueryComposer _oSQLQueryComposer, String _QueryName) 833 { 834 try 835 { 836 XQueryDefinitionsSupplier xQueryDefinitionsSuppl = UnoRuntime.queryInterface( XQueryDefinitionsSupplier.class, m_dataSource ); 837 XNameAccess xQueryDefs = xQueryDefinitionsSuppl.getQueryDefinitions(); 838 XSingleServiceFactory xSSFQueryDefs = UnoRuntime.queryInterface( XSingleServiceFactory.class, xQueryDefs ); 839 Object oQuery = xSSFQueryDefs.createInstance(); //"com.sun.star.sdb.QueryDefinition" 840 XPropertySet xPSet = UnoRuntime.queryInterface( XPropertySet.class, oQuery ); 841 842 String s = _oSQLQueryComposer.m_xQueryAnalyzer.getQuery(); 843 xPSet.setPropertyValue(PropertyNames.COMMAND, s); 844 845 XNameContainer xNameCont = UnoRuntime.queryInterface( XNameContainer.class, xQueryDefs ); 846 m_connectionTools.getObjectNames().checkNameForCreate(com.sun.star.sdb.CommandType.QUERY, _QueryName); 847 xNameCont.insertByName(_QueryName, oQuery); 848 return true; 849 } 850 catch (WrappedTargetException exception) 851 { 852 SQLException sqlError = null; 853 try 854 { 855 sqlError = (SQLException) exception.TargetException; 856 } 857 catch (ClassCastException castError) 858 { 859 } 860 861 if (sqlError != null) 862 { 863 callSQLErrorMessageDialog(sqlError, null); 864 return false; 865 } 866 exception.printStackTrace(System.out); 867 } 868 catch (SQLException e) 869 { 870 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 871 } 872 catch (Exception e) 873 { 874 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 875 } 876 return false; 877 } 878 dispose()879 public void dispose() 880 { 881 if ((DBConnection != null) && (this.bdisposeConnection)) 882 { 883 xConnectionComponent.dispose(); 884 } 885 } 886 getReportDocuments()887 public XHierarchicalNameAccess getReportDocuments() 888 { 889 XReportDocumentsSupplier xReportDocumentSuppl = UnoRuntime.queryInterface( XReportDocumentsSupplier.class, this.xModel ); 890 xReportDocumentSuppl.getReportDocuments(); 891 return UnoRuntime.queryInterface( XHierarchicalNameAccess.class, xReportDocumentSuppl.getReportDocuments() ); 892 } 893 getFormDocuments()894 public XHierarchicalNameAccess getFormDocuments() 895 { 896 XFormDocumentsSupplier xFormDocumentSuppl = UnoRuntime.queryInterface( XFormDocumentsSupplier.class, xModel ); 897 return UnoRuntime.queryInterface( XHierarchicalNameAccess.class, xFormDocumentSuppl.getFormDocuments() ); 898 } 899 hasFormDocumentByName(String _sFormName)900 public boolean hasFormDocumentByName(String _sFormName) 901 { 902 XFormDocumentsSupplier xFormDocumentSuppl = UnoRuntime.queryInterface( XFormDocumentsSupplier.class, xModel ); 903 XNameAccess xFormNameAccess = UnoRuntime.queryInterface( XNameAccess.class, xFormDocumentSuppl.getFormDocuments() ); 904 return xFormNameAccess.hasByName(_sFormName); 905 } 906 addFormDocument(XComponent _xComponent)907 public void addFormDocument(XComponent _xComponent) 908 { 909 XHierarchicalNameAccess _xFormDocNameAccess = getFormDocuments(); 910 addDatabaseDocument(_xComponent, _xFormDocNameAccess, false); 911 } 912 addReportDocument(XComponent _xComponent, boolean _bcreatedynamicreport)913 public void addReportDocument(XComponent _xComponent, boolean _bcreatedynamicreport) 914 { 915 XHierarchicalNameAccess xReportDocNameAccess = getReportDocuments(); 916 addDatabaseDocument(_xComponent, xReportDocNameAccess, _bcreatedynamicreport); 917 } 918 919 /** 920 * adds the passed document as a report or a form to the database. Afterwards the document is deleted. 921 * the document may not be open 922 * @param _xComponent 923 * @param _xDocNameAccess 924 * @param _bcreateTemplate describes the type of the document: "form" or "report" 925 */ addDatabaseDocument(XComponent _xComponent, XHierarchicalNameAccess _xDocNameAccess, boolean i_createTemplate)926 public void addDatabaseDocument(XComponent _xComponent, XHierarchicalNameAccess _xDocNameAccess, boolean i_createTemplate) 927 { 928 try 929 { 930 XModel xDocumentModel = UnoRuntime.queryInterface( XModel.class, _xComponent ); 931 String documentURL = xDocumentModel.getURL(); 932 String basename = FileAccess.getBasename(documentURL, "/"); 933 XCloseable xCloseable = UnoRuntime.queryInterface( XCloseable.class, _xComponent ); 934 xCloseable.close(false); 935 936 NamedValueCollection creationArgs = new NamedValueCollection(); 937 creationArgs.put( PropertyNames.PROPERTY_NAME, basename ); 938 creationArgs.put( PropertyNames.URL, documentURL ); 939 creationArgs.put( "AsTemplate", i_createTemplate ); 940 XMultiServiceFactory xDocMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, _xDocNameAccess ); 941 Object oDBDocument = xDocMSF.createInstanceWithArguments( "com.sun.star.sdb.DocumentDefinition", creationArgs.getPropertyValues() ); 942 XHierarchicalNameContainer xHier = UnoRuntime.queryInterface( XHierarchicalNameContainer.class, _xDocNameAccess ); 943 String sdocname = Desktop.getUniqueName(_xDocNameAccess, basename); 944 xHier.insertByHierarchicalName(sdocname, oDBDocument); 945 XInterface xInterface = (XInterface) xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess"); 946 XSimpleFileAccess xSimpleFileAccess = UnoRuntime.queryInterface( XSimpleFileAccess.class, xInterface ); 947 xSimpleFileAccess.kill(documentURL); 948 } 949 catch (Exception e) 950 { 951 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 952 } 953 } 954 createTypeInspector()955 public void createTypeInspector() throws SQLException 956 { 957 oTypeInspector = new TypeInspector(xDBMetaData.getTypeInfo()); 958 } 959 getDBDataTypeInspector()960 public TypeInspector getDBDataTypeInspector() 961 { 962 return oTypeInspector; 963 } 964 StringsFromResultSet(XResultSet _xResultSet, int _icol)965 private String[] StringsFromResultSet(XResultSet _xResultSet, int _icol) 966 { 967 String[] sColValues = null; 968 try 969 { 970 XRow xRow = UnoRuntime.queryInterface( XRow.class, _xResultSet ); 971 Vector aColVector = new Vector(); 972 while (_xResultSet.next()) 973 { 974 aColVector.addElement(xRow.getString(_icol)); 975 } 976 sColValues = new String[aColVector.size()]; 977 aColVector.toArray(sColValues); 978 } 979 catch (SQLException e) 980 { 981 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 982 } 983 return sColValues; 984 } 985 getCatalogNames()986 public String[] getCatalogNames() 987 { 988 try 989 { 990 XResultSet xResultSet = xDBMetaData.getCatalogs(); 991 return StringsFromResultSet(xResultSet, 1); 992 } 993 catch (SQLException e) 994 { 995 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 996 return null; 997 } 998 } 999 getSchemaNames()1000 public String[] getSchemaNames() 1001 { 1002 try 1003 { 1004 XResultSet xResultSet = xDBMetaData.getSchemas(); 1005 return StringsFromResultSet(xResultSet, 1); 1006 } 1007 catch (SQLException e) 1008 { 1009 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 1010 return null; 1011 } 1012 } 1013 storeDatabaseDocumentToTempPath(XComponent _xcomponent, String _storename)1014 public boolean storeDatabaseDocumentToTempPath(XComponent _xcomponent, String _storename) 1015 { 1016 try 1017 { 1018 XInterface xInterface = (XInterface) xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess"); 1019 XSimpleFileAccess xSimpleFileAccess = UnoRuntime.queryInterface( XSimpleFileAccess.class, xInterface ); 1020 String storepath = FileAccess.getOfficePath(xMSF, "Temp", xSimpleFileAccess) + "/" + _storename; 1021 XStorable xStoreable = UnoRuntime.queryInterface( XStorable.class, _xcomponent ); 1022 PropertyValue[] oStoreProperties = new PropertyValue[1]; 1023 oStoreProperties[0] = Properties.createProperty("FilterName", "writer8"); 1024 storepath += ".odt"; 1025 xStoreable.storeAsURL(storepath, oStoreProperties); 1026 return true; 1027 } 1028 catch (Exception e) 1029 { 1030 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 1031 return false; 1032 } 1033 } 1034 showMessageBox(String windowServiceName, int windowAttribute, String MessageText)1035 public int showMessageBox(String windowServiceName, int windowAttribute, String MessageText) 1036 { 1037 if (getWindowPeer() != null) 1038 { 1039 return SystemDialog.showMessageBox(xMSF, xWindowPeer, windowServiceName, windowAttribute, MessageText); 1040 } 1041 else 1042 { 1043 return SystemDialog.showMessageBox(xMSF, windowServiceName, windowAttribute, MessageText); 1044 } 1045 } 1046 1047 /** 1048 * @return Returns the xWindowPeer. 1049 */ getWindowPeer()1050 public XWindowPeer getWindowPeer() 1051 { 1052 return xWindowPeer; 1053 } 1054 1055 /** 1056 * @param windowPeer The xWindowPeer to set. 1057 * Should be called as soon as a Windowpeer of a wizard dialog is available 1058 * The windowpeer is needed to call a Messagebox 1059 */ setWindowPeer(XWindowPeer windowPeer)1060 public void setWindowPeer(XWindowPeer windowPeer) 1061 { 1062 xWindowPeer = windowPeer; 1063 } 1064 callSQLErrorMessageDialog(SQLException oSQLException, XWindow _xWindow)1065 public void callSQLErrorMessageDialog(SQLException oSQLException, XWindow _xWindow) 1066 { 1067 try 1068 { 1069 Object oDialog = xMSF.createInstance("com.sun.star.sdb.ErrorMessageDialog"); 1070 XInitialization xInitialization = UnoRuntime.queryInterface( XInitialization.class, oDialog ); 1071 PropertyValue[] aPropertyValue = new PropertyValue[2]; 1072 aPropertyValue[0] = Properties.createProperty("SQLException", oSQLException); 1073 aPropertyValue[1] = Properties.createProperty("ParentWindow", _xWindow); 1074 xInitialization.initialize(aPropertyValue); 1075 XExecutableDialog xExecutableDialog = UnoRuntime.queryInterface( XExecutableDialog.class, oDialog ); 1076 xExecutableDialog.execute(); 1077 } 1078 catch (com.sun.star.uno.Exception ex) 1079 { 1080 Logger.getLogger( getClass().getName() ).log( Level.SEVERE, "error calling the error dialog", ex ); 1081 } 1082 } 1083 finish()1084 public void finish() 1085 { 1086 xQueryNames = null; 1087 xNameAccess = null; 1088 xDatabaseContext = null; 1089 xDBMetaData = null; 1090 m_dataSource = null; 1091 xModel = null; 1092 xDataSourcePropertySet = null; 1093 xWindowPeer = null; 1094 DBConnection = null; 1095 m_connectionTools = null; 1096 xMSF = null; 1097 xConnectionComponent = null; 1098 CommandObjects = null; 1099 } 1100 } 1101