rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWDBForeignKey Class Reference
[Databases]

Represents foreign keys in a database, used when creating a database table or fetching a database table schema. More...

#include <rw/db/forkey.h>

Inheritance diagram for RWDBForeignKey:
RWCollectable

List of all members.

Public Types

enum  Constraint { cascade, restrict, nullify, defaultify }

Public Member Functions

 RWDBForeignKey ()
 RWDBForeignKey (const RWCString &refName, Constraint updateConstraint=Restrict, Constraint deleteConstraint=Restrict)
 RWDBForeignKey (const RWCString &constraintName, const RWCString &refName, Constraint updateConstraint=Restrict, Constraint deleteConstraint=Restrict)
 RWDBForeignKey (const RWDBForeignKey &fk)
RWDBForeignKeyoperator= (const RWDBForeignKey &fk)
RWDBColumn operator[] (size_t index) const
RWDBColumn operator[] (const RWCString &name) const
RWDBColumn column (size_t index) const
RWDBColumn column (const RWCString &name) const
RWDBColumn column (const RWCString &name, RWCString::caseCompare caseCompare) 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
size_t entries () const
bool isEmpty () const
RWCString constraintName () const
RWCString referenceName () const
RWDBSchemareferenceKey ()
Constraint updateConstraint () const
Constraint deleteConstraint () const
RWDBColumn appendColumn (const RWDBColumn &col)
void constraintName (const RWCString &newName)
void updateConstraint (Constraint newConstraint)
void deleteConstraint (Constraint newConstraint)
void referenceKey (const RWDBSchema &schema)
RWCString asString (const RWDBPhraseBook &phraseBook) const
void setErrorHandler (RWDBStatus::ErrorHandler handler)
RWDBStatus::ErrorHandler errorHandler () const
bool isValid () const
RWDBStatus status () const

Detailed Description

RWDBForeignKey is used to represent foreign keys in a database, used when building up an RWDBSchema in preparation for an RWDBDatabase::createTable() call. An RWDBForeignKey is also used as elements of lists that are returned from RWDBTable::foreignKeys() and RWDBTable::referredToBy(). RWDBForeignKey has much the same interface and semantics as RWDBSchema.

RWDBForeignKey is designed around the Interface/Implementation paradigm. An RWDBForeignKey instance is an interface to a reference-counted implementation; copy constructors and assignment operators produce additional references to a shared implementation. An RWDBForeignKey implementation is a base class from which a family of database-specific foreign key implementations is derived.

Synopsis

 #include <rw/db/forkey.h>

 RWDBForeignKey fk;

Related Classes

See also: RWDBDatabase, RWDBTable, RWDBForeignKeyList, and RWDBSchema.

Examples

Example 1

In this example, two tables are created, a Professor table with pid as the primary key and a Course table with profid as a foreign key into the Professor table.

 RWDBSchema  profSchema;
 RWDBSchema  primeKey;

 RWDBColumn aCol1;
 aCol1.name("profname").type(RWDBValue::String).
                      nullAllowed(false).storageLength(30);
 profSchema.appendColumn( aCol1 );

 RWDBColumn aCol2;
 aCol2.name("pid").type(RWDBValue::Int).nullAllowed(false);
 profSchema.appendColumn( aCol2 );

 primeKey.appendColumn( profSchema["pid"] ); //pid is the
                                             //primary key;
 profSchema.primaryKey( primeKey );          //Attach the
                                             //primary key
                                             //to the
                                             //professor schema.

 // Create the Professor table
 aDatabase.createTable("Professor", profSchema);

 // Now create the Course table
 RWDBSchema courseSchema;
 courseSchema.appendColumn( aCol1 );
 courseSchema.appendColumn( aCol2 );

 RWDBColumn aCol3;
 aCol3.name("coursename").type(RWDBValue::String).
                         nullAllowed(false).storageLength(30);
 courseSchema.appendColumn( aCol3 );
 RWDBColumn aCol4;
 aCol4.name("profid").type(RWDBValue::Int).nullAllowed(false);
 courseSchema.appendColumn( aCol4 );

 RWDBForeignKey keyToProf("Professor");
 keyToProf.appendColumn(courseSchema["profid"]);
                                       //profid is the
                                       //foreign key for the
                                       //Professor table.

 courseSchema.foreignKey( keyToProf ); //Attach the foreign
                                       //key to the
                                       //course Schema.
 aDatabase.createTable("Course", courseSchema);
                                       //Create the Course
                                       //table.

Example 2

This next example queries the Course table for its foreign key into the Professor table. It assumes the tables have been set up according to the example above.

 RWDBTable course = aDatabase.table("Course");
 assert( course.exists() );
 RWDBForeignKeyList aList;
 RWDBStatus status = course.foreignKeys("Professor", aList);
 RWDBForeignKey& fKey = aList[0];
 assert( fKey.referenceName() == "Professor" );
 assert( fKey[0].name() ==  "profid");

Member Enumeration Documentation

This enum defines the constraint options.

Enumerator:
cascade 

cascade

restrict 

restrict

nullify 

nullify

defaultify 

defaultify


Constructor & Destructor Documentation

RWDBForeignKey::RWDBForeignKey (  ) 

Creates an empty RWDBForeignKey. Provided for convenience only.

RWDBForeignKey::RWDBForeignKey ( const RWCString refName,
Constraint  updateConstraint = Restrict,
Constraint  deleteConstraint = Restrict 
)

This constructor creates an empty RWDBForeignKey, that is, one with zero columns. The name of the table to which self refers is refName. The update constraint associated with self is updateConstraint. The delete constraint associated with self is deleteConstraint.

RWDBForeignKey::RWDBForeignKey ( const RWCString constraintName,
const RWCString refName,
Constraint  updateConstraint = Restrict,
Constraint  deleteConstraint = Restrict 
)

Constructor.

RWDBForeignKey::RWDBForeignKey ( const RWDBForeignKey fk  ) 

Copy constructor. Self shares an implementation with fk.


Member Function Documentation

RWDBColumn RWDBForeignKey::appendColumn ( const RWDBColumn col  ) 

Creates a copy of col and appends to self. Returns the copy col.

RWCString RWDBForeignKey::asString ( const RWDBPhraseBook phraseBook  )  const

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

RWDBColumn RWDBForeignKey::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 whose status is RWDBStatus::columnNotFound.

RWDBColumn RWDBForeignKey::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 whose status is RWDBStatus::columnNotFound.

RWDBColumn RWDBForeignKey::column ( size_t  index  )  const

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

void RWDBForeignKey::constraintName ( const RWCString newName  ) 

Sets the constraint name of self to be newName. Note that this does not change the name of the foreign key in a table which already exists in the database.

RWCString RWDBForeignKey::constraintName (  )  const

Returns the constraint name of this foreign key.

void RWDBForeignKey::deleteConstraint ( Constraint  newConstraint  ) 

Sets the delete constraint of self to the newConstraint. Note that this does not change the delete constraint of a foreign key of a table which already exists.

Constraint RWDBForeignKey::deleteConstraint (  )  const

Returns the delete constraint of self.

size_t RWDBForeignKey::entries (  )  const

Returns the number of RWDBColumn instances in self.

RWDBStatus::ErrorHandler RWDBForeignKey::errorHandler (  )  const

Returns the error handler attached to self.

size_t RWDBForeignKey::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.

size_t RWDBForeignKey::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 RWDBForeignKey::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.

bool RWDBForeignKey::isEmpty (  )  const

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

bool RWDBForeignKey::isValid (  )  const

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

RWDBForeignKey& RWDBForeignKey::operator= ( const RWDBForeignKey fk  ) 

Assignment operator. Self shares an implementation with fk.

RWDBColumn RWDBForeignKey::operator[] ( const RWCString name  )  const

Returns the first RWDBColumn in self with the given name. If no such column exists, returns an RWDBColumn with a status of RWDBStatus::columnNotFound. Note that the returned RWDBColumn is not a deep copy; modifications to RWDBColumn change the containing schema.

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

Returns the RWDBColumn in self at position index. If the index is out of range, returns an RWDBColumn with a status of RWDBStatus::invalidPosition. Indexing into an empty RWDBForeignKey is treated as out of range. Note that the returned RWDBColumn is not a deep copy; modifications to RWDBColumn change the containing RWDBForeignKey.

void RWDBForeignKey::referenceKey ( const RWDBSchema schema  ) 

Sets the referenced key of self to schema. This does not change the referenced key of a foreign key in a table that already exists in the database.

RWDBSchema& RWDBForeignKey::referenceKey (  ) 

Returns a reference to the currently held reference key of self.

RWCString RWDBForeignKey::referenceName (  )  const

Returns the reference name of self.

void RWDBForeignKey::setErrorHandler ( RWDBStatus::ErrorHandler  handler  ) 

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

RWDBStatus RWDBForeignKey::status (  )  const

Returns the current status of self.

void RWDBForeignKey::updateConstraint ( Constraint  newConstraint  ) 

Sets the update constraint of self to the newConstraint. Note that this does not change the update constraint of a foreign key of a table that already exists.

Constraint RWDBForeignKey::updateConstraint (  )  const

Returns the update constraint of self.

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.