1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 import com
.sun
.star
.beans
.PropertyValue
;
29 import com
.sun
.star
.beans
.XPropertySet
;
30 import com
.sun
.star
.container
.XNameAccess
;
31 import com
.sun
.star
.frame
.XStorable
;
32 import com
.sun
.star
.frame
.XModel
;
33 import com
.sun
.star
.sdb
.XOfficeDatabaseDocument
;
34 import com
.sun
.star
.sdbc
.SQLException
;
35 import com
.sun
.star
.sdbc
.XCloseable
;
36 import com
.sun
.star
.sdbc
.XConnection
;
37 import com
.sun
.star
.sdbc
.XStatement
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
39 import com
.sun
.star
.io
.IOException
;
40 import com
.sun
.star
.sdb
.XDocumentDataSource
;
41 import com
.sun
.star
.sdbc
.XDataSource
;
42 import com
.sun
.star
.uno
.XComponentContext
;
45 import com
.sun
.star
.util
.CloseVetoException
;
52 public class HsqlDatabase
54 XComponentContext m_context
;
55 // the URL of the temporary file used for the database document
56 String m_databaseDocumentFile
;
57 // the database document
58 XOfficeDatabaseDocument m_databaseDocument
;
59 // the data source belonging to the database document
60 // the default connection
61 XConnection m_connection
;
63 // --------------------------------------------------------------------------------------------------------
64 public HsqlDatabase( XComponentContext _context
) throws Exception
70 // --------------------------------------------------------------------------------------------------------
71 public HsqlDatabase( XComponentContext _context
, String _existingDocumentURL
) throws Exception
74 createDBDocument( _existingDocumentURL
);
77 // --------------------------------------------------------------------------------------------------------
78 private void createDBDocument( String _docURL
) throws Exception
80 m_databaseDocumentFile
= _docURL
;
82 XNameAccess dbContext
= (XNameAccess
)UnoRuntime
.queryInterface( XNameAccess
.class,
83 m_context
.getServiceManager().createInstanceWithContext( "com.sun.star.sdb.DatabaseContext", m_context
) );
84 XDocumentDataSource dataSource
= (XDocumentDataSource
)UnoRuntime
.queryInterface( XDocumentDataSource
.class,
85 dbContext
.getByName( _docURL
) );
87 m_databaseDocument
= dataSource
.getDatabaseDocument();
90 /** creates an empty database document in a temporary location
92 private void createDBDocument() throws Exception
94 File documentFile
= File
.createTempFile("testdb",".odb");
95 documentFile
.deleteOnExit();
96 m_databaseDocumentFile
= URLHelper
.getFileURLFromSystemPath( documentFile
);
98 m_databaseDocument
= (XOfficeDatabaseDocument
)UnoRuntime
.queryInterface(
99 XOfficeDatabaseDocument
.class, m_context
.getServiceManager().createInstanceWithContext(
100 "com.sun.star.sdb.OfficeDatabaseDocument", m_context
) );
102 XPropertySet dsProperties
= (XPropertySet
)UnoRuntime
.queryInterface( XPropertySet
.class, m_databaseDocument
.getDataSource() );
103 dsProperties
.setPropertyValue("URL", "sdbc:embedded:hsqldb");
105 XStorable storable
= (XStorable
)UnoRuntime
.queryInterface( XStorable
.class, m_databaseDocument
);
106 storable
.storeAsURL( m_databaseDocumentFile
, new PropertyValue
[]{} );
109 /** returns a connection to the database
111 * Multiple calls to this method return the same connection. The HsqlDatabase object keeps
112 * the ownership of the connection, so you don't need to (and should not) dispose/close it.
115 public XConnection
defaultConnection() throws SQLException
117 if ( m_connection
!= null )
119 m_connection
= m_databaseDocument
.getDataSource().getConnection(new String(),new String());
123 /** executes the given SQL statement via the defaultConnection
125 public void executeSQL( String statementString
) throws SQLException
127 XStatement statement
= defaultConnection().createStatement();
128 statement
.execute( statementString
);
131 /** stores the database document
133 public void store() throws IOException
135 if ( m_databaseDocument
!= null )
137 XStorable storeDoc
= (XStorable
)UnoRuntime
.queryInterface( XStorable
.class,
138 m_databaseDocument
);
143 /** closes the database document
145 * Any CloseVetoExceptions fired by third parties are ignored, and any reference to the
146 * database document is released.
151 XCloseable closeConn
= (XCloseable
)UnoRuntime
.queryInterface( XCloseable
.class,
153 if ( closeConn
!= null )
159 catch( SQLException e
)
166 com
.sun
.star
.util
.XCloseable closeDoc
= (com
.sun
.star
.util
.XCloseable
)UnoRuntime
.queryInterface(
167 com
.sun
.star
.util
.XCloseable
.class, m_databaseDocument
);
168 if ( closeDoc
!= null )
172 closeDoc
.close( true );
174 catch( CloseVetoException e
)
178 m_databaseDocument
= null;
181 /** closes the document, and deletes the underlying file
183 public void closeAndDelete()
187 if ( m_databaseDocumentFile
!= null )
191 File file
= new File(m_databaseDocumentFile
);
197 m_databaseDocumentFile
= null;
201 /** returns the underlying database document
203 public XOfficeDatabaseDocument
getDatabaseDocument()
205 return m_databaseDocument
;
208 /** returns the associated data source
210 public XDataSource
getDataSource()
212 return m_databaseDocument
.getDataSource();
215 /** returns the model interface of the underlying database document
219 return (XModel
)UnoRuntime
.queryInterface( XModel
.class, m_databaseDocument
);
222 /** drops the table with a given name
225 the name of the table to drop
227 TRUE if it should be dropped only when it exists.
229 public void dropTable( String _name
, boolean _ifExists
) throws SQLException
231 String dropStatement
= "DROP TABLE \"" + _name
;
233 dropStatement
+= "\" IF EXISTS";
234 executeSQL( dropStatement
);
237 /** returns the URL of the ODB document represented by this instance
239 public String
getDocumentURL()
241 return m_databaseDocumentFile
;
244 /** creates a row set operating the database, with a given command/type
246 public RowSet
createRowSet( int _commandType
, String _command
)
248 return new RowSet( m_context
, getDocumentURL(), _commandType
, _command
);
251 protected void finalize() throws Throwable