The main class to create Query object is: org.eclipse.emf.query2.Query. You can use the constructors in Query class to create instance of Query. A Query requires mandatorily define following:

+

+

#For specifying select clause: One or more SelectAttrs or SelectAlias

+

#For specifying From clause: One or more FromType\FromFixedSet

+

+

You can also specify where clauses to specify conditions by specifying following:

+

+

#For normal conditions: Use following:

+

+

'''LocalWhereEntry:''' A local where-entry allows the definition of a boolean expression, which compares the primitive typed attributes of one alias.

+

+

#For Join conditions: Use any of following:

+

+

a. '''WhereComparisonAliases''': A where-comparison aliases compares two aliases to each other for equality, where both aliases have to be of the same type.

+

+

b. '''WhereComparisonAttrs''': A where-comparison attributes compares two attributes of two aliases to each other for equality, where both attributes have to be of the same primitive type.

+

+

c. '''WhereNestedReference''': A nested where-entry defines a where-entry which constrains an alias by connecting it via an association to the results of a nested query. Nested where-entries can be negated.

+

+

d. '''WhereRelationReference''': A where-relation defines a where-entry which constrains two aliases to be involved in an association relation

+

+

=== Sample Queries: ===

+

''' Note: ''' The model used in the queries is mentioned in Appendix: Sample data

Installation

Overview of Query2

Query2 provides easy mechanism to query emf metadata with minimal loading of resources. Query2 has an in-built indexing mechanism which indexes types, resources and references. This data is used during query execution and hence avoiding loading of resources wherever needed. Hence, if you want to use query2, you are mandatorily required to setup indexing. After that you can use it execute queries. We will see how to set it up in later sections. Query2 also provides you different ways to specify queries. We will see them in coming sections.

Similarly you can IndexQueryFactory .createResourceQuery() to create queries on Resources on the common index.

Step 3 Using QueryIndexBuilder: In eclipse IDE, we work on eclipse resources. Query2 indexing component provides an utility to index all the resources in your project using a custom builder. To enable it, you have to execute following steps:

1. Add the project nature: “org.eclipse.emf.query.index.ui.queryIndexNature” Open the .project file for your project and add this nature.

2. Add Builder: “org.eclipse.emf.query.index.ui.queryIndexBuilder” Open the .project file and add this builder.

3. The Builder will index all the files with extensions registered in EMF extensionToFactoryMap. You can add your file extension to factory using plugin.xml e.g as in following snippet:

Writing Queries in SQL like syntax

The support to write string based queries is present if you install query2.syntax feature. If you have these features execute following steps to have the setup ready for execution: 1. In your plugin create a new file by some name and file extension “query” e.g searchQueries.query This file will contain all the queries you want to execute

2. Specify query in the file:

a. Specify import statement to the URI which will be used to identify types in the Package at runtime e.g.

Samples Queries

Note: The model used in the queries is mentioned in Appendix: Sample data 1. Select all books borrowed by an author:

from Book as b
select b
where b.borrowedBy in (
from Manuscript as m, Person as p select p where m.author = p
)

2. Select pages with title “Linux made easy”

from Manuscript as m select m.pages where m.title = 'Linux Made Easy'

3. Select all Manuscripts only In Berlin.xml or Hamburg.xmi

from Library as lib
in resources {"platform:/resource/org.eclipse.emf.query2.librarytest/data/library/Hamburg.xmi","platform:/resource/org.eclipse.emf.query2.librarytest/data/library/Berlin.xmi"},
Book as b,
Manuscript as m
select m
where lib.books = b
and b.instanceOf = m

For accessing sample data, refer to Appendix: Sample data

Writing Queries in Object format

The main class to create Query object is: org.eclipse.emf.query2.Query. You can use the constructors in Query class to create instance of Query. A Query requires mandatorily define following:

For specifying select clause: One or more SelectAttrs or SelectAlias

For specifying From clause: One or more FromType\FromFixedSet

You can also specify where clauses to specify conditions by specifying following:

For normal conditions: Use following:

LocalWhereEntry: A local where-entry allows the definition of a boolean expression, which compares the primitive typed attributes of one alias.

For Join conditions: Use any of following:

a. WhereComparisonAliases: A where-comparison aliases compares two aliases to each other for equality, where both aliases have to be of the same type.

b. WhereComparisonAttrs: A where-comparison attributes compares two attributes of two aliases to each other for equality, where both attributes have to be of the same primitive type.

c. WhereNestedReference: A nested where-entry defines a where-entry which constrains an alias by connecting it via an association to the results of a nested query. Nested where-entries can be negated.

d. WhereRelationReference: A where-relation defines a where-entry which constrains two aliases to be involved in an association relation

Sample Queries:

Note: The model used in the queries is mentioned in Appendix: Sample data
1. Select all books borrowed by an author

Sample Data

The samples data exists in eclipse CVS at following location: “/cvsroot/modeling/org.eclipse.emf/org.eclipse.emf.query/tests/org.eclipse.emf.query2.librarytest” For accessing eclipse CVS you can refer to following page: CVS_HowTo.

Use “:pserver:anonymous@dev.eclipse.org:/cvsroot/modeling” to access projects related to Modeling. The model used in the samples data is following: