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.
Difference between revisions of "Query DB Store by using SQL"
(→Query for CDOObjects) |
(→Query for CDOObjects) |
||
(3 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. | + | 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 14: | Line 14: | ||
List<Customer> customers = query.getResult(); | 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 | 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. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
== Query for Primitive Data == | == Query for Primitive Data == | ||
Line 29: | 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. | ||
− | + | 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); | + | |
− | + | ||
The result can now be fetched by using: | The result can now be fetched by using: | ||
− | |||
− | |||
− | |||
+ | List<String> cities = query.getResult(); | ||
== Query Asynchronously == | == Query Asynchronously == | ||
Line 45: | 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: | ||
− | + | 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); | + | |
− | + | ||
The iterator can now be iterated and '''must be closed when finished''': | The iterator can now be iterated and '''must be closed when finished''': | ||
− | + | ||
− | iterator.close(); | + | iterator.close(); |
− | + |
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();