xref: /trunk/test/testuno/source/testlib/uno/DBUtil.java (revision cebb507a3778fe3ad6e9619f5c9b011689597537)
1e6e6073dSLiu Zhe /**************************************************************
2e6e6073dSLiu Zhe  *
3e6e6073dSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4e6e6073dSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5e6e6073dSLiu Zhe  * distributed with this work for additional information
6e6e6073dSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7e6e6073dSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8e6e6073dSLiu Zhe  * "License"); you may not use this file except in compliance
9e6e6073dSLiu Zhe  * with the License.  You may obtain a copy of the License at
10e6e6073dSLiu Zhe  *
11e6e6073dSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12e6e6073dSLiu Zhe  *
13e6e6073dSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14e6e6073dSLiu Zhe  * software distributed under the License is distributed on an
15e6e6073dSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16e6e6073dSLiu Zhe  * KIND, either express or implied.  See the License for the
17e6e6073dSLiu Zhe  * specific language governing permissions and limitations
18e6e6073dSLiu Zhe  * under the License.
19e6e6073dSLiu Zhe  *
20e6e6073dSLiu Zhe  *************************************************************/
21e6e6073dSLiu Zhe 
22e6e6073dSLiu Zhe package testlib.uno;
23e6e6073dSLiu Zhe 
24e6e6073dSLiu Zhe import com.sun.star.beans.PropertyValue;
25e6e6073dSLiu Zhe import com.sun.star.beans.PropertyState;
26e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet;
27e6e6073dSLiu Zhe import com.sun.star.container.ElementExistException;
28e6e6073dSLiu Zhe import com.sun.star.container.XNameAccess;
29e6e6073dSLiu Zhe import com.sun.star.frame.XStorable;
30e6e6073dSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
31e6e6073dSLiu Zhe import com.sun.star.sdb.XDocumentDataSource;
32e6e6073dSLiu Zhe import com.sun.star.sdb.XOfficeDatabaseDocument;
33e6e6073dSLiu Zhe import com.sun.star.sdbc.SQLException;
34e6e6073dSLiu Zhe import com.sun.star.sdbc.XCloseable;
35e6e6073dSLiu Zhe import com.sun.star.sdbc.XConnection;
36e6e6073dSLiu Zhe import com.sun.star.sdbc.XDataSource;
37e6e6073dSLiu Zhe import com.sun.star.sdbc.XStatement;
38e6e6073dSLiu Zhe import com.sun.star.sdbcx.XAppend;
39e6e6073dSLiu Zhe import com.sun.star.sdbcx.XTablesSupplier;
40e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime;
41e6e6073dSLiu Zhe import com.sun.star.util.CloseVetoException;
42e6e6073dSLiu Zhe 
43e6e6073dSLiu Zhe import java.util.HashMap;
44e6e6073dSLiu Zhe import java.util.Iterator;
45e6e6073dSLiu Zhe import java.util.Set;
46e6e6073dSLiu Zhe import java.io.File;
47e6e6073dSLiu Zhe 
48e6e6073dSLiu Zhe import org.openoffice.test.common.FileUtil;
49e6e6073dSLiu Zhe 
50e6e6073dSLiu Zhe 
51e6e6073dSLiu Zhe public class DBUtil {
52e6e6073dSLiu Zhe     // the service factory
53e6e6073dSLiu Zhe     protected static XMultiServiceFactory m_orb;
54e6e6073dSLiu Zhe     // the URL of the temporary file used for the database document
55e6e6073dSLiu Zhe     protected static String m_databaseDocumentFile;
56e6e6073dSLiu Zhe     // the database document
57e6e6073dSLiu Zhe     protected static XOfficeDatabaseDocument m_databaseDocument;
58e6e6073dSLiu Zhe     // the data source belonging to the database document
59e6e6073dSLiu Zhe     protected static XDataSource m_dataSource;
60e6e6073dSLiu Zhe     // the default connection
61e6e6073dSLiu Zhe     protected static XConnection m_connection;
62e6e6073dSLiu Zhe 
63e6e6073dSLiu Zhe 
64e6e6073dSLiu Zhe     static public void createNewDocument(final XMultiServiceFactory orb)
65e6e6073dSLiu Zhe             throws Exception {
66e6e6073dSLiu Zhe         m_orb = orb;
67e6e6073dSLiu Zhe         createDBDocument();
68e6e6073dSLiu Zhe     }
69e6e6073dSLiu Zhe 
70e6e6073dSLiu Zhe     static public void loadNewDocument(final XMultiServiceFactory orb,
71e6e6073dSLiu Zhe             final String _existingDocumentURL) throws Exception {
72e6e6073dSLiu Zhe         m_orb = orb;
73e6e6073dSLiu Zhe         getDocument(_existingDocumentURL);
74e6e6073dSLiu Zhe     }
75e6e6073dSLiu Zhe 
76e6e6073dSLiu Zhe     /**
77e6e6073dSLiu Zhe      * creates an empty database document in a temporary location
78e6e6073dSLiu Zhe      */
79e6e6073dSLiu Zhe     public static void createDBDocument() throws Exception {
80e6e6073dSLiu Zhe         final File documentFile = File.createTempFile("testdb", ".odb");
81e6e6073dSLiu Zhe         if (documentFile.exists())
82e6e6073dSLiu Zhe             documentFile.delete();
83e6e6073dSLiu Zhe         m_databaseDocumentFile = FileUtil.getUrl(documentFile);
84e6e6073dSLiu Zhe         m_databaseDocument = (XOfficeDatabaseDocument) UnoRuntime
85e6e6073dSLiu Zhe                 .queryInterface(
86e6e6073dSLiu Zhe                         XOfficeDatabaseDocument.class,
87e6e6073dSLiu Zhe                         m_orb.createInstance("com.sun.star.sdb.OfficeDatabaseDocument"));
88e6e6073dSLiu Zhe         m_dataSource = m_databaseDocument.getDataSource();
89e6e6073dSLiu Zhe 
90e6e6073dSLiu Zhe         final XPropertySet dsProperties = (XPropertySet) UnoRuntime
91e6e6073dSLiu Zhe                 .queryInterface(XPropertySet.class,
92e6e6073dSLiu Zhe                         m_databaseDocument.getDataSource());
93e6e6073dSLiu Zhe         dsProperties.setPropertyValue("URL", "sdbc:embedded:hsqldb");
94e6e6073dSLiu Zhe 
95e6e6073dSLiu Zhe         final XStorable storable = (XStorable) UnoRuntime.queryInterface(
96e6e6073dSLiu Zhe                 XStorable.class, m_databaseDocument);
97e6e6073dSLiu Zhe         storable.storeAsURL(m_databaseDocumentFile,
98e6e6073dSLiu Zhe                 new PropertyValue[] { new PropertyValue("PickListEntry", 0,
99e6e6073dSLiu Zhe                         false, PropertyState.DIRECT_VALUE) });
100e6e6073dSLiu Zhe     }
101e6e6073dSLiu Zhe 
102e6e6073dSLiu Zhe 
103e6e6073dSLiu Zhe     public static void getDocument(final String _docURL) throws Exception {
104e6e6073dSLiu Zhe         m_databaseDocumentFile = _docURL;
105e6e6073dSLiu Zhe 
106*cebb507aSLiu Zhe         final XNameAccess dbContext = (XNameAccess) UnoRuntime.queryInterface(
107e6e6073dSLiu Zhe                 XNameAccess.class,
108e6e6073dSLiu Zhe                 m_orb.createInstance("com.sun.star.sdb.DatabaseContext"));
109*cebb507aSLiu Zhe         final XDocumentDataSource dataSource = (XDocumentDataSource) UnoRuntime.queryInterface(
110e6e6073dSLiu Zhe                 XDocumentDataSource.class, dbContext.getByName(_docURL));
111e6e6073dSLiu Zhe 
112e6e6073dSLiu Zhe         m_databaseDocument = dataSource.getDatabaseDocument();
113e6e6073dSLiu Zhe         m_dataSource = m_databaseDocument.getDataSource();
114e6e6073dSLiu Zhe     }
115e6e6073dSLiu Zhe 
116e6e6073dSLiu Zhe     /**
117e6e6073dSLiu Zhe      * drops the table with a given name
118e6e6073dSLiu Zhe      *
119e6e6073dSLiu Zhe      * @param _name
120e6e6073dSLiu Zhe      *            the name of the table to drop
121e6e6073dSLiu Zhe      * @param _ifExists
122e6e6073dSLiu Zhe      *            TRUE if it should be dropped only when it exists.
123e6e6073dSLiu Zhe      */
124e6e6073dSLiu Zhe     static public void dropTable(final String _name, final boolean _ifExists)
125e6e6073dSLiu Zhe             throws SQLException {
126e6e6073dSLiu Zhe         final StringBuffer dropStatement = new StringBuffer("DROP TABLE \"");
127e6e6073dSLiu Zhe         dropStatement.append(_name);
128e6e6073dSLiu Zhe         if (_ifExists) {
129e6e6073dSLiu Zhe             dropStatement.append("\" IF EXISTS");
130e6e6073dSLiu Zhe         }
131e6e6073dSLiu Zhe         executeSQL(dropStatement.toString());
132e6e6073dSLiu Zhe     }
133e6e6073dSLiu Zhe 
134e6e6073dSLiu Zhe     static public void createTable(String _name,
135e6e6073dSLiu Zhe             HsqlColumnDescriptor[] _columns, final boolean _dropIfExists)
136e6e6073dSLiu Zhe             throws SQLException {
137e6e6073dSLiu Zhe         if (_dropIfExists) {
138e6e6073dSLiu Zhe             dropTable(_name, true);
139e6e6073dSLiu Zhe         }
140e6e6073dSLiu Zhe         createTable(_name, _columns);
141e6e6073dSLiu Zhe     }
142e6e6073dSLiu Zhe 
143e6e6073dSLiu Zhe     /**
144e6e6073dSLiu Zhe      * creates a table
145e6e6073dSLiu Zhe      */
146e6e6073dSLiu Zhe     static public void createTable(String _name, HsqlColumnDescriptor[] _columns)
147e6e6073dSLiu Zhe             throws SQLException {
148e6e6073dSLiu Zhe         StringBuffer createStatement = new StringBuffer(
149e6e6073dSLiu Zhe                 "CREATE CACHED TABLE \"");
150e6e6073dSLiu Zhe         createStatement.append(_name);
151e6e6073dSLiu Zhe         createStatement.append("\" ( ");
152e6e6073dSLiu Zhe 
153e6e6073dSLiu Zhe         String primaryKeyList = "";
154e6e6073dSLiu Zhe 
155e6e6073dSLiu Zhe         final HashMap foreignKeys = new HashMap();
156e6e6073dSLiu Zhe         final HashMap foreignKeyRefs = new HashMap();
157e6e6073dSLiu Zhe 
158e6e6073dSLiu Zhe         final HsqlColumnDescriptor[] columns = _columns;
159e6e6073dSLiu Zhe         for (int i = 0; i < columns.length; ++i) {
160e6e6073dSLiu Zhe             if (i > 0) {
161e6e6073dSLiu Zhe                 createStatement.append(", ");
162e6e6073dSLiu Zhe             }
163e6e6073dSLiu Zhe 
164e6e6073dSLiu Zhe             createStatement.append("\"" + columns[i].getName());
165e6e6073dSLiu Zhe             createStatement.append("\" " + columns[i].getTypeName());
166e6e6073dSLiu Zhe 
167e6e6073dSLiu Zhe             if (columns[i].isRequired()) {
168e6e6073dSLiu Zhe                 createStatement.append(" NOT NULL");
169e6e6073dSLiu Zhe             }
170e6e6073dSLiu Zhe 
171e6e6073dSLiu Zhe             if (columns[i].isPrimaryKey()) {
172e6e6073dSLiu Zhe                 if (primaryKeyList.length() > 0) {
173e6e6073dSLiu Zhe                     primaryKeyList += ", ";
174e6e6073dSLiu Zhe                 }
175e6e6073dSLiu Zhe                 primaryKeyList += "\"" + columns[i].getName() + "\"";
176e6e6073dSLiu Zhe             }
177e6e6073dSLiu Zhe 
178e6e6073dSLiu Zhe             if (columns[i].isForeignKey()) {
179e6e6073dSLiu Zhe                 final String foreignTable = columns[i].getForeignTable();
180e6e6073dSLiu Zhe 
181e6e6073dSLiu Zhe                 String foreignKeysForTable = foreignKeys
182e6e6073dSLiu Zhe                         .containsKey(foreignTable) ? (String) foreignKeys
183e6e6073dSLiu Zhe                         .get(foreignTable) : "";
184e6e6073dSLiu Zhe                 if (foreignKeysForTable.length() > 0) {
185e6e6073dSLiu Zhe                     foreignKeysForTable += ", ";
186e6e6073dSLiu Zhe                 }
187e6e6073dSLiu Zhe                 foreignKeysForTable += "\"" + columns[i].getName() + "\"";
188e6e6073dSLiu Zhe                 foreignKeys.put(foreignTable, foreignKeysForTable);
189e6e6073dSLiu Zhe 
190e6e6073dSLiu Zhe                 final StringBuffer foreignKeyRefsForTable = new StringBuffer(
191e6e6073dSLiu Zhe                         foreignKeyRefs.containsKey(foreignTable) ? (String) foreignKeyRefs
192e6e6073dSLiu Zhe                                 .get(foreignTable) : "");
193e6e6073dSLiu Zhe                 if (foreignKeyRefsForTable.length() > 0) {
194e6e6073dSLiu Zhe                     foreignKeyRefsForTable.append(", ");
195e6e6073dSLiu Zhe                 }
196e6e6073dSLiu Zhe                 foreignKeyRefsForTable.append("\""
197e6e6073dSLiu Zhe                         + columns[i].getForeignColumn() + "\"");
198e6e6073dSLiu Zhe                 foreignKeyRefs.put(foreignTable,
199e6e6073dSLiu Zhe                         foreignKeyRefsForTable.toString());
200e6e6073dSLiu Zhe             }
201e6e6073dSLiu Zhe         }
202e6e6073dSLiu Zhe 
203e6e6073dSLiu Zhe         if (primaryKeyList.length() > 0) {
204e6e6073dSLiu Zhe             createStatement.append(", PRIMARY KEY (");
205e6e6073dSLiu Zhe             createStatement.append(primaryKeyList);
206e6e6073dSLiu Zhe             createStatement.append(')');
207e6e6073dSLiu Zhe         }
208e6e6073dSLiu Zhe 
209e6e6073dSLiu Zhe         final Set foreignKeyTables = foreignKeys.keySet();
210e6e6073dSLiu Zhe         for (final Iterator foreignKey = foreignKeyTables.iterator(); foreignKey
211e6e6073dSLiu Zhe                 .hasNext();) {
212e6e6073dSLiu Zhe             final String foreignTable = (String) foreignKey.next();
213e6e6073dSLiu Zhe 
214e6e6073dSLiu Zhe             createStatement.append(", FOREIGN KEY (");
215e6e6073dSLiu Zhe             createStatement.append((String) foreignKeys.get(foreignTable));
216e6e6073dSLiu Zhe             createStatement.append(") REFERENCES \"");
217e6e6073dSLiu Zhe             createStatement.append(foreignTable);
218e6e6073dSLiu Zhe             createStatement.append("\"(");
219e6e6073dSLiu Zhe             createStatement.append((String) foreignKeyRefs.get(foreignTable));
220e6e6073dSLiu Zhe             createStatement.append(')');
221e6e6073dSLiu Zhe         }
222e6e6073dSLiu Zhe 
223e6e6073dSLiu Zhe         createStatement.append(')');
224e6e6073dSLiu Zhe 
225e6e6073dSLiu Zhe         // System.err.println( createStatement );
226e6e6073dSLiu Zhe         executeSQL(createStatement.toString());
227e6e6073dSLiu Zhe     }
228e6e6073dSLiu Zhe 
229e6e6073dSLiu Zhe 
230e6e6073dSLiu Zhe     /**
231e6e6073dSLiu Zhe      * executes the given SQL statement via the defaultConnection
232e6e6073dSLiu Zhe      */
233e6e6073dSLiu Zhe     static public void executeSQL(final String statementString)
234e6e6073dSLiu Zhe             throws SQLException {
235e6e6073dSLiu Zhe         final XStatement statement = defaultConnection().createStatement();
236e6e6073dSLiu Zhe         statement.execute(statementString);
237e6e6073dSLiu Zhe     }
238e6e6073dSLiu Zhe 
239e6e6073dSLiu Zhe     /**
240e6e6073dSLiu Zhe      * returns a connection to the database
241e6e6073dSLiu Zhe      *
242e6e6073dSLiu Zhe      * Multiple calls to this method return the same connection. The
243e6e6073dSLiu Zhe      * DbaseDatabase object keeps the ownership of the connection, so you don't
244e6e6073dSLiu Zhe      * need to (and should not) dispose/close it.
245e6e6073dSLiu Zhe      */
246e6e6073dSLiu Zhe     static public XConnection defaultConnection() throws SQLException {
247e6e6073dSLiu Zhe         if (m_connection == null)
248e6e6073dSLiu Zhe             m_connection = m_databaseDocument.getDataSource().getConnection("",
249e6e6073dSLiu Zhe                     "");
250e6e6073dSLiu Zhe 
251e6e6073dSLiu Zhe         return m_connection;
252e6e6073dSLiu Zhe     }
253e6e6073dSLiu Zhe 
254e6e6073dSLiu Zhe     /**
255e6e6073dSLiu Zhe      * closes the database document
256e6e6073dSLiu Zhe      *
257e6e6073dSLiu Zhe      * Any CloseVetoExceptions fired by third parties are ignored, and any
258e6e6073dSLiu Zhe      * reference to the database document is released.
259e6e6073dSLiu Zhe      */
260e6e6073dSLiu Zhe     static public void close() {
261e6e6073dSLiu Zhe         // close connection
262*cebb507aSLiu Zhe         final XCloseable closeConn = (XCloseable) UnoRuntime.queryInterface(
263e6e6073dSLiu Zhe                 XCloseable.class, m_connection != null ? m_connection : null);
264e6e6073dSLiu Zhe         if (closeConn != null) {
265e6e6073dSLiu Zhe             try {
266e6e6073dSLiu Zhe                 closeConn.close();
267e6e6073dSLiu Zhe             } catch (SQLException e) {
268e6e6073dSLiu Zhe             }
269e6e6073dSLiu Zhe         }
270e6e6073dSLiu Zhe         m_connection = null;
271e6e6073dSLiu Zhe 
272e6e6073dSLiu Zhe         // close document
273*cebb507aSLiu Zhe         final com.sun.star.util.XCloseable closeDoc = (com.sun.star.util.XCloseable) UnoRuntime
274e6e6073dSLiu Zhe                 .queryInterface(com.sun.star.util.XCloseable.class,
275e6e6073dSLiu Zhe                         m_databaseDocument);
276e6e6073dSLiu Zhe         if (closeDoc != null) {
277e6e6073dSLiu Zhe             try {
278e6e6073dSLiu Zhe                 closeDoc.close(true);
279e6e6073dSLiu Zhe             } catch (CloseVetoException e) {
280e6e6073dSLiu Zhe             }
281e6e6073dSLiu Zhe         }
282e6e6073dSLiu Zhe         m_databaseDocument = null;
283e6e6073dSLiu Zhe     }
284e6e6073dSLiu Zhe 
285e6e6073dSLiu Zhe     /**
286e6e6073dSLiu Zhe      * returns the underlying database document
287e6e6073dSLiu Zhe      */
288e6e6073dSLiu Zhe     static public XOfficeDatabaseDocument getDatabaseDocument() {
289e6e6073dSLiu Zhe         return m_databaseDocument;
290e6e6073dSLiu Zhe     }
291e6e6073dSLiu Zhe 
292e6e6073dSLiu Zhe     static public String getDocumentURL() {
293e6e6073dSLiu Zhe         return m_databaseDocumentFile;
294e6e6073dSLiu Zhe     }
295e6e6073dSLiu Zhe }
296