User: obo Date: 2006/07/10 07:18:10 Modified: dba/connectivity/qa/connectivity/tools/HsqlDatabase.java
Log: INTEGRATION: CWS qiq (1.2.54); FILE MERGED 2006/06/29 10:17:15 fs 1.2.54.4: support for foreign keys 2006/06/27 20:14:11 fs 1.2.54.3: allow table creation via SDBCX means 2006/06/26 21:05:42 fs 1.2.54.2: +createRowSet 2006/06/22 09:20:31 fs 1.2.54.1: some more helpers File Changes: Directory: /dba/connectivity/qa/connectivity/tools/ =================================================== File [changed]: HsqlDatabase.java Url: http://dba.openoffice.org/source/browse/dba/connectivity/qa/connectivity/tools/HsqlDatabase.java?r1=1.2&r2=1.3 Delta lines: +116 -19 ---------------------- --- HsqlDatabase.java 6 Feb 2006 16:43:13 -0000 1.2 +++ HsqlDatabase.java 10 Jul 2006 14:18:08 -0000 1.3 @@ -35,15 +35,25 @@ package connectivity.tools; +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.XPropertySet; +import com.sun.star.container.ElementExistException; +import com.sun.star.frame.XStorable; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.sdb.XOfficeDatabaseDocument; +import com.sun.star.sdbc.SQLException; +import com.sun.star.sdbc.XCloseable; +import com.sun.star.sdbc.XConnection; +import com.sun.star.sdbc.XStatement; +import com.sun.star.sdbcx.XAppend; +import com.sun.star.sdbcx.XTablesSupplier; +import com.sun.star.uno.UnoRuntime; 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; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Set; /** * @@ -55,8 +65,10 @@ 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 + // the database document XOfficeDatabaseDocument m_databaseDocument; + // the data source belonging to the database document + DataSource m_dataSource; // the default connection XConnection m_connection; @@ -77,8 +89,8 @@ 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 ); + m_dataSource = new DataSource( m_orb, m_databaseDocument.getDataSource() ); + XPropertySet dsProperties = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, m_databaseDocument.getDataSource() ); dsProperties.setPropertyValue("URL", "sdbc:embedded:hsqldb"); XStorable storable = (XStorable)UnoRuntime.queryInterface( XStorable.class, m_databaseDocument ); @@ -115,7 +127,7 @@ public void close() { // close connection - com.sun.star.sdbc.XCloseable closeConn = (com.sun.star.sdbc.XCloseable)UnoRuntime.queryInterface( XCloseable.class, + XCloseable closeConn = (XCloseable)UnoRuntime.queryInterface( XCloseable.class, m_connection ); if ( closeConn != null ) { @@ -165,33 +177,75 @@ } } + /** drops the table with a given name + + @param _name + the name of the table to drop + @param _ifExists + TRUE if it should be dropped only when it exists. + */ + public void dropTable( String _name, boolean _ifExists ) throws SQLException + { + String dropStatement = "DROP TABLE \"" + _name; + if ( _ifExists ) + dropStatement += "\" IF EXISTS"; + executeSQL( dropStatement ); + } + + public void createTable( HsqlTableDescriptor _tableDesc, boolean _dropIfExists ) throws SQLException + { + if ( _dropIfExists ) + dropTable( _tableDesc.getName(), true ); + createTable( _tableDesc ); + } + /** creates a table */ public void createTable( HsqlTableDescriptor _tableDesc ) throws SQLException { - String createStatement = "CREATE TABLE \""; + String createStatement = "CREATE CACHED TABLE \""; createStatement += _tableDesc.getName(); createStatement += "\" ( "; String primaryKeyList = ""; + HashMap foreignKeys = new HashMap(); + HashMap foreignKeyRefs = new HashMap(); + HsqlColumnDescriptor[] columns = _tableDesc.getColumns(); for ( int i=0; i<columns.length; ++i ) { if ( i > 0 ) createStatement += ", "; - createStatement += "\"" + columns[i].Name; - createStatement += "\"" + columns[i].TypeName; + createStatement += "\"" + columns[i].getName(); + createStatement += "\" " + columns[i].getTypeName(); - if ( columns[i].NotNull ) + if ( columns[i].isRequired() ) createStatement += " NOT NULL"; - if ( columns[i].PrimaryKey ) + if ( columns[i].isPrimaryKey() ) { if ( primaryKeyList.length() > 0 ) primaryKeyList += ", "; - primaryKeyList += "\"" + columns[i].Name + "\""; + primaryKeyList += "\"" + columns[i].getName() + "\""; + } + + if ( columns[i].isForeignKey() ) + { + String foreignTable = columns[i].getForeignTable(); + + String foreignKeysForTable = foreignKeys.containsKey( foreignTable ) ? (String)foreignKeys.get( foreignTable ) : ""; + if ( foreignKeysForTable.length() > 0 ) + foreignKeysForTable += ", "; + foreignKeysForTable += "\"" + columns[i].getName() + "\""; + foreignKeys.put( foreignTable, foreignKeysForTable ); + + String foreignKeyRefsForTable = foreignKeyRefs.containsKey( foreignTable ) ? (String)foreignKeyRefs.get( foreignTable ) : ""; + if ( foreignKeyRefsForTable.length() > 0 ) + foreignKeyRefsForTable += ", "; + foreignKeyRefsForTable += "\"" + columns[i].getForeignColumn() + "\""; + foreignKeyRefs.put( foreignTable, foreignKeyRefsForTable ); } } @@ -202,16 +256,59 @@ createStatement += ")"; } + Set foreignKeyTables = foreignKeys.keySet(); + for ( Iterator foreignKey = foreignKeyTables.iterator(); + foreignKey.hasNext(); + ) + { + String foreignTable = (String)foreignKey.next(); + + createStatement += ", FOREIGN KEY ("; + createStatement += (String)foreignKeys.get(foreignTable); + createStatement += ") REFERENCES \""; + createStatement += foreignTable; + createStatement += "\"("; + createStatement += (String)foreignKeyRefs.get(foreignTable); + createStatement += ")"; + } + createStatement += ")"; + //System.err.println( createStatement ); executeSQL( createStatement ); } + /** creates a table in the database. using the SDBCX-API + */ + public void createTableInSDBCX( HsqlTableDescriptor _tableDesc ) throws SQLException, ElementExistException + { + XPropertySet sdbcxDescriptor = _tableDesc.createSdbcxDescriptor( defaultConnection() ); + XTablesSupplier suppTables = (XTablesSupplier)UnoRuntime.queryInterface( + XTablesSupplier.class, defaultConnection() ); + XAppend appendTable = (XAppend)UnoRuntime.queryInterface( + XAppend.class, suppTables.getTables() ); + appendTable.appendByDescriptor( sdbcxDescriptor ); + } + /** returns the URL of the ODB document represented by this instance */ public String getDocumentURL() { return m_databaseDocumentFile; + } + + /** returns the data source belonging to this database + */ + public DataSource getDataSource() + { + return m_dataSource; + } + + /** creates a row set operating the database, with a given command/type + */ + public RowSet createRowSet( int _commandType, String _command ) + { + return new RowSet(m_orb, getDocumentURL(), _commandType, _command); } protected void finalize() throws Throwable --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
