Adding and Editing Predefined Queries

From Apache OpenOffice Wiki
Jump to: navigation, search



The query definitions container com.sun.star.sdb.DefinitionContainer is used to work with the query definitions of a data source. It is returned by the com.sun.star.sdb.XQueryDefinitionsSupplier interface of the data source, which has a single method for this purpose:

  com::sun::star::container::XNameAccess getQueryDefinitions()

The DefinitionContainer is not only an XNameAccess, but a com.sun.star.container.XNameContainer, that is, add new query definitions by name (see First Steps). Besides the name access, obtain query definitions through com.sun.star.container.XIndexAccess and com.sun.star.container.XEnumerationAccess.

DefinitionContainer And QueryDefinition

New query definitions are created by the com.sun.star.lang.XSingleServiceFactory interface of the query definitions container. Its method createInstance() provides an empty QueryDefinition to configure, as required. Then, the new query definition is added to the DefinitionContainer using insertByName() at the XNameContainer interface.

Documentation note.png The optional interface com.sun.star.util.XRefreshable is not supported by the DefinitionContainer implementation.

A QueryDefinition is configured through the following properties:

Properties of com.sun.star.sdb.QueryDefinition
Name string - The name of the queryDefinition.
Command string - The SQL SELECT command.
EscapeProcessing boolean - If true, determines that the query must not be touched by the built-in SQL parser of Apache OpenOffice API.
UpdateCatalogName string - The name of the update table catalog used to identify tables, supported by some databases.
UpdateSchemaName string - The name of the update table schema used to identify tables, supported by some databases.
UpdateTableName string - The name of the update table catalog used to identify tables, supported by some databases The name of the table which should be updated. This is usually used for queries based on more than one table and makes such queries partially editable. The property UpdateTableName must contain the name of the table with unique rows in the result set. In a 1:n join this is usually the table on the n side of the join.

The following example adds a new query definition Query1 to the data source Bibliography that is provided with Apache OpenOffice API.

  // creates a new query definition named Query1
  public static void createQuerydefinition(XMultiServiceFactory _rMSF) throws com.sun.star.uno.Exception {
      XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(
          XNameAccess.class, _rMSF.createInstance( "com.sun.star.sdb.DatabaseContext") );
 
      // we use the datasource Bibliography
      XQueryDefinitionsSupplier xQuerySup = (XQueryDefinitionsSupplier) UnoRuntime.queryInterface(
          XQueryDefinitionsSupplier.class, xNameAccess.getByName( "Bibliography" )); 
 
      // get the container for query definitions
      XNameAccess xQDefs = xQuerySup.getQueryDefinitions();
 
      // for new query definitions we need the com.sun.star.lang.XSingleServiceFactory interface 
      // of the query definitions container
      XSingleServiceFactory xSingleFac = (XSingleServiceFactory)UnoRuntime.queryInterface(
          XSingleServiceFactory.class, xQDefs);
 
      // order a new query and get its com.sun.star.beans.XPropertySet interface
      XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(
          XPropertySet.class, xSingleFac.createInstance());
 
      // configure the query
      xProp.setPropertyValue("Command","SELECT * FROM biblio");
          xProp.setPropertyValue("EscapeProcessing", new Boolean(true));
 
      // insert it into the query definitions container
      XNameContainer xCont = (XNameContainer) UnoRuntime.queryInterface(
          XNameContainer.class, xQDefs);
 
      try{
          if ( xCont.hasByName("Query1") )
              xCont.removeByName("Query1");
      }catch(com.sun.star.uno.Exception e){}
 
      xCont.insertByName("Query1", xProp);
      XStorable store = ( XStorable)UnoRuntime.queryInterface(XStorable.class, xQuerySup);
      store.store();
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages