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