Chapter 2. Variable Classes

Table of Contents
2.1. Overview
2.2. Common Methods
2.3. The Nullable Class
2.4. The Varchar Class
2.5. The Number Class
2.6. The Date Class
2.7. The Rowtype Class

2.1. Overview

Ora++ provides variable classes that are used as the host variables for binding to input and output placeholders and SELECT list columns. The Nullable class serves as the base class for Varchar and Number. Objects of these classes can handle null values for either input or output.

A Nullable object can only be null; it cannot be set to any value. An attempt to assign to a Nullable or to return any value from it will throw Value_Error.

Other classes support assignment of built-in types and other Nullables that make sense. Assigning a Nullable object to an object of a derived class sets that object to null.


Example 2-1. Nullable assignment

Nullable null;     // always null
Number n;          // initially null
Varchar vc("Fred");
n = 100;
vc = null;                 // same as vc.set_null()
null = "Smerd";            // throws Value_Error

Methods are provided to retrieve values of a particular type from a Nullable object. If the object cannot return such a type, Value_Error will be thrown. The methods may be called with default values that will be returned if the object is null.


Example 2-2. Nullable value methods

Nullable null;
Number n;
Varchar vc("Fred");
cout << null.str() << endl;  // throws Value_Error
string s = null.str("Smerd");     // assigns "Smerd" to s
long l = n.lng();                      // throws Value_Error
double d = n.dbl(3.14);                // assigns 3.14 to d
n = 3.14;
vc = n.str();                          // assigns "3.14" to vc

Insertion operators are provided for the Nullables that output the string representations, so that the type methods are not required:


Example 2-3. Using insertion operators with Nullables

Nullable null;
Varchar vc("Fred");
cout << null << ", ", << vc << cout;   // outputs "<NULL>, Fred"

Ora++ provides a Rowtype class which is a useful container for Nullable-derived objects. A Rowtype object is constructed according to the column types of an executed Select_Stmt:


Example 2-4. Using the Rowtype class

Select_Stmt stmt(db, "select col1, col2 from mytable");
stmt.exec();
Rowtype row(stmt);
stmt.bind_col(row);
while (stmt.fetch())
    cout << row["COL1"] << ", " << row[1] << endl;

You use operator[] to access the column values, either by column name or zero-based position in the SELECT list.