Difference between revisions of "Documentation/DevGuide/Database/The SingleSelectQueryComposer"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (1 revision(s))
m
Line 14: Line 14:
  
 
A query composer <idl>com.sun.star.sdb.SingleSelectQueryComposer</idl> is retrieved over the <idl>com.sun.star.lang.XMultiServiceFactory</idl> interface of a <idl>com.sun.star.sdb.Connection</idl>:
 
A query composer <idl>com.sun.star.sdb.SingleSelectQueryComposer</idl> is retrieved over the <idl>com.sun.star.lang.XMultiServiceFactory</idl> interface of a <idl>com.sun.star.sdb.Connection</idl>:
 
+
<source lang="idl">
 
   com::sun::star::uno::XInterface createInstance( [in] string aServiceSpecifier )
 
   com::sun::star::uno::XInterface createInstance( [in] string aServiceSpecifier )
 
+
</source>
 
The interface <idl>com.sun.star.sdb.XSingleSelectQueryAnalyzer</idl> is used to supply the <code>SingleSelectQueryComposer</code> with the necessary information. It has the following methods:
 
The interface <idl>com.sun.star.sdb.XSingleSelectQueryAnalyzer</idl> is used to supply the <code>SingleSelectQueryComposer</code> with the necessary information. It has the following methods:
 
+
<source lang="idl">
 
   // provide SQL string
 
   // provide SQL string
 
   void setQuery( [in] string command)
 
   void setQuery( [in] string command)
Line 38: Line 38:
 
   string getOrder()
 
   string getOrder()
 
   com::sun::star::container::XIndexAccess getOrderColumns();
 
   com::sun::star::container::XIndexAccess getOrderColumns();
 
+
</source>
 
The example below shows a simple test case for the <idl>com.sun.star.sdb.SingleSelectQueryComposer</idl>:
 
The example below shows a simple test case for the <idl>com.sun.star.sdb.SingleSelectQueryComposer</idl>:
 
+
<source lang="java">
 
   public void testSingleSelectQueryComposer() {
 
   public void testSingleSelectQueryComposer() {
 
       log.println("testing SingleSelectQueryComposer");
 
       log.println("testing SingleSelectQueryComposer");
Line 127: Line 127:
 
       }
 
       }
 
   }
 
   }
 
+
</source>
 
In the previous code example, a query command is passed to <code>setQuery()</code>, then the criteria for <code>WHERE</code>, and <code>GROUP BY</code>, and <code>HAVING</code>, and <code>ORDER BY</code> is added. The <code>WHERE</code> expressions are passed without the <code>WHERE</code> keyword to <code>setFilter()</code>, and the method <code>setOrder()</code>, with comma-separated <code>ORDER BY</code> columns or column numbers, is provided.  
 
In the previous code example, a query command is passed to <code>setQuery()</code>, then the criteria for <code>WHERE</code>, and <code>GROUP BY</code>, and <code>HAVING</code>, and <code>ORDER BY</code> is added. The <code>WHERE</code> expressions are passed without the <code>WHERE</code> keyword to <code>setFilter()</code>, and the method <code>setOrder()</code>, with comma-separated <code>ORDER BY</code> columns or column numbers, is provided.  
  
Line 139: Line 139:
  
 
The interface <idl>com.sun.star.sdbcx.XTablesSupplier</idl> provides access to the tables that are used in the FROM part of the SQL-Statement:
 
The interface <idl>com.sun.star.sdbcx.XTablesSupplier</idl> provides access to the tables that are used in the FROM part of the SQL-Statement:
 
+
<source lang="idl">
 
   com::sun::star::container::XNameAccess getTables()
 
   com::sun::star::container::XNameAccess getTables()
 
+
</source>
 
The interface <idl>com.sun.star.sdbcx.XColumnsSupplier</idl> provides the selected columns, which are listed after the SELECT keyword:
 
The interface <idl>com.sun.star.sdbcx.XColumnsSupplier</idl> provides the selected columns, which are listed after the SELECT keyword:
 
+
<source lang="idl">
 
   com::sun::star::container::XNameAccess getColumns()
 
   com::sun::star::container::XNameAccess getColumns()
 
+
</source>
 
The interface <idl>com.sun.star.sdb.XParametersSupplier</idl> provides the parameters, which are used in the where clause:
 
The interface <idl>com.sun.star.sdb.XParametersSupplier</idl> provides the parameters, which are used in the where clause:
 
+
<source lang="idl">
 
   com::sun::star::container::XIndexAccess getParameters()
 
   com::sun::star::container::XIndexAccess getParameters()
 
+
</source>
 
{{PDL1}}
 
{{PDL1}}
 
[[Category: Database Access]]
 
[[Category: Database Access]]

Revision as of 10:22, 19 April 2008



The service com.sun.star.sdb.SingleSelectQueryComposer is a tool that analyzes and composes single select statement strings. It is a replacement for the service com.sun.star.sdb.SQLQueryComposer. The query composer is divided into two parts. The first part defines the analyzing of the single select statement. The service com.sun.star.sdb.SingleSelectQueryAnalyzer hides the complexity of parsing and evaluating a single select statement, and provides methods for accessing a statements filter, group by, having and order criteria, as well as the corresponding select columns and tables. If supported, the service gives access to the parameters contained in the single select statement.

The second part of the query composer modifies the single select statement. The service com.sun.star.sdb.SingleSelectQueryComposer extends the service com.sun.star.sdb.SingleSelectQueryAnalyzer and provides methods for expanding a statement with filter, group by, having and order criteria. To get the new, extended statement, the methods from com.sun.star.sdb.SingleSelectQueryAnalyzer have to be used.

A query composer com.sun.star.sdb.SingleSelectQueryComposer is retrieved over the com.sun.star.lang.XMultiServiceFactory interface of a com.sun.star.sdb.Connection:

  com::sun::star::uno::XInterface createInstance( [in] string aServiceSpecifier )

The interface com.sun.star.sdb.XSingleSelectQueryAnalyzer is used to supply the SingleSelectQueryComposer with the necessary information. It has the following methods:

  // provide SQL string
  void setQuery( [in] string command)
  string getQuery()
 
  // filter
  string getFilter()
  sequence< sequence< com::sun::star::beans::PropertyValue > > getStructuredFilter()
 
  // GROUP BY
  string getGroup(); 
  com::sun::star::container::XIndexAccess getGroupColumns();
 
  // HAVING
  string getHavingClause(); 
  sequence< sequence<com::sun::star::beans::PropertyValue> > getStructuredHavingFilter();
 
  // control the ORDER BY clause
  string getOrder()
  com::sun::star::container::XIndexAccess getOrderColumns();

The example below shows a simple test case for the com.sun.star.sdb.SingleSelectQueryComposer:

  public void testSingleSelectQueryComposer() {
      log.println("testing SingleSelectQueryComposer");
 
      try
      {
          XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class,
              ((XMultiServiceFactory)param.getMSF()).createInstance("com.sun.star.sdb.DatabaseContext"));
          // we use the first datasource
          XDataSource xDS = (XDataSource)UnoRuntime.queryInterface(XDataSource.class, 
              xNameAccess.getByName( "Bibliography" )); 
 
          log.println("check XMultiServiceFactory");
          XMultiServiceFactory xConn = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xDS.getConnection(new String(),new String()));
 
          log.println("check getAvailableServiceNames");
          String[] sServiceNames = xConn.getAvailableServiceNames();
          assure("Service 'SingleSelectQueryComposer' not supported" ,sServiceNames[0].equals("com.sun.star.sdb.SingleSelectQueryComposer"));
          XSingleSelectQueryAnalyzer xQueryAna = (XSingleSelectQueryAnalyzer)
              UnoRuntime.queryInterface(XSingleSelectQueryAnalyzer.class,xConn.createInstance( sServiceNames[0]));
 
          log.println("check setQuery");
          xQueryAna.setQuery("SELECT * FROM \"biblio\"");
          assure("Query not identical", xQueryAna.getQuery().equals("SELECT * FROM \"biblio\""));
 
          // XSingleSelectQueryComposer
          XSingleSelectQueryComposer xComposer = (XSingleSelectQueryComposer)
              UnoRuntime.queryInterface(XSingleSelectQueryComposer.class,xQueryAna);
 
          log.println("check setFilter");
          // filter
          xComposer.setFilter("\"Identifier\" = 'BOR02b'");
          assure("Query not identical:" + xQueryAna.getFilter() + " -> \"Identifier\" = 'BOR02b'", xQueryAna.getFilter().equals("\"Identifier\" = 'BOR02b'"));
 
          log.println("check setGroup");
          // group by
          xComposer.setGroup("\"Identifier\"");
          assure("Query not identical:" + xQueryAna.getGroup() + " -> \"Identifier\"", xQueryAna.getGroup().equals("\"Identifier\""));
 
          log.println("check setOrder");
          // order by
          xComposer.setOrder("\"Identifier\"");
          assure("Query not identical:" + xQueryAna.getOrder() + " -> \"Identifier\"", xQueryAna.getOrder().equals("\"Identifier\""));
 
          log.println("check setHavingClause");
          // having
          xComposer.setHavingClause("\"Identifier\" = 'BOR02b'");
          assure("Query not identical:" + xQueryAna.getHavingClause() + " -> \"Identifier\" = 'BOR02b'", xQueryAna.getHavingClause().equals("\"Identifier\" = 'BOR02b'"));
 
          log.println("check getOrderColumns");
          // order by columns
          XIndexAccess xOrderColumns = xQueryAna.getOrderColumns();
          assure("Order columns doesn't exist -> \"Identifier\"", xOrderColumns != null && xOrderColumns.getCount() == 1 && xOrderColumns.getByIndex(0) != null);
 
          log.println("check getGroupColumns");
          // group by columns
          XIndexAccess xGroupColumns = xQueryAna.getGroupColumns();
          assure("Group columns doesn't exist -> \"Identifier\"", xGroupColumns != null && xGroupColumns.getCount() == 1 && xGroupColumns.getByIndex(0) != null);
 
          log.println("check getColumns");
          // XColumnsSupplier
          XColumnsSupplier xSelectColumns = (XColumnsSupplier)
          UnoRuntime.queryInterface(XColumnsSupplier.class,xQueryAna);
          assure("Select columns doesn't exist", xSelectColumns != null && xSelectColumns.getColumns() != null && xSelectColumns.getColumns().getElementNames().length != 0);
 
          log.println("check structured filter");
          // structured filter
          xQueryAna.setQuery("SELECT \"Identifier\", \"Type\", \"Address\" FROM \"biblio\" \"biblio\"");
          xComposer.setFilter(complexFilter);
          PropertyValue[][] aStructuredFilter = xQueryAna.getStructuredFilter();
          xComposer.setFilter("");
          xComposer.setStructuredFilter(aStructuredFilter);
          assure("Structured Filter not identical" , xQueryAna.getFilter().equals(complexFilter));
 
          log.println("check structured having");
          // structured having clause
          xComposer.setHavingClause(complexFilter);
          PropertyValue[][] aStructuredHaving = xQueryAna.getStructuredHavingFilter();
          xComposer.setHavingClause("");
          xComposer.setStructuredHavingFilter(aStructuredHaving);
          assure("Structured Having Clause not identical" , xQueryAna.getHavingClause().equals(complexFilter));
      }
      catch(Exception e)
      {
          assure("Exception catched: " + e,false);
      }
  }

In the previous code example, a query command is passed to setQuery(), then the criteria for WHERE, and GROUP BY, and HAVING, and ORDER BY is added. The WHERE expressions are passed without the WHERE keyword to setFilter(), and the method setOrder(), with comma-separated ORDER BY columns or column numbers, is provided.

As an alternative, add WHERE conditions using appendFilterByColumn(). This method expects a com.sun.star.sdb.DataColumn service providing the name and the value for the filter. Similarly, the method appendOrderByColumn() adds columns that are used for ordering. The same applies to appendGroupByColumn() and appendHavingFilterByColumn(). These columns can come from the RowSet.

The Orignal property at the service com.sun.star.sdb.SingleSelectQueryAnalyzer holds the original single select statement.

The methods getQuery(), getFilter() and getOrder() return the complete SELECT, WHERE and ORDER BY part of the single select statement as a string.

The method getStructuredFilter() returns the filter split into OR levels. Within each OR level, filters are provided as AND criteria, with the name of the column and the filter condition string.

The interface com.sun.star.sdbcx.XTablesSupplier provides access to the tables that are used in the FROM part of the SQL-Statement:

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

The interface com.sun.star.sdbcx.XColumnsSupplier provides the selected columns, which are listed after the SELECT keyword:

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

The interface com.sun.star.sdb.XParametersSupplier provides the parameters, which are used in the where clause:

  com::sun::star::container::XIndexAccess getParameters()
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools