SourcePro® API Reference Guide

 
List of all members | Public Types | Public Member Functions
RWDBForeignKey Class Reference

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

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)
 
RWDBColumn appendColumn (const RWDBColumn &col)
 
RWCString asString (const RWDBPhraseBook &phraseBook) const
 
RWspace binaryStoreSize () const
 
RWDBForeignKey clone () const
 
RWDBColumn column (size_t index) const
 
RWDBColumn column (const RWCString &name) const
 
RWDBColumn column (const RWCString &name, RWCString::caseCompare caseCompare) const
 
virtual int compareTo (const RWCollectable *c) const
 
RWCString constraintName () const
 
void constraintName (const RWCString &newName)
 
virtual RWCollectablecopy () const
 
Constraint deleteConstraint () const
 
void deleteConstraint (Constraint newConstraint)
 
size_t entries () const
 
RWDBStatus::ErrorHandler errorHandler () const
 
virtual unsigned hash () 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
 
virtual RWClassID isA () const
 
bool isEmpty () const
 
virtual bool isEqual (const RWCollectable *c) const
 
bool isValid () const
 
virtual RWCollectablenewSpecies () const
 
RWDBForeignKeyoperator= (const RWDBForeignKey &fk)
 
bool operator== (const RWDBForeignKey &fk) const
 
RWDBColumn operator[] (size_t index) const
 
RWDBColumn operator[] (const RWCString &name) const
 
RWDBSchemareferenceKey ()
 
void referenceKey (const RWDBSchema &schema)
 
RWCString referenceName () const
 
virtual void restoreGuts (RWFile &file)
 
virtual void restoreGuts (RWvistream &stream)
 
virtual void saveGuts (RWFile &file) const
 
virtual void saveGuts (RWvostream &stream) const
 
void setErrorHandler (RWDBStatus::ErrorHandler handler)
 
RWDBStatus status () const
 
Constraint updateConstraint () const
 
void updateConstraint (Constraint newConstraint)
 
- Public Member Functions inherited from RWCollectable
virtual ~RWCollectable ()
 
RWspace recursiveStoreSize () const
 
RWStringID stringID () const
 

Additional Inherited Members

- Static Public Member Functions inherited from RWCollectable
static RWClassID classID (const RWStringID &name)
 
static RWClassID classIsA ()
 
static bool isAtom (RWClassID id)
 
static RWspace nilStoreSize ()
 

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>
See also
RWDBDatabase, RWDBTable, RWDBForeignKeyList, and RWDBSchema.
Example

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

RWspace RWDBForeignKey::binaryStoreSize ( ) const
virtual

Returns the number of bytes used by the virtual function saveGuts(RWFile&) to store an object. Typically, this involves adding up the space required to store all primitives, plus the results of calling recursiveStoreSize() for all objects inheriting from RWCollectable. See the Essential Tools Module User's Guide for details.

Reimplemented from RWCollectable.

RWDBForeignKey RWDBForeignKey::clone ( ) const

Returns a deep copy of self.

RWDBColumn RWDBForeignKey::column ( size_t  index) const

Returns a shallow 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.

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

virtual int RWDBForeignKey::compareTo ( const RWCollectable c) const
virtual

If c points to an RWDBForeignKey, returns the result of asString().compareTo(c->asString()) using the default phrasebook, otherwise returns a nonzero value.

Reimplemented from RWCollectable.

RWCString RWDBForeignKey::constraintName ( ) const

Returns the constraint name of this foreign key.

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.

virtual RWCollectable* RWDBForeignKey::copy ( ) const
virtual

Returns a new, copy-constructed object of the same type as self. The caller is responsible for deleting the object.

Reimplemented from RWCollectable.

Constraint RWDBForeignKey::deleteConstraint ( ) const

Returns the delete constraint of self.

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.

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.

virtual unsigned RWDBForeignKey::hash ( ) const
virtual

Returns asString().hash().

Reimplemented from RWCollectable.

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.

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

virtual RWClassID RWDBForeignKey::isA ( ) const
virtual

Returns __RWDBFOREIGNKEY.

Reimplemented from RWCollectable.

bool RWDBForeignKey::isEmpty ( ) const

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

virtual bool RWDBForeignKey::isEqual ( const RWCollectable t) const
virtual

Behaves as if compareTo(t) was invoked, returning true if the result equals 0, false otherwise.

Reimplemented from RWCollectable.

bool RWDBForeignKey::isValid ( ) const

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

virtual RWCollectable* RWDBForeignKey::newSpecies ( ) const
virtual

Returns a new, default-constructed object of the same type as self. The caller is responsible for deleting the object.

Reimplemented from RWCollectable.

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

Assignment operator. Self shares an implementation with fk.

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

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

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.

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.

RWDBSchema& RWDBForeignKey::referenceKey ( )

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

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.

RWCString RWDBForeignKey::referenceName ( ) const

Returns the reference name of self.

virtual void RWDBForeignKey::restoreGuts ( RWFile )
virtual

Reads an object's state from a binary file, using class RWFile, replacing the previous state.

Reimplemented from RWCollectable.

virtual void RWDBForeignKey::restoreGuts ( RWvistream )
virtual

Reads an object's state from an input stream, replacing the previous state.

Reimplemented from RWCollectable.

virtual void RWDBForeignKey::saveGuts ( RWFile ) const
virtual

Writes an object's state to a binary file, using class RWFile.

Reimplemented from RWCollectable.

virtual void RWDBForeignKey::saveGuts ( RWvostream ) const
virtual

Writes an object's state to an output stream.

Reimplemented from RWCollectable.

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.

Constraint RWDBForeignKey::updateConstraint ( ) const

Returns the update constraint 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.

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