xref: /trunk/main/connectivity/qa/connectivity/tools/FlatFileDatabase.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 package connectivity.tools;
28 
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.frame.XStorable;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.sdb.XOfficeDatabaseDocument;
34 import com.sun.star.sdbc.SQLException;
35 import com.sun.star.uno.UnoRuntime;
36 
37 import helper.URLHelper;
38 import java.io.File;
39 
40 class FlatFileDatabase extends AbstractDatabase
41 {
42     // --------------------------------------------------------------------------------------------------------
43     protected FlatFileDatabase( final XMultiServiceFactory i_orb, final String i_urlSubScheme ) throws Exception
44     {
45         super(i_orb);
46         m_urlSubScheme = i_urlSubScheme;
47         createDBDocument();
48     }
49 
50     // --------------------------------------------------------------------------------------------------------
51     protected FlatFileDatabase(final XMultiServiceFactory i_orb, final String i_existingDocumentURL,
52         final String i_urlSubScheme ) throws Exception
53     {
54         super( i_orb, i_existingDocumentURL );
55         m_urlSubScheme = i_urlSubScheme;
56 
57         final XPropertySet dsProperties = UnoRuntime.queryInterface(XPropertySet.class, m_databaseDocument.getDataSource());
58         final String url = (String)dsProperties.getPropertyValue( "URL" );
59         final String expectedURLPrefix = "sdbc:" + m_urlSubScheme + ":";
60         if ( !url.startsWith( expectedURLPrefix ) )
61             throw new IllegalArgumentException( i_existingDocumentURL + " is of wrong type" );
62 
63         final String location = url.substring( expectedURLPrefix.length() );
64         m_tableFileLocation = new File( location );
65         if ( m_tableFileLocation.isDirectory() )
66             throw new IllegalArgumentException( "unsupported table file location (must be a folder)" );
67     }
68 
69     /**
70      * returns a {@link File} which represents the folder where the database's table files reside.
71      */
72     public File getTableFileLocation()
73     {
74         return m_tableFileLocation;
75     }
76 
77     /** creates an empty database document in a temporary location
78      */
79     private void createDBDocument() throws Exception
80     {
81         final File documentFile = File.createTempFile( m_urlSubScheme, ".odb" );
82         if ( documentFile.exists() )
83             documentFile.delete();
84         m_tableFileLocation = new File(documentFile.getParent() + File.separator + documentFile.getName().replace(".odb", "") + File.separator );
85         m_tableFileLocation.mkdir();
86         //subPath.deleteOnExit();
87         m_databaseDocumentFile = URLHelper.getFileURLFromSystemPath(documentFile);
88         final String path = URLHelper.getFileURLFromSystemPath( m_tableFileLocation.getPath() );
89 
90         m_databaseDocument = UnoRuntime.queryInterface( XOfficeDatabaseDocument.class,
91             m_orb.createInstance("com.sun.star.sdb.OfficeDatabaseDocument"));
92         m_dataSource = new DataSource(m_orb, m_databaseDocument.getDataSource());
93 
94         final XPropertySet dsProperties = UnoRuntime.queryInterface(XPropertySet.class, m_databaseDocument.getDataSource());
95         dsProperties.setPropertyValue("URL", "sdbc:" + m_urlSubScheme + ":" + path);
96 
97         final XStorable storable = UnoRuntime.queryInterface( XStorable.class, m_databaseDocument );
98         storable.storeAsURL( m_databaseDocumentFile, new PropertyValue[] { } );
99     }
100 
101     /** drops the table with a given name
102 
103     @param _name
104     the name of the table to drop
105     @param _ifExists
106     TRUE if it should be dropped only when it exists.
107      */
108     public void dropTable(final String _name,final boolean _ifExists) throws SQLException
109     {
110         String dropStatement = "DROP TABLE \"" + _name;
111         executeSQL(dropStatement);
112     }
113 
114     final String    m_urlSubScheme;
115     File            m_tableFileLocation = null;
116 }
117