Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

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.<br>
 
It is possible to query the DB Store backend for CDO objects and also for more primitive data types.<br>
== 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>
 
<pre>
CDOQuery cdoQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM CUSTOMER");
+
CDOQuery query = view.createQuery("sql", "SELECT cdo_id FROM Customer");
 
</pre>
 
</pre>
 +
 
The objects can now be fetched by using:
 
The objects can now be fetched by using:
 
<pre>
 
<pre>
final List<Product1> products = cdoQuery.getResult(Product1.class);
+
List<Customer> customers = query.getResult();
 
</pre>
 
</pre>
== Query for primitive data type ==
+
 
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>
+
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.
 +
 
 +
Note: Until [https://bugs.eclipse.org/bugs/show_bug.cgi?id=397948 Bug 397948] is fixed the result list may need to be copied into an ArrayList in rare cases such as:
 
<pre>
 
<pre>
CDOQuery cdoQuery = transaction.createQuery("sql", "SELECT CITY FROM REPO1.CUSTOMER");
+
List<Cutomer> result = query.getResult();
cdoQuery.setParameter("cdoObjectQuery", false);
+
myObject.getCustomers().removeAll(new ArrayList<Customer>(result));
 
</pre>
 
</pre>
 +
 +
 +
== 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.
 +
 +
<pre>
 +
CDOQuery query = view.createQuery("sql", "SELECT city FROM Customer");
 +
query.setParameter("cdoObjectQuery", false);
 +
</pre>
 +
 
The result can now be fetched by using:
 
The result can now be fetched by using:
 
<pre>
 
<pre>
List<String> cities = new ArrayList<String>(cdoQuery.getResult(String.class));
+
List<String> cities = query.getResult();
 
</pre>
 
</pre>
== Do an asynchronous query ==
+
 
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:
+
== 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:
 +
 
 
<pre>
 
<pre>
CDOQuery productQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM PRODUCT1");
+
CDOQuery query = view.createQuery("sql", "SELECT cdo_id FROM Customer");
CloseableIterator<Product1> iterator = productQuery.getResultAsync(Product1.class);
+
CloseableIterator<Customer> iterator = query.getResultAsync(Customer.class);
 
</pre>
 
</pre>
The iterator can now be iterated and must be closed when finished:
+
 
 +
The iterator can now be iterated and '''must be closed when finished''':
 
<pre>
 
<pre>
 
iterator.close();
 
iterator.close();
 
</pre>
 
</pre>

Revision as of 00:34, 22 January 2013

It is possible to query the DB Store backend for CDO objects and also for more primitive data types.


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.

Note: Until Bug 397948 is fixed the result list may need to be copied into an ArrayList in rare cases such as:

List<Cutomer> result = query.getResult();
myObject.getCustomers().removeAll(new ArrayList<Customer>(result));


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