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 import com.sun.star.beans.PropertyValue; 25 import com.sun.star.beans.XPropertySet; 26 import com.sun.star.container.XNameAccess; 27 import com.sun.star.frame.XStorable; 28 import com.sun.star.frame.XModel; 29 import com.sun.star.sdb.XOfficeDatabaseDocument; 30 import com.sun.star.sdbc.SQLException; 31 import com.sun.star.sdbc.XCloseable; 32 import com.sun.star.sdbc.XConnection; 33 import com.sun.star.sdbc.XStatement; 34 import com.sun.star.uno.UnoRuntime; 35 import com.sun.star.io.IOException; 36 import com.sun.star.sdb.XDocumentDataSource; 37 import com.sun.star.sdbc.XDataSource; 38 import com.sun.star.uno.XComponentContext; 39 import java.io.File; 40 41 import com.sun.star.util.CloseVetoException; 42 import java.io.File; 43 44 /** 45 * 46 * @author fs93730 47 */ 48 public class HsqlDatabase 49 { 50 XComponentContext m_context; 51 // the URL of the temporary file used for the database document 52 String m_databaseDocumentFile; 53 // the database document 54 XOfficeDatabaseDocument m_databaseDocument; 55 // the data source belonging to the database document 56 // the default connection 57 XConnection m_connection; 58 59 // -------------------------------------------------------------------------------------------------------- HsqlDatabase( XComponentContext _context )60 public HsqlDatabase( XComponentContext _context ) throws Exception 61 { 62 m_context = _context; 63 createDBDocument(); 64 } 65 66 // -------------------------------------------------------------------------------------------------------- HsqlDatabase( XComponentContext _context, String _existingDocumentURL )67 public HsqlDatabase( XComponentContext _context, String _existingDocumentURL ) throws Exception 68 { 69 m_context = _context; 70 createDBDocument( _existingDocumentURL ); 71 } 72 73 // -------------------------------------------------------------------------------------------------------- createDBDocument( String _docURL )74 private void createDBDocument( String _docURL ) throws Exception 75 { 76 m_databaseDocumentFile = _docURL; 77 78 XNameAccess dbContext = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, 79 m_context.getServiceManager().createInstanceWithContext( "com.sun.star.sdb.DatabaseContext", m_context ) ); 80 XDocumentDataSource dataSource = (XDocumentDataSource)UnoRuntime.queryInterface( XDocumentDataSource.class, 81 dbContext.getByName( _docURL ) ); 82 83 m_databaseDocument = dataSource.getDatabaseDocument(); 84 } 85 86 /** creates an empty database document in a temporary location 87 */ createDBDocument()88 private void createDBDocument() throws Exception 89 { 90 File documentFile = File.createTempFile("testdb",".odb"); 91 documentFile.deleteOnExit(); 92 m_databaseDocumentFile = URLHelper.getFileURLFromSystemPath( documentFile ); 93 94 m_databaseDocument = (XOfficeDatabaseDocument)UnoRuntime.queryInterface( 95 XOfficeDatabaseDocument.class, m_context.getServiceManager().createInstanceWithContext( 96 "com.sun.star.sdb.OfficeDatabaseDocument", m_context ) ); 97 98 XPropertySet dsProperties = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, m_databaseDocument.getDataSource() ); 99 dsProperties.setPropertyValue("URL", "sdbc:embedded:hsqldb"); 100 101 XStorable storable = (XStorable)UnoRuntime.queryInterface( XStorable.class, m_databaseDocument ); 102 storable.storeAsURL( m_databaseDocumentFile, new PropertyValue[]{} ); 103 } 104 105 /** returns a connection to the database 106 * 107 * Multiple calls to this method return the same connection. The HsqlDatabase object keeps 108 * the ownership of the connection, so you don't need to (and should not) dispose/close it. 109 * 110 */ defaultConnection()111 public XConnection defaultConnection() throws SQLException 112 { 113 if ( m_connection != null ) 114 return m_connection; 115 m_connection = m_databaseDocument.getDataSource().getConnection(new String(),new String()); 116 return m_connection; 117 } 118 119 /** executes the given SQL statement via the defaultConnection 120 */ executeSQL( String statementString )121 public void executeSQL( String statementString ) throws SQLException 122 { 123 XStatement statement = defaultConnection().createStatement(); 124 statement.execute( statementString ); 125 } 126 127 /** stores the database document 128 */ store()129 public void store() throws IOException 130 { 131 if ( m_databaseDocument != null ) 132 { 133 XStorable storeDoc = (XStorable)UnoRuntime.queryInterface( XStorable.class, 134 m_databaseDocument ); 135 storeDoc.store(); 136 } 137 } 138 139 /** closes the database document 140 * 141 * Any CloseVetoExceptions fired by third parties are ignored, and any reference to the 142 * database document is released. 143 */ close()144 public void close() 145 { 146 // close connection 147 XCloseable closeConn = (XCloseable)UnoRuntime.queryInterface( XCloseable.class, 148 m_connection ); 149 if ( closeConn != null ) 150 { 151 try 152 { 153 closeConn.close(); 154 } 155 catch( SQLException e ) 156 { 157 } 158 } 159 m_connection = null; 160 161 // close document 162 com.sun.star.util.XCloseable closeDoc = (com.sun.star.util.XCloseable)UnoRuntime.queryInterface( 163 com.sun.star.util.XCloseable.class, m_databaseDocument ); 164 if ( closeDoc != null ) 165 { 166 try 167 { 168 closeDoc.close( true ); 169 } 170 catch( CloseVetoException e ) 171 { 172 } 173 } 174 m_databaseDocument = null; 175 } 176 177 /** closes the document, and deletes the underlying file 178 */ closeAndDelete()179 public void closeAndDelete() 180 { 181 close(); 182 183 if ( m_databaseDocumentFile != null ) 184 { 185 try 186 { 187 File file = new File(m_databaseDocumentFile); 188 file.delete(); 189 } 190 catch(Exception e) 191 { 192 } 193 m_databaseDocumentFile = null; 194 } 195 } 196 197 /** returns the underlying database document 198 */ getDatabaseDocument()199 public XOfficeDatabaseDocument getDatabaseDocument() 200 { 201 return m_databaseDocument; 202 } 203 204 /** returns the associated data source 205 */ getDataSource()206 public XDataSource getDataSource() 207 { 208 return m_databaseDocument.getDataSource(); 209 } 210 211 /** returns the model interface of the underlying database document 212 */ getModel()213 XModel getModel() 214 { 215 return (XModel)UnoRuntime.queryInterface( XModel.class, m_databaseDocument ); 216 } 217 218 /** drops the table with a given name 219 220 @param _name 221 the name of the table to drop 222 @param _ifExists 223 TRUE if it should be dropped only when it exists. 224 */ dropTable( String _name, boolean _ifExists )225 public void dropTable( String _name, boolean _ifExists ) throws SQLException 226 { 227 String dropStatement = "DROP TABLE \"" + _name; 228 if ( _ifExists ) 229 dropStatement += "\" IF EXISTS"; 230 executeSQL( dropStatement ); 231 } 232 233 /** returns the URL of the ODB document represented by this instance 234 */ getDocumentURL()235 public String getDocumentURL() 236 { 237 return m_databaseDocumentFile; 238 } 239 240 /** creates a row set operating the database, with a given command/type 241 */ createRowSet( int _commandType, String _command )242 public RowSet createRowSet( int _commandType, String _command ) 243 { 244 return new RowSet( m_context, getDocumentURL(), _commandType, _command ); 245 } 246 finalize()247 protected void finalize() throws Throwable 248 { 249 closeAndDelete(); 250 super.finalize(); 251 } 252 } 253