SourcePro® API Reference Guide

 
List of all members | Public Member Functions | Friends
RWDBPrimaryKey Class Reference

Represents a primary key, used when creating a database table or fetching a database table primary key. More...

#include <rw/db/primarykey.h>

Public Member Functions

 RWDBPrimaryKey (const RWCString &constraintName="")
 
 RWDBPrimaryKey (const RWDBPrimaryKey &pk)
 
 RWDBPrimaryKey (const RWDBSchema &sch)
 
RWDBColumn appendColumn (const RWCString &name, RWDBValue::ValueType type=RWDBValue::NoType, long storageLength=RWDB_NO_TRAIT, int nativeType=RWDB_NO_TRAIT, int precision=RWDB_NO_TRAIT, int scale=RWDB_NO_TRAIT, bool nullAllowed=true, RWDBColumn::ParamType paramType=RWDBColumn::notAParameter)
 
RWDBColumn appendColumn (const RWDBColumn &col)
 
RWCString asString (const RWDBPhraseBook &pb) const
 
RWspace binaryStoreSize () const
 
RWDBPrimaryKey clone () const
 
RWDBColumn column (size_t index) const
 
RWDBColumn column (const RWCString &name) const
 
RWDBColumn column (const RWCString &name, RWCString::caseCompare caseCompare) const
 
RWCString columnName (size_t index) const
 
RWCString constraintName () const
 
RWDBPrimaryKeyconstraintName (const RWCString &newName)
 
size_t entries () const
 
RWDBStatus::ErrorHandler errorHandler () const
 
size_t index (const RWCString &name) const
 
size_t index (const RWCString &name, RWCString::caseCompare caseCompare) const
 
size_t index (const RWDBColumn &column) const
 
bool isEmpty () const
 
bool isValid () const
 
bool operator!= (const RWDBPrimaryKey &pk) const
 
RWDBPrimaryKeyoperator= (const RWDBPrimaryKey &pk)
 
RWDBPrimaryKeyoperator= (const RWDBSchema &sch)
 
bool operator== (const RWDBPrimaryKey &pk) const
 
RWDBColumn operator[] (size_t index) const
 
RWDBColumn operator[] (const RWCString &name) const
 
void setErrorHandler (RWDBStatus::ErrorHandler handler)
 
RWDBStatus status () const
 

Friends

RWvostreamoperator<< (RWvostream &v, const RWDBPrimaryKey &pk)
 
RWFileoperator<< (RWFile &f, const RWDBPrimaryKey &pk)
 
RWvistreamoperator>> (RWvistream &v, RWDBPrimaryKey &pk)
 
RWFileoperator>> (RWFile &f, RWDBPrimaryKey &pk)
 

Detailed Description

Class RWDBPrimaryKey is an ordered collection of RWDBColumn instances. An RWDBPrimaryKey serves as an encapsulation of the database notion of primary key, a set of attributes defining a primary key.

An RWDBTable can have an RWDBPrimaryKey. An application can interrogate an RWDBTable for primary key information, or obtain a copy of an RWDBPrimaryKey for its own use. An RWDBPrimaryKey instance can be used with RWDBSchema to create a database table with a primary key. To do this, an application can use an RWDBPrimaryKey obtained from an existing RWDBTable, modify an existing RWDBPrimaryKey, or build one from scratch using the methods appendColumn() and constraintName().

This class is implemented using a technique called copy-on-write. With this technique, the copy constructor and assignment operator reference the existing object, making a copy only when the object changes. The result is excellent performance, with easy-to-understand copy semantics.

Synopsis
#include <rw/db/primarykey.h>
pk = myTable.primaryKey();

Constructor & Destructor Documentation

RWDBPrimaryKey::RWDBPrimaryKey ( const RWCString constraintName = "")

The default constructor creates an empty RWDBPrimaryKey, that is, one with zero columns.

RWDBPrimaryKey::RWDBPrimaryKey ( const RWDBPrimaryKey pk)

Copy constructor. Self shares an implementation with pk until the data is modified.

RWDBPrimaryKey::RWDBPrimaryKey ( const RWDBSchema sch)

Constructor. Self shares an implementation with sch until the data is modified.

Member Function Documentation

RWDBColumn RWDBPrimaryKey::appendColumn ( const RWCString name,
RWDBValue::ValueType  type = RWDBValue::NoType,
long  storageLength = RWDB_NO_TRAIT,
int  nativeType = RWDB_NO_TRAIT,
int  precision = RWDB_NO_TRAIT,
int  scale = RWDB_NO_TRAIT,
bool  nullAllowed = true,
RWDBColumn::ParamType  paramType = RWDBColumn::notAParameter 
)

Constructs a new RWDBColumn according to the supplied parameters, appends it to self, and returns it. Applications using this method must usually supply only a name.

Parameters
nameRepresents the name of the column appended to the primary key.
typeShould be one of the data type attributes of the DB Interface Module. See RWDBValue::ValueType for more explanation.
storageLengthShould be set if the data type requires a size, such as String or Blob data types.
nativeTypeShould be set if one of the data type attributes of the DB Interface Module cannot represent the native data type of the database. If this field is set, type should be left as the default to show that a native data type was requested. You can determine native data types of a specific database through documentation or header files.
precisionprecision is set when specifying a numeric data type, such as Decimal. The precision of the resulting column is set accordingly.
scalescale is set when specifying a numeric data type, such as Decimal. The precision and scale of the resulting column is set accordingly.
nullAllowedShould be set to false if a column requires a value. The default value of true allows a column's value to be set to NULL.
paramTypeNot applicable to primary keys.
RWDBColumn RWDBPrimaryKey::appendColumn ( const RWDBColumn col)

Creates a deep copy of the RWDBColumn col, using the RWDBColumn::clone() method. Appends the copied column to self, and returns it.

RWCString RWDBPrimaryKey::asString ( const RWDBPhraseBook pb) const

Returns an RWCString representing self as a string based on the format found in pb.

RWspace RWDBPrimaryKey::binaryStoreSize ( ) const

Returns the number of bytes necessary to store the object using the global functions:

RWDBPrimaryKey RWDBPrimaryKey::clone ( ) const

Produces a deep copy of self.

RWDBColumn RWDBPrimaryKey::column ( size_t  index) const

Returns a shallow copy of the column from self at the specified index. If index is out of range, returns an RWDBColumn with a status of RWDBStatus::invalidPosition.

RWDBColumn RWDBPrimaryKey::column ( const RWCString name) const

Returns a shallow copy of the column from self whose name matches the given name. If there is no match, returns an RWDBColumn with a status of RWDBStatus::columnNotFound.

RWDBColumn RWDBPrimaryKey::column ( const RWCString name,
RWCString::caseCompare  caseCompare 
) const

Returns a shallow copy of the column from self whose name matches the given name, according to caseCompare. If there is no match, returns an RWDBColumn with a status of RWDBStatus::columnNotFound.

RWCString RWDBPrimaryKey::columnName ( size_t  index) const

Returns the name of the column at the specified index. Returns an empty string if the index is out of range.

RWCString RWDBPrimaryKey::constraintName ( ) const

Returns the constraint name of this primary key.

RWDBPrimaryKey& RWDBPrimaryKey::constraintName ( const RWCString newName)

Sets the constraint name of self to newName. Returns a reference to self.

Note
This does not change the name of the primary key on a table that already exists in the database.
size_t RWDBPrimaryKey::entries ( ) const

Returns the number of RWDBColumn instances in self.

RWDBStatus::ErrorHandler RWDBPrimaryKey::errorHandler ( ) const

Returns the error handler attached to self.

size_t RWDBPrimaryKey::index ( const RWCString name) const

Returns the index of the first RWDBColumn in self with the given name. Returns RW_NPOS if no such column is found.

size_t RWDBPrimaryKey::index ( const RWCString name,
RWCString::caseCompare  caseCompare 
) const

Returns the index of the first column from self whose name matches the given name, according to caseCompare. If there is no match, returns RW_NPOS.

size_t RWDBPrimaryKey::index ( const RWDBColumn column) const

Returns the index of the first RWDBColumn in self with the same name as the name of the given column. Returns RW_NPOS if no such column is found.

bool RWDBPrimaryKey::isEmpty ( ) const

Returns true if self contains zero entries, otherwise returns false.

bool RWDBPrimaryKey::isValid ( ) const

Returns true if self's status is RWDBStatus::ok, otherwise returns false.

bool RWDBPrimaryKey::operator!= ( const RWDBPrimaryKey pk) const

Compares self to pk. Returns true if pk and self do not share the same implementation.

This is equivalent to:

!(self == pk)
RWDBPrimaryKey& RWDBPrimaryKey::operator= ( const RWDBPrimaryKey pk)

Assignment operator. Self shares an implementation with pk until the data is modified.

RWDBPrimaryKey& RWDBPrimaryKey::operator= ( const RWDBSchema sch)

Assignment operator. Self shares an implementation with sch until the data is modified.

bool RWDBPrimaryKey::operator== ( const RWDBPrimaryKey pk) const

Compares self to pk. Returns true if pk and self share the same implementation.

RWDBColumn RWDBPrimaryKey::operator[] ( size_t  index) const

Returns the RWDBColumn in self at position index. If index is out of range, returns an RWDBColumn with a status of RWDBStatus::invalidPosition. Indexing into an empty primary key is treated as out of range.

Note
The returned RWDBColumn is not a deep copy; modifications to it change the containing primary key.
RWDBColumn RWDBPrimaryKey::operator[] ( const RWCString name) const

Returns the first RWDBColumn in self with the given name. If no such column exists, an RWDBColumn is returned with a status RWDBStatus::columnNotFound.

Note
The returned RWDBColumn is not a deep copy; modifications to it change the containing primary key.
void RWDBPrimaryKey::setErrorHandler ( RWDBStatus::ErrorHandler  handler)

Installs handler as self's error handler. The supplied handler is inherited by all objects produced by self. By default, the RWDBStatus::ErrorHandler of an RWDBPrimaryKey is inherited from the object that produced it; this method overrides the default.

RWDBStatus RWDBPrimaryKey::status ( ) const

Returns the current status of self.

Friends And Related Function Documentation

RWvostream& operator<< ( RWvostream v,
const RWDBPrimaryKey pk 
)
friend

Saves pk to an RWvostream.

Throws an RWExternalErr if the stream is corrupted.

RWFile& operator<< ( RWFile f,
const RWDBPrimaryKey pk 
)
friend

Saves pk to an RWFile.

Throws an RWExternalErr if the file is corrupted.

RWvistream& operator>> ( RWvistream v,
RWDBPrimaryKey pk 
)
friend

Restores an RWDBPrimaryKey into pk from an RWvistream, replacing the previous contents of pk.

Throws an RWExternalErr if the stream is corrupted.

RWFile& operator>> ( RWFile f,
RWDBPrimaryKey pk 
)
friend

Restores an RWDBPrimaryKey into pk from an RWFile, replacing the previous contents of pk.

Throws an RWExternalErr if the file is corrupted.

Copyright © 2022 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.