casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions
casa::ROTableRow Class Reference

Readonly access to a table row. More...

#include <TableRow.h>

Inheritance diagram for casa::ROTableRow:
casa::TableRow

List of all members.

Public Member Functions

 ROTableRow ()
 Create a detached ROTableRow object.
 ROTableRow (const Table &table, Bool storedColumnsOnly=True)
 Create a ROTableRow object for the given Table.
 ROTableRow (const Table &table, const Vector< String > &columnNames, Bool exclude=False)
 Create a ROTableRow object for the given Table.
 ROTableRow (const ROTableRow &)
 Copy constructor (copy semantics).
 ~ROTableRow ()
ROTableRowoperator= (const ROTableRow &)
 Assignment (copy semantics).
Bool isAttached () const
 Test if a Table is attached to this object.
const Tabletable () const
 Get the Table used for this object.
const TableRecordrecord () const
 Get the record containing all fields.
Int rowNumber () const
 Get the number of the last row read.
Vector< StringcolumnNames () const
 Get a vector consisting of all columns names.
const TableRecordget (uInt rownr, Bool alwaysRead=False) const
 Get the values of all columns used from the given row.
const Block< Bool > & getDefined () const
 Get the block telling for each column if its value in the row was indefined in the table.

Protected Member Functions

void copy (const ROTableRow &that)
 Copy that object to this object.
void create (const Table &table, Bool storedColumnsOnly, Bool writable)
 Create the record, column, and field objects for all columns in the table.
void create (const Table &table, const Vector< String > &columnNames, Bool exclude, Bool writable)
 Create the record, column, and field objects for the given columns.
void putRecord (uInt rownr)
 Put the values found in the internal TableRecord at the given row.
void putField (uInt rownr, const TableRecord &record, Int whichColumn, Int whichField)
 Put a value in the given field in the TableRecord into the given row and column.
void setReread (uInt rownr)
 Set the switch to reread when the current row has been put.

Protected Attributes

TableRecorditsRecord
Table itsTable
Block< void * > itsTabCols
Block< void * > itsColumns
Block< void * > itsFields
Block< BoolitsDefined
uInt itsNrused
IntitsLastRow
BoolitsReread

Private Member Functions

void init ()
 Initialize the object.
void makeDescExclude (RecordDesc &description, const Vector< String > &columnNames, Bool writable)
 Make a RecordDesc from the table with some excluded column names.
void addColumnToDesc (RecordDesc &description, const TableColumn &column, Bool skipOther)
 Add a column to the record.
void makeObjects (const RecordDesc &description)
 Make the required objects.
void deleteObjects ()
 Delete all objects.

Detailed Description

Readonly access to a table row.

Intended use:

Public interface

Review Status

Reviewed By:
Paul Shannon
Date Reviewed:
1996/05/10
Test programs:
tTableRow

Prerequisite

Synopsis

This class provides easy access to the contents of a table, one row at a time. 'Normal' access to a table is by columns, each of which contains values of the same type. A table row, by contrast, will be a collection of heterogeneous data, similar to a C struct. For this reason, the TableRow classes (ROTableRow and TableRow) are built around and provide access to the class TableRecord . The TableRow delegates much of its behaviour to the TableRecord class. For example:

      Table table ("some.table");
      ROTableRow row (table);                 // construct TableRow object
      cout << row.record().description();     // show its description
      // Get the values in row 17.
      const TableRecord& record = row.get (17);
      // column name is "Title", and automatically becomes the record
      // key for this field of the record:
      String row17title = record.asString ("Title");  
      Int    row17count = record.asInt ("Count");

The simplest constructor will include all columns in the TableRow object (although columns with a non-standard data type will be excluded, because they cannot be represented in a TableRecord). However, it is possible to be more selective and to include only some columns in the TableRow object. The various constructors show how this can be done.

It is possible to have multiple TableRow objects for the same table. They can contain different columns or they can share columns.

On construction an internal TableRecord object is created containing the required fields. The contents of this record will be changed with each get call, but the structure of it is fixed. This means that RORecordFieldPtr objects can be constructed once and used many times. This results in potentially faster access to the record, because it avoids unnecessary name lookups.

Example

    // Open the table as readonly and define a row object containing
    // the given columns.
    // Note that the function stringToVector is a very convenient
    // way to construct a Vector<String>.
    // Show the description of the fields in the row.
    Table table("Some.table");
    ROTableRow row (table, stringToVector("col1,col2,col3"));
    cout << row.record().description();
    // Loop through all rows and get their values.
    for (uInt i=0; i<table.nrow(); i++) {
        const TableRecord& values = row.get (i);
        someString = values.asString ("col1");
        somedouble = values.asdouble ("col2");
        someArrayInt = values.asArrayInt ("col3");
    }
   
    // Provided the structure of the record is known, the RecordFieldPtr
    // objects could be used as follows.
    // This is faster than the previous method, because it avoids a name
    // lookup for each iteration.
    RORecordFieldPtr<String> col1(row.record(), "col1");
    RORecordFieldPtr<double> col2(row.record(), "col2");
    RORecordFieldPtr<Array<Int> > col3(row.record(), "col3");
    for (uInt i=0; i<table.nrow(); i++) {
        row.get (i);
        someString = *col1;
        somedouble = *col2;
        someArrayInt = *col3;
    }

Please note that the TableRecord& returned by the get() function is the same as returned by the record() function. Therefore the RORecordField objects can be created in advance.

Definition at line 138 of file TableRow.h.


Constructor & Destructor Documentation

Create a detached ROTableRow object.

This means that no Table, etc. is contained in it. Function isAttached will return False for it.
This constructor should normally not be used, because it does not result in a valid object. It should only be used when really needed (e.g. when an array of objects has to be used).

casa::ROTableRow::ROTableRow ( const Table table,
Bool  storedColumnsOnly = True 
) [explicit]

Create a ROTableRow object for the given Table.

Its TableRecord will contain all columns except columns with datatype TpOther (i.e. non-standard data types).
If the flag storedColumnsOnly is True, only the columns actually stored by a storage manager will be selected. This is useful when the contents of an entire row have to be copied. Virtual columns are calculated on-the-fly (often using stored columns), thus it makes no sense to copy their data.
Caution: If the table contains columns with large arrays, it may be better not to use this constructor; Each get will read in all data in the row, thus also the large data array(s); In that case it is better to use the constructor which includes selected columns only;

casa::ROTableRow::ROTableRow ( const Table table,
const Vector< String > &  columnNames,
Bool  exclude = False 
)

Create a ROTableRow object for the given Table.

Its TableRecord will contain all columns given in the Vector. An exception is thrown if an unknown column name is given.
When exclude=True, all columns except the given columns are taken. In that case an unknown name does not result in an exception.

Copy constructor (copy semantics).


Member Function Documentation

void casa::ROTableRow::addColumnToDesc ( RecordDesc description,
const TableColumn column,
Bool  skipOther 
) [private]

Add a column to the record.

When skipOther is True, columns with a non-standard data type will be silently skipped.

Get a vector consisting of all columns names.

This can, for instance, be used to construct a TableRow object with the same columns in another table.

void casa::ROTableRow::copy ( const ROTableRow that) [protected]

Copy that object to this object.

The writable flag determines if writable or readonly TableColumn objects will be created.

void casa::ROTableRow::create ( const Table table,
Bool  storedColumnsOnly,
Bool  writable 
) [protected]

Create the record, column, and field objects for all columns in the table.

The writable flag determines if writable or readonly TableColumn objects will be created.

void casa::ROTableRow::create ( const Table table,
const Vector< String > &  columnNames,
Bool  exclude,
Bool  writable 
) [protected]

Create the record, column, and field objects for the given columns.

The writable flag determines if writable or readonly TableColumn objects will be created.

void casa::ROTableRow::deleteObjects ( ) [private]

Delete all objects.

const TableRecord& casa::ROTableRow::get ( uInt  rownr,
Bool  alwaysRead = False 
) const

Get the values of all columns used from the given row.

When the given row number equals the current one, nothing will be read unless the alwaysRead flag is set to True.
The TableRecord& returned is the same one as returned by the record() function. So one can ignore the return value of get().

const Block< Bool > & casa::ROTableRow::getDefined ( ) const [inline]

Get the block telling for each column if its value in the row was indefined in the table.

Note that array values might be undefined in the table, but in the record they will be represented as empty arrays.

Definition at line 515 of file TableRow.h.

References itsDefined.

void casa::ROTableRow::init ( ) [private]

Initialize the object.

Bool casa::ROTableRow::isAttached ( ) const [inline]

Test if a Table is attached to this object.

Definition at line 499 of file TableRow.h.

References itsRecord.

void casa::ROTableRow::makeDescExclude ( RecordDesc description,
const Vector< String > &  columnNames,
Bool  writable 
) [private]

Make a RecordDesc from the table with some excluded column names.

void casa::ROTableRow::makeObjects ( const RecordDesc description) [private]

Make the required objects.

These are the TableRecord and for each column a TableColumn and RecordFieldPtr.

ROTableRow& casa::ROTableRow::operator= ( const ROTableRow )

Assignment (copy semantics).

void casa::ROTableRow::putField ( uInt  rownr,
const TableRecord record,
Int  whichColumn,
Int  whichField 
) [protected]

Put a value in the given field in the TableRecord into the given row and column.

This is a helper function for class TableRow.

void casa::ROTableRow::putRecord ( uInt  rownr) [protected]

Put the values found in the internal TableRecord at the given row.

This is a helper function for class TableRow.

Referenced by casa::TableRow::put().

const TableRecord & casa::ROTableRow::record ( ) const [inline]

Get the record containing all fields.

Definition at line 511 of file TableRow.h.

References itsRecord.

Int casa::ROTableRow::rowNumber ( ) const [inline]

Get the number of the last row read.

-1 is returned when no Table is attached or no row has been read yet.

Definition at line 507 of file TableRow.h.

References itsLastRow.

void casa::ROTableRow::setReread ( uInt  rownr) [protected]

Set the switch to reread when the current row has been put.

const Table & casa::ROTableRow::table ( ) const [inline]

Get the Table used for this object.

Definition at line 503 of file TableRow.h.

References itsTable.


Member Data Documentation

Block<void*> casa::ROTableRow::itsColumns [protected]

Definition at line 256 of file TableRow.h.

Block<Bool> casa::ROTableRow::itsDefined [mutable, protected]

Definition at line 261 of file TableRow.h.

Referenced by getDefined().

Block<void*> casa::ROTableRow::itsFields [protected]

Definition at line 259 of file TableRow.h.

Definition at line 266 of file TableRow.h.

Referenced by rowNumber().

Definition at line 263 of file TableRow.h.

Definition at line 248 of file TableRow.h.

Referenced by isAttached(), record(), and casa::TableRow::record().

Definition at line 269 of file TableRow.h.

Block<void*> casa::ROTableRow::itsTabCols [protected]

Definition at line 254 of file TableRow.h.

Definition at line 250 of file TableRow.h.

Referenced by table().


The documentation for this class was generated from the following file: