1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package connectivity.tools; 24 25 import com.sun.star.beans.PropertyValue; 26 import com.sun.star.beans.XPropertySet; 27 import com.sun.star.frame.XStorable; 28 import com.sun.star.lang.XMultiServiceFactory; 29 import com.sun.star.sdb.XOfficeDatabaseDocument; 30 import com.sun.star.sdbc.SQLException; 31 import com.sun.star.uno.UnoRuntime; 32 33 import helper.URLHelper; 34 import java.io.File; 35 36 class FlatFileDatabase extends AbstractDatabase 37 { 38 // -------------------------------------------------------------------------------------------------------- FlatFileDatabase( final XMultiServiceFactory i_orb, final String i_urlSubScheme )39 protected FlatFileDatabase( final XMultiServiceFactory i_orb, final String i_urlSubScheme ) throws Exception 40 { 41 super(i_orb); 42 m_urlSubScheme = i_urlSubScheme; 43 createDBDocument(); 44 } 45 46 // -------------------------------------------------------------------------------------------------------- FlatFileDatabase(final XMultiServiceFactory i_orb, final String i_existingDocumentURL, final String i_urlSubScheme )47 protected FlatFileDatabase(final XMultiServiceFactory i_orb, final String i_existingDocumentURL, 48 final String i_urlSubScheme ) throws Exception 49 { 50 super( i_orb, i_existingDocumentURL ); 51 m_urlSubScheme = i_urlSubScheme; 52 53 final XPropertySet dsProperties = UnoRuntime.queryInterface(XPropertySet.class, m_databaseDocument.getDataSource()); 54 final String url = (String)dsProperties.getPropertyValue( "URL" ); 55 final String expectedURLPrefix = "sdbc:" + m_urlSubScheme + ":"; 56 if ( !url.startsWith( expectedURLPrefix ) ) 57 throw new IllegalArgumentException( i_existingDocumentURL + " is of wrong type" ); 58 59 final String location = url.substring( expectedURLPrefix.length() ); 60 m_tableFileLocation = new File( location ); 61 if ( m_tableFileLocation.isDirectory() ) 62 throw new IllegalArgumentException( "unsupported table file location (must be a folder)" ); 63 } 64 65 /** 66 * returns a {@link File} which represents the folder where the database's table files reside. 67 */ getTableFileLocation()68 public File getTableFileLocation() 69 { 70 return m_tableFileLocation; 71 } 72 73 /** creates an empty database document in a temporary location 74 */ createDBDocument()75 private void createDBDocument() throws Exception 76 { 77 final File documentFile = File.createTempFile( m_urlSubScheme, ".odb" ); 78 if ( documentFile.exists() ) 79 documentFile.delete(); 80 m_tableFileLocation = new File(documentFile.getParent() + File.separator + documentFile.getName().replace(".odb", "") + File.separator ); 81 m_tableFileLocation.mkdir(); 82 //subPath.deleteOnExit(); 83 m_databaseDocumentFile = URLHelper.getFileURLFromSystemPath(documentFile); 84 final String path = URLHelper.getFileURLFromSystemPath( m_tableFileLocation.getPath() ); 85 86 m_databaseDocument = UnoRuntime.queryInterface( XOfficeDatabaseDocument.class, 87 m_orb.createInstance("com.sun.star.sdb.OfficeDatabaseDocument")); 88 m_dataSource = new DataSource(m_orb, m_databaseDocument.getDataSource()); 89 90 final XPropertySet dsProperties = UnoRuntime.queryInterface(XPropertySet.class, m_databaseDocument.getDataSource()); 91 dsProperties.setPropertyValue("URL", "sdbc:" + m_urlSubScheme + ":" + path); 92 93 final XStorable storable = UnoRuntime.queryInterface( XStorable.class, m_databaseDocument ); 94 storable.storeAsURL( m_databaseDocumentFile, new PropertyValue[] { } ); 95 } 96 97 /** drops the table with a given name 98 99 @param _name 100 the name of the table to drop 101 @param _ifExists 102 TRUE if it should be dropped only when it exists. 103 */ dropTable(final String _name,final boolean _ifExists)104 public void dropTable(final String _name,final boolean _ifExists) throws SQLException 105 { 106 String dropStatement = "DROP TABLE \"" + _name; 107 executeSQL(dropStatement); 108 } 109 110 final String m_urlSubScheme; 111 File m_tableFileLocation = null; 112 } 113