Tag: cws_src680_dba202f
User: fs      
Date: 06/01/23 04:16:52

Added:
 /dba/connectivity/qa/connectivity/tools/
  HsqlDatabase.java

Log:
 small wrapper around an ODB document file representing an embedded HSQL DB

File Changes:

Directory: /dba/connectivity/qa/connectivity/tools/
===================================================

File [added]: HsqlDatabase.java
Url: 
http://dba.openoffice.org/source/browse/dba/connectivity/qa/connectivity/tools/HsqlDatabase.java?rev=1.1.2.1&content-type=text/vnd.viewcvs-markup
Added lines: 142
----------------
/*
 * HsqlDatabase.java
 *
 * Created on 18. Januar 2006, 11:40
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package connectivity.tools;

import java.io.File;

import com.sun.star.uno.UnoRuntime;
import com.sun.star.sdb.*;
import com.sun.star.sdbc.*;
import com.sun.star.lang.*;
import com.sun.star.beans.*;
import com.sun.star.frame.*;
import com.sun.star.util.CloseVetoException;

/**
 *
 * @author fs93730
 */
public class HsqlDatabase
{
    // the service factory
    XMultiServiceFactory    m_orb;
    // the URL of the temporary file used for the database document
    String                  m_databaseDocumentFile;
    // the data source belonging to the database document
    XOfficeDatabaseDocument m_databaseDocument;
    // the default connection
    XConnection             m_connection;

    /** Creates a new instance of HsqlDatabase */
    public HsqlDatabase( XMultiServiceFactory orb ) throws Exception
    {
        m_orb = orb;
        createDBDocument();
    }

    private void createDBDocument() throws Exception
    {
        m_databaseDocumentFile = new String();
        String str = File.createTempFile("testdb",".odb").getCanonicalPath();
        str = str.replaceAll(" ","%20");
        str = "file:///" +str;
        m_databaseDocumentFile = str.replace('\\','/');

        m_databaseDocument = (XOfficeDatabaseDocument)UnoRuntime.queryInterface(
            XOfficeDatabaseDocument.class, m_orb.createInstance( 
"com.sun.star.sdb.OfficeDatabaseDocument" ) );
        XDataSource dataSource = m_databaseDocument.getDataSource();
        XPropertySet dsProperties = (XPropertySet)UnoRuntime.queryInterface( 
XPropertySet.class, dataSource );
        dsProperties.setPropertyValue("URL", "sdbc:embedded:hsqldb");

        XStorable storable = (XStorable)UnoRuntime.queryInterface( 
XStorable.class, m_databaseDocument );
        storable.storeAsURL(m_databaseDocumentFile,new PropertyValue[]{});
    }

    /** returns a connection to the database
     *
     * Multiple calls to this method return the same connection. The 
HsqlDatabase object keeps
     * the ownership of the connection, so you don't need to (and should not) 
dispose/close it.
     *
     */
    public XConnection defaultConnection() throws SQLException
    {
        if ( m_connection != null )
            return m_connection;
        m_connection  = m_databaseDocument.getDataSource().getConnection(new 
String(),new String());
        return m_connection;
    }

    /** executes the given SQL statement via the defaultConnection
     */
    public void executeStatement( String statementString ) throws SQLException
    {
        XStatement statement = defaultConnection().createStatement();
        statement.execute( statementString );
    }

    /** closes the database document
     *
     *  Any CloseVetoExceptions fired by third parties are ignored, and any 
reference to the
     *  database document is released.
     */
    public void close()
    {
        // close connection
        com.sun.star.sdbc.XCloseable closeConn = 
(com.sun.star.sdbc.XCloseable)UnoRuntime.queryInterface( XCloseable.class,
            m_connection );
        if ( closeConn != null )
        {
            try
            {
                closeConn.close();
            }
            catch( SQLException e )
            {
            }
        }
        m_connection = null;

        // close document
        com.sun.star.util.XCloseable closeDoc = 
(com.sun.star.util.XCloseable)UnoRuntime.queryInterface( XCloseable.class,
            m_databaseDocument );
        if ( closeDoc != null )
        {
            try
            {
                closeDoc.close( true );
            }
            catch( CloseVetoException e )
            {
            }
        }
        m_databaseDocument = null;
    }

    public String getDocumentURL()
    {
        return m_databaseDocumentFile;
    }

    protected void finalize() throws Throwable
    {
        close();

        try
        {
            File file = new File(m_databaseDocumentFile);
            file.delete();
        }
        catch(Exception e)
        {
        }
        super.finalize();
    }
}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to