RWDBForeignKey RWCollectable
Data Types | |
enum Constraint |
#include <rw/db/forkey.h> RWDBForeignKey fk;
RWDBForeignKey is used to represent foreign keys in a database. They are used when building up an RWDBSchema in preparation for an RWDBDatabase::createTable() call. They are also used as elements of lists that are returned from RWDBTable::foreignKeys() and RWDBTable::referedToBy(). 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.
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.
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");
See also: RWDBDatabase, RWDBTable, RWDBForeignKeyList, and RWDBSchema.
enum Constraint { cascade = 'C', restrict='R', nullify = 'N' };
RWDBForeignKey();
Creates an empty RWDBForeignKey. Provided only for convenience.
RWDBForeignKey(const RWCString& refName = "", Constraint updateConstraint=restrict, Constraint deleteConstraint=restrict);
The default 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(const RWDBForeignKey& fk);
Copy constructor. Self shares an implementation with fk.
RWDBForeignKey& operator=(const RWDBForeignKey& fk);
Assignment operator. Self shares an implementation with fk.
RWDBColumn 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 foreignKey is treated as out of range. Note that the returned RWDBColumn is not a deep copy; modifications to RWDBColumn change the containing foreignKey.
RWDBColumn 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 appendColumn(const RWDBColumn& col);
Appends col to self and returns col.
Constructs a new RWDBColumn according to the supplied parameters, appends it to self, and returns it. Applications using this method usually need to supply only name and type; storage length is also required if the type is RWDBValue::string.
name represents the name of the new column appended to the schema.
type should be one of the DBTools.h++ datatype attributes. See class RWDBValue for more explanation.
storageLength should be set if the datatype requires a size, such as String or Blob datatypes.
nativeType should be set if one of the DBTools.h++ datatype attributes cannot represent the native datatype of the database. If this field is set, you must leave type as the default to show that a native datatype was requested. The native datatypes of a specific database can be determined through documentation or header files.
precision and scale are used when specifying a numeric datatype, such as Decimal. The precision and scale of the resulting column is set accordingly.
nullAllowed should 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.
paramType should be set when specifying the parameters of a stored procedure. See RWDBStoredProcedure for more information on how to use this parameter. For normal tables, use the default value.
RWCString asString(const RWDBPhraseBook& phraseBook) const;
Returns an RWCString representing self as string based on the format found in phraseBook.
RWDBColumn 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.
RWDBColumn column(const RWCString& name) const;
Returns a deep copy of the column from self whose name matches the given name. If there is no match, returns an RWDBColumn whose status is RWDBStatus::invalidPosition.
RWDBColumn column(const RWCString& name, RWCString::caseCompare caseCompare) const;
Returns a deep 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::invalidPosition.
Constraint deleteConstraint();
Returns the delete constraint of self.
size_t entries() const;
Returns the number of RWDBColumns in self.
RWDBStatus::ErrorHandler errorHandler() const;
Returns the error handler attached to self.
size_t 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 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 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.
RWBoolean isEmpty() const;
Returns TRUE if self contains zero entries, otherwise returns FALSE.
RWBoolean isValid() const;
Returns TRUE if self's status is RWDBStatus::ok, otherwise returns FALSE.
RWCString referenceName();
Returns the reference name of self.
void 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 RWDBForeignKey error handler is inherited from the object that produced it; this method overrides the default. ErrorHandler is declared as a typedef within the scope of RWDBStatus:
typedef void (*ErrorHandler)(const RWDBStatus&);
RWDBStatus status() const;
Returns the current status of self.
Constraint updateConstraint();
Returns the update constraint of self.
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.