casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
casacore::ROTableRow Class Reference

Readonly access to a table row. More...

#include <TableRow.h>

Inheritance diagram for casacore::ROTableRow:
casacore::TableRow

Public Member Functions

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

Protected Member Functions

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

Protected Attributes

TableRecorditsRecord
 
Table itsTable
 
Block< void * > itsTabCols
 
Block< void * > itsColumns
 
Block< void * > itsFields
 
Block< BoolitsDefined
 
uInt itsNrused
 
Int64 itsLastRow
 
Bool itsReread
 

Private Member Functions

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

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 (rownr_t 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 (rownr_t 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 137 of file TableRow.h.

Constructor & Destructor Documentation

casacore::ROTableRow::ROTableRow ( )

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).

casacore::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;

casacore::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.

casacore::ROTableRow::ROTableRow ( const ROTableRow )

Copy constructor (copy semantics).

casacore::ROTableRow::~ROTableRow ( )

Member Function Documentation

void casacore::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.

Vector<String> casacore::ROTableRow::columnNames ( ) const

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 casacore::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 casacore::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 casacore::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 casacore::ROTableRow::deleteObjects ( )
private

Delete all objects.

const TableRecord& casacore::ROTableRow::get ( rownr_t  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 > & casacore::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 513 of file TableRow.h.

References itsDefined.

void casacore::ROTableRow::init ( )
private

Initialize the object.

Bool casacore::ROTableRow::isAttached ( ) const
inline

Test if a Table is attached to this object.

Definition at line 497 of file TableRow.h.

References itsRecord.

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

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

void casacore::ROTableRow::makeObjects ( const RecordDesc description)
private

Make the required objects.

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

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

Assignment (copy semantics).

void casacore::ROTableRow::putField ( rownr_t  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 casacore::ROTableRow::putRecord ( rownr_t  rownr)
protected

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

This is a helper function for class TableRow.

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

const TableRecord & casacore::ROTableRow::record ( ) const
inline

Get the record containing all fields.

Definition at line 509 of file TableRow.h.

References itsRecord.

Int64 casacore::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 505 of file TableRow.h.

References itsLastRow.

void casacore::ROTableRow::setReread ( rownr_t  rownr)
protected

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

const Table & casacore::ROTableRow::table ( ) const
inline

Get the Table used for this object.

Definition at line 501 of file TableRow.h.

References itsTable.

Member Data Documentation

Block<void*> casacore::ROTableRow::itsColumns
protected

Definition at line 255 of file TableRow.h.

Block<Bool> casacore::ROTableRow::itsDefined
mutableprotected

Definition at line 260 of file TableRow.h.

Referenced by getDefined().

Block<void*> casacore::ROTableRow::itsFields
protected

Definition at line 258 of file TableRow.h.

Int64 casacore::ROTableRow::itsLastRow
mutableprotected

Definition at line 264 of file TableRow.h.

Referenced by rowNumber().

uInt casacore::ROTableRow::itsNrused
protected

Definition at line 262 of file TableRow.h.

TableRecord* casacore::ROTableRow::itsRecord
protected

Definition at line 247 of file TableRow.h.

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

Bool casacore::ROTableRow::itsReread
mutableprotected

Definition at line 267 of file TableRow.h.

Block<void*> casacore::ROTableRow::itsTabCols
protected

Definition at line 253 of file TableRow.h.

Table casacore::ROTableRow::itsTable
protected

Definition at line 249 of file TableRow.h.

Referenced by table().


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