Difference between revisions of "Documentation/DevGuide/Database/Adding and Editing Predefined Queries"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (1 revision(s))
m
Line 10: Line 10:
 
<!--<idltopic>com.sun.star.sdb.DefinitionContainer</idltopic>-->
 
<!--<idltopic>com.sun.star.sdb.DefinitionContainer</idltopic>-->
 
The query definitions container <idl>com.sun.star.sdb.DefinitionContainer</idl> is used to work with the query definitions of a data source. It is returned by the <idl>com.sun.star.sdb.XQueryDefinitionsSupplier</idl> interface of the data source, which has a single method for this purpose:
 
The query definitions container <idl>com.sun.star.sdb.DefinitionContainer</idl> is used to work with the query definitions of a data source. It is returned by the <idl>com.sun.star.sdb.XQueryDefinitionsSupplier</idl> interface of the data source, which has a single method for this purpose:
 
+
<source lang="idl">
 
   com::sun::star::container::XNameAccess getQueryDefinitions()
 
   com::sun::star::container::XNameAccess getQueryDefinitions()
 
+
</source>
 
The <code>DefinitionContainer</code> is not only an <code>XNameAccess</code>, but a <idl>com.sun.star.container.XNameContainer</idl>, that is, add new query definitions by name (see [[Documentation/DevGuide/FirstSteps/First Steps|First Steps]]). Besides the name access, obtain query definitions through <idl>com.sun.star.container.XIndexAccess</idl> and <idl>com.sun.star.container.XEnumerationAccess</idl>.
 
The <code>DefinitionContainer</code> is not only an <code>XNameAccess</code>, but a <idl>com.sun.star.container.XNameContainer</idl>, that is, add new query definitions by name (see [[Documentation/DevGuide/FirstSteps/First Steps|First Steps]]). Besides the name access, obtain query definitions through <idl>com.sun.star.container.XIndexAccess</idl> and <idl>com.sun.star.container.XEnumerationAccess</idl>.
  
Line 48: Line 48:
 
The following example adds a new query definition <code>Query1</code> to the data source Bibliography that is provided with {{PRODUCTNAME}} API.  
 
The following example adds a new query definition <code>Query1</code> to the data source Bibliography that is provided with {{PRODUCTNAME}} API.  
 
<!--[SOURCE:Database/CodeSamples.java]-->
 
<!--[SOURCE:Database/CodeSamples.java]-->
 
+
<source lang="java">
 
   // creates a new query definition named Query1
 
   // creates a new query definition named Query1
 
   public static void createQuerydefinition(XMultiServiceFactory _rMSF) throws com.sun.star.uno.Exception {
 
   public static void createQuerydefinition(XMultiServiceFactory _rMSF) throws com.sun.star.uno.Exception {
Line 87: Line 87:
 
       store.store();
 
       store.store();
 
   }
 
   }
 
+
</source>
 
{{PDL1}}
 
{{PDL1}}
 
[[Category: Database Access]]
 
[[Category: Database Access]]

Revision as of 20:18, 14 April 2008



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.

Template:Documentation/Note

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 OpenOffice.org 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 OpenOffice.org 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