SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
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 ,
  cascade , restrict , nullify , defaultify
}
 

Public Member Functions

 RWDBForeignKey ()
 
 RWDBForeignKey (const RWCString &constraintName, const RWCString &refName, Constraint updateConstraint=Restrict, Constraint deleteConstraint=Restrict)
 
 RWDBForeignKey (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 (const RWCString &name) const
 
RWDBColumn column (const RWCString &name, RWCString::caseCompare caseCompare) const
 
RWDBColumn column (size_t index) 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[] (const RWCString &name) const
 
RWDBColumn operator[] (size_t index) 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>
Represents foreign keys in a database, used when creating a database table or fetching a database tab...
Definition forkey.h:128
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")
.nullAllowed(false)
.storageLength(30);
profSchema.appendColumn(aCol1);
RWDBColumn aCol2;
aCol2.name("pid").type(RWDBValue::Int).nullAllowed(false);
profSchema.appendColumn(aCol2);
// pid is the primary key;
primeKey.appendColumn(profSchema["pid"]);
// Attach the primary key to the professor schema.
profSchema.primaryKey(primeKey);
// 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")
.nullAllowed(false)
.storageLength(30);
courseSchema.appendColumn(aCol3);
RWDBColumn aCol4;
aCol4.name("profid").type(RWDBValue::Int).nullAllowed(false);
courseSchema.appendColumn(aCol4);
RWDBForeignKey keyToProf("Professor");
// profid is the foreign key for the Professor table.
keyToProf.appendColumn(courseSchema["profid"]);
//Attach the foreign key to the course Schema.
courseSchema.foreignKey(keyToProf);
// Create the Course table.
aDatabase.createTable("Course", courseSchema);
Represents a column within a table or schema, or a particular parameter of a stored procedure.
Definition column.h:147
RWCString name() const
An ordered collection of RWDBColumn instances, encapsulating the database notion of a schema.
Definition schema.h:62
RWDBSchema & primaryKey(const RWDBPrimaryKey &pk)
RWDBSchema & foreignKey(const RWDBForeignKey &fk)
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)
@ Int
Definition value.h:166
@ String
Definition value.h:241

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");
Holds an ordered collection of RWDBForeignKey instances.
Definition forkey.h:493
RWDBStatus status() const
RWCString referenceName() const
Encapsulates the error state of an object or operation.
Definition status.h:80
Base class for a family of classes that represent the abstract notion of a database table in a number...
Definition table.h:89
bool exists(bool forceLookup=false)
RWDBStatus foreignKeys(const RWDBConnection &conn, const RWCString &refName, RWDBForeignKeyList &keyList)

Member Enumeration Documentation

◆ Constraint

This enum defines the constraint options.

Enumerator
Cascade 

cascade

Restrict 

restrict

Nullify 

nullify

Defaultify 

defaultify

cascade 

cascade

Note
Only available on platforms without restrict keyword
restrict 

restrict

Note
Only available on platforms without restrict keyword
nullify 

nullify

Note
Only available on platforms without restrict keyword
defaultify 

defaultify

Note
Only available on platforms without restrict keyword

Constructor & Destructor Documentation

◆ RWDBForeignKey() [1/4]

RWDBForeignKey::RWDBForeignKey ( )

Creates an empty RWDBForeignKey. Provided for convenience only.

◆ RWDBForeignKey() [2/4]

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() [3/4]

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

Constructor.

◆ RWDBForeignKey() [4/4]

RWDBForeignKey::RWDBForeignKey ( const RWDBForeignKey & fk)

Copy constructor. Self shares an implementation with fk.

Member Function Documentation

◆ appendColumn()

RWDBColumn RWDBForeignKey::appendColumn ( const RWDBColumn & col)

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

◆ asString()

RWCString RWDBForeignKey::asString ( const RWDBPhraseBook & phraseBook) const

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

◆ binaryStoreSize()

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.

◆ clone()

RWDBForeignKey RWDBForeignKey::clone ( ) const

Returns a deep copy of self.

◆ column() [1/3]

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.

◆ column() [2/3]

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.

◆ column() [3/3]

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.

◆ compareTo()

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.

◆ constraintName() [1/2]

RWCString RWDBForeignKey::constraintName ( ) const

Returns the constraint name of this foreign key.

◆ constraintName() [2/2]

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.

◆ copy()

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.

◆ deleteConstraint() [1/2]

Constraint RWDBForeignKey::deleteConstraint ( ) const

Returns the delete constraint of self.

◆ deleteConstraint() [2/2]

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.

◆ entries()

size_t RWDBForeignKey::entries ( ) const

Returns the number of RWDBColumn instances in self.

◆ errorHandler()

RWDBStatus::ErrorHandler RWDBForeignKey::errorHandler ( ) const

Returns the error handler attached to self.

◆ hash()

virtual unsigned RWDBForeignKey::hash ( ) const
virtual

Returns asString().hash().

Reimplemented from RWCollectable.

◆ index() [1/3]

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.

◆ index() [2/3]

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.

◆ index() [3/3]

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.

◆ isA()

virtual RWClassID RWDBForeignKey::isA ( ) const
virtual

Returns __RWDBFOREIGNKEY.

Reimplemented from RWCollectable.

◆ isEmpty()

bool RWDBForeignKey::isEmpty ( ) const

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

◆ isEqual()

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.

◆ isValid()

bool RWDBForeignKey::isValid ( ) const

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

◆ newSpecies()

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.

◆ operator=()

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

Assignment operator. Self shares an implementation with fk.

◆ operator==()

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

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

◆ operator[]() [1/2]

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.

◆ operator[]() [2/2]

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.

◆ referenceKey() [1/2]

RWDBSchema & RWDBForeignKey::referenceKey ( )

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

◆ referenceKey() [2/2]

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.

◆ referenceName()

RWCString RWDBForeignKey::referenceName ( ) const

Returns the reference name of self.

◆ restoreGuts() [1/2]

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.

◆ restoreGuts() [2/2]

virtual void RWDBForeignKey::restoreGuts ( RWvistream & )
virtual

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

Reimplemented from RWCollectable.

◆ saveGuts() [1/2]

virtual void RWDBForeignKey::saveGuts ( RWFile & ) const
virtual

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

Reimplemented from RWCollectable.

◆ saveGuts() [2/2]

virtual void RWDBForeignKey::saveGuts ( RWvostream & ) const
virtual

Writes an object's state to an output stream.

Reimplemented from RWCollectable.

◆ setErrorHandler()

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.

◆ status()

RWDBStatus RWDBForeignKey::status ( ) const

Returns the current status of self.

◆ updateConstraint() [1/2]

Constraint RWDBForeignKey::updateConstraint ( ) const

Returns the update constraint of self.

◆ updateConstraint() [2/2]

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 © 2024 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.