Skip to main content
Jump to: navigation, search

Difference between revisions of "Query DB Store by using SQL"

(Query for CDOObjects)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
This feature is implemented in current CVS Head for the CDO 3.0 release.<br>
+
It is possible to query the DB Store backend for CDO objects and also for more primitive data types.
It is possible to query the DB Store backend for CDO objects and also for more primitive data types.<br>
+
See also the Javadocs on [http://git.eclipse.org/c/cdo/cdo.git/tree/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/SQLQueryHandler.java SQLQueryHandler.java] for possibly newer information.
== Query for CDO object ==
+
 
This is done by using a CDOQuery object that can be obtained by a transaction. As query language "sql" must be used and the select statement must return the CDO ID of the object to fetch in the first column.<br>
+
== Query for CDOObjects ==
 +
 
 +
This is done by using a CDOQuery object that can be obtained from a CDOView or CDOTransaction.  
 +
The query language is "sql" and the select statement must return the CDOID(s) of the CDOObject(s) to fetch in the first column.
 +
 
 
For example:
 
For example:
<pre>
+
 
CDOQuery cdoQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM CUSTOMER");
+
  CDOQuery query = view.createQuery("sql", "SELECT cdo_id FROM Customer");
</pre>
+
 
 
The objects can now be fetched by using:
 
The objects can now be fetched by using:
<pre>
+
 
final List<Product1> products = cdoQuery.getResult(Product1.class);
+
  List<Customer> customers = query.getResult();
</pre>
+
 
== Query for primitive data type ==
+
The returned objects are valid in the context of the CDOView or CDOTransaction that the query has been created from
It is also possible to fetch more primitive types. In that case you have to switch the option "cdoObjectQuery" of the CDOQuery object (see the code below. By default this is set to true to query CDO objects.<br>
+
and can be navigated with normal EMF APIs.
<pre>
+
 
CDOQuery cdoQuery = transaction.createQuery("sql", "SELECT CITY FROM REPO1.CUSTOMER");
+
== Query for Primitive Data ==
cdoQuery.setParameter("cdoObjectQuery", false);
+
 
</pre>
+
It is also possible to fetch more primitive types. In that case you have to set the "cdoObjectQuery" query parameter to "false"
 +
(see the code below). By default this is set to true to query CDO objects.
 +
 
 +
  CDOQuery query = view.createQuery("sql", "SELECT city FROM Customer");
 +
  query.setParameter("cdoObjectQuery", false);
 +
 
 
The result can now be fetched by using:
 
The result can now be fetched by using:
<pre>
+
 
List<String> cities = new ArrayList<String>(cdoQuery.getResult(String.class));
+
  List<String> cities = query.getResult();
</pre>
+
 
== Do an asynchronous query ==
+
== Query Asynchronously ==
In the previous examples we queried the DB Store backend in a synchronously way (we always waited for the query to be completed). This is done automatically when using the getResult method of CDOQuery.
+
 
The DB Store backend can also be queried in an asynchronously way. That is done by using the getResultAsync method of CDOQuery:
+
In the previous examples we queried the DBStore backend in a synchronous way, i.e. query.getResult() blocked until the query was completed.  
<pre>
+
The DBStore backend can also be queried in an asynchronous way. That is done by using the getResultAsync() method of CDOQuery:
CDOQuery productQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM PRODUCT1");
+
 
CloseableIterator<Product1> iterator = productQuery.getResultAsync(Product1.class);
+
  CDOQuery query = view.createQuery("sql", "SELECT cdo_id FROM Customer");
</pre>
+
  CloseableIterator<Customer> iterator = query.getResultAsync(Customer.class);
The iterator can now be iterated and must be closed when finished:
+
 
<pre>
+
The iterator can now be iterated and '''must be closed when finished''':
iterator.close();
+
 
</pre>
+
  iterator.close();

Latest revision as of 00:58, 22 January 2013

It is possible to query the DB Store backend for CDO objects and also for more primitive data types. See also the Javadocs on SQLQueryHandler.java for possibly newer information.

Query for CDOObjects

This is done by using a CDOQuery object that can be obtained from a CDOView or CDOTransaction. The query language is "sql" and the select statement must return the CDOID(s) of the CDOObject(s) to fetch in the first column.

For example:

 CDOQuery query = view.createQuery("sql", "SELECT cdo_id FROM Customer");

The objects can now be fetched by using:

 List<Customer> customers = query.getResult();

The returned objects are valid in the context of the CDOView or CDOTransaction that the query has been created from and can be navigated with normal EMF APIs.

Query for Primitive Data

It is also possible to fetch more primitive types. In that case you have to set the "cdoObjectQuery" query parameter to "false" (see the code below). By default this is set to true to query CDO objects.

 CDOQuery query = view.createQuery("sql", "SELECT city FROM Customer");
 query.setParameter("cdoObjectQuery", false);

The result can now be fetched by using:

 List<String> cities = query.getResult();

Query Asynchronously

In the previous examples we queried the DBStore backend in a synchronous way, i.e. query.getResult() blocked until the query was completed. The DBStore backend can also be queried in an asynchronous way. That is done by using the getResultAsync() method of CDOQuery:

 CDOQuery query = view.createQuery("sql", "SELECT cdo_id FROM Customer");
 CloseableIterator<Customer> iterator = query.getResultAsync(Customer.class);

The iterator can now be iterated and must be closed when finished:

 iterator.close();

Back to the top