Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be 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"

(Query for CDOObjects)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
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.
 
+
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 CDOObjects ==
 
== Query for CDOObjects ==
Line 8: Line 8:
  
 
For example:
 
For example:
<pre>
+
 
CDOQuery query = view.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>
+
 
List<Customer> customers = query.getResult();
+
  List<Customer> customers = query.getResult();
</pre>
+
  
 
The returned objects are valid in the context of the CDOView or CDOTransaction that the query has been created from  
 
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.
 
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>
 
List<Cutomer> result = query.getResult();
 
myObject.getCustomers().removeAll(new ArrayList<Customer>(result));
 
</pre>
 
 
  
 
== Query for Primitive Data ==
 
== Query for Primitive Data ==
Line 32: Line 23:
 
(see the code below). By default this is set to true to query CDO objects.
 
(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");
CDOQuery query = view.createQuery("sql", "SELECT city FROM Customer");
+
  query.setParameter("cdoObjectQuery", false);
query.setParameter("cdoObjectQuery", false);
+
</pre>
+
  
 
The result can now be fetched by using:
 
The result can now be fetched by using:
<pre>
 
List<String> cities = query.getResult();
 
</pre>
 
  
 +
  List<String> cities = query.getResult();
  
 
== Query Asynchronously ==
 
== Query Asynchronously ==
Line 48: Line 35:
 
The DBStore backend can also be queried in an asynchronous way. That is done by using the getResultAsync() method of CDOQuery:
 
The DBStore backend can also be queried in an asynchronous way. That is done by using the getResultAsync() method of CDOQuery:
  
<pre>
+
  CDOQuery query = view.createQuery("sql", "SELECT cdo_id FROM Customer");
CDOQuery query = view.createQuery("sql", "SELECT cdo_id FROM Customer");
+
  CloseableIterator<Customer> iterator = query.getResultAsync(Customer.class);
CloseableIterator<Customer> iterator = query.getResultAsync(Customer.class);
+
</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>
+
 
iterator.close();
+
  iterator.close();
</pre>
+

Latest revision as of 01: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