Workspace 6.21.5
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
SqlBindingRelation Class Referenceabstract

A simple structure for specifying relationship information between objects. More...

#include <DataAnalysis/DataStructures/sqlbindingrelation.h>

Inheritance diagram for SqlBindingRelation:
[legend]

Public Member Functions

 SqlBindingRelation (const SqlBindingRelation &)=default
 
SqlBindingRelationclone () const override=0
 
virtual SqlBindingRelationdeepCopy (const SqlBoundObjectGroupData *owner, DataExecution::DataObject &boundObject)=0
 
virtual QStringList getAllRelationNames () const =0
 
DataExecution::DataObjectgetBoundObject ()
 
virtual const DataExecution::DataObjectgetBoundObject () const =0
 
virtual const SqlBoundObjectClassDatagetBoundObjectClassData () const =0
 
virtual const DataExecution::DataFactorygetBoundType () const =0
 
RelationCardinality getCardinality () const
 
const QString & getForeignKey () const
 
virtual QString getJoinStatement () const =0
 
virtual SqlBindingRelationMap getRelationBindings (const QStringList &relationNames, bool recursive)=0
 
virtual QStringList getSqlValidFieldList () const =0
 
virtual bool isBoundObjectArray () const =0
 
SqlBindingRelationoperator= (const SqlBindingRelation &)=default
 
SqlDataModelResult populateData (SqlDataModel &model, const QSqlRecord &record, QSqlQuery &query, const QStringList &relations=QStringList())
 
virtual SqlDataModelResult saveBoundObject (SqlDataModel &model, const QStringList &relations) const =0
 
- Public Member Functions inherited from Clonable
virtual ~Clonable ()=default
 
virtual Clonableclone () const =0
 

Protected Member Functions

 SqlBindingRelation (RelationCardinality cardinality, const QString &foreignKey)
 
const SqlBoundObjectGroupDatagetOwner () const
 
virtual SqlDataModelResult populateDataImpl (SqlDataModel &model, const QSqlRecord &record, QSqlQuery &query, const QStringList &relations)=0
 
void setOwner (const SqlBoundObjectGroupData &owner)
 

Protected Attributes

RelationCardinality cardinality_
 
QString foreignKey_
 
const SqlBoundObjectGroupDataowner_
 
bool populating_
 

Detailed Description

The SqlBindingRelation essentially represents a valid relationship between one SqlBoundObjectGroup derived class and another. When bound to a specific SqlBoundObjectGroup, a SqlBindingRelation object contains the necessary information to retrieve the relevant associative information from the underlying database and embed it in the related object.

SqlBindingRelations are bound to SqlBoundObjectGroups at the time the object group is constructed. For example, if we look at an example object constructor for an Author object, we see that it has two relationships - a many-to-one relation, and a many-to-many relation:

```
Author() : DataAnalysis::TypedSqlBoundObjectGroup<Author, int>()
{
    bindField("Id", id);
    bindField("LastName", lastName);
    bindField("FirstName", firstName);
    bindField("Address", address);
    bindField("City", city);
    bindField("Publisher", publisher);
    bindField("Books", books);
    bindPrimaryKeyField(id);
    bindRelation(DataAnalysis::ManyToOneRelation<Testing::Publisher>(publisher, "Publisher_Id"));
    bindRelation(DataAnalysis::ManyToManyRelation<Testing::Book>(books, "Book_Id", "Author_Id", "Author_Book"));
}
```

Each relation is constructed differently, but essentially we provide it a reference to the underlying object (which must already be bound to the object via 'bindField'), and a number of parameters specifying how the relation information is obtained via foreign keys and / or intersection tables.

As can be seen from the above code, relations are bound to an "owner" by invoking the bindRelation() function in the constructor of that owner object.

Constructor & Destructor Documentation

◆ SqlBindingRelation() [1/2]

SqlBindingRelation ( const SqlBindingRelation )
default

◆ SqlBindingRelation() [2/2]

SqlBindingRelation ( RelationCardinality  cardinality,
const QString &  foreignKey 
)
protected
Parameters
cardinalityThe cardinality of this relation type.
foreignKeyThe foreign key field associated with this relation.
Note
This is a protected constructor and should be called by derived classes only.

Member Function Documentation

◆ clone()

SqlBindingRelation * clone ( ) const
overridepure virtual
Returns
A shallow copy of this relation; one that refers to the same bound object and has the same owner.

Implements Clonable.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ deepCopy()

virtual SqlBindingRelation * deepCopy ( const SqlBoundObjectGroupData owner,
DataExecution::DataObject boundObject 
)
pure virtual
Parameters
ownerThe SqlBoundObjectGroupData object that is to take ownership of the newly constructed relation object.
boundObjectThe new SqlBoundObjectGroup that is going to be the bound object in the newly constructed relation object.
Returns
A new SqlBindingRelation object that is identical to this one, except that it references the specified boundObject (provided by calling code, assumed to be already constructed), and the specified owner.

Constructs a deep copy of the relation, setting the owner to owner and the bound object to boundObject. boundObject is assumed to have been created by the calling code and is assumed to be of the correct type (subclasses enforce this).

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ getAllRelationNames()

virtual QStringList getAllRelationNames ( ) const
pure virtual
Returns
A string list of all the relation field names associated with this relations bound object and its children.

Recursively retrieves all the relations that exist below this relation; i.e. all of those field names on the bound object (as well as its child relations).

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ getBoundObject() [1/2]

DataExecution::DataObject & getBoundObject ( )
inline

◆ getBoundObject() [2/2]

virtual const DataExecution::DataObject & getBoundObject ( ) const
pure virtual
Returns
A reference to the bound object associated with this relation; i.e. the object that will store containing the referenced data.
Note
For some relation types, this DataObject may refer to a specify TypedObjectArray containing multiple individual related objects.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ getBoundObjectClassData()

virtual const SqlBoundObjectClassData & getBoundObjectClassData ( ) const
pure virtual
Returns
A reference to the class data associated with the relation's bound object.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ getBoundType()

virtual const DataExecution::DataFactory & getBoundType ( ) const
pure virtual
Returns
The data factory associated with the underlying data type that this relation is associated with; i.e. the bound object's type, or if the bound object is an array, the type of object stored in the array.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ getCardinality()

RelationCardinality getCardinality ( ) const
Returns
The cardinality of this relation (see RelationCardinality)

◆ getForeignKey()

const QString & getForeignKey ( ) const
inline
Returns
The foreign key field associated with this relation. Every relation type has a foreign key field, it is just not necessarily attached to the bound object's underlying table.

◆ getJoinStatement()

virtual QString getJoinStatement ( ) const
pure virtual
Returns
A QString containing the component of an SQL statement required to join the owner of this relation (i.e. the owner table) with the table associated with the bound object.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ getOwner()

const SqlBoundObjectGroupData & getOwner ( ) const
inlineprotected

◆ getRelationBindings()

virtual SqlBindingRelationMap getRelationBindings ( const QStringList relationNames,
bool  recursive 
)
pure virtual
Parameters
relationNamesA list of relation names that we will follow when retrieving the related bindings. Callers may not be interested in all related bindings.
recursiveWhether or not to continue through the entire tree of relations, or to stop at the immedate level.
Returns
A map containing all SqlBindingRelations associated with this relation's bound object.

Retrieves the set of SqlBindingRelations associated with this relation's bound object. This is necessary to retrieve multiple levels of relations from a single point in an object hierarchy.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ getSqlValidFieldList()

virtual QStringList getSqlValidFieldList ( ) const
pure virtual
Returns
A list of the field names from the relation's bound object. The field names are formatted ready for us in an SQL query and take the form:
tableName.fieldName fieldAlias
Note
Relation field names (i.e. those fields associated with other SqlBoundObjectGroups) are not contained in this list, as it is not valid to use these in an SQL query directly. That's what getJoinStatement() is for.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ isBoundObjectArray()

virtual bool isBoundObjectArray ( ) const
pure virtual

◆ operator=()

SqlBindingRelation & operator= ( const SqlBindingRelation )
default

◆ populateData()

SqlDataModelResult populateData ( SqlDataModel model,
const QSqlRecord &  record,
QSqlQuery &  query,
const QStringList relations = QStringList() 
)
Parameters
modelThe model being used to populate the data, which is attached to a database.
recordA valid record containing the column names of the records referred to by query.
queryA valid query object referring to one or more records.
relationsA list of relations to follow in the case that the bound object associated with this relation contains is own child objects.
Returns
A SqlDataModelResult object containing the results of the populate method.

Populates this relations data from the specified QSqlQuery, which is must be pointing at a valid record. For efficiency reasons, a record parameter is also provided which contains the column names.

If the bound object associated with this relation has its own child relations, this method will also follow and populate those that are included in the list relations.

◆ populateDataImpl()

virtual SqlDataModelResult populateDataImpl ( SqlDataModel model,
const QSqlRecord &  record,
QSqlQuery &  query,
const QStringList relations 
)
protectedpure virtual
Parameters
modelThe model being used to populate the data, which is attached to a database.
recordA valid record containing the column names of the records referred to by query.
queryA valid query object referring to one or more records.
relationsA list of relations to follow in the case that the bound object associated with this relation contains is own child objects.
Returns
A SqlDataModelResult object containing the results of the populate method.

Subclasses must implement this method in order to populate the data contained in this relation, and its associated bound object (as well as its children - the method is recursive).

Note
The implementation does not need to move the query cursor forward in any way, the calling code will manage this for populating the next record.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ saveBoundObject()

virtual SqlDataModelResult saveBoundObject ( SqlDataModel model,
const QStringList relations 
) const
pure virtual
Parameters
modelThe SqlDataModel through which we are saving this relation.
relationsA list of relation names that we will follow when saving related data in the case that the bound object itself has its own relations.
Returns
An SqlDataModelResult containing the result of the save operation.

Saves this relation to the database underlying the specified model so that it is persistent.

Implemented in ManyToManyRelation< T >, ManyToOneRelation< T >, OneToManyRelation< T >, and OneToOneRelation< T >.

◆ setOwner()

void setOwner ( const SqlBoundObjectGroupData owner)
inlineprotected

Member Data Documentation

◆ cardinality_

RelationCardinality cardinality_
protected

◆ foreignKey_

QString foreignKey_
protected

◆ owner_

const SqlBoundObjectGroupData* owner_
protected

◆ populating_

bool populating_
protected