1
/* This file is part of the KDE project
2
Copyright (C) 2003-2004 Jarosław Staniek <staniek@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#ifndef KEXIDB_RELATIONSHIP_H
21
#define KEXIDB_RELATIONSHIP_H
28
/*! KexiDB::Relationship provides information about one-to-many relationship between two tables.
29
Relationship is defined by a pair of (potentially multi-field) indices:
30
- "one" or "master" side: unique key
31
- "many" or "details" side: referenced foreign key
33
[unique key, master] ----< [foreign key, details]
36
In this documentation, we will call table that owns fields of "one" side as
37
"master side of the relationship", and the table that owns foreign key fields of
38
as "details side of the relationship".
39
Use masterTable(), and detailsTable() to get one-side table and many-side table, respectively.
41
Note: some engines (e.g. MySQL with InnoDB) requires that indices at both sides
42
have to be explicitly created.
44
\todo (js) It is planned that this will be handled by KexiDB internally and transparently.
46
Each (of the two) key can be defined (just like index) as list of fields owned by one table.
47
Indeed, relationship info can retrieved from Relationship object in two ways:
48
-# pair of indices; use masterIndex(), detailsIndex() for that
49
-# ordered list of field pairs (<master-side-field, details-side-field>); use fieldPairs() for that
51
No assigned objects (like fields, indices) are owned by Relationship object. The exception is that
52
list of field-pairs is internally created (on demand) and owned.
54
Relationship object is owned by IndexSchema object (the one that is defined at master-side of the
56
Note also that IndexSchema objects are owned by appropriate tables, so thus
57
there is implicit ownership between TableSchema and Relationship.
59
If Relationship object is not attached to IndexSchema object,
60
you should care about destroying it by hand.
66
| Table A [uk]----r3---<
70
Table A has two relationships (r1, r2) at details side and one (r3) at master side.
71
[uk] stands for unique key.
78
class CALLIGRADB_EXPORT Relationship
81
typedef QList<Relationship*> List;
82
typedef QList<Relationship*>::ConstIterator ListIterator;
84
/*! Creates uninitialized Relationship object.
85
setIndices() will be required to call.
89
/*! Creates Relationship object and initialises it just by
90
calling setIndices(). If setIndices() failed, object is still uninitialised.
92
Relationship(IndexSchema* masterIndex, IndexSchema* detailsIndex);
94
virtual ~Relationship();
96
/*! \return index defining master side of this relationship
97
or null if there is no information defined. */
98
IndexSchema* masterIndex() const {
102
/*! \return index defining referenced side of this relationship.
103
or null if there is no information defined. */
104
IndexSchema* detailsIndex() const {
105
return m_detailsIndex;
108
/*! \return ordered list of field pairs -- alternative form
109
for representation of relationship or null if there is no information defined.
110
Each pair has a form of <master-side-field, details-side-field>. */
111
Field::PairList* fieldPairs() {
115
bool isEmpty() const {
116
return m_pairs.isEmpty();
119
/*! \return table assigned at "master / one" side of this relationship.
120
or null if there is no information defined. */
121
TableSchema* masterTable() const;
123
/*! \return table assigned at "details / many / foreign" side of this relationship.
124
or null if there is no information defined. */
125
TableSchema* detailsTable() const;
127
/*! Sets \a masterIndex and \a detailsIndex indices for this relationship.
128
This also sets information about tables for master- and details- sides.
130
- both indices must contain the same number of fields
131
- both indices must not be owned by the same table, and table (owner) must be not null.
132
- corresponding field types must be the same
133
- corresponding field types' signedness must be the same
134
If above rules are not fulfilled, information about this relationship is cleared.
135
On success, this Relationship object is detached from previous IndexSchema objects that were
136
assigned before, and new are attached.
138
void setIndices(IndexSchema* masterIndex, IndexSchema* detailsIndex);
141
Relationship(QuerySchema *query, Field *field1, Field *field2);
143
void createIndices(QuerySchema *query, Field *field1, Field *field2);
145
/*! Internal version of setIndices(). \a ownedByMaster parameter is passed
146
to IndexSchema::attachRelationship() */
147
void setIndices(IndexSchema* masterIndex, IndexSchema* detailsIndex, bool ownedByMaster);
149
IndexSchema *m_masterIndex;
150
IndexSchema *m_detailsIndex;
152
Field::PairList m_pairs;
154
bool m_masterIndexOwned : 1;
155
bool m_detailsIndexOwned : 1;
157
friend class Connection;
158
friend class TableSchema;
159
friend class QuerySchema;
160
friend class IndexSchema;