diff options
Diffstat (limited to 'connectivity/qa/connectivity')
-rw-r--r-- | connectivity/qa/connectivity/GeneralTest.java | 62 | ||||
-rw-r--r-- | connectivity/qa/connectivity/makefile.mk | 65 | ||||
-rwxr-xr-x | connectivity/qa/connectivity/tools/AbstractDatabase.java | 222 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/CRMDatabase.java | 292 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/DataSource.java | 145 | ||||
-rwxr-xr-x | connectivity/qa/connectivity/tools/DatabaseAccess.java | 63 | ||||
-rwxr-xr-x | connectivity/qa/connectivity/tools/DbaseDatabase.java | 98 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/HsqlColumnDescriptor.java | 84 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/HsqlDatabase.java | 213 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/HsqlTableDescriptor.java | 102 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/QueryDefinition.java | 74 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/RowSet.java | 292 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/makefile.mk | 68 | ||||
-rw-r--r-- | connectivity/qa/connectivity/tools/sdb/Connection.java | 93 |
14 files changed, 1873 insertions, 0 deletions
diff --git a/connectivity/qa/connectivity/GeneralTest.java b/connectivity/qa/connectivity/GeneralTest.java new file mode 100644 index 000000000000..a69ac5c1048f --- /dev/null +++ b/connectivity/qa/connectivity/GeneralTest.java @@ -0,0 +1,62 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +package complex.connectivity; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.sdbc.*; + +import com.sun.star.lang.XMultiServiceFactory; + +import complexlib.ComplexTestCase; + + +//import complex.connectivity.DBaseStringFunctions; + +public class GeneralTest extends ComplexTestCase { + + public String[] getTestMethodNames() { + return new String[] { "test" }; + } + + public String getTestObjectName() { + return "GeneralTest"; + } + public void assure2(String s,boolean b){ + assure(s,b); + } + + public void test() throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException { + try + { + XDriverManager driverManager = UnoRuntime.queryInterface( XDriverManager.class, ((XMultiServiceFactory)param.getMSF()).createInstance( "com.sun.star.sdbc.DriverManager" ) ); + String databaseURL = "sdbc:calc:singin' in the rain" ; + XConnection catalogConnection = driverManager.getConnection(databaseURL); + failed(); + } + catch(SQLException e){} + } +} diff --git a/connectivity/qa/connectivity/makefile.mk b/connectivity/qa/connectivity/makefile.mk new file mode 100644 index 000000000000..785f20692da3 --- /dev/null +++ b/connectivity/qa/connectivity/makefile.mk @@ -0,0 +1,65 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ = ..$/.. +TARGET = GeneralTest +PRJNAME = connectivity +PACKAGE = complex$/connectivity + +# --- Settings ----------------------------------------------------- +.INCLUDE: settings.mk + +#----- compile .java files ----------------------------------------- + +JARFILES = ridl.jar unoil.jar jurt.jar juh.jar java_uno.jar OOoRunner.jar +JAVAFILES =\ + $(TARGET).java + +JAVACLASSFILES = $(foreach,i,$(JAVAFILES) $(CLASSDIR)$/$(PACKAGE)$/$(i:b).class) + +#----- make a jar from compiled files ------------------------------ + +MAXLINELENGTH = 100000 + +JARCLASSDIRS = $(PACKAGE) +JARTARGET = $(TARGET).jar +JARCOMPRESS = TRUE + +# --- Targets ------------------------------------------------------ + +.IF "$(depend)" == "" +ALL : ALLTAR +.ELSE +ALL: ALLDEP +.ENDIF + +.INCLUDE : target.mk + + +run: + java -cp $(CLASSPATH)$(PATH_SEPERATOR)$(SOLARBINDIR)$/OOoRunner.jar org.openoffice.Runner -TestBase java_complex -o complex.connectivity.$(TARGET) + diff --git a/connectivity/qa/connectivity/tools/AbstractDatabase.java b/connectivity/qa/connectivity/tools/AbstractDatabase.java new file mode 100755 index 000000000000..b47c7c7961da --- /dev/null +++ b/connectivity/qa/connectivity/tools/AbstractDatabase.java @@ -0,0 +1,222 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +package connectivity.tools; + +import com.sun.star.container.XNameAccess; +import com.sun.star.frame.XModel; +import com.sun.star.frame.XStorable; +import com.sun.star.io.IOException; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.sdb.XDocumentDataSource; +import com.sun.star.sdb.XOfficeDatabaseDocument; +import com.sun.star.sdbc.SQLException; +import com.sun.star.sdbc.XCloseable; +import com.sun.star.sdbc.XStatement; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.util.CloseVetoException; +import connectivity.tools.sdb.Connection; +import java.io.File; + +/** + * + * @author oj93728 + */ +public abstract class AbstractDatabase implements DatabaseAccess +{ + // the service factory + + protected final XMultiServiceFactory m_orb; + // the URL of the temporary file used for the database document + protected String m_databaseDocumentFile; + // the database document + protected XOfficeDatabaseDocument m_databaseDocument; + // the data source belonging to the database document + protected DataSource m_dataSource; + // the default connection + protected Connection m_connection; + + public AbstractDatabase(final XMultiServiceFactory orb) throws Exception + { + m_orb = orb; + } + + // -------------------------------------------------------------------------------------------------------- + public AbstractDatabase(final XMultiServiceFactory orb, final String _existingDocumentURL ) throws Exception + { + m_orb = orb; + createDBDocument( _existingDocumentURL ); + } + + /** returns a connection to the database + * + * Multiple calls to this method return the same connection. The DbaseDatabase object keeps + * the ownership of the connection, so you don't need to (and should not) dispose/close it. + * + */ + public Connection defaultConnection() throws SQLException + { + if ( m_connection == null ) + m_connection = new Connection( m_databaseDocument.getDataSource().getConnection("", "") ); + + return m_connection; + } + + /** executes the given SQL statement via the defaultConnection + */ + public void executeSQL(final String statementString) throws SQLException + { + final XStatement statement = defaultConnection().createStatement(); + statement.execute(statementString); + } + + /** stores the database document + */ + public void store() throws IOException + { + if (m_databaseDocument != null) + { + final XStorable storeDoc = UnoRuntime.queryInterface(XStorable.class, m_databaseDocument); + storeDoc.store(); + } + } + + /** 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 + final XCloseable closeConn = UnoRuntime.queryInterface( XCloseable.class, + m_connection != null ? m_connection.getXConnection() : null ); + if (closeConn != null) + { + try + { + closeConn.close(); + } + catch (SQLException e) + { + } + } + m_connection = null; + + // close document + final com.sun.star.util.XCloseable closeDoc = UnoRuntime.queryInterface( com.sun.star.util.XCloseable.class, m_databaseDocument ); + if (closeDoc != null) + { + try + { + closeDoc.close(true); + } + catch (CloseVetoException e) + { + } + } + m_databaseDocument = null; + } + + /** closes the document, and deletes the underlying file + */ + public void closeAndDelete() + { + close(); + + if (m_databaseDocumentFile != null) + { + try + { + final File file = new File(m_databaseDocumentFile); + file.delete(); + } + catch (Exception e) + { + } + } + } + + /** returns the underlying database document + */ + public XOfficeDatabaseDocument getDatabaseDocument() + { + return m_databaseDocument; + } + + /** returns the model interface of the underlying database document + */ + public XModel getModel() + { + return UnoRuntime.queryInterface( XModel.class, m_databaseDocument ); + } + + public XMultiServiceFactory getORB() + { + return m_orb; + } + + // -------------------------------------------------------------------------------------------------------- + final protected void createDBDocument(final String _docURL) throws Exception + { + m_databaseDocumentFile = _docURL; + + final XNameAccess dbContext = UnoRuntime.queryInterface( XNameAccess.class, + m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); + final XDocumentDataSource dataSource = UnoRuntime.queryInterface( XDocumentDataSource.class, dbContext.getByName( _docURL ) ); + + m_databaseDocument = dataSource.getDatabaseDocument(); + m_dataSource = new DataSource(m_orb, m_databaseDocument.getDataSource()); + } + + /** 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(final int _commandType, final String _command) + { + return new RowSet(m_orb, getDocumentURL(), _commandType, _command); + } + + @Override + protected void finalize() throws Throwable + { + closeAndDelete(); + super.finalize(); + } +} diff --git a/connectivity/qa/connectivity/tools/CRMDatabase.java b/connectivity/qa/connectivity/tools/CRMDatabase.java new file mode 100644 index 000000000000..c35faac75ff7 --- /dev/null +++ b/connectivity/qa/connectivity/tools/CRMDatabase.java @@ -0,0 +1,292 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +package connectivity.tools; + +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.PropertyState; +import com.sun.star.container.ElementExistException; +import com.sun.star.container.NoSuchElementException; +import com.sun.star.frame.XComponentLoader; +import com.sun.star.frame.XController; +import com.sun.star.frame.XModel; +import com.sun.star.io.IOException; +import com.sun.star.lang.IllegalArgumentException; +import com.sun.star.lang.WrappedTargetException; +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.sdb.XSingleSelectQueryComposer; +import com.sun.star.sdb.application.XDatabaseDocumentUI; +import com.sun.star.sdbc.SQLException; +import com.sun.star.sdbcx.XTablesSupplier; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.util.XRefreshable; +import connectivity.tools.sdb.Connection; + +/** implements a small Customer Relationship Management database + * + * Not finished, by far. Feel free to add features as you need them. + */ +public class CRMDatabase +{ + private static final String INTEGER = "INTEGER"; + private static final String VARCHAR50 = "VARCHAR(50)"; + private final XMultiServiceFactory m_orb; + private final HsqlDatabase m_database; + private final DataSource m_dataSource; + private final Connection m_connection; + + /** constructs the CRM database + */ + public CRMDatabase( XMultiServiceFactory _orb, boolean _withUI ) throws Exception + { + m_orb = _orb; + + m_database = new HsqlDatabase( m_orb ); + m_dataSource = m_database.getDataSource(); + + if ( _withUI ) + { + final XComponentLoader loader = UnoRuntime.queryInterface( XComponentLoader.class, + m_orb.createInstance( "com.sun.star.frame.Desktop" ) ); + PropertyValue[] loadArgs = new PropertyValue[] { + new PropertyValue( "PickListEntry", 0, false, PropertyState.DIRECT_VALUE ) + }; + loader.loadComponentFromURL( m_database.getDocumentURL(), "_blank", 0, loadArgs ); + getDocumentUI().connect(); + m_connection = new Connection( getDocumentUI().getActiveConnection() ); + } + else + { + m_connection = m_database.defaultConnection(); + } + + createTables(); + createQueries(); + } + + /** + * creates a CRMDatabase from an existing document, given by URL + * @param _orb + * @param _existingDocumentURL + * @throws Exceptio + */ + public CRMDatabase( XMultiServiceFactory _orb, final String _existingDocumentURL ) throws Exception + { + m_orb = _orb; + + m_database = new HsqlDatabase( m_orb, _existingDocumentURL ); + m_dataSource = m_database.getDataSource(); + m_connection = m_database.defaultConnection(); + } + + // -------------------------------------------------------------------------------------------------------- + /** returns the database document underlying the CRM database + */ + public final HsqlDatabase getDatabase() + { + return m_database; + } + + // -------------------------------------------------------------------------------------------------------- + /** returns the default connection to the database + */ + public final Connection getConnection() + { + return m_connection; + } + + // -------------------------------------------------------------------------------------------------------- + public void saveAndClose() throws SQLException, IOException + { + XDatabaseDocumentUI ui = getDocumentUI(); + if ( ui != null ) + ui.closeSubComponents(); + m_database.store(); + m_database.closeAndDelete(); + } + + // -------------------------------------------------------------------------------------------------------- + public XDatabaseDocumentUI getDocumentUI() + { + XModel docModel = UnoRuntime.queryInterface( XModel.class, m_database.getDatabaseDocument() ); + return UnoRuntime.queryInterface( XDatabaseDocumentUI.class, docModel.getCurrentController() ); + } + + // -------------------------------------------------------------------------------------------------------- + public XController loadSubComponent( final int _objectType, final String _name ) throws IllegalArgumentException, SQLException, NoSuchElementException + { + XDatabaseDocumentUI docUI = getDocumentUI(); + if ( !docUI.isConnected() ) + docUI.connect(); + + XComponent subComponent = docUI.loadComponent( _objectType, _name, false ); + XController controller = UnoRuntime.queryInterface( XController.class, subComponent ); + if ( controller != null ) + return controller; + XModel document = UnoRuntime.queryInterface( XModel.class, subComponent ); + return document.getCurrentController(); + } + + // -------------------------------------------------------------------------------------------------------- + private void createTables() throws SQLException + { + HsqlTableDescriptor table = new HsqlTableDescriptor( "categories", + new HsqlColumnDescriptor[] { + new HsqlColumnDescriptor( "ID",INTEGER, HsqlColumnDescriptor.PRIMARY ), + new HsqlColumnDescriptor( "Name",VARCHAR50), + new HsqlColumnDescriptor( "Description", "VARCHAR(1024)" ), + new HsqlColumnDescriptor( "Image", "LONGVARBINARY" ) } ); + m_database.createTable( table, true ); + + m_database.executeSQL( "INSERT INTO \"categories\" ( \"ID\", \"Name\" ) VALUES ( 1, 'Food' )" ); + m_database.executeSQL( "INSERT INTO \"categories\" ( \"ID\", \"Name\" ) VALUES ( 2, 'Furniture' )" ); + + table = new HsqlTableDescriptor( "products", + new HsqlColumnDescriptor[] { + new HsqlColumnDescriptor( "ID",INTEGER, HsqlColumnDescriptor.PRIMARY ), + new HsqlColumnDescriptor( "Name",VARCHAR50), + new HsqlColumnDescriptor( "CategoryID",INTEGER, HsqlColumnDescriptor.REQUIRED, "categories", "ID" ) } ); + m_database.createTable( table, true ); + + m_database.executeSQL( "INSERT INTO \"products\" VALUES ( 1, 'Oranges', 1 )" ); + m_database.executeSQL( "INSERT INTO \"products\" VALUES ( 2, 'Apples', 1 )" ); + m_database.executeSQL( "INSERT INTO \"products\" VALUES ( 3, 'Pears', 1 )" ); + m_database.executeSQL( "INSERT INTO \"products\" VALUES ( 4, 'Strawberries', 1 )" ); + + table = new HsqlTableDescriptor( "customers", + new HsqlColumnDescriptor[] { + new HsqlColumnDescriptor( "ID",INTEGER, HsqlColumnDescriptor.PRIMARY ), + new HsqlColumnDescriptor( "Name",VARCHAR50), + new HsqlColumnDescriptor( "Address",VARCHAR50), + new HsqlColumnDescriptor( "City",VARCHAR50), + new HsqlColumnDescriptor( "Postal",VARCHAR50), + new HsqlColumnDescriptor( "Comment","LONGVARCHAR")} ); + m_database.createTable( table, true ); + + m_database.executeSQL( "INSERT INTO \"customers\" VALUES(1,'Food, Inc.','Down Under','Melbourne','509','Prefered') " ); + m_database.executeSQL( "INSERT INTO \"customers\" VALUES(2,'Simply Delicious','Down Under','Melbourne','518',null) " ); + m_database.executeSQL( "INSERT INTO \"customers\" VALUES(3,'Pure Health','10 Fish St.','San Francisco','94107',null) " ); + m_database.executeSQL( "INSERT INTO \"customers\" VALUES(4,'Milk And More','Arlington Road 21','Dublin','31021','Good one.') " ); + + table = new HsqlTableDescriptor( "orders", + new HsqlColumnDescriptor[] { + new HsqlColumnDescriptor( "ID",INTEGER, HsqlColumnDescriptor.PRIMARY ), + new HsqlColumnDescriptor( "CustomerID",INTEGER, HsqlColumnDescriptor.REQUIRED, "customers", "ID" ), + new HsqlColumnDescriptor( "OrderDate", "DATE" ), + new HsqlColumnDescriptor( "ShipDate", "DATE" ) } ); + m_database.createTable( table, true ); + + m_database.executeSQL( "INSERT INTO \"orders\" (\"ID\", \"CustomerID\", \"OrderDate\") VALUES(1, 1, {D '2009-01-01'})" ); + m_database.executeSQL( "INSERT INTO \"orders\" VALUES(2, 2, {D '2009-01-01'}, {D '2009-01-23'})" ); + + table = new HsqlTableDescriptor( "orders_details", + new HsqlColumnDescriptor[] { + new HsqlColumnDescriptor( "OrderID",INTEGER, HsqlColumnDescriptor.PRIMARY, "orders", "ID" ), + new HsqlColumnDescriptor( "ProductID",INTEGER, HsqlColumnDescriptor.PRIMARY, "products", "ID" ), + new HsqlColumnDescriptor( "Quantity",INTEGER) } ); + m_database.createTable( table, true ); + + m_database.executeSQL( "INSERT INTO \"orders_details\" VALUES(1, 1, 100)" ); + m_database.executeSQL( "INSERT INTO \"orders_details\" VALUES(1, 2, 100)" ); + m_database.executeSQL( "INSERT INTO \"orders_details\" VALUES(2, 2, 2000)" ); + m_database.executeSQL( "INSERT INTO \"orders_details\" VALUES(2, 3, 2000)" ); + m_database.executeSQL( "INSERT INTO \"orders_details\" VALUES(2, 4, 2000)" ); + + // since we created the tables by directly executing the SQL statements, we need to refresh + // the tables container + m_connection.refreshTables(); + } + + // -------------------------------------------------------------------------------------------------------- + private void validateUnparseable() + { + // The "unparseable" query should be indeed be unparseable by OOo (though a valid HSQL query) + XSingleSelectQueryComposer composer; + QueryDefinition unparseableQuery; + try + { + final XMultiServiceFactory factory = UnoRuntime.queryInterface( + XMultiServiceFactory.class, m_database.defaultConnection().getXConnection() ); + composer = UnoRuntime.queryInterface( + XSingleSelectQueryComposer.class, factory.createInstance( "com.sun.star.sdb.SingleSelectQueryComposer" ) ); + unparseableQuery = m_dataSource.getQueryDefinition( "unparseable" ); + } + catch( Exception e ) + { + throw new RuntimeException( "caught an unexpected exception: " + e.getMessage() ); + } + + boolean caughtExpected = false; + try + { + composer.setQuery( unparseableQuery.getCommand() ); + } + catch (WrappedTargetException e) { } + catch( SQLException e ) + { + caughtExpected = true; + } + + if ( !caughtExpected ) + throw new RuntimeException( "Somebody improved the parser! This is bad :), since we need an unparsable query here!" ); + } + + // -------------------------------------------------------------------------------------------------------- + private void createQueries() throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException + { + m_database.getDataSource().createQuery( + "all orders", + "SELECT \"orders\".\"ID\" AS \"Order No.\", " + + "\"customers\".\"Name\" AS \"Customer Name\", " + + "\"orders\".\"OrderDate\" AS \"Order Date\", " + + "\"orders\".\"ShipDate\" AS \"Ship Date\", " + + "\"orders_details\".\"Quantity\", " + + "\"products\".\"Name\" AS \"Product Name\" " + + "FROM \"orders_details\" AS \"orders_details\", " + + "\"orders\" AS \"orders\", " + + "\"products\" AS \"products\", " + + "\"customers\" AS \"customers\" " + + "WHERE ( \"orders_details\".\"OrderID\" = \"orders\".\"ID\" " + + "AND \"orders_details\".\"ProductID\" = \"products\".\"ID\" " + + "AND \"orders\".\"CustomerID\" = \"customers\".\"ID\" )" + ); + + m_database.getDataSource().createQuery( + "unshipped orders", + "SELECT * " + + "FROM \"all orders\"" + + "WHERE ( \"ShipDate\" IS NULL )" + ); + + m_database.getDataSource().createQuery( "parseable", "SELECT * FROM \"customers\"" ); + m_database.getDataSource().createQuery( "parseable native", "SELECT * FROM INFORMATION_SCHEMA.SYSTEM_VIEWS", false ); + m_database.getDataSource().createQuery( "unparseable", + "SELECT CAST( \"ID\" AS VARCHAR(3) ) AS \"ID_VARCHAR\" FROM \"products\"", false ); + + validateUnparseable(); + } +} diff --git a/connectivity/qa/connectivity/tools/DataSource.java b/connectivity/qa/connectivity/tools/DataSource.java new file mode 100644 index 000000000000..221ada3cb487 --- /dev/null +++ b/connectivity/qa/connectivity/tools/DataSource.java @@ -0,0 +1,145 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +package connectivity.tools; + +import com.sun.star.container.ElementExistException; +import com.sun.star.container.NoSuchElementException; +import com.sun.star.container.XNameAccess; +import com.sun.star.container.XNameContainer; +import com.sun.star.lang.WrappedTargetException; +import com.sun.star.lang.XSingleServiceFactory; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.beans.XPropertySet; +import com.sun.star.sdb.XQueryDefinitionsSupplier; +import com.sun.star.sdbc.XDataSource; +import com.sun.star.uno.Exception; +import com.sun.star.uno.UnoRuntime; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class DataSource +{ + // the service factory + + private final XMultiServiceFactory m_orb; + private XDataSource m_dataSource; + + public DataSource(final XMultiServiceFactory _orb, final String _registeredName) throws Exception + { + m_orb = _orb; + + final XNameAccess dbContext = UnoRuntime.queryInterface( + XNameAccess.class, _orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); + + m_dataSource = UnoRuntime.queryInterface( XDataSource.class, dbContext.getByName( _registeredName ) ); + } + + public DataSource(final XMultiServiceFactory _orb,final XDataSource _dataSource) + { + m_orb = _orb; + m_dataSource = _dataSource; + } + + final public XDataSource getXDataSource() + { + return m_dataSource; + } + + /** creates a query with a given name and SQL command + */ + public void createQuery(final String _name, final String _sqlCommand) throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException + { + createQuery(_name, _sqlCommand, true); + } + + /** creates a query with a given name, SQL command, and EscapeProcessing flag + */ + public void createQuery(final String _name, final String _sqlCommand, final boolean _escapeProcessing) throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException + { + final XSingleServiceFactory queryDefsFac = UnoRuntime.queryInterface( XSingleServiceFactory.class, getQueryDefinitions() ); + XPropertySet queryDef = null; + try + { + queryDef = UnoRuntime.queryInterface( XPropertySet.class, queryDefsFac.createInstance() ); + queryDef.setPropertyValue("Command", _sqlCommand); + queryDef.setPropertyValue("EscapeProcessing", Boolean.valueOf(_escapeProcessing)); + } + catch (com.sun.star.uno.Exception e) + { + e.printStackTrace(System.err); + } + + final XNameContainer queryDefsContainer = UnoRuntime.queryInterface( XNameContainer.class, getQueryDefinitions() ); + queryDefsContainer.insertByName(_name, queryDef); + } + + /** provides the query definition with the given name + */ + public QueryDefinition getQueryDefinition(final String _name) throws NoSuchElementException + { + final XNameAccess allDefs = getQueryDefinitions(); + try + { + return new QueryDefinition( UnoRuntime.queryInterface( XPropertySet.class, allDefs.getByName( _name) ) ); + } + catch (WrappedTargetException e) + { + } + throw new NoSuchElementException(); + } + + /** provides the container of query definitions of the data source + */ + public XNameAccess getQueryDefinitions() + { + final XQueryDefinitionsSupplier suppQueries = UnoRuntime.queryInterface( + XQueryDefinitionsSupplier.class, m_dataSource); + return suppQueries.getQueryDefinitions(); + } + + /** returns the name of the data source + * + * If a data source is registered at the database context, the name is the registration + * name. Otherwise, its the URL which the respective database document is based on. + * + * Note that the above definition is from the UNO API, not from this wrapper here. + */ + public String getName() + { + String name = null; + try + { + final XPropertySet dataSourceProps = UnoRuntime.queryInterface( XPropertySet.class, m_dataSource ); + name = (String) dataSourceProps.getPropertyValue("Name"); + } + catch (Exception ex) + { + Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex); + } + return name; + } +}; diff --git a/connectivity/qa/connectivity/tools/DatabaseAccess.java b/connectivity/qa/connectivity/tools/DatabaseAccess.java new file mode 100755 index 000000000000..c099a44d1fb9 --- /dev/null +++ b/connectivity/qa/connectivity/tools/DatabaseAccess.java @@ -0,0 +1,63 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +package connectivity.tools; + +import com.sun.star.frame.XModel; +import com.sun.star.io.IOException; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.sdb.XOfficeDatabaseDocument; +import com.sun.star.sdbc.SQLException; +import connectivity.tools.sdb.Connection; + +/** + * + * @author oj93728 + */ +public interface DatabaseAccess +{ + Connection defaultConnection() throws SQLException; + + void executeSQL(final String statementString) throws SQLException; + + void store() throws IOException; + + void close(); + + void closeAndDelete(); + + XOfficeDatabaseDocument getDatabaseDocument(); + + XModel getModel(); + + String getDocumentURL(); + + DataSource getDataSource(); + + RowSet createRowSet(final int _commandType, final String _command); + + XMultiServiceFactory getORB(); +} diff --git a/connectivity/qa/connectivity/tools/DbaseDatabase.java b/connectivity/qa/connectivity/tools/DbaseDatabase.java new file mode 100755 index 000000000000..ae40be4222aa --- /dev/null +++ b/connectivity/qa/connectivity/tools/DbaseDatabase.java @@ -0,0 +1,98 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +package connectivity.tools; + +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.XPropertySet; +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.uno.UnoRuntime; + +import helper.URLHelper; +import java.io.File; + +/** + * + * @author Ocke + */ +public class DbaseDatabase extends AbstractDatabase +{ + // -------------------------------------------------------------------------------------------------------- + + public DbaseDatabase(final XMultiServiceFactory orb) throws Exception + { + super(orb); + createDBDocument(); + } + + // -------------------------------------------------------------------------------------------------------- + public DbaseDatabase(final XMultiServiceFactory orb, final String _existingDocumentURL) throws Exception + { + super(orb, _existingDocumentURL); + } + + /** creates an empty database document in a temporary location + */ + private void createDBDocument() throws Exception + { + final File documentFile = File.createTempFile("dbase", ".odb"); + if ( documentFile.exists() ) + documentFile.delete(); + final File subPath = new File(documentFile.getParent() + File.separator + documentFile.getName().replaceAll(".odb", "") + File.separator ); + subPath.mkdir(); + //subPath.deleteOnExit(); + m_databaseDocumentFile = URLHelper.getFileURLFromSystemPath(documentFile); + final String path = URLHelper.getFileURLFromSystemPath(subPath.getPath()); + + m_databaseDocument = (XOfficeDatabaseDocument) UnoRuntime.queryInterface( + XOfficeDatabaseDocument.class, m_orb.createInstance("com.sun.star.sdb.OfficeDatabaseDocument")); + m_dataSource = new DataSource(m_orb, m_databaseDocument.getDataSource()); + + final XPropertySet dsProperties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, m_databaseDocument.getDataSource()); + dsProperties.setPropertyValue("URL", "sdbc:dbase:" + path); + + final XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, m_databaseDocument); + storable.storeAsURL(m_databaseDocumentFile, new PropertyValue[] + { + }); + } + + /** 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(final String _name,final boolean _ifExists) throws SQLException + { + String dropStatement = "DROP TABLE \"" + _name; + executeSQL(dropStatement); + } +} diff --git a/connectivity/qa/connectivity/tools/HsqlColumnDescriptor.java b/connectivity/qa/connectivity/tools/HsqlColumnDescriptor.java new file mode 100644 index 000000000000..c0c46d07149f --- /dev/null +++ b/connectivity/qa/connectivity/tools/HsqlColumnDescriptor.java @@ -0,0 +1,84 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package connectivity.tools; + +/** is a very simply and rudimentary descriptor of table columns, for creating HSQLDB tables + */ +public class HsqlColumnDescriptor +{ + private String Name; + private String TypeName; + private boolean Required; + private boolean PrimaryKey; + private String ForeignTable; + private String ForeignColumn; + + public final String getName() { return Name; } + public final String getTypeName() { return TypeName; } + public final boolean isRequired() { return Required; } + public final boolean isPrimaryKey() { return PrimaryKey; } + + public final boolean isForeignKey() { return ( ForeignTable.length() != 0 ) && ( ForeignColumn.length() != 0 ); } + public final String getForeignTable() { return ForeignTable; } + public final String getForeignColumn() { return ForeignColumn; } + + /// determines that a column is required, i.e. not nullable + public final static int REQUIRED = 1; + /// determines that a column is part of the primary key of its table + public final static int PRIMARY = 2; + + public HsqlColumnDescriptor( String _Name, String _TypeName ) + { + Name = _Name; + TypeName = _TypeName; + Required = false; + PrimaryKey = false; + ForeignTable = ""; + ForeignColumn = ""; + } + + public HsqlColumnDescriptor( String _Name, String _TypeName, int _Flags ) + { + Name = _Name; + TypeName = _TypeName; + Required = ( _Flags & REQUIRED ) != 0; + PrimaryKey = ( _Flags & PRIMARY ) != 0; + ForeignTable = ""; + ForeignColumn = ""; + } + + public HsqlColumnDescriptor( String _Name, String _TypeName, int _Flags, String _ForeignTable, String _ForeignColumn ) + { + Name = _Name; + TypeName = _TypeName; + Required = ( _Flags & REQUIRED ) != 0; + PrimaryKey = ( _Flags & PRIMARY ) != 0; + ForeignTable = _ForeignTable; + ForeignColumn = _ForeignColumn; + } +}; diff --git a/connectivity/qa/connectivity/tools/HsqlDatabase.java b/connectivity/qa/connectivity/tools/HsqlDatabase.java new file mode 100644 index 000000000000..058c61e1afaa --- /dev/null +++ b/connectivity/qa/connectivity/tools/HsqlDatabase.java @@ -0,0 +1,213 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +package connectivity.tools; + +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.PropertyState; +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.sdbcx.XAppend; +import com.sun.star.sdbcx.XTablesSupplier; +import com.sun.star.uno.UnoRuntime; + +import helper.URLHelper; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Set; +import java.io.File; + +/** + * + * @author fs93730 + */ +public class HsqlDatabase extends AbstractDatabase +{ + + // -------------------------------------------------------------------------------------------------------- + public HsqlDatabase(final XMultiServiceFactory orb) throws Exception + { + super(orb); + createDBDocument(); + } + + // -------------------------------------------------------------------------------------------------------- + public HsqlDatabase(final XMultiServiceFactory orb, final String _existingDocumentURL) throws Exception + { + super(orb, _existingDocumentURL); + } + + /** creates an empty database document in a temporary location + */ + private void createDBDocument() throws Exception + { + final File documentFile = File.createTempFile("testdb", ".odb"); + if ( documentFile.exists() ) + documentFile.delete(); + m_databaseDocumentFile = URLHelper.getFileURLFromSystemPath(documentFile); + + m_databaseDocument = (XOfficeDatabaseDocument) UnoRuntime.queryInterface( + XOfficeDatabaseDocument.class, m_orb.createInstance("com.sun.star.sdb.OfficeDatabaseDocument")); + m_dataSource = new DataSource(m_orb, m_databaseDocument.getDataSource()); + + final XPropertySet dsProperties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, m_databaseDocument.getDataSource()); + dsProperties.setPropertyValue("URL", "sdbc:embedded:hsqldb"); + + final XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, m_databaseDocument); + storable.storeAsURL( m_databaseDocumentFile, new PropertyValue[] + { new PropertyValue( "PickListEntry", 0, false, PropertyState.DIRECT_VALUE ) + } ); + } + + /** 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(final String _name, final boolean _ifExists) throws SQLException + { + final StringBuffer dropStatement = new StringBuffer("DROP TABLE \""); + dropStatement.append(_name); + if (_ifExists) + { + dropStatement.append("\" IF EXISTS"); + } + executeSQL(dropStatement.toString()); + } + + public void createTable(final HsqlTableDescriptor _tableDesc, final boolean _dropIfExists) throws SQLException + { + if (_dropIfExists) + { + dropTable(_tableDesc.getName(), true); + } + createTable(_tableDesc); + } + + /** creates a table + */ + public void createTable(final HsqlTableDescriptor _tableDesc) throws SQLException + { + StringBuffer createStatement = new StringBuffer("CREATE CACHED TABLE \""); + createStatement.append(_tableDesc.getName()); + createStatement.append("\" ( "); + + String primaryKeyList = ""; + + final HashMap foreignKeys = new HashMap(); + final HashMap foreignKeyRefs = new HashMap(); + + final HsqlColumnDescriptor[] columns = _tableDesc.getColumns(); + for (int i = 0; i < columns.length; ++i) + { + if (i > 0) + { + createStatement.append(", "); + } + + createStatement.append("\"" + columns[i].getName()); + createStatement.append("\" " + columns[i].getTypeName()); + + if (columns[i].isRequired()) + { + createStatement.append(" NOT NULL"); + } + + if (columns[i].isPrimaryKey()) + { + if (primaryKeyList.length() > 0) + { + primaryKeyList += ", "; + } + primaryKeyList += "\"" + columns[i].getName() + "\""; + } + + if (columns[i].isForeignKey()) + { + final 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); + + final StringBuffer foreignKeyRefsForTable = new StringBuffer(foreignKeyRefs.containsKey(foreignTable) ? (String) foreignKeyRefs.get(foreignTable) : ""); + if (foreignKeyRefsForTable.length() > 0) + { + foreignKeyRefsForTable.append(", "); + } + foreignKeyRefsForTable.append("\"" + columns[i].getForeignColumn() + "\""); + foreignKeyRefs.put(foreignTable, foreignKeyRefsForTable.toString()); + } + } + + if (primaryKeyList.length() > 0) + { + createStatement.append(", PRIMARY KEY ("); + createStatement.append(primaryKeyList); + createStatement.append(')'); + } + + final Set foreignKeyTables = foreignKeys.keySet(); + for (final Iterator foreignKey = foreignKeyTables.iterator(); + foreignKey.hasNext();) + { + final String foreignTable = (String) foreignKey.next(); + + createStatement.append(", FOREIGN KEY ("); + createStatement.append((String) foreignKeys.get(foreignTable)); + createStatement.append(") REFERENCES \""); + createStatement.append(foreignTable); + createStatement.append("\"("); + createStatement.append((String) foreignKeyRefs.get(foreignTable)); + createStatement.append(')'); + } + + createStatement.append(')'); + + //System.err.println( createStatement ); + executeSQL(createStatement.toString()); + } + + /** creates a table in the database. using the SDBCX-API + */ + public void createTableInSDBCX(final HsqlTableDescriptor _tableDesc) throws SQLException, ElementExistException + { + final XPropertySet sdbcxDescriptor = _tableDesc.createSdbcxDescriptor(defaultConnection()); + final XTablesSupplier suppTables = UnoRuntime.queryInterface( XTablesSupplier.class, defaultConnection().getXConnection() ); + final XAppend appendTable = UnoRuntime.queryInterface( XAppend.class, suppTables.getTables() ); + appendTable.appendByDescriptor(sdbcxDescriptor); + } +} diff --git a/connectivity/qa/connectivity/tools/HsqlTableDescriptor.java b/connectivity/qa/connectivity/tools/HsqlTableDescriptor.java new file mode 100644 index 000000000000..dcda754f8b8c --- /dev/null +++ b/connectivity/qa/connectivity/tools/HsqlTableDescriptor.java @@ -0,0 +1,102 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package connectivity.tools; + +import com.sun.star.beans.XPropertySet; +import com.sun.star.container.XNameAccess; +import com.sun.star.sdbc.ColumnValue; +import com.sun.star.sdbcx.XColumnsSupplier; +import com.sun.star.sdbcx.XDataDescriptorFactory; +import com.sun.star.sdbcx.XTablesSupplier; +import com.sun.star.uno.UnoRuntime; +import connectivity.tools.sdb.Connection; + +/** is a very simply descriptor of a HSQL table, to be used with a HsqlDatabase.createTable method + */ +public class HsqlTableDescriptor +{ + private String m_name; + private HsqlColumnDescriptor[] m_columns; + + /** Creates a new instance of HsqlTableDescriptor */ + public HsqlTableDescriptor( String _name, HsqlColumnDescriptor[] _columns ) + { + m_name = _name; + m_columns = _columns; + } + + /** returns the name of the table + */ + public String getName() + { + return m_name; + } + + /** returns the set of column descriptors for the table + */ + public HsqlColumnDescriptor[] getColumns() + { + return m_columns; + } + + public XPropertySet createSdbcxDescriptor( Connection _forConnection ) + { + XTablesSupplier suppTables = UnoRuntime.queryInterface( XTablesSupplier.class, _forConnection.getXConnection() ); + XDataDescriptorFactory tableDescFac = UnoRuntime.queryInterface( XDataDescriptorFactory.class, suppTables.getTables() ); + XPropertySet tableDesc = tableDescFac.createDataDescriptor(); + + try + { + tableDesc.setPropertyValue( "Name", getName() ); + } + catch ( Exception e ) { e.printStackTrace( System.err ); } + + XColumnsSupplier suppDescCols = UnoRuntime.queryInterface( XColumnsSupplier.class, tableDesc ); + + XNameAccess descColumns = suppDescCols.getColumns(); + XDataDescriptorFactory columnDescFac = UnoRuntime.queryInterface( XDataDescriptorFactory.class, descColumns ); + + HsqlColumnDescriptor[] myColumns = getColumns(); + for ( int i = 0; i < myColumns.length; ++i ) + { + XPropertySet columnDesc = columnDescFac.createDataDescriptor(); + try + { + columnDesc.setPropertyValue( "Name", myColumns[i].getName() ); + columnDesc.setPropertyValue( "IsNullable", new Integer( myColumns[i].isRequired() ? ColumnValue.NO_NULLS : ColumnValue.NULLABLE) ); + columnDesc.setPropertyValue( "TypeName", myColumns[i].getTypeName() ); + if ( myColumns[i].isPrimaryKey() || myColumns[i].isForeignKey() ) + // not yet implemented + throw new java.lang.UnsupportedOperationException("creating a primary or foreign key via SDBCX not yet implemented" ); + } + catch( com.sun.star.uno.Exception e ) { e.printStackTrace( System.err ); } + } + + return tableDesc; + } +} diff --git a/connectivity/qa/connectivity/tools/QueryDefinition.java b/connectivity/qa/connectivity/tools/QueryDefinition.java new file mode 100644 index 000000000000..ebc9d1a25cfe --- /dev/null +++ b/connectivity/qa/connectivity/tools/QueryDefinition.java @@ -0,0 +1,74 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package connectivity.tools; + +import com.sun.star.beans.PropertyVetoException; +import com.sun.star.beans.UnknownPropertyException; +import com.sun.star.beans.XPropertySet; +import com.sun.star.lang.WrappedTargetException; +import com.sun.star.lang.IllegalArgumentException; + +public class QueryDefinition +{ + XPropertySet m_queryDef; + + public QueryDefinition( XPropertySet _queryDef ) + { + m_queryDef = _queryDef; + } + + /** retrieves the command underlying the query definition + * + * This method is a mere wrapped around the <code>getPropertyValue( "Command" )</code> call + */ + public final String getCommand() throws WrappedTargetException + { + String command = null; + try { + command = (String)m_queryDef.getPropertyValue( "Command" ); + } + catch (UnknownPropertyException e) { } + + return command; + } + + /** retrieves the command underlying the query definition + * + * This method is a mere wrapped around the <code>getPropertyValue( "Command" )</code> call + */ + public void setCommand( String _command ) throws WrappedTargetException + { + try + { + m_queryDef.setPropertyValue( "Command", _command ); + } + catch (UnknownPropertyException e) { } + catch (PropertyVetoException e) { } + catch (IllegalArgumentException e) { } + } +}; diff --git a/connectivity/qa/connectivity/tools/RowSet.java b/connectivity/qa/connectivity/tools/RowSet.java new file mode 100644 index 000000000000..a26456dcc746 --- /dev/null +++ b/connectivity/qa/connectivity/tools/RowSet.java @@ -0,0 +1,292 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package connectivity.tools; + +import com.sun.star.beans.XPropertySet; +import com.sun.star.container.XIndexAccess; +import com.sun.star.container.XNameAccess; +import com.sun.star.io.XInputStream; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.sdbc.SQLException; +import com.sun.star.sdbc.XArray; +import com.sun.star.sdbc.XBlob; +import com.sun.star.sdbc.XClob; +import com.sun.star.sdbc.XRef; +import com.sun.star.sdbc.XRow; +import com.sun.star.sdbc.XRowSet; +import com.sun.star.sdbc.XRowSetListener; +import com.sun.star.sdbcx.XColumnsSupplier; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.util.Date; +import com.sun.star.util.DateTime; +import com.sun.star.util.Time; + +public class RowSet implements XRowSet, XRow +{ + private XMultiServiceFactory m_orb; + private XRowSet m_rowSet; + private XRow m_row; + private XPropertySet m_rowSetProps; + + public RowSet( XMultiServiceFactory _orb, String _dataSource, int _commandType, String _command ) + { + try + { + m_rowSetProps = (XPropertySet)UnoRuntime.queryInterface( + XPropertySet.class, _orb.createInstance("com.sun.star.sdb.RowSet") ); + m_rowSetProps.setPropertyValue( "DataSourceName", _dataSource ); + m_rowSetProps.setPropertyValue( "CommandType", new Integer( _commandType ) ); + m_rowSetProps.setPropertyValue( "Command", _command ); + + m_rowSet = (XRowSet)UnoRuntime.queryInterface( XRowSet.class, m_rowSetProps ); + m_row = (XRow)UnoRuntime.queryInterface( XRow.class, m_rowSetProps ); + } + catch ( Exception e ) + { + e.printStackTrace(System.err); + throw new java.lang.InstantiationError(); + } + } + + // misc + public int getColumnCount() + { + XColumnsSupplier suppCols = (XColumnsSupplier)UnoRuntime.queryInterface( + XColumnsSupplier.class, m_rowSet ); + XIndexAccess columns = (XIndexAccess)UnoRuntime.queryInterface( + XIndexAccess.class, suppCols.getColumns() ); + return columns.getCount(); + } + + // XRowSet + public void execute() throws SQLException + { + m_rowSet.execute(); + } + + public void addRowSetListener( XRowSetListener _listener ) + { + m_rowSet.addRowSetListener( _listener ); + } + + public void removeRowSetListener( XRowSetListener _listener ) + { + m_rowSet.removeRowSetListener( _listener ); + } + + public boolean next() throws SQLException + { + return m_rowSet.next(); + } + + public boolean isBeforeFirst() throws SQLException + { + return m_rowSet.isBeforeFirst(); + } + + public boolean isAfterLast() throws SQLException + { + return m_rowSet.isAfterLast(); + } + + public boolean isFirst() throws SQLException + { + return m_rowSet.isFirst(); + } + + public boolean isLast() throws SQLException + { + return m_rowSet.isLast(); + } + + public void beforeFirst() throws SQLException + { + m_rowSet.beforeFirst(); + } + + public void afterLast() throws SQLException + { + m_rowSet.afterLast(); + } + + public boolean first() throws SQLException + { + return m_rowSet.first(); + } + + public boolean last() throws SQLException + { + return m_rowSet.last(); + } + + public int getRow() throws SQLException + { + return m_rowSet.getRow(); + } + + public boolean absolute(int i) throws SQLException + { + return m_rowSet.absolute(i); + } + + public boolean relative(int i) throws SQLException + { + return m_rowSet.relative(i); + } + + public boolean previous() throws SQLException + { + return m_rowSet.previous(); + } + + public void refreshRow() throws SQLException + { + m_rowSet.refreshRow(); + } + + public boolean rowUpdated() throws SQLException + { + return m_rowSet.rowUpdated(); + } + + public boolean rowInserted() throws SQLException + { + return m_rowSet.rowInserted(); + } + + public boolean rowDeleted() throws SQLException + { + return m_rowSet.rowDeleted(); + } + + // XRow + public Object getStatement() throws SQLException + { + return m_rowSet.getStatement(); + } + + public boolean wasNull() throws SQLException + { + return m_row.wasNull(); + } + + public String getString(int i) throws SQLException + { + return m_row.getString(i); + } + + public boolean getBoolean(int i) throws SQLException + { + return m_row.getBoolean(i); + } + + public byte getByte(int i) throws SQLException + { + return m_row.getByte(i); + } + + public short getShort(int i) throws SQLException + { + return m_row.getShort(i); + } + + public int getInt(int i) throws SQLException + { + return m_row.getInt(i); + } + + public long getLong(int i) throws SQLException + { + return m_row.getLong(i); + } + + public float getFloat(int i) throws SQLException + { + return m_row.getFloat(i); + } + + public double getDouble(int i) throws SQLException + { + return m_row.getDouble(i); + } + + public byte[] getBytes(int i) throws SQLException + { + return m_row.getBytes(i); + } + + public Date getDate(int i) throws SQLException + { + return m_row.getDate(i); + } + + public Time getTime(int i) throws SQLException + { + return m_row.getTime(i); + } + + public DateTime getTimestamp(int i) throws SQLException + { + return m_row.getTimestamp(i); + } + + public XInputStream getBinaryStream(int i) throws SQLException + { + return m_row.getBinaryStream(i); + } + + public XInputStream getCharacterStream(int i) throws SQLException + { + return m_row.getCharacterStream(i); + } + + public Object getObject(int i, XNameAccess xNameAccess) throws SQLException + { + return m_row.getObject(i, xNameAccess); + } + + public XRef getRef(int i) throws SQLException + { + return m_row.getRef(i); + } + + public XBlob getBlob(int i) throws SQLException + { + return m_row.getBlob(i); + } + + public XClob getClob(int i) throws SQLException + { + return m_row.getClob(i); + } + + public XArray getArray(int i) throws SQLException + { + return m_row.getArray(i); + } +}; diff --git a/connectivity/qa/connectivity/tools/makefile.mk b/connectivity/qa/connectivity/tools/makefile.mk new file mode 100644 index 000000000000..0f3c9c84b92e --- /dev/null +++ b/connectivity/qa/connectivity/tools/makefile.mk @@ -0,0 +1,68 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ = ../../.. +TARGET = ConnectivityTools +PRJNAME = connectivity +PACKAGE = connectivity/tools + +# --- Settings ----------------------------------------------------- +.INCLUDE: settings.mk + +.IF "$(SOLAR_JAVA)" == "" +all: + @echo "Java not available. Build skipped" +.ELSE + +.IF "$(BUILD_QADEVOOO)" == "YES" +#----- compile .java files ----------------------------------------- + +JARFILES = ridl.jar unoil.jar jurt.jar juh.jar java_uno.jar OOoRunnerLight.jar +JAVAFILES := $(shell @$(FIND) . -name "*.java") +JAVACLASSFILES := $(foreach,i,$(JAVAFILES) $(CLASSDIR)/$(PACKAGE)/$(i:d)$(i:b).class) + +#----- make a jar from compiled files ------------------------------ + +MAXLINELENGTH = 100000 + +JARCLASSDIRS = $(PACKAGE) +JARTARGET = $(TARGET).jar +JARCOMPRESS = TRUE + +# --- Targets ------------------------------------------------------ + +.IF "$(depend)" == "" +ALL : ALLTAR +.ELSE +ALL: ALLDEP +.ENDIF + +.ENDIF + +.ENDIF # "$(SOLAR_JAVA)" == "" + +.INCLUDE : target.mk diff --git a/connectivity/qa/connectivity/tools/sdb/Connection.java b/connectivity/qa/connectivity/tools/sdb/Connection.java new file mode 100644 index 000000000000..aac120fb1e73 --- /dev/null +++ b/connectivity/qa/connectivity/tools/sdb/Connection.java @@ -0,0 +1,93 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package connectivity.tools.sdb; + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.sdb.XSingleSelectQueryComposer; +import com.sun.star.sdbc.SQLException; +import com.sun.star.sdbc.XConnection; +import com.sun.star.sdbc.XDatabaseMetaData; +import com.sun.star.sdbc.XPreparedStatement; +import com.sun.star.sdbc.XResultSet; +import com.sun.star.sdbc.XStatement; +import com.sun.star.sdbcx.XTablesSupplier; +import com.sun.star.uno.Exception; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.util.XRefreshable; + +/** + * is a convenience wrapper around a SDB-level connection object + */ +public class Connection +{ + private final XConnection m_connection; + + public Connection( final XConnection _connection ) + { + m_connection = _connection; + } + + public XConnection getXConnection() + { + return m_connection; + } + + public boolean execute( final String _sql ) throws SQLException + { + XStatement statement = createStatement(); + return statement.execute( _sql ); + } + + public XResultSet executeQuery( final String _sql ) throws SQLException + { + XStatement statement = createStatement(); + return statement.executeQuery( _sql ); + } + + public int executeUpdate( final String _sql ) throws SQLException + { + XStatement statement = createStatement(); + return statement.executeUpdate( _sql ); + } + + public void refreshTables() + { + final XTablesSupplier suppTables = UnoRuntime.queryInterface(XTablesSupplier.class, m_connection); + final XRefreshable refresh = UnoRuntime.queryInterface( XRefreshable.class, suppTables.getTables() ); + refresh.refresh(); + } + + public XSingleSelectQueryComposer createSingleSelectQueryComposer() throws Exception + { + final XMultiServiceFactory connectionFactory = UnoRuntime.queryInterface( XMultiServiceFactory.class, m_connection ); + return UnoRuntime.queryInterface( + XSingleSelectQueryComposer.class, connectionFactory.createInstance( "com.sun.star.sdb.SingleSelectQueryComposer" ) ); + } + + public + XStatement createStatement() throws SQLException + { + return m_connection.createStatement(); + } + + public + XPreparedStatement prepareStatement( String _sql ) throws SQLException + { + return m_connection.prepareStatement( _sql ); + } + + public + XDatabaseMetaData getMetaData() throws SQLException + { + return m_connection.getMetaData(); + } + + public + void close() throws SQLException + { + m_connection.close(); + } +} |