Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
DB Interface Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

5.2 Expressions, Criterions, and Assignments

One of the great advantages of a client/server environment is the ability of the client to defer computation to the server. When a complicated query is requested, the client does not have to execute the query itself; the server does the work. This is illustrated by SQL statements like this:

The WHERE clause, which is specified by the client but executed by the server, contains a type of expression that is encapsulated in the DB Interface Module by the classes RWDBExpr, RWDBCriterion, and RWDBAssignment. These classes represent the following expressions:

The various databases have minor differences in their expression syntax. For example, the Sybase database has a modulus operator, while the Oracle database uses a function call instead. The DB Interface Module reconciles these differences by using standard C++ expression syntax involving columns, literals, and function invocations to encapsulate expressions. The Access module then translates expressions into forms appropriate for the database in use. These translations are described in the guides for the Access Modules.

Instances of the RWDBExpr, RWDBCriterion, and RWDBAssignment classes are usually created anonymously. The following sections show how objects of these classes originate and interact.

5.2.1 RWDBCriterion

Both RWDBCriterion and RWDBAssignment derive from RWDBExpr. The next example creates a query based on the criterion that COL1 from some table has a value equal to 17:

The RWDBSelector instance on //2 is explained in Section 5.3 of this manual. For now, look at //3. The where() member function of RWDBSelector accepts an RWDBCriterion instance as an argument. A C++ expression involving an RWDBColumn instance column1, the operator ==, and a literal 17 are passed to it. Through automatic type conversions and overloading of the relational operator==, an RWDBCriterion instance is provided. Here's what happens:

  1. First, the compiler tries to apply operator== to the instance of the RWDBColumn and the literal integer 17.

  2. Since there is no operator to accomplish this, it tries to cast the column and integer into objects on which it can apply some operator==. The only action the compiler can take is to cast the RWDBColumn instance into an RWDBExpr.

  3. The compiler then does the same thing for the literal integer.

  4. Once these are cast, the compiler can apply the operator== for the two instances of RWDBExpr. Since operator== for two RWDBExpr instances returns an instance of RWDBCriterion, the where() member function is satisfied.

5.2.2 RWDBCriterion and RWDBExpr

Internally, RWDBExpr instances are data structures in the form of trees. The RWDBCriterion class derives from RWDBExpr and, therefore, is similarly structured. The example in Section 5.2.1 results in an RWDBCriterion instance containing a three-node binary tree. The root node represents the dyadic expression of the operator ==. The two leaf nodes represent the column and the literal integer 17.

It is important to note that evaluation of RWDBExpr instances is done by the library at the time of the execution. Expressions that produce RWDBExpr or RWDBCriterion instances might look like any other C++ expression, but their evaluation is deferred to the access library module.

Here is another example, which constructs a more complex RWDBCriterion instance. Assume that an RWDBSelector instance already exists:

The underlying access library module evaluates this expression according to its own syntax. For the Oracle database, the expression would be evaluated as:

For Sybase it would be slightly different:

This difference arises because the Oracle database has a modulus function, while the Sybase database has a modulus operator.

5.2.3 RWDBAssignment

RWDBAssignment also derives from RWDBExpr. It is used in assigning new values to columns in tables, in other words, updating. The only way to create an instance of class RWDBAssignment is through the invocation of the RWDBColumn::assign() method. An RWDBAssignment is an encapsulation of the SQL phrase:

where column refers to the RWDBColumn instance whose assign() method produced the RWDBAssignment, and expression refers to its argument. Here is an example:

This is equivalent to the SQL phrase:

The compiler interprets the SQL phrase as follows:

5.2.4 Summary

Class RWDBExpr and its derived classes RWDBCriterion and RWDBAssignment serve as encapsulations of expressions to be evaluated by the DB Interface Module and executed at the database. They allow the use of the familiar C++ expression syntax, while offering the advantage of database portability.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.