Table of Contents
Being able to only match structure is of limited usefulness in querying the database. We must also be able to consider the semantic information contained in object and link attributes when defining and matching queries. Conditions allow queries to move beyond purely structural matches to consider database content. Specifically, conditions serve to restrict the number of query matches by placing requirements on the attributes of an object or link in the database.
Conditions are commonly, but certainly not exclusively, used to enforce type restrictions in query matches. However, QGraph only tests and compares attribute values; it makes no assumptions about how type information is stored. It’s up to you to specify the appropriate attribute to examine.
QGraph provides two types of conditions:
Attribute value conditions restrict matches to only those database entities that match the attribute values specified in the query.
Existence conditions test only to see whether the corresponding object or link has a specified attribute, but does not require that the attribute have any particular value.
To see attribute value conditions in action, let’s revisit the database originally shown in Figure 2.2 and repeated below. This database contains information on authors, books, movies, and the relationships among these entities.
In this database, object type information is represented as the value of the
ObjType attribute and link type information is
represented as the value of the LinkType attribute.
The query in Figure 3.2
identifies all author-book pairs in the data by
matching database structures that connect person objects to book
objects via author-of links.
The text under each query element specifies a condition on that element.
The author vertex has the condition
ObjType = person. Only
objects having the value person for the
ObjType attribute will match this vertex.
Similarly, the query will only match links whose
LinkType attribute have the value
author-of.
Once we add conditions, it’s common to use semantically meaningful names for the vertices and edges in a query. Such names usually reflect the restrictions imposed by the conditions, such as we see above. Although these names may duplicate required attribute values, it is the condition, not the label, that determines query behavior. Executing this query on the database fragment above yields the results shown in Figure 3.3.
Compare these results to the purely structural matches shown in
Figure 2.4.
Adding conditions to the query limits
the results to only those
subgraphs that have the
required attribute values. Specifically, the new query requires that the
author vertex match only
person objects, the
book vertex matches only
book objects, and the
author-of edge matches only
author-of links.
The new query yields only two matches versus the four subgraphs found
by the original query.
As we noted earlier, unlike the SELECT
statement in SQL,
a QGraph query does not specify which attributes
of matching objects or edges are included in the query results
(subgraphs).
This continues to hold even when queries include conditions that
mention only some of these attributes.
All the object and link attributes, not just those mentioned in the
query, are available for inspection in the query results.
In addition to testing for equality, conditions can compare
attribute values to the specified values using the comparison
operators <>, <,
<=, >, and
>=.
For example, to find authors who have published before 1990,
we create the following query:
Because the book The Talisman is the only object
with a PubDate before
1990, this query returns the
same results as the earlier example.