Composing FROM Clause
In most cases, it is not necessary to deal explicitly with
FROM entries while using class
RWDBSelector, as they can be deduced from the columns to be selected and those referenced in the instance of
RWDBCriterion set for the
WHERE clause. However, in certain circumstances it might be desirable to manipulate the
FROM clause. For example, when using
RWDBJoinExpr, correlated sub-queries, or when adding an unreferenced table.
RWDBSelector provides methods —
from(),
fromClear(),
fromGeneration(),
fromExtern(), and
fromExternClear() — to manipulate the
FROM clause generation.
The list of the FROM entries deduced from the select column list and the WHERE criterion forms the implicit FROM entries list. Use of from() methods forms the explicit FROM entries list. Each invocation of a from() method adds an entry to the explicit FROM entries list. Method fromClear() may be used to clear the explicit FROM entries list.
The enumeration
FromGeneration defines the behavior used to compose the
FROM clause from the implicit and explicit
FROM entries lists. The two values the
enum defines are
ExplicitOrImplicit and
ExplicitAndImplicit. The
enum ExplicitOrImplicit composes the
FROM clause based on the explicit
FROM entries list if there are any entries; otherwise it uses the implicit
FROM entries list. Thus, it results in the use of entries in
either explicit or implicit lists, but not both. The
enum ExplicitAndImplicit composes the
FROM clause as a
union of the implicit and explicit
FROM entries lists. It combines entries from both the lists, removing duplicate entries. It also removes the table entries that are a part of a join expression included in the explicit list. The
fromGeneration()methods act as get and set methods for defining the behavior. Default behavior is
ExplicitOrImplicit.
If a table is in the implicit
FROM list, but should not be included in the
FROM clause, it should be marked external by using the method
fromExtern(). A table marked external using
fromExtern() is assumed to have been defined outside the query and hence will not be included in the
FROM clause. For example, in a correlated sub-query, a table defined in the outer SQL is used inside the sub-query. The sub-query should not define the table, as the table definition is external to the sub-query. (See
Subqueries.). Every call to
fromExtern() adds a table to the extern table list. Method
fromExternClear() may be used to clear the extern table list.
In the following example,
myDbase is a valid
RWDBDatabase instance,
t1 through
t6 are valid
RWDBTable instances, and
j1 is a valid
RWDBJoinExpr instance joining tables
t5 and
t6.
RWDBTable t1, t2, t3, t4, t5, t6;
... // Define valid RWDBTables t1, t2, t3, t4, t5, t6
RWDBJoinExpr j1 = rwdbLeftOuter(t5, t6);
... // Define the rest of the RWDBJoinExpr j1
RWDBSelector sel1 = myDbase.selector();
sel1 << t1["col"] << t2["col"] << t3["col"];
sel1.fromExtern(t2);
... // Define rest of the RWDBSelector sel1
RWDBSelector sel2 = myDbase.selector();
sel2 << t4["col"] << t5["col"] << t6["col"];
sel2.from(j1);
sel2.from(t4);
... // Define the rest of the RWDBSelector sel2
RWDBSelector sel3 = myDbase.selector();
sel3.fromGeneration(RWDBSelector::ExplicitAndImplicit);
sel3 << t1["col"] << t2["col"] << t3["col"];
sel3 << t5["col"] << t6["col"];
sel3.from(j1);
sel3.from(t4);
sel3.externFrom(t2);
... // Define the rest of the RWDBSelector sel3
RWDBSelector sel1 has by default the FROM clause generation behavior of ExplicitOrImplicit. Since no explicit from() entries are added, the FROM clause is generated using the implicit list, consisting of tables t1, t2 and t3 from the select column list. However since table t2 is marked as external, the FROM clause will have tables t1 and t3 only.
RWDBSelector sel2 also has by default the FROM clause generation behavior of ExplicitOrImplicit. Since explicit from() entries are added, the FROM clause is generated using the explicit list, consisting of join expression j1 and table t4.
RWDBSelector sel3 has the FROM clause generation behavior of ExplicitAndImplicit. The implicit list has entries for tables t1, t2, t3, t5 and t6 from the select column list. The from() calls have created the explicit list of join expression j1 and table t4. The union of the two will result in the join expression j1 and tables t1, t2, t3 and t4. Note that tables t5 and t6 are removed as they are part of the join expression j1. Since table t2 is marked as external, the FROM clause will consist of join expression j1 and tables t1, t3 and t4.