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