Sun WBEM SDK Developer's Guide

Querying

The enumeration APIs return all instances in a class or class hierarchy. You can choose to return the instance names or the details of the instance. Querying allows you to narrow your search by specifying a query string. You can search for instances that match a specified query in a particular class or in all classes in a particular namespace. For example, you can search for all instances of the Solaris_DiskDrive class that have a particular value for the Storage_Capacity property.

The execQuery Method

The execQuery method retrieves an enumeration of CIM instances that match a query string. The query string must be formed using the WBEM Query Language (WQL).

Syntax

The syntax for the execQuery method is:

Enumeration execQuery(CIMObjectPath relNS, java.lang.String query, int ql)

The execQuery method takes the following parameters and returns an enumeration of CIM instances:

Parameter 

Data Type 

Description 

relNS 

CIMObjectPath 

The namespace relative to the namespace to which you are connected. For example, if you are connected to the root namespace and want to query classes in the root\cimv2 namespace, you would pass new CIMObjectPath("", "cimv2");.

query 

String 

The text of the query in WBEM Query Language 

ql 

Integer constant 

Identifies the query language. WQL level 1 is the only currently supported query language. 

Example

The following execQuery call returns an enumeration of all instances of the CIM_device class in the current namespace.

cc.execQuery(new CIMObjectPath(), SELECT * FROM CIM_device, cc.WQL)

Using the WBEM Query Language

The WBEM Query Language is a subset of standard American National Standards Institute Structured Query Language (ANSI SQL) with semantic changes to support WBEM on Solaris. Unlike SQL, in this release WQL is a retreval-only language. You cannot use WQL to modify, insert, or delete information.

SQL was written to query databases, in which data is stored in tables with a row-column structure. WQL has been adapted to query data that is stored using the CIM data model. In the CIM model, information about objects is stored in CIM classes and CIM instances. CIM instances can contain properties, which have a name, data type, and value. WQL maps the CIM object model to SQL tables.

Table 4–2 Mapping of SQL to WQL Data

SQL 

Is Represented in WQL as... 

Table 

CIM class 

Row 

CIM instance 

Column 

CIM property 

Supported WQL Key Words

The Sun WBEM SDK supports Level 1 WBEM SQL, which enables simple select operations without joins. The following table describes the WQL key words supported in the Sun WBEM SDK.

Table 4–3 Supported WQL Key Words

Key Word 

Description 

AND 

Combines two Boolean expressions and returns TRUE when both expressions are TRUE. 

FROM 

Specifies the classes that contain the properties listed in a SELECT statement. 

NOT 

Comparison operator used with NULL. 

OR 

Combines two conditions. When more than one logical operator is used in a statement, OR operators are evaluated after AND operators. 

SELECT 

Specifies the properties that will be used in a query. 

WHERE 

Narrows the scope of a query. 

WBEM Query Language Operators

The following table lists the standard WQL operators that can be used in the WHERE clause of a SELECT statement.

Table 4–4 WQL Operators

Operator 

Description 

Equal to 

Less than 

Greater than 

<= 

Less than or equal to 

>= 

Greater than or equal to 

<> 

Not equal to 

Making a Data Query

Data queries are statements that request instances of classes. To issue a data query, applications use the execQuery method to pass a WBEM Query Language string to the CIM Object Manager.

The SELECT Statement

The SELECT statement is the SQL statement for retrieving information, with a few restrictions and extensions specific to WQL. Although the SQL SELECT statement is typically used in the database environment to retrieve particular columns from tables, the WQL SELECT statement is used to retrieve instances of a single class. WQL does not support queries across multiple classes.

The SELECT statement specifies the properties to query in an object specified in the FROM clause.

The basic syntax for the SELECT statement is:

SELECT instance FROM class

The following tables shows examples of using arguments in the SELECT clause to refine a search.

Table 4–5 SELECT Statement

Example Query 

Description 

SELECT * FROM class

Selects all instances of the specified class and any of its subclasses. 

SELECT PropertyA FROM class 

Selects only instances of the specified class and any of its subclasses that contain PropertyA.

SELECT PropertyA, PropertyB FROM class

Selects only instances of the specified class and any of its subclasses that contain PropertyA or PropertyB.

The WHERE Clause

You can use the WHERE clause to narrow the scope of a query. The WHERE clause can contain a property or key word, an operator, and a constant. All WHERE clauses must specify one of the predefined WQL operators.

The basic syntax for appending the WHERE clause to the SELECT statement is:

SELECT instance FROM class WHERE expression

The expression is composed of a property or key word, an operator, and a constant. You can append the WHERE clause to the SELECT statement using one of the following forms:

SELECT instance FROM class [[WHERE property operator constant]]

SELECT instance FROM class [[WHERE constant operator property]]

Valid WHERE clauses follow these rules:

Multiple groups of properties, operators, and constants can be combined in a WHERE clause using logical operators and parenthetical expressions. Each group must be joined with the AND, OR, or NOT operators as shown in the following table.

Table 4–6 Queries Using Logical Operators

Example Query 

Description 

SELECT * FROM Solaris_FileSystem WHERE Name= "home" OR Name= "files" 

Retrieves all instances of the Solaris_FileSystem class with the Name property set to either home or files.

SELECT * FROM Solaris_FileSystem WHERE (Name = “home” OR Name = “files”) AND AvailableSpace > 2000000 AND FileSystem = “Solaris” 

Retrieves disks named home and files only if they have a certain amount of available space remaining and have Solaris file systems.